Customer.php 3.4 KB

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