CompanyStaff.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 = $this->whereop('company_staff.company_id');
  51. $list = $this->model
  52. ->with(['company'])
  53. ->where($where)
  54. ->where($where_op)
  55. ->order($sort, $order)
  56. ->paginate($limit);
  57. foreach ($list as $row) {
  58. $row->getRelation('company')->visible(['name']);
  59. }
  60. $result = array("total" => $list->total(), "rows" => $list->items());
  61. return json($result);
  62. }
  63. return $this->view->fetch();
  64. }
  65. /**
  66. * 添加
  67. */
  68. public function add()
  69. {
  70. if ($this->request->isPost()) {
  71. $params = $this->request->post("row/a");
  72. $params = $this->preExcludeFields($params);
  73. if (!$params) {
  74. $this->error(__('Parameter %s can not be empty', ''));
  75. }
  76. //是否采用模型验证
  77. if ($this->modelValidate) {
  78. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  79. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  80. $this->model->validateFailException(true)->validate($validate);
  81. }
  82. //密码和盐
  83. if (isset($params['password'])) {
  84. if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
  85. $this->error('请输入6-30位密码');
  86. }
  87. $params['salt'] = Random::alnum();
  88. $params['password'] = md5(md5($params['password']) . $params['salt']);
  89. }
  90. //检查
  91. $check2 = Db::name('company_staff')->where('mobile',$params['mobile'])->find();
  92. if($check2){
  93. $this->error('该手机已经被注册为员工或商户管理员');
  94. }
  95. $check2 = Db::name('admin')->where('username',$params['mobile'])->find();
  96. if($check2){
  97. $this->error('该手机已经被注册为员工或商户管理员');
  98. }
  99. Db::startTrans();
  100. //保存
  101. $result = Db::name('company_staff')->insertGetId($params);
  102. if (!$result) {
  103. Db::rollback();
  104. $this->error(__('No rows were inserted'));
  105. }
  106. //同步到admin
  107. $admin = [
  108. 'username' => $params['mobile'],
  109. 'nickname' => $params['truename'],
  110. 'password' => $params['password'],
  111. 'salt' => $params['salt'],
  112. 'avatar' => '/assets/img/avatar.png',
  113. 'mobile' => $params['mobile'],
  114. 'createtime' => time(),
  115. 'status' => 'normal',
  116. 'company_id' => $params['company_id'],
  117. 'staff_id' => $result,
  118. ];
  119. $admin_id = Db::name('admin')->insertGetId($admin);
  120. if(!$admin_id){
  121. Db::rollback();
  122. $this->error('添加员工失败');
  123. }
  124. //管理员加组
  125. $access[] = [
  126. 'uid' => $admin_id,
  127. 'group_id' => $params['type'] == 2 ? 8 : 6, //8员工组,6管理组
  128. ];
  129. model('AuthGroupAccess')->saveAll($access);
  130. Db::commit();
  131. $this->success();
  132. }
  133. return $this->view->fetch();
  134. }
  135. /**
  136. * 编辑
  137. */
  138. public function edit($ids = null)
  139. {
  140. $row = $this->model->get($ids);
  141. if (!$row) {
  142. $this->error(__('No Results were found'));
  143. }
  144. $adminIds = $this->getDataLimitAdminIds();
  145. if (is_array($adminIds)) {
  146. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  147. $this->error(__('You have no permission'));
  148. }
  149. }
  150. if ($this->request->isPost()) {
  151. $params = $this->request->post("row/a");
  152. if (!$params) {
  153. $this->error(__('Parameter %s can not be empty', ''));
  154. }
  155. $params = $this->preExcludeFields($params);
  156. //是否采用模型验证
  157. if ($this->modelValidate) {
  158. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  159. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  160. $row->validateFailException(true)->validate($validate);
  161. }
  162. //密码和盐
  163. if ($params['password']) {
  164. if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
  165. $this->error('请输入6-30位密码');
  166. }
  167. $params['salt'] = Random::alnum();
  168. $params['password'] = md5(md5($params['password']) . $params['salt']);
  169. }else {
  170. unset($params['password'], $params['salt']);
  171. }
  172. //检查
  173. $check2 = Db::name('company_staff')->where('id','neq',$ids)->where('mobile',$params['mobile'])->find();
  174. if($check2){
  175. $this->error('该手机已经被注册为员工或商户管理员');
  176. }
  177. $check2 = Db::name('admin')->where('staff_id','neq',$ids)->where('username',$params['mobile'])->find();
  178. if($check2){
  179. $this->error('该手机已经被注册为员工或商户管理员');
  180. }
  181. Db::startTrans();
  182. //保存
  183. $result = $row->allowField(true)->save($params);
  184. if ($result === false) {
  185. Db::rollback();
  186. $this->error(__('No rows were updated'));
  187. }
  188. $row = $this->model->get($ids);
  189. //同步到admin
  190. $admin_info = Db::name('admin')->where('staff_id',$ids)->find();
  191. if(!empty($admin_info)){
  192. $admin = [
  193. 'username' => $params['mobile'],
  194. 'nickname' => $params['truename'],
  195. 'password' => $row['password'],
  196. 'salt' => $row['salt'],
  197. 'mobile' => $params['mobile'],
  198. 'updatetime' => time(),
  199. ];
  200. $admin_rs = Db::name('admin')->where('staff_id',$ids)->update($admin);
  201. if($admin_rs === false){
  202. Db::rollback();
  203. $this->error('修改员工失败');
  204. }
  205. }else{
  206. //新增
  207. //同步到admin
  208. $admin = [
  209. 'username' => $params['mobile'],
  210. 'nickname' => $params['truename'],
  211. 'password' => $row['password'],
  212. 'salt' => $row['salt'],
  213. 'avatar' => '/assets/img/avatar.png',
  214. 'mobile' => $params['mobile'],
  215. 'createtime' => time(),
  216. 'status' => 'normal',
  217. 'company_id' => $row['company_id'],
  218. 'staff_id' => $ids,
  219. ];
  220. $admin_id = Db::name('admin')->insertGetId($admin);
  221. if(!$admin_id){
  222. Db::rollback();
  223. $this->error('编辑员工失败');
  224. }
  225. //管理员加组
  226. $access[] = [
  227. 'uid' => $admin_id,
  228. 'group_id' => $params['type'] == 2 ? 8 : 6, //8员工组,6管理组
  229. ];
  230. model('AuthGroupAccess')->saveAll($access);
  231. }
  232. Db::commit();
  233. $this->success();
  234. }
  235. $this->view->assign("row", $row);
  236. return $this->view->fetch();
  237. }
  238. }