model = new StaffModel(); $this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isSuperAdmin()); $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin()); //角色组 $groupList = collection(MemberGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray(); Tree::instance()->init($groupList); $groupdata = []; if ($this->auth->isSuperAdmin()) { $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0)); foreach ($result as $k => $v) { $groupdata[$v['id']] = $v['name']; } } else { $result = []; $groups = $this->auth->getGroups(); foreach ($groups as $m => $n) { $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id'])); $temp = []; foreach ($childlist as $k => $v) { $temp[$v['id']] = $v['name']; } $result[__($n['name'])] = $temp; } $groupdata = $result; } // var_dump($groupdata);die; $this->view->assign('groupdata', $groupdata); } /** * 员工列表 * @return string */ public function index() { $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $wheres['id']=['in',StaffModel::getMyStaffIds()]; $list = $this->model->with(['parent','admin','staffrole']) ->where($where)->order($sort, $order)->paginate($limit); $row = $list->items(); $result = array("total" => $list->total(), "rows" => $row); return json($result); } //获取当前企业的席位数 $vip_user_nums = 1000000; $user_nums = $this->model->where('cid','=',CID)->count(); $company = Db::name("company")->where('id','=',CID)->find(); $vip_user_nums = $company['vip_user_nums']; $this->view->assign('vip_user_nums', $vip_user_nums); $this->view->assign('user_nums', $user_nums); return $this->view->fetch(); } /** * 添加员工 * @return string */ public function add() { if ($this->request->isAjax()) { //获取当前企业的席位数 $user_nums = $this->model->where('cid','=',CID)->count(); $company = Db::name("company")->where('id','=',CID)->find(); $vip_user_nums = $company['vip_user_nums']; if($user_nums>=$vip_user_nums){ $this->error('您的席位数已用完,请购买席位后重新添加'); } $data = $this->request->post('row/a'); $mobile=$data['mobile']; $count = StaffModel::where(['mobile'=>$mobile,'CID'=>CID])->count(); //如果存在 看关联表 是否与该公司关联 如果没有 新增关联关系 如果有 更新关系即可 if($count > 0){ $this->error('用户已存在'); } // // $count = StaffModel::where(['mobile'=>$mobile,'CID'=>CID])->count(); // $newSalt = substr(md5(uniqid(true)), 0, 6); // $newPassword = md5(md5($data['password']) . $newSalt); // $data['salt'] = $newSalt; // $data['password'] = $newPassword; $data['status'] = 1; if(empty($data['img'])){ $data['img'] = '/assets/img/avatar.png'; //设置新管理员默认头像。 } Db::startTrans(); try { $group = $this->request->post("group/a"); //过滤不允许的组别,避免越权 $group = array_intersect($this->childrenGroupIds, $group); if (!$group) { exception(__('The parent group exceeds permission limit')); } $data['group_ids']=implode(',',$group); $admin = Admin::where(['username'=>$mobile])->find(); if($admin){ $data['admin_id'] = $admin['id']; } $result = $this->model->save($data); $staffId = $this->model->getLastInsID(); $admin = Admin::where(['username'=>$mobile])->find(); if($admin){ //当前表再次同步下admin_id $userId = $admin['id']; $this->model->save(['admin_id'=>$userId],['id'=>$staffId]); //同步企业数据 $memberUserCount = MemberCompany::where(['uid'=>$userId,'cid'=>CID])->count(); if($memberUserCount == 0){ $memberCompany = [ 'uid' => $userId, 'cid' => CID, 'join_date' => time(), 'position' => $data['post'], ]; $memberCompanym = new MemberCompany(); $memberCompanym->save($memberCompany); } //同步权限数据 foreach ($group as $gid){ $staffGroupData = [ 'uid' => $userId, 'cid' => CID, 'group_id' => $gid ]; $memberGroupAccess = new MemberGroupAccess(); $memberGroupAccess->save($staffGroupData); } } Db::commit(); }catch (Exception $e){ Db::rollback(); $this->error($e->getMessage()); } if (!$result) { $this->error('提交失败'); } $this->success('提交成功'); } $staffname = StaffModel::where(["cid"=>CID])->column('id,name'); $staffs = ['' => '无']; foreach ($staffname as $id => $name) { $staffs[$id] = $name; } $this->view->assign('roles',StaffRole::where(['cid'=>CID])->column('name','id')); $this->view->assign('staffs', $staffs); return $this->view->fetch(); } /** * 修改员工 * @param null $ids * @return string */ public function edit($ids = null) { $map['id'] = $ids; if ($this->request->isAjax()) { $data = $this->request->post('row/a'); // if($data['password']){ // $newSalt = substr(md5(uniqid(true)), 0, 6); // $newPassword = md5(md5($data['password']) . $newSalt); // $data['salt'] = $newSalt; // $data['password'] = $newPassword; // }else{ // unset($data['password']); // } $mobile=$data['mobile']; $count = StaffModel::where(['mobile'=>$mobile,'id'=>['neq',$ids],'CID'=>CID])->count(); if($count > 0){ $this->error('员工手机号已存在'); } if(empty($data['img'])){ $data['img'] = '/assets/img/avatar.png'; //设置新管理员默认头像。 } Db::startTrans(); try { $group = $this->request->post("group/a",[]); //过滤不允许的组别,避免越权 $group = array_intersect($this->childrenGroupIds, $group); if (!$group) { exception(__('The parent group exceeds permission limit')); } $data['group_ids']=implode(',',$group); $data['id']=$map['id']; $result=$this->model->save($data,$map); $admin = Admin::where(['username'=>$mobile])->find(); if($admin){ $userId = $admin['id']; //当前表再次同步下admin_id $nowData = StaffModel::where(['mobile'=>$mobile,'id'=>['neq',$ids],'CID'=>CID])->count(); if(!$nowData['admin_id']){ $this->model->save(['admin_id'=>$userId],['id'=>$map['id']]); } //删除现有权限 重新添加权限 $memberGroupAccess = new MemberGroupAccess(); $memberGroupAccess->where('uid','=',$userId)->delete(); //同步权限数据 foreach ($group as $gid){ $staffGroupData = [ 'uid' => $userId, 'cid' => CID, 'group_id' => $gid ]; $memberGroupAccess->save($staffGroupData); } } Db::commit(); }catch (Exception $e){ Db::rollback(); $this->error($e->getMessage()); } if (!$result) { $this->error('修改失败'); } $this->success('修改成功'); } $data = StaffModel::where($map)->find(); $this->view->assign("row", $data); $staffname = StaffModel::where(["cid"=>CID])->column('id,name'); $staffs = ['' => '无']; foreach ($staffname as $id => $name) { $staffs[$id] = $name; } $this->view->assign('roles',StaffRole::where(["cid"=>CID])->column('name','id')); $this->view->assign('staffs', $staffs); return $this->view->fetch(); } /** * 删除员工 */ public function del($ids = null) { if ($this->request->isAjax()) { $map['id'] = array('in', $ids); $findinfo = StaffModel::where($map)->select(); foreach($findinfo as $k=>$v){ if($v['admin_id'] ==1){ $this->error('管理员不可删除'); } } $result = StaffModel::destroy($map); if (!$result) { $this->error('删除失败'); } $cids = Customer::where(['owner_staff_id' => $ids])->column('id'); try { foreach ($cids as $id) { Customer::moveSeas($id); } } catch (Exception $e) { $this->error($e->getMessage()); } $this->success('删除成功'); } return $this->view->fetch(); } /** * 更新状态禁用账号 */ public function update_status() { $id = input('ids'); $status = input('status', 2, 'intval'); $staff = $this->model->where(['id' => $id])->find(); if (empty($staff)) { $this->error('员工不存在'); } if ($this->model->isUpdate(true)->save(['id' => $id, 'status' => $status])) { $this->success('操作成功'); } $this->error('操作失败'); } /** * 获取员工角色 */ public function getstaffrole(){ $model=new StaffRole(); $result = $model->where([])->field('id,name')->select(); $searchlist = []; foreach ($result as $key => $value) { $searchlist[] = ['id' => $value['id'], 'name' => $value['name']]; } $data = ['searchlist' => $searchlist]; $this->success('', null, $data); } /** * 获取管理员账户 */ public function admin_username() { $params = input('name', ''); $where['username'] = array('like', '%' . $params . '%'); $list = Admin::where($where)->field('id,username as name')->select(); $data['list'] = $list; return json_encode($data); } /** * 获取管理员邮箱 */ public function admin_email() { $params = input('name', ''); $where['email'] = array('like', '%' . $params . '%'); $list = Admin::where($where)->field('id,email as name')->select(); $data['list'] = $list; return json_encode($data); } }