Roles.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class Roles extends Base
  5. {
  6. public function updates(){
  7. $data = request()->post();
  8. $data['org_id'] = cur_org_id();
  9. $result = validate('Roles')->check($data,[],'');
  10. if(true !== $result){
  11. $this->error = validate('Roles')->getError();
  12. return false;
  13. }
  14. $id = $data['id'];
  15. unset($data['id']);
  16. if($data['parent_id']!=9){//9是调度
  17. $data['level'] = 2;
  18. }
  19. if($id > 0){
  20. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  21. }else{
  22. $ret = $this->allowField(true)->save($data);
  23. }
  24. if(!$ret){
  25. $this->error = '操作失败';
  26. return false;
  27. }
  28. return true;
  29. }
  30. /**
  31. * 获取组织公共角色
  32. * @param int $type 2=管理员 1=项目管理员
  33. */
  34. public function getCommonList($type=1){
  35. $lists = Db::name('roles')
  36. ->where('del',0)
  37. ->where('enable',1)
  38. ->where('type',$type)
  39. ->where('org_id',0)
  40. ->select();
  41. return $lists?$lists:[];
  42. }
  43. /**
  44. * 获取组织的角色
  45. * @param $orgId
  46. */
  47. public function getList($type=0){//type==1 获取护工角色
  48. $map = [];
  49. if($type >0){
  50. $map[] = ['parent_id','=',11];
  51. }
  52. $list =Db::name('roles')
  53. ->field('id,name as title')
  54. ->where('org_id',cur_org_id())
  55. ->where('del',0)
  56. ->where($map)
  57. ->where('enable',1)
  58. ->select();
  59. return $list;
  60. }
  61. // 获取角色有效权限 type 1=后台权限 2=app权限
  62. public function getRolesAuths($id,$type=1){
  63. if($type == 1){
  64. $auths = db('roles')->where('id',$id)->value('auths');
  65. $auths = $auths?explode(',',$auths):[];
  66. if($auths){
  67. $auths = Db::name('menu')->where('id','in',$auths)->where('del',0)->where('enable',1)->column('id');
  68. }
  69. }else{
  70. $auths = db('roles')->where('id',$id)->value('appauths');
  71. $auths = $auths?explode(',',$auths):[];
  72. if($auths){
  73. $auths = Db::name('app_icon')->where('id','in',$auths)->where('del',0)->where('enable',1)->column('id');
  74. }
  75. }
  76. return $auths?$auths:[];
  77. }
  78. public function authSave(){
  79. $id = input('rolesId/d',0);
  80. if($id <= 0){
  81. $this->error = '参数错误';
  82. return false;
  83. }
  84. $ids = input('ids/a',[]);
  85. $appids = input('appids',[]);
  86. $ids = array_filter($ids,'check_val_empty');
  87. $appids = array_filter($appids,'check_val_empty');
  88. $data = [
  89. 'auths' => $ids?implode(',',$ids):'',
  90. 'appauths' => $appids?implode(',',$appids):'',
  91. ];
  92. $ret = Db::name('roles')->where('id',$id)->update($data);
  93. if($ret === false){
  94. $this->error = '参数错误';
  95. return false;
  96. }
  97. return true;
  98. }
  99. // 根据公共角色获取某组织下的角色列表
  100. public function getChildrenIds($id,$orgId){
  101. $ids = Db::name('roles')
  102. ->where('parent_id',$id)
  103. ->where('org_id',$orgId)
  104. ->where('del',0)
  105. ->where('enable',1)
  106. ->column('id');
  107. return $ids?$ids:[];
  108. }
  109. /*
  110. * 获取某个组织下全部角色
  111. * @param $orgId
  112. */
  113. public function getRolesAll($orgId=0){
  114. $list =Db::name('roles')
  115. ->field('id,name as title')
  116. ->where('org_id',$orgId)
  117. ->where('del',0)
  118. ->where('enable',1)
  119. ->select();
  120. return $list;
  121. }
  122. /**
  123. * 获取APP模块某人的某项二级权限
  124. * @param $userId
  125. * @param $auth 模块id
  126. * @return bool
  127. */
  128. public function getAppAuth($userId,$auth){
  129. $userRoles = Db::name('user_roles')
  130. ->alias('a')
  131. ->join('roles b','a.roles_id=b.id')
  132. ->where('a.user_id',$userId)
  133. ->value('b.appauths');
  134. $userRoles = $userRoles?explode(',',$userRoles):[];
  135. if($userRoles){
  136. $modes = Db::name('app_icon')->where('id','in',$userRoles)->where('del',0)->where('enable',1)->column('mode');
  137. if($modes && in_array($auth,$modes)){
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. /**
  144. * 获取某组织某角色的所有下级角色
  145. * @param $id
  146. * @param $orgId
  147. * @return array
  148. */
  149. public function getRoleIds($id,$orgId){
  150. $ids = Db::name('roles')
  151. ->where('parent_id',$id)
  152. ->where('org_id',$orgId)
  153. ->where('del',0)
  154. ->where('enable',1)
  155. ->column('id');
  156. return $ids?$ids:[];
  157. }
  158. // 检查某人是否有某项后台权限
  159. public function checkUserAuths($userId,$auth){
  160. $user = Db::name('user')->where('id',$userId)->where('enable',1)->where('del',0)->find();
  161. if(!$user){
  162. return false;
  163. }
  164. if($user['type'] == 1){ // 总公司人员默认有权限
  165. return true;
  166. }
  167. $ainfo = Db::name('menu')->where('url',$auth)->where('enable',1)->where('del',0)->find();
  168. if(!$ainfo){ // 没有查到权限,不验证
  169. return true;
  170. }
  171. $rolesId = Db::name('user_roles')->where('user_id',$userId)->value('roles_id');
  172. if(!$rolesId){ // 没有角色,默认无权限
  173. return false;
  174. }
  175. $auths = db('roles')->where('id',$rolesId)->value('auths');
  176. $auths = $auths?explode(',',$auths):[];
  177. if($auths){
  178. $auths = Db::name('menu')->where('id','in',$auths)->where('del',0)->where('enable',1)->column('id');
  179. $auths = $auths?$auths:[];
  180. }
  181. if(!$auths){
  182. return false;
  183. }
  184. return in_array($ainfo['id'],$auths);
  185. }
  186. //查看有派单权限的人员
  187. public function getDispatchOrder($mode,$orgId){
  188. $map[]=['','exp',Db::raw("FIND_IN_SET($mode,work_type_mode)")];
  189. // $map[] = ['parent_id','=',9];
  190. $map[] = ['enable','=',1];
  191. $map[] = ['del','=',0];
  192. $map[] = ['org_id','=',$orgId];
  193. $roles = Db::name('roles')->where($map)
  194. ->column('id');
  195. $data = [];
  196. if(!empty($roles)){
  197. $user = Db::name('user_roles')
  198. ->where('roles_id','in',$roles)
  199. ->column('user_id');
  200. $data = $user;
  201. }
  202. return $data;
  203. }
  204. }