CompanyStaff.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use fast\Random;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use think\Validate;
  9. /**
  10. * 商家员工
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class CompanyStaff extends Backend
  15. {
  16. /**
  17. * CompanyStaff模型对象
  18. * @var \app\admin\model\CompanyStaff
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\CompanyStaff;
  25. $this->view->assign("typeList", $this->model->getTypeList());
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. //当前是否为关联查询
  39. $this->relationSearch = true;
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. //只能看自己的
  49. $where_op = [];
  50. if($this->auth->company_id){
  51. $where_op['company_staff.company_id'] = $this->auth->company_id;
  52. }
  53. $list = $this->model
  54. ->with(['company'])
  55. ->where($where)
  56. ->where($where_op)
  57. ->order($sort, $order)
  58. ->paginate($limit);
  59. foreach ($list as $row) {
  60. $row->getRelation('company')->visible(['name']);
  61. }
  62. $result = array("total" => $list->total(), "rows" => $list->items());
  63. return json($result);
  64. }
  65. return $this->view->fetch();
  66. }
  67. /**
  68. * 添加
  69. */
  70. public function add()
  71. {
  72. if ($this->request->isPost()) {
  73. $params = $this->request->post("row/a");
  74. $params = $this->preExcludeFields($params);
  75. if (!$params) {
  76. $this->error(__('Parameter %s can not be empty', ''));
  77. }
  78. $result = false;
  79. try {
  80. //是否采用模型验证
  81. if ($this->modelValidate) {
  82. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  83. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  84. $this->model->validateFailException(true)->validate($validate);
  85. }
  86. if (isset($params['password'])) {
  87. if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
  88. $this->error(__("Please input correct password"));
  89. }
  90. $params['salt'] = Random::alnum();
  91. $params['password'] = md5(md5($params['password']) . $params['salt']);
  92. }
  93. $result = $this->model->allowField(true)->save($params);
  94. } catch (ValidateException|PDOException|Exception $e) {
  95. $this->error($e->getMessage());
  96. }
  97. if ($result == false) {
  98. $this->error(__('No rows were inserted'));
  99. }
  100. $this->success();
  101. }
  102. return $this->view->fetch();
  103. }
  104. /**
  105. * 编辑
  106. */
  107. public function edit($ids = null)
  108. {
  109. $row = $this->model->get($ids);
  110. if (!$row) {
  111. $this->error(__('No Results were found'));
  112. }
  113. $adminIds = $this->getDataLimitAdminIds();
  114. if (is_array($adminIds)) {
  115. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  116. $this->error(__('You have no permission'));
  117. }
  118. }
  119. if ($this->request->isPost()) {
  120. $params = $this->request->post("row/a");
  121. if (!$params) {
  122. $this->error(__('Parameter %s can not be empty', ''));
  123. }
  124. $params = $this->preExcludeFields($params);
  125. $result = false;
  126. try {
  127. //是否采用模型验证
  128. if ($this->modelValidate) {
  129. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  130. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  131. $row->validateFailException(true)->validate($validate);
  132. }
  133. if (isset($params['password'])) {
  134. if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
  135. $this->error(__("Please input correct password"));
  136. }
  137. $params['salt'] = Random::alnum();
  138. $params['password'] = md5(md5($params['password']) . $params['salt']);
  139. }
  140. $result = $row->allowField(true)->save($params);
  141. } catch (ValidateException|PDOException|Exception $e) {
  142. $this->error($e->getMessage());
  143. }
  144. if ($result == false) {
  145. $this->error(__('No rows were updated'));
  146. }
  147. $this->success();
  148. }
  149. $this->view->assign("row", $row);
  150. return $this->view->fetch();
  151. }
  152. }