4 changed files with 153 additions and 0 deletions
@ -0,0 +1,95 @@ |
|||
<?php |
|||
declare (strict_types = 1); |
|||
|
|||
namespace app\controller; |
|||
|
|||
use app\BaseController; |
|||
use app\Model\CustomerService as CustomerServiceModel; |
|||
use app\validate\CustomerService as CustomerServiceValidate; |
|||
use think\exception\ValidateException; |
|||
use think\facade\Request; |
|||
|
|||
class CustomerService extends BaseController |
|||
{ |
|||
/** |
|||
* 显示资源列表 |
|||
*/ |
|||
public function list() |
|||
{ |
|||
$request = Request::param(); |
|||
|
|||
$limit = $request['limit'] ?? 10; |
|||
|
|||
$where = []; |
|||
|
|||
$CustomerServiceModel = new CustomerServiceModel(); |
|||
|
|||
# 查询用户列表 |
|||
$field = 'id,name,wx_number'; |
|||
$res = $CustomerServiceModel->field($field)->where($where)->order('id desc')->paginate($limit); |
|||
|
|||
$list = $res->items(); |
|||
$total = $res->total(); |
|||
|
|||
return $this->renderSuccess('数据返回成功', ['list' => $list, 'total' => $total]); |
|||
} |
|||
|
|||
/** |
|||
* 保存新建的资源 |
|||
*/ |
|||
public function save() |
|||
{ |
|||
$param = Request::param(); |
|||
|
|||
try { |
|||
$id = $param['id'] ?? 0; |
|||
|
|||
if ($id) { |
|||
validate(CustomerServiceValidate::class)->scene('edit')->check($param); |
|||
$CustomerServiceModel = CustomerServiceModel::find($id); |
|||
$CustomerServiceModel->name = $param['name']; |
|||
$CustomerServiceModel->wx_number = $param['wx_number']; |
|||
$CustomerServiceModel->update_time = date("Y-m-d H:i:s",time()); |
|||
$CustomerServiceModel->save(); |
|||
} else { |
|||
validate(CustomerServiceValidate::class)->scene('add')->check($param); |
|||
|
|||
$CustomerService = new CustomerServiceModel(); |
|||
$CustomerService->save([ |
|||
'name' => $param['name'], |
|||
'wx_number' => $param['wx_number'], |
|||
'create_time' => date("Y-m-d H:i:s",time()) |
|||
]); |
|||
} |
|||
|
|||
return $this->renderSuccess($id ? '编辑成功' : '添加成功'); |
|||
} catch (ValidateException $validateException) { |
|||
return $this->renderError($validateException->getMessage()); |
|||
} catch (\Exception $e) { |
|||
return $this->renderError('操作失败'); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 删除指定资源 |
|||
*/ |
|||
public function delete() |
|||
{ |
|||
$param = Request::param(); |
|||
|
|||
try { |
|||
|
|||
validate(CustomerServiceValidate::class)->scene('del')->check($param); |
|||
|
|||
$result = CustomerServiceModel::destroy($param['id']); |
|||
|
|||
if (!$result) throw new ValidateException('删除失败'); |
|||
|
|||
return $this->renderSuccess('已删除'); |
|||
} catch (ValidateException $validateException) { |
|||
return $this->renderError($validateException->getMessage()); |
|||
} catch (\Exception $e) { |
|||
return $this->renderError('操作失败'); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
<?php |
|||
declare (strict_types = 1); |
|||
|
|||
namespace app\model; |
|||
|
|||
use think\Model; |
|||
use think\model\concern\SoftDelete; |
|||
|
|||
/** |
|||
* @mixin \think\Model |
|||
*/ |
|||
class CustomerService extends Model |
|||
{ |
|||
use SoftDelete; |
|||
protected $deleteTime = 'delete_time'; |
|||
|
|||
|
|||
} |
|||
@ -0,0 +1,35 @@ |
|||
<?php |
|||
declare (strict_types = 1); |
|||
|
|||
namespace app\validate; |
|||
|
|||
use think\Validate; |
|||
|
|||
class CustomerService extends Validate |
|||
{ |
|||
/** |
|||
* 定义验证规则 |
|||
* 格式:'字段名' => ['规则1','规则2'...] |
|||
* |
|||
* @var array |
|||
*/ |
|||
protected $rule = [ |
|||
'name|客服名称' => 'require', |
|||
'wx_number|微信号码' => 'require|min:6|max:20|alphaDash', |
|||
'id|客服id' => 'require|number', |
|||
]; |
|||
|
|||
/** |
|||
* 定义错误信息 |
|||
* 格式:'字段名.规则名' => '错误信息' |
|||
* |
|||
* @var array |
|||
*/ |
|||
protected $message = []; |
|||
|
|||
protected $scene = [ |
|||
'add' => ['name','wx_number'], |
|||
'edit' => ['name','wx_number','id'], |
|||
'del' => ['id'] |
|||
]; |
|||
} |
|||
Loading…
Reference in new issue