CompanyStaff.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. $list = $this->model
  49. ->with(['company'])
  50. ->where($where)
  51. ->order($sort, $order)
  52. ->paginate($limit);
  53. foreach ($list as $row) {
  54. $row->getRelation('company')->visible(['name']);
  55. }
  56. $result = array("total" => $list->total(), "rows" => $list->items());
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 添加
  63. */
  64. public function add()
  65. {
  66. if ($this->request->isPost()) {
  67. $params = $this->request->post("row/a");
  68. $params = $this->preExcludeFields($params);
  69. if (!$params) {
  70. $this->error(__('Parameter %s can not be empty', ''));
  71. }
  72. $result = false;
  73. try {
  74. //是否采用模型验证
  75. if ($this->modelValidate) {
  76. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  77. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  78. $this->model->validateFailException(true)->validate($validate);
  79. }
  80. if (isset($params['password'])) {
  81. if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
  82. $this->error(__("Please input correct password"));
  83. }
  84. $params['salt'] = Random::alnum();
  85. $params['password'] = md5(md5($params['password']) . $params['salt']);
  86. }
  87. $result = $this->model->allowField(true)->save($params);
  88. } catch (ValidateException|PDOException|Exception $e) {
  89. $this->error($e->getMessage());
  90. }
  91. if ($result == false) {
  92. $this->error(__('No rows were inserted'));
  93. }
  94. $this->success();
  95. }
  96. return $this->view->fetch();
  97. }
  98. /**
  99. * 编辑
  100. */
  101. public function edit($ids = null)
  102. {
  103. $row = $this->model->get($ids);
  104. if (!$row) {
  105. $this->error(__('No Results were found'));
  106. }
  107. $adminIds = $this->getDataLimitAdminIds();
  108. if (is_array($adminIds)) {
  109. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  110. $this->error(__('You have no permission'));
  111. }
  112. }
  113. if ($this->request->isPost()) {
  114. $params = $this->request->post("row/a");
  115. if (!$params) {
  116. $this->error(__('Parameter %s can not be empty', ''));
  117. }
  118. $params = $this->preExcludeFields($params);
  119. $result = false;
  120. try {
  121. //是否采用模型验证
  122. if ($this->modelValidate) {
  123. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  124. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  125. $row->validateFailException(true)->validate($validate);
  126. }
  127. if (isset($params['password'])) {
  128. if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
  129. $this->error(__("Please input correct password"));
  130. }
  131. $params['salt'] = Random::alnum();
  132. $params['password'] = md5(md5($params['password']) . $params['salt']);
  133. }
  134. $result = $row->allowField(true)->save($params);
  135. } catch (ValidateException|PDOException|Exception $e) {
  136. $this->error($e->getMessage());
  137. }
  138. if ($result == false) {
  139. $this->error(__('No rows were updated'));
  140. }
  141. $this->success();
  142. }
  143. $this->view->assign("row", $row);
  144. return $this->view->fetch();
  145. }
  146. }