<?php
|
|
namespace app\admin\controller;
|
|
use think\Exception;
|
use think\facade\Cache;
|
use think\facade\Db;
|
use think\facade\View;
|
use think\facade\Request;
|
|
|
class PaymentTool extends Common {
|
private $tableName = 'payment_tool';
|
private $viewPath = 'payment_tool';
|
/**
|
* 分销区域列表
|
* @return string
|
*/
|
public function index() {
|
////////
|
if (!Request::isPost()) {
|
$other['title'] = '收款工具管理';
|
View::assign('other', $other);
|
View::assign('viewPath', $this->viewPath);
|
return View::fetch();
|
} else {
|
try {
|
$kw = input('kw');
|
if (!empty($kw)) {
|
$condition[] = array('payment_tool_name', 'like', "%$kw%");
|
}
|
$p = input('p', 1);
|
$other['page_size'] = input('page_size', 20);
|
$order = 'order_id asc,payment_tool_id asc';
|
$count = Db::name($this->tableName)->where($condition)->count();
|
$list = Db::name($this->tableName)->field("*")->where($condition)->order($order)->page($p, $other['page_size'])->select()->toArray();
|
$other['count'] = $count;
|
if (!$list) {
|
$list = [];
|
}
|
$result['other'] = $other;
|
$result['list'] = $list;
|
return $this->successResponse($result);
|
} catch (Exception $exc) {
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
}
|
|
/**
|
*保存分销区域
|
*/
|
public function save() {
|
if (!Request::isPost()) {
|
return $this->errorResponse(lang('request_method_incorrect'));
|
}
|
//开始事务
|
Db::startTrans();
|
try {
|
$data = Request::post();
|
$validate = new \app\admin\validate\paymentTool();
|
$check_result = $validate->check($data);
|
if (!$check_result) {
|
return $this->errorResponse($validate->getError());
|
}
|
$payment_tool_id = input('post.payment_tool_id');
|
if (is_numeric($payment_tool_id)) {
|
//更新数据
|
$update_result = Db::name($this->tableName)->where('payment_tool_id', $payment_tool_id)->withoutField('id')->save($data);
|
if ($update_result === FALSE) {
|
return $this->errorResponse(lang('editing_failed'));
|
}
|
//提交事务
|
Db::commit();
|
$this->delCache();
|
return $this->successResponse('修改成功!');
|
} else {
|
//添加数据
|
unset($data['payment_tool_id']);
|
$add_result = Db::name($this->tableName)->insertGetId($data);
|
if (!$add_result) {
|
return $this->errorResponse('添加失败!');
|
}
|
Db::name($this->tableName)->where('payment_tool_id', $add_result)->save(['order_id' => $add_result]);
|
//提交事务
|
Db::commit();
|
$this->delCache();
|
return $this->successResponse('添加成功!');
|
}
|
} catch (Exception $exc) {
|
// 回滚事务
|
Db::rollback();
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
|
function updateExt() {
|
try {
|
if (!Request::isPost()) {
|
throw new Exception(lang('request_method_incorrect'));
|
}
|
$postField = 'payment_tool_id,is_show';
|
$data = Request::only(explode(',', $postField), 'post');
|
if (!$data['payment_tool_id']) {
|
throw new Exception(lang('parameter_error'));
|
}
|
Db::name($this->tableName)->update($data);
|
$result['message'] = '操作成功';
|
return $this->successResponse($result);
|
} catch (\Exception $exc) {
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
|
/**
|
* 删除分销区域
|
* @return array|int[]
|
*/
|
public function delete() {
|
if (!Request::isPost()) {
|
return $this->errorResponse(lang('request_method_incorrect'));
|
}
|
//开始事务
|
Db::startTrans();
|
try {
|
$payment_tool_id = input('payment_tool_id');
|
if (!is_numeric($payment_tool_id)) {
|
return $this->errorResponse(lang('parameter_error'));
|
}
|
$condition['payment_tool_id'] = $payment_tool_id;
|
$info = Db::name($this->tableName)->field('payment_tool_id')->where($condition)->find();
|
if (!$info) {
|
return $this->errorResponse(lang('parameter_error'));
|
}
|
Db::name($this->tableName)->where('payment_tool_id', $payment_tool_id)->delete();
|
//提交事务
|
Db::commit();
|
$this->delCache();
|
return $this->successResponse('删除成功!');
|
} catch (Exception $exc) {
|
// 回滚事务
|
Db::rollback();
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
|
/**
|
* 清除缓存
|
* @return void
|
*/
|
private function delCache() {
|
Cache::tag('payment_tool')->clear();
|
}
|
|
}
|