CompanyStaff.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. use think\Db;
  10. /**
  11. * 商家员工
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class CompanyStaff extends Backend
  16. {
  17. /**
  18. * CompanyStaff模型对象
  19. * @var \app\admin\model\CompanyStaff
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\CompanyStaff;
  26. $this->view->assign("typeList", $this->model->getTypeList());
  27. $this->view->assign("statusList", $this->model->getStatusList());
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. //当前是否为关联查询
  40. $this->relationSearch = true;
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags', 'trim']);
  43. if ($this->request->isAjax()) {
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('keyField')) {
  46. return $this->selectpage();
  47. }
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. //只能看自己的
  50. $where_op = [];
  51. if($this->auth->company_id){
  52. $where_op['company_staff.company_id'] = $this->auth->company_id;
  53. }
  54. $list = $this->model
  55. ->with(['company'])
  56. ->where($where)
  57. ->where($where_op)
  58. ->order($sort, $order)
  59. ->paginate($limit);
  60. foreach ($list as $row) {
  61. $row->getRelation('company')->visible(['name']);
  62. }
  63. $result = array("total" => $list->total(), "rows" => $list->items());
  64. return json($result);
  65. }
  66. return $this->view->fetch();
  67. }
  68. /**
  69. * 添加
  70. */
  71. public function add()
  72. {
  73. if ($this->request->isPost()) {
  74. $params = $this->request->post("row/a");
  75. $params = $this->preExcludeFields($params);
  76. if (!$params) {
  77. $this->error(__('Parameter %s can not be empty', ''));
  78. }
  79. //是否采用模型验证
  80. if ($this->modelValidate) {
  81. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  82. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  83. $this->model->validateFailException(true)->validate($validate);
  84. }
  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. //检查
  94. $check2 = Db::name('company_staff')->where('mobile',$params['mobile'])->find();
  95. if($check2){
  96. $this->error('该手机已经被注册为员工或商户管理员');
  97. }
  98. $check2 = Db::name('admin')->where('username',$params['mobile'])->find();
  99. if($check2){
  100. $this->error('该手机已经被注册为员工或商户管理员');
  101. }
  102. Db::startTrans();
  103. //保存
  104. $result = $this->model->allowField(true)->save($params);
  105. if ($result == false) {
  106. Db::rollback();
  107. $this->error(__('No rows were inserted'));
  108. }
  109. //同步到admin
  110. $admin = [
  111. 'username' => $params['mobile'],
  112. 'nickname' => $params['truename'],
  113. 'password' => $params['password'],
  114. 'salt' => $params['salt'],
  115. 'avatar' => '/assets/img/avatar.png',
  116. 'mobile' => $params['mobile'],
  117. 'createtime' => time(),
  118. 'status' => 'normal',
  119. 'company_id' => $params['company_id'],
  120. 'staff_id' => $result->id,
  121. ];
  122. $admin_id = Db::name('admin')->insertGetId($admin);
  123. if(!$admin_id){
  124. Db::rollback();
  125. $this->error('添加员工失败');
  126. }
  127. //管理员加组
  128. $access[] = [
  129. 'uid' => $admin_id,
  130. 'group_id' => $params['type'] == 2 ? 8 : 6, //8员工组,6管理组
  131. ];
  132. model('AuthGroupAccess')->saveAll($access);
  133. Db::commit();
  134. $this->success();
  135. }
  136. return $this->view->fetch();
  137. }
  138. /**
  139. * 编辑
  140. */
  141. public function edit($ids = null)
  142. {
  143. $row = $this->model->get($ids);
  144. if (!$row) {
  145. $this->error(__('No Results were found'));
  146. }
  147. $adminIds = $this->getDataLimitAdminIds();
  148. if (is_array($adminIds)) {
  149. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  150. $this->error(__('You have no permission'));
  151. }
  152. }
  153. if ($this->request->isPost()) {
  154. $params = $this->request->post("row/a");
  155. if (!$params) {
  156. $this->error(__('Parameter %s can not be empty', ''));
  157. }
  158. $params = $this->preExcludeFields($params);
  159. //是否采用模型验证
  160. if ($this->modelValidate) {
  161. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  162. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  163. $row->validateFailException(true)->validate($validate);
  164. }
  165. //密码和盐
  166. if (isset($params['password'])) {
  167. if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
  168. $this->error(__("Please input correct password"));
  169. }
  170. $params['salt'] = Random::alnum();
  171. $params['password'] = md5(md5($params['password']) . $params['salt']);
  172. }
  173. //检查
  174. $check2 = Db::name('company_staff')->where('id','neq',$ids)->where('mobile',$params['mobile'])->find();
  175. if($check2){
  176. $this->error('该手机已经被注册为员工或商户管理员');
  177. }
  178. $check2 = Db::name('admin')->where('staff_id','neq',$ids)->where('username',$params['mobile'])->find();
  179. if($check2){
  180. $this->error('该手机已经被注册为员工或商户管理员');
  181. }
  182. Db::startTrans();
  183. //保存
  184. $result = $row->allowField(true)->save($params);
  185. if ($result == false) {
  186. Db::rollback();
  187. $this->error(__('No rows were updated'));
  188. }
  189. //同步到admin
  190. $admin = [
  191. 'username' => $params['mobile'],
  192. 'nickname' => $params['truename'],
  193. 'password' => $params['password'],
  194. 'salt' => $params['salt'],
  195. 'mobile' => $params['mobile'],
  196. 'updatetime' => time(),
  197. ];
  198. $admin_rs = Db::name('admin')->where('staff_id',$ids)->update($admin);
  199. if($admin_rs === false){
  200. Db::rollback();
  201. $this->error('修改员工失败');
  202. }
  203. Db::commit();
  204. $this->success();
  205. }
  206. $this->view->assign("row", $row);
  207. return $this->view->fetch();
  208. }
  209. }