企微短剧业务系统

PermissionService.php 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. * Created by:PhpStorm
  4. * Author:chenzhiyuan
  5. * Date: 2022/4/19
  6. * Time: 1:49 下午
  7. */
  8. namespace App\Service\Admin;
  9. use App\Log;
  10. use App\Models\System\AdminManageCorp;
  11. use App\Models\System\AdminManageRole;
  12. use App\Models\System\Permission;
  13. use App\Models\System\RolePermission;
  14. use App\Models\System\Users;
  15. class PermissionService
  16. {
  17. CONST ROUTE_EXIST = 4400,
  18. PARENT_ID_NOT_EXIST = 4401,
  19. SORT_ERROR = 4402,
  20. NOT_FOUND = 4403,
  21. SUCCESS = 0;
  22. /**
  23. * 获取权限树
  24. * @param $view_type
  25. * @param int $parent_id
  26. * @param int $level
  27. * @param false $only_permission_id_arr [false:查看全部权限。权限集合数组:只查看特定的权限]
  28. * @return array
  29. */
  30. public static function permission_tree($view_type,$parent_id = 0,$level = 1,$only_permission_id_arr = false){
  31. $query = Permission::query()->where("is_delete",0)
  32. ->where("view_type",$view_type)
  33. ->where("parent_id",$parent_id);
  34. //是否只查看特定权限
  35. if($only_permission_id_arr!==false){
  36. $query->whereIn("id",$only_permission_id_arr);
  37. }
  38. $list = $query->select("id","title","route","icon")
  39. ->orderBy("weight")
  40. ->get()
  41. ->toArray();
  42. if(empty($list)) return [];
  43. foreach ($list as $k => $item){
  44. $list[$k]['level'] = $level;
  45. $list[$k]['sub'] = self::permission_tree($view_type,$item['id'],++$level,$only_permission_id_arr);
  46. }
  47. return $list;
  48. }
  49. /**
  50. * 权限列表
  51. * @param $view_type
  52. * @param $parent_id
  53. * @return array
  54. */
  55. public static function permission_list($view_type,$parent_id){
  56. $list = Permission::query()->where("parent_id",$parent_id)
  57. ->where("view_type",$view_type)
  58. ->where("is_delete",0)
  59. ->select("id","title","parent_id","route","icon","created_at")
  60. ->orderBy("weight","asc")
  61. ->get()->toArray();
  62. foreach ($list as $k => $item){
  63. //统计子权限数量
  64. $list[$k]['sub_num'] = Permission::query()->where("parent_id",$item['id'])
  65. ->where("view_type",$view_type)
  66. ->where("is_delete",0)
  67. ->count();
  68. }
  69. return $list;
  70. }
  71. /**
  72. * 删除权限
  73. * @param $view_type
  74. * @param $id
  75. */
  76. public static function del_permission($view_type,$id)
  77. {
  78. /**删除指定权限**/
  79. $find = Permission::query()->where("is_delete",0)
  80. ->where("view_type",$view_type)
  81. ->where("id",$id)
  82. ->first();
  83. if(empty($find)) return self::NOT_FOUND;
  84. Permission::query()->where("id",$id)->update(['is_delete'=>1]);
  85. //todo 删除权限引用
  86. /**删除子集权限**/
  87. $sub = Permission::query()->where("is_delete")
  88. ->where("view_type",$view_type)
  89. ->where("parent_id",$id)
  90. ->get()->toArray();
  91. foreach ($sub as $item){
  92. self::del_permission($view_type,$item['id']); //递归删除
  93. }
  94. return self::SUCCESS;
  95. }
  96. /**
  97. * 创建权限
  98. * @param $view_type
  99. * @param $admin_id
  100. * @param $parent_id
  101. * @param $title
  102. * @param $route
  103. * @param $weight
  104. * @param $icon
  105. */
  106. public static function create_permission($view_type,$admin_id,$parent_id,$title,$route,$weight,$icon){
  107. //检查上级id是否存在
  108. if( $parent_id != 0 ){
  109. $check_parent_id = Permission::query()
  110. ->where("view_type",$view_type)
  111. ->where("id",$parent_id)
  112. ->where("is_delete",0)
  113. ->first();
  114. if(empty($check_parent_id)) return self::PARENT_ID_NOT_EXIST;
  115. }
  116. //检查路由地址是否已存在
  117. // $find = Permission::query()->where("root_admin_id",$root_admin_id)
  118. // ->where("route",$route)
  119. // ->where("is_delete",0)
  120. // ->first();
  121. // if(!empty($find)) return self::ROUTE_EXIST;
  122. Permission::query()->create(
  123. [
  124. 'view_type' => $view_type,
  125. 'title' => trim($title),
  126. 'parent_id' => $parent_id,
  127. 'route' => trim($route),
  128. 'icon' => trim($icon),
  129. 'weight' => $weight,
  130. 'create_admin_id' => $admin_id
  131. ]
  132. );
  133. return self::SUCCESS;
  134. }
  135. /**
  136. * 编辑权限(顶级)
  137. * @param $view_type
  138. * @param $id
  139. * @param $title
  140. * @param $route
  141. * @param $icon
  142. */
  143. public static function edit_permission_l1($view_type,$id, $title, $route, $icon){
  144. $find = Permission::query()->where("id",$id)
  145. ->where("view_type",$view_type)
  146. ->where("parent_id",0)
  147. ->first();
  148. if(empty($find)) return self::NOT_FOUND;
  149. Permission::query()->where("id",$id)
  150. ->update([
  151. 'title' => trim($title),
  152. 'route' => trim($route),
  153. 'icon' => trim($icon)
  154. ]);
  155. return self::SUCCESS;
  156. }
  157. /**
  158. * 编辑权限(顶级)
  159. * @param $view_type
  160. * @param $id
  161. * @param $title
  162. * @param $route
  163. */
  164. public static function edit_permission_l2($view_type,$id, $title, $route){
  165. $find = Permission::query()->where("id",$id)
  166. ->where("view_type",$view_type)
  167. ->where("parent_id","!=",0)
  168. ->first();
  169. if(empty($find)) return self::NOT_FOUND;
  170. Permission::query()->where("id",$id)
  171. ->update([
  172. 'title' => trim($title),
  173. 'route' => trim($route),
  174. ]);
  175. return self::SUCCESS;
  176. }
  177. /***
  178. * 对权限进行排序设置
  179. * @param $view_type
  180. * @param $parent_id
  181. * @param $permission_id_arr
  182. */
  183. public static function be_sort($view_type,$parent_id,$permission_id_arr){
  184. $count = Permission::query()
  185. ->where("view_type",$view_type)
  186. ->where("parent_id",$parent_id)
  187. ->where("is_delete",0)
  188. ->count();
  189. if($count != count($permission_id_arr)){
  190. return self::SORT_ERROR;
  191. }
  192. foreach ($permission_id_arr as $num => $p_id){
  193. Permission::query()->where("id",$p_id)
  194. ->where("parent_id",$parent_id)
  195. ->where("is_delete",0)
  196. ->update(['weight'=>$num]);
  197. }
  198. return self::SUCCESS;
  199. }
  200. /**
  201. * 获取用户的权限集合
  202. * @param $view_type
  203. * @param $admin_id
  204. * @param $is_system_user
  205. */
  206. public static function user_permission($view_type, $admin_id, $is_system_user,$sys_group_id)
  207. {
  208. $only_permission_id_arr = false;
  209. /**非超级用户根据用户角色,找到角色对应的所有权限**/
  210. if($is_system_user != Users::SYSTEM_ADMIN){
  211. # 判断账号是否已授权企微
  212. $hasAccount = AdminManageCorp::query()
  213. ->where("sys_user_id", $sys_group_id)
  214. ->where("is_delete",0)
  215. ->exists();
  216. if($hasAccount) {
  217. # 获取用户可操作的权限集
  218. $base_permission_id_arr = RoleService::getPermissionIdsOfUser($admin_id, $sys_group_id);
  219. } else {
  220. $base_permission_id_arr = RoleService::getPermissionIdsOfUser($admin_id, $sys_group_id, [24, 20]);
  221. }
  222. if(!is_array($base_permission_id_arr)) $base_permission_id_arr = $base_permission_id_arr->toArray();
  223. //补充权限集的上一集
  224. $parent_permission_id_arr = self::get_parent_permission_id($view_type,$base_permission_id_arr);
  225. //补充公共菜单
  226. $public_permission_id_arr = Permission::query()->where("is_delete",0)
  227. ->where("view_type",$view_type)
  228. ->where("public",1)
  229. ->pluck("id")->toArray();
  230. $parent_permission_id_arr = array_merge($public_permission_id_arr,$parent_permission_id_arr);
  231. //汇总权限集
  232. $only_permission_id_arr = array_unique(array_merge($base_permission_id_arr,$parent_permission_id_arr));
  233. }
  234. return self::permission_tree($view_type,0,1,$only_permission_id_arr);
  235. }
  236. /**
  237. * 获取所有的上级权限列表
  238. * @param $view_type
  239. * @param $sub_permission_id_arr
  240. * @return array
  241. */
  242. public static function get_parent_permission_id($view_type,$sub_permission_id_arr){
  243. if(empty($sub_permission_id_arr)){
  244. return [];
  245. }
  246. $parent_permission_id_arr = Permission::query()->where("is_delete",0)
  247. ->where("view_type",$view_type)
  248. ->whereIn("id",$sub_permission_id_arr)
  249. ->pluck("parent_id")->toArray();
  250. return array_merge($parent_permission_id_arr,self::get_parent_permission_id($view_type,$parent_permission_id_arr));
  251. }
  252. }