Roles.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. $cate = input('cate/d',0);
  80. $id = input('rolesId/d',0);
  81. if($id <= 0){
  82. $this->error = '参数错误';
  83. return false;
  84. }
  85. $ids = input('ids/a',[]);
  86. $appids = input('appids',[]);
  87. if($cate==1){
  88. $appids = $ids;
  89. }
  90. $ids = array_filter($ids,'check_val_empty');
  91. $appids = array_filter($appids,'check_val_empty');
  92. if($cate==1){
  93. $data = [
  94. 'appauths' => $appids?implode(',',$appids):'',
  95. ];
  96. }else{
  97. $data = [
  98. 'auths' => $ids?implode(',',$ids):'',
  99. ];
  100. }
  101. $ret = Db::name('roles')->where('id',$id)->update($data);
  102. if($ret === false){
  103. $this->error = '参数错误';
  104. return false;
  105. }
  106. return true;
  107. }
  108. // 根据公共角色获取某组织下的角色列表
  109. public function getChildrenIds($id,$orgId){
  110. $ids = Db::name('roles')
  111. ->where('parent_id',$id)
  112. ->where('org_id',$orgId)
  113. ->where('del',0)
  114. ->where('enable',1)
  115. ->column('id');
  116. return $ids?$ids:[];
  117. }
  118. /*
  119. * 获取某个组织下全部角色
  120. * @param $orgId
  121. */
  122. public function getRolesAll($orgId=0){
  123. $list =Db::name('roles')
  124. ->field('id,name as title')
  125. ->where('org_id',$orgId)
  126. ->where('del',0)
  127. ->where('enable',1)
  128. ->select();
  129. return $list;
  130. }
  131. /**
  132. * 获取APP模块某人的某项二级权限
  133. * @param $userId
  134. * @param $auth 模块id
  135. * @return bool
  136. */
  137. public function getAppAuth($userId,$auth){
  138. $userRoles = Db::name('user_roles')
  139. ->alias('a')
  140. ->join('roles b','a.roles_id=b.id')
  141. ->where('a.user_id',$userId)
  142. ->value('b.appauths');
  143. $userRoles = $userRoles?explode(',',$userRoles):[];
  144. if($userRoles){
  145. $modes = Db::name('app_icon')->where('id','in',$userRoles)->where('del',0)->where('enable',1)->column('mode');
  146. if($modes && in_array($auth,$modes)){
  147. return true;
  148. }
  149. }
  150. return false;
  151. }
  152. /**
  153. * 获取某组织某角色的所有下级角色
  154. * @param $id
  155. * @param $orgId
  156. * @return array
  157. */
  158. public function getRoleIds($id,$orgId){
  159. $ids = Db::name('roles')
  160. ->where('parent_id',$id)
  161. ->where('org_id',$orgId)
  162. ->where('del',0)
  163. ->where('enable',1)
  164. ->column('id');
  165. return $ids?$ids:[];
  166. }
  167. // 检查某人是否有某项后台权限
  168. public function checkUserAuths($userId,$auth){
  169. $user = Db::name('user')->where('id',$userId)->where('enable',1)->where('del',0)->find();
  170. if(!$user){
  171. return false;
  172. }
  173. if($user['type'] == 1){ // 总公司人员默认有权限
  174. return true;
  175. }
  176. $ainfo = Db::name('menu')->where('url',$auth)->where('enable',1)->where('del',0)->find();
  177. if(!$ainfo){ // 没有查到权限,不验证
  178. return true;
  179. }
  180. $rolesId = Db::name('user_roles')->where('user_id',$userId)->value('roles_id');
  181. if(!$rolesId){ // 没有角色,默认无权限
  182. return false;
  183. }
  184. $auths = db('roles')->where('id',$rolesId)->value('auths');
  185. $auths = $auths?explode(',',$auths):[];
  186. if($auths){
  187. $auths = Db::name('menu')->where('id','in',$auths)->where('del',0)->where('enable',1)->column('id');
  188. $auths = $auths?$auths:[];
  189. }
  190. if(!$auths){
  191. return false;
  192. }
  193. return in_array($ainfo['id'],$auths);
  194. }
  195. //查看有派单权限的人员
  196. public function getDispatchOrder($mode,$orgId){
  197. $map[]=['','exp',Db::raw("FIND_IN_SET($mode,work_type_mode)")];
  198. // $map[] = ['parent_id','=',9];
  199. $map[] = ['enable','=',1];
  200. $map[] = ['del','=',0];
  201. $map[] = ['org_id','=',$orgId];
  202. $roles = Db::name('roles')->where($map)
  203. ->column('id');
  204. $data = [];
  205. if(!empty($roles)){
  206. $user = Db::name('user_roles')
  207. ->where('roles_id','in',$roles)
  208. ->column('user_id');
  209. $data = $user;
  210. }
  211. return $data;
  212. }
  213. }