Customer.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Session;
  5. use think\Db;
  6. /**
  7. * 客户管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Customer extends Backend
  12. {
  13. /**
  14. * Customer模型对象
  15. * @var \app\admin\model\Customer
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Customer;
  22. }
  23. /**
  24. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  25. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  26. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  27. */
  28. /**
  29. * 查看
  30. *
  31. * @return string|Json
  32. * @throws \think\Exception
  33. * @throws DbException
  34. */
  35. public function index()
  36. {
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if (false === $this->request->isAjax()) {
  40. return $this->view->fetch();
  41. }
  42. //如果发送的来源是 Selectpage,则转发到 Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  47. $user_info = Session::get('admin');
  48. $map = [];
  49. if ($user_info['id'] !== 1){
  50. $map['company_id'] = $user_info['company_id'];
  51. }
  52. $list = $this->model
  53. ->where($where)
  54. ->where($map)
  55. ->order($sort, $order)
  56. ->paginate($limit);
  57. $result = ['total' => $list->total(), 'rows' => $list->items()];
  58. return json($result);
  59. }
  60. /**
  61. * 添加
  62. *
  63. * @return string
  64. * @throws \think\Exception
  65. */
  66. public function add()
  67. {
  68. if (false === $this->request->isPost()) {
  69. return $this->view->fetch();
  70. }
  71. $params = $this->request->post('row/a');
  72. if (empty($params)) {
  73. $this->error(__('Parameter %s can not be empty', ''));
  74. }
  75. $params = $this->preExcludeFields($params);
  76. $user_info = Session::get('admin');
  77. $params['user_id'] = $user_info['id'];
  78. $params['company_id'] = $user_info['company_id'];
  79. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  80. $params[$this->dataLimitField] = $this->auth->id;
  81. }
  82. $result = false;
  83. Db::startTrans();
  84. try {
  85. //是否采用模型验证
  86. if ($this->modelValidate) {
  87. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  88. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  89. $this->model->validateFailException()->validate($validate);
  90. }
  91. $result = $this->model->allowField(true)->save($params);
  92. Db::commit();
  93. } catch (ValidateException|PDOException|Exception $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. }
  97. if ($result === false) {
  98. $this->error(__('No rows were inserted'));
  99. }
  100. $this->success();
  101. }
  102. }