input('id/d',0), 'account' => input('account','','trim'), 'real_name' => input('real_name','','trim'), 'mobile' => input('mobile','','trim'), 'password' => input('password','','trim'), 'depId' => input('depId/d',0), 'rolesId' => input('rolesId/d',0), 'enable' => input('enable/d',1), 'addr_id' => input('addr_id/d',0), 'sort'=>input('sort',0,'trim'), 'type_id' => input('type_id/d', 0), 'sex' => input('sex/d', 0), ]; $orgId = cur_org_id(); $result = validate('User')->check($data,[],'add'); if(true !== $result){ $this->error = validate('User')->getError(); return false; } Db::startTrans(); try{ $id = $data['id']; unset($data['id']); $depId = $data['depId']; $rolesId = $data['rolesId']; unset($data['depId']); unset($data['rolesId']); $role = Db::name('roles') ->where('id',$rolesId) ->find(); if($rolesId!=7 && $role['parent_id']!=7){ unset($data['type_id']); } if($id > 0){ if(!$data['password']){ unset($data['password']); } $data['update_time'] = date('Y-m-d H:i:s'); $ret = $this->allowField(true)->save($data,['id'=>$id]); if(!$ret){ \exception('操作失败'); } $ud = Db::name('user_dep')->where('user_id',$id)->find(); if(!$ud){ Db::name('user_dep')->insert(['user_id'=>$id,'dep_id'=>$depId]); }else{ Db::name('user_dep')->where('user_id',$id)->setField('dep_id',$depId); } $ur = Db::name('user_roles')->where('user_id',$id)->find(); if(!$ur){ Db::name('user_roles')->insert(['user_id'=>$id,'roles_id'=>$rolesId]); }else{ Db::name('user_roles')->where('user_id',$id)->setField('roles_id',$rolesId); } $rolesParentId = Db::name('roles') ->where('id',$rolesId) ->value('parent_id'); if($rolesParentId==11){ $check = Db::name('worker') ->where('user_id',$id)->find(); if(empty($check)){ $a = [ 'user_id'=>$id ]; $res = Db::name('worker') ->insertGetId($a); if(!$res){ \exception('护工保存失败'); } } } }else{ $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT); $data['create_time'] = date('Y-m-d H:i:s'); $data['ip'] = request()->ip(); $ret = $this->insertGetId($data); $ids = $ret; if(!$ret){ \exception('操作失败'); } Db::name('user_org')->insert(['user_id'=>$ids,'org_id'=>$orgId]); Db::name('user_dep')->insert(['user_id'=>$ids,'dep_id'=>$depId]); Db::name('user_roles')->insert(['user_id'=>$ids,'roles_id'=>$rolesId]); $rolesParentId = Db::name('roles') ->where('id',$rolesId) ->value('parent_id'); if($rolesParentId==11){ Db::name('worker') ->where('user_id',$ids) ->delete(); $a = [ 'user_id'=>$ids ]; $res = Db::name('worker') ->insertGetId($a); if(!$res){ \exception('护工保存失败'); } } } Db::commit(); return true; }catch (Exception $e){ Db::rollback(); $this->error = $e->getMessage(); return false; } } public function wxaddUser(){ $data = [ 'id' => input('id/d',0), 'real_name' => input('real_name','','trim'), 'enable' => input('enable/d',1), ]; $result = validate('User')->check($data,[],'wxedit'); if(true !== $result){ $this->error = validate('User')->getError(); return false; } Db::startTrans(); try{ $id = $data['id']; unset($data['id']); if($id > 0){ $data['update_time'] = date('Y-m-d H:i:s'); $ret = $this->allowField(true)->save($data,['id'=>$id]); if(!$ret){ \exception('操作失败'); } }else{ } Db::commit(); return true; }catch (Exception $e){ Db::rollback(); $this->error = $e->getMessage(); return false; } } public function addCompany(){ $data = [ 'id' => input('id/d',0), 'account' => input('account','','trim'), 'real_name' => input('real_name','','trim'), 'mobile' => input('mobile','','trim'), 'password' => input('password','','trim'), 'orgs' => input('orgs','','trim'), 'rolesId' => input('rolesId/d',0), 'enable' => input('enable/d',1), 'type' => 1 ]; $result = validate('User')->check($data,[],'addCompany'); if(true !== $result){ $this->error = validate('User')->getError(); return false; } Db::startTrans(); try{ $id = $data['id']; unset($data['id']); $rolesId = $data['rolesId']; unset($data['rolesId']); if($rolesId == 1){ //超级管理员 $data['orgs'] = ''; } if($id > 0){ if(!$data['password']){ unset($data['password']); } $data['update_time'] = date('Y-m-d H:i:s'); $ret = $this->allowField(true)->save($data,['id'=>$id]); if(!$ret){ \exception('操作失败'); } Db::name('user_roles')->where('user_id',$id)->setField('roles_id',$rolesId); }else{ $data['create_time'] = date('Y-m-d H:i:s'); $data['ip'] = request()->ip(); $ret = $this->allowField(true)->save($data); if(!$ret){ \exception('操作失败'); } Db::name('user_roles')->insert(['user_id'=>$this->id,'roles_id'=>$rolesId]); } Db::commit(); return true; }catch (Exception $e){ Db::rollback(); $this->error = $e->getMessage(); return false; } } // 修改密码 public function modifyPass($data){ $result = validate('User')->check($data,[],'modifyPass'); if(true !== $result){ $this->error = validate('User')->getError(); return false; } $info = Db::name('user')->where('id',$data['id'])->where('del',0)->find(); if(!$info){ $this->error = '用户不存在'; return false; } if (!password_verify($data['oldpass'], $info['password'])) { $this->error = "旧密码错误"; return false; } $data['update_time'] = date('Y-m-d H:i:s'); $sdata = [ 'update_time' => date('Y-m-d H:i:s'), 'password' => $data['pass'], 'login' => 1 ]; $ret = $this->allowField(true)->save($sdata,['id'=>$data['id']]); if(!$ret){ $this->error = "密码修改失败"; return false; } return true; } //app登录 public function login($accountName,$password){ // 登录限流 $ret = (new \app\common\util\ThrottlesUtil(config('app.login_throttles')))->tooManyAttempts($accountName); if($ret){ $this->error ='账号已被锁定,请稍后重试'; return false; } // 登录是根据账号和手机号组成,先查账号再查手机号 $user = Db::name('user') ->where('account',$accountName) ->where('del',0) ->find(); if(empty($user)){ $user = Db::name('user') ->where('mobile',$accountName) ->where('del',0) ->find(); } if(empty($user)){ $this->error = '账号或密码错误'; return false; } $pass = aes_decrypt($password); if(!password_verify($pass, $user['password'])){ $this->error = '账号或密码错误'; return false; } $token = $this->authLogin($user['id']); if(!$token){ $this->error = '登录失败'; return false; } // 登录成功,重置限流 (new \app\common\util\ThrottlesUtil(config('app.login_throttles')))->resetAttempts($accountName); return ['user_id' => $user['id'],'token' => $token]; } private function authLogin($userId,$openid=''){ Db::startTrans(); try{ $res = Db::name('user')->where('id',$userId)->update([ 'last_login_time' => date('Y-m-d H:i:s') ]); if(!$res){ \exception('登录失败'); } $token = model('Token')->updateToken($userId); if(!$token){ \exception(model('Token')->getError()); } if($openid){ $ret = Db::name('user_oauth')->insert([ 'user_id' => $userId, 'openid' => $openid, 'type' => 1, 'create_time' => date('Y-m-d H:i:s') ]); if (!$ret) { \exception('事务失败'); } } Db::commit(); return $token; }catch (Exception $e){ Db::rollback(); $this->error = $e->getMessage(); return false; } } // 小程序手机号登录 public function wxpLogin($phone,$orgId){ // 1.检查用户是否存在,存在登录,不存在告诉用户进行下一步 $user = Db::name('user')->where('mobile',$phone)->where('del',0)->find(); if(!$user){ // 不存在注册 HelpHander::error('用户不存在',2,['phone'=>$phone]); // 用户进行下一步 Db::startTrans(); try{ $data = [ 'account' => md5($phone.$orgId.get_unique_id().time()), 'real_name' => $phone, 'mobile' => $phone, 'password' => $phone, 'enable' => 1, 'type' => 2, 'last_login_time' => date('Y-m-d H:i:s'), ]; $data['create_time'] = date('Y-m-d H:i:s'); $data['ip'] = request()->ip(); $ret = $this->allowField(true)->save($data); if(!$ret){ \exception('操作失败'); } $userId = $this->id; $token = model('Token')->updateToken($userId); if(!$token){ \exception(model('Token')->getError()); } Db::name('user_org')->insert([ 'user_id' => $userId, 'org_id' => $orgId ]); Db::commit(); }catch (Exception $e){ Db::rollback(); $this->error = $e->getMessage(); return false; } return ['user_id' => $userId,'token' => $token,'org_id' => $orgId]; }else{ if($user['enable'] != 1){ HelpHander::error('用户被禁用,请联系管理员'); } // 获取用户组织 $uo = Db::name('user_org')->where('user_id',$user['id'])->find(); if(!$uo){ Db::name('user_org')->insert([ 'user_id' => $user['id'], 'org_id' => $orgId ]); $curOrgId = $orgId; }else{ $curOrgId = $uo['org_id']; } $org = Db::name('org')->where('id',$curOrgId)->where('del',0)->where('enable',1)->where('type',2)->find(); if(!$org){ $this->error = "项目不存在或已禁用"; return false; } Db::startTrans(); try{ $data = [ 'last_login_time' => date('Y-m-d H:i:s'), ]; $ret = Db::name('user')->where('id',$user['id'])->update($data); if(!$ret){ \exception('操作失败'); } $token = model('Token')->updateToken($user['id']); if(!$token){ \exception(model('Token')->getError()); } Db::commit(); }catch (Exception $e){ Db::rollback(); $this->error = $e->getMessage(); return false; } return ['user_id' => $user['id'],'token' => $token,'org_id' => $curOrgId]; } } // 小程序登录 public function wxLogin($openid,$nickname,$avatar,$orgId){ $oauth = Db::name('user_oauth') ->where('openid',$openid) ->where('type',1) ->find(); if($oauth){ return $this->wxPreLogin($openid,$orgId); } $org = Db::name('org')->where('id',$orgId)->where('del',0)->where('enable',1)->where('type',2)->find(); if(!$org){ $this->error = "项目不存在"; return false; } Db::startTrans(); try{ $data = [ 'account' => $orgId.'_'.$openid, 'real_name' => $nickname, 'head_image' => $avatar, 'mobile' => '', 'password' => $openid, 'enable' => 1, 'type' => 2, 'last_login_time' => date('Y-m-d H:i:s'), ]; $data['create_time'] = date('Y-m-d H:i:s'); $data['ip'] = request()->ip(); $ret = $this->allowField(true)->save($data); if(!$ret){ \exception('操作失败'); } $userId = $this->id; $token = model('Token')->updateToken($userId); if(!$token){ \exception(model('Token')->getError()); } if($openid){ $ret = Db::name('user_oauth')->insert([ 'user_id' => $userId, 'openid' => $openid, 'type' => 1, 'create_time' => date('Y-m-d H:i:s') ]); if (!$ret) { \exception('事务失败'); } } Db::name('user_org')->insert([ 'user_id' => $userId, 'org_id' => $orgId ]); Db::commit(); }catch (Exception $e){ Db::rollback(); $this->error = $e->getMessage(); return false; } return ['user_id' => $userId,'token' => $token]; } // 微信小程序预登陆 public function wxPreLogin($openid,$orgId){ $org = Db::name('org')->where('id',$orgId)->where('del',0)->where('enable',1)->where('type',2)->find(); if(!$org){ $this->error = "项目不存在"; return false; } $oauth = Db::name('user_oauth') ->where('openid',$openid) ->where('type',1) ->find(); if(empty($oauth)){ // 账号不存在注册一个 $data = [ 'account' => $orgId.'_'.$openid, 'real_name' => "", 'mobile' => '', 'password' => $openid, 'head_image' => "", 'enable' => 1, 'type' => 2, ]; $data['create_time'] = date('Y-m-d H:i:s'); $data['ip'] = request()->ip(); $ret = $this->allowField(true)->save($data); if(!$ret){ \exception('操作失败'); } $userId = $this->id; $uoData = [ 'openid'=>$openid, 'user_id'=>$userId, 'create_time'=>date('Y-m-d H:i:s'), 'type'=>1 ]; $uo = Db::name('user_oauth')->insert($uoData); if(!$uo){ $this->error = '登录失败'; return false; } }else{ $userId = $oauth['user_id']; } $user = Db::name('user') ->where('id',$userId) ->where('del',0) ->find(); if(empty($user)){ $this->error = '账号不存在'; return false; } $uo = Db::name('user_org')->where('user_id',$user['id'])->where('org_id',$orgId)->find(); if(!$uo){ Db::name('user_org')->insert([ 'user_id' => $user['id'], 'org_id' => $orgId ]); } $token = $this->authLogin($user['id'],'',$orgId); if(!$token){ $this->error = '登录失败'; return false; } return ['user_id' => $user['id'],'token' => $token,'org_name' => $org['name']]; } /** * 专项保洁获取人员 * * @author wst * @date 2021/9/6 10:45 */ public function getCleanWorker(){ $list = (new WorkTypeMode())->getRolesUser(14,cur_org_id()); $arr = []; foreach ($list as $k=>$v){ if(!empty($v['user'])){ foreach ($v['user'] as $k1=>$v1){ $arr[] = [ 'id'=>$v1['id'], 'title'=>$v1['real_name'], ]; } } } return $arr; } /** * 设备台账获取人员 * * @author wst * @date 2021/9/8 14:59 * @return array */ public function getDeviceWorker(){ $list = (new WorkTypeMode())->getRolesUser(10,cur_org_id()); $arr = []; foreach ($list as $k=>$v){ if(!empty($v['user'])){ foreach ($v['user'] as $k1=>$v1){ $arr[] = [ 'id'=>$v1['id'], 'title'=>$v1['real_name'], ]; } } } return $arr; } /** * 巡更模块获取人员 * * @author wst * @date 2021/9/8 15:00 * @return array */ public function getPatrolWorker($id){ $list = (new WorkTypeMode())->getRolesUser($id,cur_org_id()); $arr = []; foreach ($list as $k=>$v){ if(!empty($v['user'])){ foreach ($v['user'] as $k1=>$v1){ $arr[] = [ 'id'=>$v1['id'], 'title'=>$v1['real_name'], ]; } } } return $arr; } /** * 医废获取人员 * * @author wst * @date 2021/9/8 15:01 * @param string $test * @return bool */ public function getWasteWorker(){ $list = (new WorkTypeMode())->getRolesUser(11,cur_org_id()); $arr = []; foreach ($list as $k=>$v){ if(!empty($v['user'])){ foreach ($v['user'] as $k1=>$v1){ $arr[] = [ 'id'=>$v1['id'], 'title'=>$v1['real_name'], ]; } } } return $arr; } /** * 获取医废未转运提示人员 * * @author wst * @date 2021/9/8 15:01 * @param string $test * @return bool */ public function getWasteWorkerList(){ $user = Db::name('user') ->alias('u') ->field('u.id,u.real_name as title') ->join('user_org uo','uo.user_id=u.id') ->where([ 'uo.org_id'=>cur_org_id(), 'u.del'=>0, 'u.enable'=>1, ]) ->select(); return $user; } /** * 获取用户消息 * @param $userId */ public function info($userId,$orgId=0){ $user = Db::name('user') ->where('id',$userId) ->where('del',0) ->where('enable',1) ->field('id,real_name,mobile,type,head_image,work,station,addr_id,device_sn,login') ->find(); if(!$user){ $this->error = '用户不存在或被禁用'; return false; } $dep = Db::name('user_dep') ->alias('ud') ->join('dep d','d.id = ud.dep_id') ->field('d.id,d.title') ->where('ud.user_id',$userId) ->find(); $user['dep_id'] = $dep?$dep['id']:0; $user['dep_name'] = $dep?$dep['title']:''; $addrTitle = Db::name('address') ->where('id',$user['addr_id']) ->value('title'); $user['addr_title'] = $user['addr_id']>0?$addrTitle:''; $roles = Db::name('user_roles') ->alias('ur') ->join('roles r','r.id = ur.roles_id') ->field('r.id,r.name,r.parent_id') ->where('ur.user_id',$userId) ->find(); $user['roles_id'] = $roles?$roles['id']:0; $user['roles_name'] = $roles?$roles['name']:''; $parentId = $roles?$roles['parent_id']:0; $user['userType'] = $parentId==3?1:2;//2其它1客户 $user['workTypeMode'] = ''; $user['isTwoDispatch'] = 2; if($user['roles_id'] >0){ $rolesInfo = Db::name('roles') ->where('id',$user['roles_id']) ->find(); $user['workTypeMode'] = $rolesInfo['work_type_mode']; $user['isTwoDispatch'] = $rolesInfo['level']; } return $user; } /* * 获取某组织下全部人员 * @param $orgId */ public function gerUserAll($orgId = 0){ $user = Db::name('user') ->alias('u') ->field('u.id,u.real_name as title') ->join('user_org uo','u.id=uo.user_id') ->where('uo.org_id',$orgId) ->where('enable',1) ->where('del',0) ->select(); return $user; } //获取二级调度角色下用户、 public function get_two_dispatch_user($rolesId){ $user = Db::name('user_roles') ->alias('a') ->join('user u','a.user_id=u.id') ->where('a.roles_id',$rolesId) ->where('u.del',0) ->where('u.enable',1) ->where('u.type',0) ->select(); return $user; } //后台获取所有二级调度角色 public function get_two_dispatch_role($orgId,$type){ $map[] = ['enable','=',1]; $map[] = ['level','=',2]; $map[] = ['del','=',0]; $map[] = ['parent_id','=',9]; $map[] = ['org_id','=',$orgId]; $map[] = ['','exp',Db::raw("FIND_IN_SET(".$type.",work_type_mode)")]; $role = Db::name('roles') ->field('id,name as title') ->where($map) ->select(); return $role; } //api获取所有二级调度角色 public function get_two_dispatch_role1($orgId,$type){ $map[] = ['enable','=',1]; $map[] = ['level','=',2]; $map[] = ['del','=',0]; $map[] = ['parent_id','=',9]; $map[] = ['org_id','=',$orgId]; $map[] = ['','exp',Db::raw("FIND_IN_SET(".$type.",work_type_mode)")]; $role = Db::name('roles') ->field('id,name as realName') ->where($map) ->select(); return $role; } // 根据id获取用户信息 public function getUserInfo($id){ $info = Db::name('user') ->where('id',$id) ->where('del',0) ->find(); return $info; } //获取所有调度 public function get_dispatch_user($orgId){ $roles = Db::name('roles') ->where('parent_id',9) ->where('org_id',$orgId) ->where('del',0) ->where('enable',1) ->column('id'); if(empty($roles)){ return []; } $user = Db::name('user') ->alias('a') ->join('user_roles u','a.id=u.user_id') ->where('u.roles_id','in',$roles) ->where('a.del',0) ->where('a.enable',1) ->where('a.type',0) ->select(); return $user; } /** * 获取拥有陪护模块权限的人 * @param $orgId */ public function get_ph_dispatch_user($orgId){ $appId = Db::name('app_icon')->where('mode',25)->value('id'); if(!$appId){ return []; } $map[] = ['org_id','=',$orgId]; $map[] = ['del','=',0]; $map[] = ['enable','=',1]; $map[] = ['','exp',Db::raw("FIND_IN_SET('appauths','".$appId."')")]; $roles = Db::name('roles')->where($map)->column('id'); if(empty($roles)){ return []; } $user = Db::name('user') ->alias('a') ->join('user_roles u','a.id=u.user_id') ->where('u.roles_id','in',$roles) ->where('a.del',0) ->where('a.enable',1) ->where('a.type',0) ->select(); return $user; } /** * 运送获取人员 * * @author wst * @date 2021/9/8 14:59 * @return array */ public function getConveyWorker(){ $list = (new WorkTypeMode())->getRolesUser(6,cur_org_id()); $arr = []; foreach ($list as $k=>$v){ if(!empty($v['user'])){ foreach ($v['user'] as $k1=>$v1){ $arr[] = [ 'id'=>$v1['id'], 'title'=>$v1['real_name'], ]; } } } return $arr; } public function wxRegister($post){ $post['id'] = 0; $result = validate('User')->check($post,[],'wxadd'); if(true !== $result){ $this->error = validate('User')->getError(); return false; } $data = [ 'mobile' => $post['mobile'], 'real_name' => $post['real_name'], 'password' => $post['password'], ]; Db::startTrans(); try{ $data['account'] = md5($data['mobile'].$data['password'].get_unique_id().time()); $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT); $data['create_time'] = date('Y-m-d H:i:s'); $data['ip'] = request()->ip(); $data['update_time'] = date('Y-m-d H:i:s'); $data['last_login_time'] = date('Y-m-d H:i:s'); $data['type'] = 2; $data['enable'] = 1; $data['del'] = 0; $uid = Db::name('user')->insertGetId($data); if(!$uid){ \exception('操作失败'); } Db::name('user_org')->insert(['user_id'=>$uid,'org_id'=>$post['orgId']]); if($post['depId'] > 0){ Db::name('user_dep')->insert(['user_id'=>$uid,'dep_id'=>$post['depId']]); } $token = model('Token')->updateToken($uid); if(!$token){ \exception(model('Token')->getError()); } Db::commit(); return ['user_id' => $uid,'token' => $token,'org_id' => $post['orgId']]; }catch (Exception $e){ Db::rollback(); $this->error = $e->getMessage(); return false; } } /** * 绿化养护获取人员 * * @author wst * @date 2021/9/6 10:45 */ public function getGreenWorker() { $list = (new WorkTypeMode())->getRolesUser(19, cur_org_id()); $arr = []; foreach ($list as $k => $v) { if (!empty($v['user'])) { foreach ($v['user'] as $k1 => $v1) { $arr[] = [ 'id' => $v1['id'], 'title' => $v1['real_name'], ]; } } } return $arr; } public function getYsUser($orgId,$work=0){ $list = (new WorkTypeMode())->getRolesUser(3,$orgId,1); $arr = []; foreach ($list as $k=>$v){ if(!empty($v['user'])){ foreach ($v['user'] as $k1=>$v1){ $arr[] = [ 'id'=>$v1['id'], 'real_name'=>$v1['real_name'], ]; } } } return $arr; } public function getWorkTypeModeUser($id,$orgId,$work=0){ $list = (new WorkTypeMode())->getRolesUser($id,$orgId,$work); $arr = []; foreach ($list as $k=>$v){ if(!empty($v['user'])){ foreach ($v['user'] as $k1=>$v1){ $arr[] = [ 'id'=>$v1['id'], 'title'=>$v1['real_name'], ]; } } } return $arr; } }