CompanyStaff.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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('请输入6-30位密码');
  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 = Db::name('company_staff')->insertGetId($params);
  105. if (!$result) {
  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,
  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 ($params['password']) {
  167. if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
  168. $this->error('请输入6-30位密码');
  169. }
  170. $params['salt'] = Random::alnum();
  171. $params['password'] = md5(md5($params['password']) . $params['salt']);
  172. }else {
  173. unset($params['password'], $params['salt']);
  174. }
  175. //检查
  176. $check2 = Db::name('company_staff')->where('id','neq',$ids)->where('mobile',$params['mobile'])->find();
  177. if($check2){
  178. $this->error('该手机已经被注册为员工或商户管理员');
  179. }
  180. $check2 = Db::name('admin')->where('staff_id','neq',$ids)->where('username',$params['mobile'])->find();
  181. if($check2){
  182. $this->error('该手机已经被注册为员工或商户管理员');
  183. }
  184. Db::startTrans();
  185. //保存
  186. $result = $row->allowField(true)->save($params);
  187. if ($result === false) {
  188. Db::rollback();
  189. $this->error(__('No rows were updated'));
  190. }
  191. $row = $this->model->get($ids);
  192. //同步到admin
  193. $admin_info = Db::name('admin')->where('staff_id',$ids)->find();
  194. if(!empty($admin_info)){
  195. $admin = [
  196. 'username' => $params['mobile'],
  197. 'nickname' => $params['truename'],
  198. 'password' => $row['password'],
  199. 'salt' => $row['salt'],
  200. 'mobile' => $params['mobile'],
  201. 'updatetime' => time(),
  202. ];
  203. $admin_rs = Db::name('admin')->where('staff_id',$ids)->update($admin);
  204. if($admin_rs === false){
  205. Db::rollback();
  206. $this->error('修改员工失败');
  207. }
  208. }else{
  209. //新增
  210. //同步到admin
  211. $admin = [
  212. 'username' => $params['mobile'],
  213. 'nickname' => $params['truename'],
  214. 'password' => $row['password'],
  215. 'salt' => $row['salt'],
  216. 'avatar' => '/assets/img/avatar.png',
  217. 'mobile' => $params['mobile'],
  218. 'createtime' => time(),
  219. 'status' => 'normal',
  220. 'company_id' => $row['company_id'],
  221. 'staff_id' => $ids,
  222. ];
  223. $admin_id = Db::name('admin')->insertGetId($admin);
  224. if(!$admin_id){
  225. Db::rollback();
  226. $this->error('编辑员工失败');
  227. }
  228. //管理员加组
  229. $access[] = [
  230. 'uid' => $admin_id,
  231. 'group_id' => $params['type'] == 2 ? 8 : 6, //8员工组,6管理组
  232. ];
  233. model('AuthGroupAccess')->saveAll($access);
  234. }
  235. Db::commit();
  236. $this->success();
  237. }
  238. $this->view->assign("row", $row);
  239. return $this->view->fetch();
  240. }
  241. }