Staff.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace app\admin\controller\qingdong\department;
  3. use addons\qingdong\model\Customer;
  4. use addons\qingdong\model\Staff as StaffModel;
  5. use addons\qingdong\model\StaffRole;
  6. use app\admin\model\AuthGroup;
  7. use app\admin\controller\qingdong\Base;
  8. use fast\Tree;
  9. use think\Db;
  10. use think\Exception;
  11. use app\admin\model\Admin;
  12. /**
  13. * 员工管理
  14. */
  15. class Staff extends Base {
  16. public function _initialize() {
  17. parent::_initialize();
  18. $this->model = new StaffModel();
  19. $this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isSuperAdmin());
  20. $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin());
  21. //角色组
  22. $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
  23. Tree::instance()->init($groupList);
  24. $groupdata = [];
  25. if ($this->auth->isSuperAdmin()) {
  26. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  27. foreach ($result as $k => $v) {
  28. $groupdata[$v['id']] = $v['name'];
  29. }
  30. } else {
  31. $result = [];
  32. $groups = $this->auth->getGroups();
  33. foreach ($groups as $m => $n) {
  34. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
  35. $temp = [];
  36. foreach ($childlist as $k => $v) {
  37. $temp[$v['id']] = $v['name'];
  38. }
  39. $result[__($n['name'])] = $temp;
  40. }
  41. $groupdata = $result;
  42. }
  43. $this->view->assign('groupdata', $groupdata);
  44. }
  45. /**
  46. * 员工列表
  47. * @return string
  48. */
  49. public function index() {
  50. $this->request->filter(['strip_tags']);
  51. if ($this->request->isAjax()) {
  52. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  53. $wheres=[];
  54. $wheres['id']=['in',StaffModel::getMyStaffIds()];
  55. $list = $this->model->with(['parent','admin','staffrole'])
  56. ->where($where)->order($sort, $order)->paginate($limit);
  57. $row = $list->items();
  58. $result = array("total" => $list->total(), "rows" => $row);
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 添加员工
  65. * @return string
  66. */
  67. public function add() {
  68. if ($this->request->isAjax()) {
  69. $data = $this->request->post('row/a');
  70. $mobile=$data['mobile'];
  71. $count = StaffModel::where(['mobile'=>$mobile])->count();
  72. if($count > 0){
  73. $this->error('员工手机号已存在');
  74. }
  75. $newSalt = substr(md5(uniqid(true)), 0, 6);
  76. $newPassword = md5(md5($data['password']) . $newSalt);
  77. $data['salt'] = $newSalt;
  78. $data['password'] = $newPassword;
  79. $data['status'] = 1;
  80. if(empty($data['img'])){
  81. $data['img'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  82. }
  83. Db::startTrans();
  84. try {
  85. $group = $this->request->post("group/a");
  86. //过滤不允许的组别,避免越权
  87. $group = array_intersect($this->childrenGroupIds, $group);
  88. if (!$group) {
  89. exception(__('The parent group exceeds permission limit'));
  90. }
  91. $data['group_ids']=implode(',',$group);
  92. $result = $this->model->save($data);
  93. Db::commit();
  94. }catch (Exception $e){
  95. Db::rollback();
  96. $this->error($e->getMessage());
  97. }
  98. if (!$result) {
  99. $this->error('提交失败');
  100. }
  101. $this->success('提交成功');
  102. }
  103. $staffname = StaffModel::where([])->column('id,name');
  104. $staffs = ['' => '无'];
  105. foreach ($staffname as $id => $name) {
  106. $staffs[$id] = $name;
  107. }
  108. $this->view->assign('roles',StaffRole::where([])->column('name','id'));
  109. $this->view->assign('staffs', $staffs);
  110. return $this->view->fetch();
  111. }
  112. /**
  113. * 修改员工
  114. * @param null $ids
  115. * @return string
  116. */
  117. public function edit($ids = null) {
  118. $map['id'] = $ids;
  119. if ($this->request->isAjax()) {
  120. $data = $this->request->post('row/a');
  121. if($data['password']){
  122. $newSalt = substr(md5(uniqid(true)), 0, 6);
  123. $newPassword = md5(md5($data['password']) . $newSalt);
  124. $data['salt'] = $newSalt;
  125. $data['password'] = $newPassword;
  126. }else{
  127. unset($data['password']);
  128. }
  129. $mobile=$data['mobile'];
  130. $count = StaffModel::where(['mobile'=>$mobile,'id'=>['neq',$ids]])->count();
  131. if($count > 0){
  132. $this->error('员工手机号已存在');
  133. }
  134. if(empty($data['img'])){
  135. $data['img'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  136. }
  137. Db::startTrans();
  138. try {
  139. $group = $this->request->post("group/a",[]);
  140. //过滤不允许的组别,避免越权
  141. $group = array_intersect($this->childrenGroupIds, $group);
  142. if (!$group) {
  143. exception(__('The parent group exceeds permission limit'));
  144. }
  145. $data['group_ids']=implode(',',$group);
  146. $data['id']=$map['id'];
  147. $result=$this->model->save($data,$map);
  148. Db::commit();
  149. }catch (Exception $e){
  150. Db::rollback();
  151. $this->error($e->getMessage());
  152. }
  153. if (!$result) {
  154. $this->error('修改失败');
  155. }
  156. $this->success('修改成功');
  157. }
  158. $data = StaffModel::where($map)->find();
  159. $this->view->assign("row", $data);
  160. $staffname = StaffModel::where([])->column('id,name');
  161. $staffs = ['' => '无'];
  162. foreach ($staffname as $id => $name) {
  163. $staffs[$id] = $name;
  164. }
  165. $this->view->assign('roles',StaffRole::where([])->column('name','id'));
  166. $this->view->assign('staffs', $staffs);
  167. return $this->view->fetch();
  168. }
  169. /**
  170. * 删除员工
  171. */
  172. public function del($ids = null) {
  173. if ($this->request->isAjax()) {
  174. $map['id'] = array('in', $ids);
  175. $findinfo = StaffModel::where($map)->select();
  176. foreach($findinfo as $k=>$v){
  177. if($v['admin_id'] ==1){
  178. $this->error('管理员不可删除');
  179. }
  180. }
  181. $result = StaffModel::destroy($map);
  182. if (!$result) {
  183. $this->error('删除失败');
  184. }
  185. $cids = Customer::where(['owner_staff_id' => $ids])->column('id');
  186. try {
  187. foreach ($cids as $id) {
  188. Customer::moveSeas($id);
  189. }
  190. } catch (Exception $e) {
  191. $this->error($e->getMessage());
  192. }
  193. $this->success('删除成功');
  194. }
  195. return $this->view->fetch();
  196. }
  197. /**
  198. * 更新状态禁用账号
  199. */
  200. public function update_status() {
  201. $id = input('ids');
  202. $status = input('status', 2, 'intval');
  203. $staff = $this->model->where(['id' => $id])->find();
  204. if (empty($staff)) {
  205. $this->error('员工不存在');
  206. }
  207. if ($this->model->isUpdate(true)->save(['id' => $id, 'status' => $status])) {
  208. $this->success('操作成功');
  209. }
  210. $this->error('操作失败');
  211. }
  212. /**
  213. * 获取员工角色
  214. */
  215. public function getstaffrole(){
  216. $model=new StaffRole();
  217. $result = $model->where([])->field('id,name')->select();
  218. $searchlist = [];
  219. foreach ($result as $key => $value) {
  220. $searchlist[] = ['id' => $value['id'], 'name' => $value['name']];
  221. }
  222. $data = ['searchlist' => $searchlist];
  223. $this->success('', null, $data);
  224. }
  225. /**
  226. * 获取管理员账户
  227. */
  228. public function admin_username()
  229. {
  230. $params = input('name', '');
  231. $where['username'] = array('like', '%' . $params . '%');
  232. $list = Admin::where($where)->field('id,username as name')->select();
  233. $data['list'] = $list;
  234. return json_encode($data);
  235. }
  236. /**
  237. * 获取管理员邮箱
  238. */
  239. public function admin_email()
  240. {
  241. $params = input('name', '');
  242. $where['email'] = array('like', '%' . $params . '%');
  243. $list = Admin::where($where)->field('id,email as name')->select();
  244. $data['list'] = $list;
  245. return json_encode($data);
  246. }
  247. }