0
0

Roles.php 6.6 KB

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