| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 | <?phpnamespace app\common\model;use think\Db;class Roles extends Base{    public function updates(){        $data = request()->post();        $data['org_id'] = cur_org_id();        $result = validate('Roles')->check($data,[],'');        if(true !== $result){            $this->error = validate('Roles')->getError();            return false;        }        $id = $data['id'];        unset($data['id']);        if($data['parent_id']!=9){//9是调度            $data['level'] = 2;        }        if($id > 0){            $ret = $this->allowField(true)->save($data,['id'=>$id]);        }else{            $ret = $this->allowField(true)->save($data);        }        if(!$ret){            $this->error = '操作失败';            return false;        }        return true;    }    /**     * 获取组织公共角色     * @param int $type 2=管理员 1=项目管理员     */    public function getCommonList($type=1){        $lists = Db::name('roles')            ->where('del',0)            ->where('enable',1)            ->where('type',$type)            ->where('org_id',0)            ->select();        return $lists?$lists:[];    }    /**     * 获取组织的角色     * @param $orgId     */    public function getList($type=0){//type==1 获取护工角色        $map = [];        if($type >0){            $map[] = ['parent_id','=',11];        }        $list =Db::name('roles')            ->field('id,name as title')            ->where('org_id',cur_org_id())            ->where('del',0)            ->where($map)            ->where('enable',1)            ->select();        return $list;    }    // 获取角色有效权限 type 1=后台权限 2=app权限    public function getRolesAuths($id,$type=1){        if($type == 1){            $auths = db('roles')->where('id',$id)->value('auths');            $auths = $auths?explode(',',$auths):[];            if($auths){                $auths = Db::name('menu')->where('id','in',$auths)->where('del',0)->where('enable',1)->column('id');            }        }else{            $auths = db('roles')->where('id',$id)->value('appauths');            $auths = $auths?explode(',',$auths):[];            if($auths){                $auths = Db::name('app_icon')->where('id','in',$auths)->where('del',0)->where('enable',1)->column('id');            }        }        return $auths?$auths:[];    }    public function authSave(){        $id = input('rolesId/d',0);        if($id <= 0){            $this->error = '参数错误';            return false;        }        $ids = input('ids/a',[]);        $appids = input('appids',[]);        $ids = array_filter($ids,'check_val_empty');        $appids = array_filter($appids,'check_val_empty');        $data = [            'auths' => $ids?implode(',',$ids):'',            'appauths' => $appids?implode(',',$appids):'',        ];        $ret = Db::name('roles')->where('id',$id)->update($data);        if($ret === false){            $this->error = '参数错误';            return false;        }        return true;    }    // 根据公共角色获取某组织下的角色列表    public function getChildrenIds($id,$orgId){        $ids = Db::name('roles')            ->where('parent_id',$id)            ->where('org_id',$orgId)            ->where('del',0)            ->where('enable',1)            ->column('id');        return $ids?$ids:[];    }    /*   * 获取某个组织下全部角色   * @param $orgId   */    public function getRolesAll($orgId=0){        $list =Db::name('roles')            ->field('id,name as title')            ->where('org_id',$orgId)            ->where('del',0)            ->where('enable',1)            ->select();        return $list;    }    /**     * 获取APP模块某人的某项二级权限     * @param $userId     * @param $auth 模块id     * @return bool     */    public function getAppAuth($userId,$auth){        $userRoles = Db::name('user_roles')            ->alias('a')            ->join('roles b','a.roles_id=b.id')            ->where('a.user_id',$userId)            ->value('b.appauths');        $userRoles = $userRoles?explode(',',$userRoles):[];        if($userRoles){            $modes = Db::name('app_icon')->where('id','in',$userRoles)->where('del',0)->where('enable',1)->column('mode');            if($modes && in_array($auth,$modes)){                return true;            }        }        return false;    }    /**     * 获取某组织某角色的所有下级角色     * @param $id     * @param $orgId     * @return array     */    public function getRoleIds($id,$orgId){        $ids = Db::name('roles')            ->where('parent_id',$id)            ->where('org_id',$orgId)            ->where('del',0)            ->where('enable',1)            ->column('id');        return $ids?$ids:[];    }    // 检查某人是否有某项后台权限    public function checkUserAuths($userId,$auth){        $user = Db::name('user')->where('id',$userId)->where('enable',1)->where('del',0)->find();        if(!$user){            return false;        }        if($user['type'] == 1){ // 总公司人员默认有权限            return true;        }        $ainfo = Db::name('menu')->where('url',$auth)->where('enable',1)->where('del',0)->find();        if(!$ainfo){ // 没有查到权限,不验证            return true;        }        $rolesId = Db::name('user_roles')->where('user_id',$userId)->value('roles_id');        if(!$rolesId){ // 没有角色,默认无权限            return false;        }        $auths = db('roles')->where('id',$rolesId)->value('auths');        $auths = $auths?explode(',',$auths):[];        if($auths){            $auths = Db::name('menu')->where('id','in',$auths)->where('del',0)->where('enable',1)->column('id');            $auths = $auths?$auths:[];        }        if(!$auths){            return false;        }        return in_array($ainfo['id'],$auths);    }    //查看有派单权限的人员    public function getDispatchOrder($mode,$orgId){        $map[]=['','exp',Db::raw("FIND_IN_SET($mode,work_type_mode)")];//        $map[] = ['parent_id','=',9];        $map[] = ['enable','=',1];        $map[] = ['del','=',0];        $map[] = ['org_id','=',$orgId];        $roles = Db::name('roles')->where($map)            ->column('id');        $data = [];        if(!empty($roles)){            $user = Db::name('user_roles')                ->where('roles_id','in',$roles)                ->column('user_id');            $data = $user;        }        return $data;    }}
 |