Geen omschrijving

PermissionService.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <?php
  2. namespace App\Services\Sys;
  3. use App\Models\Sys\SysGroup;
  4. use App\Models\Sys\SysUsers;
  5. use App\Models\Sys\SysPermission;
  6. use App\Models\Sys\SysRolePermission;
  7. use App\Models\Sys\SysRole;
  8. use App\Models\Sys\SysUserRole;
  9. use App\Models\Sys\SysUserAdver;
  10. use App\Models\Sys\SysUserCusts;
  11. use App\Models\Sys\SysCustomer;
  12. use App\Models\JuxingAdAccount;
  13. use App\Support\RedisModel;
  14. use Illuminate\Support\Facades\Hash;
  15. class PermissionService
  16. {
  17. public static function createPermission($parent_id,$title,$route,$weight,$icon)
  18. {
  19. $per = new SysPermission();
  20. $per->parent_id = $parent_id;
  21. $per->title = $title;
  22. $per->route = $route;
  23. $per->weight = $weight;
  24. $per->icon = $icon;
  25. return [$per->save(), 0];
  26. }
  27. public static function editPermission($id,$title,$route,$icon)
  28. {
  29. $per = SysPermission::where('id', $id)->first();
  30. if(!isset($per->id)){
  31. return ['参数有误', 1102];
  32. }
  33. $per->title = $title;
  34. $per->route = $route;
  35. $per->icon = $icon;
  36. return [$per->save(), 0];
  37. }
  38. /***
  39. * 对权限进行排序设置
  40. * @param $parent_id
  41. * @param $permission_id_arr
  42. */
  43. public static function be_sort($parent_id, $permission_id_arr){
  44. foreach ($permission_id_arr as $num => $p_id){
  45. SysPermission::query()->where("id",$p_id)
  46. ->where("parent_id",$parent_id)
  47. ->update(['weight'=>$num]);
  48. }
  49. return true;
  50. }
  51. public static function permissionList($parent_id)
  52. {
  53. $res = SysPermission::where('parent_id', $parent_id)
  54. ->where('enable', 1)
  55. ->orderBy('weight')
  56. ->get();
  57. if($res->isEmpty()){
  58. return $res;
  59. }
  60. foreach($res as $item){
  61. $item->child_count = SysPermission::where('parent_id', $item->id)->where('enable', 1)->count();
  62. }
  63. return $res;
  64. }
  65. public static function delPermission($id)
  66. {
  67. $res = SysPermission::where('id', $id)->update([
  68. 'enable' => 0
  69. ]);
  70. #删除子权限
  71. $res = SysPermission::where('parent_id', $id)->update([
  72. 'enable' => 0
  73. ]);
  74. return true;
  75. }
  76. /**
  77. * 获取权限树
  78. * @param int $parent_id
  79. * @param int $level
  80. * @param false $only_permission_id_arr [false:查看全部权限。权限集合数组:只查看特定的权限]
  81. * @return array
  82. */
  83. public static function permissionTree($parent_id = 0,$level = 1,$only_permission_id_arr = false, $is_tab = false){
  84. $admin_id = \Auth::id();
  85. $query = SysPermission::query()->where("enable",1)
  86. ->where("parent_id",$parent_id);
  87. if ($is_tab) {
  88. $query->where('is_view', 1);
  89. }
  90. if($admin_id != 1){
  91. $query->where('id', '!=', 5); //只有管理员能操作菜单权限
  92. }
  93. //是否只查看特定权限
  94. if($only_permission_id_arr!==false){
  95. $query->whereIn("id",$only_permission_id_arr);
  96. }
  97. $list = $query->select("id","title","route","icon")
  98. ->orderBy("weight")
  99. ->get()
  100. ->toArray();
  101. if(empty($list)) return [];
  102. foreach ($list as $k => $item){
  103. $list[$k]['level'] = $level;
  104. $list[$k]['sub'] = self::permissionTree($item['id'],$level+1,$only_permission_id_arr, $is_tab);
  105. }
  106. return $list;
  107. }
  108. public static function userPermission($admin_id)
  109. {
  110. # 获取用户权限集
  111. $role_ids = SysUserRole::where('sys_user_id', $admin_id)->where('enable', 1)->pluck('role_id')->all();
  112. if(empty($role_ids)){
  113. return null;
  114. }
  115. if(in_array(1, $role_ids)){
  116. //超级管理员拥有所有权限
  117. return self::permissionTree(0, 1, false, true);
  118. }
  119. $permissionIds = SysRolePermission::whereIn('role_id', $role_ids)->where('enable', 1)->pluck('permission_id')->all();
  120. $res = self::permissionTree(0, 1, $permissionIds, true);
  121. return $res;
  122. }
  123. public static function addRole($name,$desc,$per_ids)
  124. {
  125. $role = new SysRole();
  126. $role->name = $name;
  127. $role->desc = $desc;
  128. $role->save();
  129. #角色权限
  130. $data = array();
  131. foreach($per_ids as $permission_id){
  132. $data[] = [
  133. 'role_id' => $role->id,
  134. 'permission_id' => $permission_id
  135. ];
  136. }
  137. SysRolePermission::insert($data);
  138. return ['添加成功', 0];
  139. }
  140. public static function editRole($id,$name,$desc,$per_ids)
  141. {
  142. $role = SysRole::where('id', $id)->first();
  143. if(!isset($role->id)){
  144. return ['参数有误', 1102];
  145. }
  146. $role->name = $name;
  147. $role->desc = $desc;
  148. $role->save();
  149. #查出现有权限
  150. $al_per_ids = SysRolePermission::where('role_id', $id)
  151. ->where('enable', 1)
  152. ->pluck('permission_id')
  153. ->all();
  154. $new_per_ids = array_diff($per_ids, $al_per_ids);
  155. $del_per_ids = array_diff($al_per_ids, $per_ids);
  156. if(!empty($new_per_ids)){
  157. $data = [];
  158. foreach($new_per_ids as $permission_id){
  159. $data[] = [
  160. 'role_id' => $id,
  161. 'permission_id' => $permission_id
  162. ];
  163. }
  164. SysRolePermission::insert($data);
  165. }
  166. if(!empty($del_per_ids)){
  167. SysRolePermission::whereIn('permission_id', $del_per_ids)
  168. ->where('role_id', $id)
  169. ->update(['enable'=>0]);
  170. }
  171. return ['操作成功', 0];
  172. }
  173. public static function delRole($id)
  174. {
  175. $role = SysRole::where('id', $id)->where('enable', 1)->first();
  176. if(!isset($role->id)){
  177. return ['参数有误', 1102];
  178. }
  179. $role->enable = 0;
  180. return [$role->save(), 0];
  181. }
  182. public static function roleList()
  183. {
  184. $listQ = SysRole::where('enable', 1)->where('id','>',1);
  185. $list = $listQ->orderBy('id')->get();
  186. return $list;
  187. }
  188. public static function roleDetail($role_id)
  189. {
  190. $role = SysRole::where('id', $role_id)->first();
  191. if(!isset($role->id)){
  192. return null;
  193. }
  194. #查角色权限
  195. if($role_id == 1){
  196. $per_ids = SysRolePermission::where('enable', 1)->pluck('permission_id')->all();
  197. } else {
  198. $per_ids = SysRolePermission::where('enable', 1)->where('role_id', $role_id)->pluck('permission_id')->all();
  199. }
  200. $role->permissionIds = $per_ids;
  201. return $role;
  202. }
  203. public static function addUser($name,$password,$role_ids,$advertiser_ids,$desc,$cust_ids,$group_id)
  204. {
  205. if(strlen($password)<6){
  206. return ['', 2001];
  207. }
  208. $user = new SysUsers;
  209. $user->name = $name;
  210. $user->desc = $desc;
  211. $user->password = Hash::make($password);
  212. $user->group_id = $group_id;
  213. $user->save();
  214. #角色
  215. $data = array();
  216. foreach($role_ids as $role_id){
  217. $data[] = [
  218. 'sys_user_id' => $user->id,
  219. 'role_id' => $role_id
  220. ];
  221. }
  222. SysUserRole::insert($data);
  223. #广告主
  224. $data = array();
  225. foreach($cust_ids as $customer_id){
  226. $data[] = [
  227. 'sys_user_id' => $user->id,
  228. 'customer_id' => $customer_id
  229. ];
  230. }
  231. SysUserCusts::insert($data);
  232. return ['添加成功', 0];
  233. }
  234. public static function editUser($id,$name,$password,$role_ids,$advertiser_ids,$desc,$cust_ids,$group_id)
  235. {
  236. if($password && strlen($password)<6){
  237. return ['', 2001];
  238. }
  239. $user = SysUsers::where('id', $id)->first();
  240. if(!isset($user->id)){
  241. return ['参数有误', 1102];
  242. }
  243. $user->name = $name;
  244. $user->desc = $desc;
  245. $user->group_id = $group_id;
  246. if($password) $user->password = Hash::make($password);
  247. $user->save();
  248. #查出现有角色
  249. $al_role_ids = SysUserRole::where('sys_user_id', $id)
  250. ->where('enable', 1)
  251. ->pluck('role_id')
  252. ->all();
  253. $new_role_ids = array_diff($role_ids, $al_role_ids);
  254. $del_role_ids = array_diff($al_role_ids, $role_ids);
  255. if(!empty($new_role_ids)){
  256. $data = [];
  257. foreach($new_role_ids as $role_id){
  258. $data[] = [
  259. 'sys_user_id' => $id,
  260. 'role_id' => $role_id
  261. ];
  262. }
  263. SysUserRole::insert($data);
  264. }
  265. if(!empty($del_role_ids)){
  266. SysUserRole::whereIn('role_id', $del_role_ids)
  267. ->where('sys_user_id', $id)
  268. ->update(['enable'=>0]);
  269. }
  270. #客户绑定
  271. $al_cust_ids = SysUserCusts::where('sys_user_id', $id)
  272. ->where('enable', 1)
  273. ->pluck('customer_id')
  274. ->all();
  275. $new_cust_ids = array_diff($cust_ids, $al_cust_ids);
  276. $del_cust_ids = array_diff($al_cust_ids, $cust_ids);
  277. if(!empty($new_cust_ids)){
  278. $data = [];
  279. foreach($new_cust_ids as $customer_id){
  280. $data[] = [
  281. 'sys_user_id' => $id,
  282. 'customer_id' => $customer_id
  283. ];
  284. }
  285. SysUserCusts::insert($data);
  286. }
  287. if(!empty($del_cust_ids)){
  288. SysUserCusts::whereIn('customer_id', $del_cust_ids)
  289. ->where('sys_user_id', $id)
  290. ->update(['enable'=>0]);
  291. }
  292. return ['操作成功', 0];
  293. }
  294. public static function delUser($id)
  295. {
  296. $user = SysUsers::where('id', $id)->where('enable', 1)->first();
  297. if(!isset($user->id)){
  298. return ['参数有误', 1102];
  299. }
  300. $user->enable = 0;
  301. return [$user->save(), 0];
  302. }
  303. public static function userList($page, $pageSize)
  304. {
  305. $offset = ($page-1) * $pageSize;
  306. $listQ = SysUsers::where('enable', 1)->where('id','>',1);
  307. $count = $listQ->count();
  308. $list = $listQ->select('name', 'id', 'desc', 'group_id')
  309. ->orderBy('id')
  310. ->offset($offset)
  311. ->limit($pageSize)
  312. ->get();
  313. $groupListMap = SysGroup::query()
  314. ->whereIn('id', $list->pluck('group_id'))
  315. ->where('enable', 1)
  316. ->pluck('name', 'id');
  317. foreach($list as $item){
  318. $role_ids = SysUserRole::where('sys_user_id', $item->id)
  319. ->where('enable', 1)
  320. ->pluck('role_id')
  321. ->all();
  322. $item->roles = SysRole::whereIn('id', $role_ids)->where('enable', 1)->select('id', 'name')->get();
  323. /*$adver_ids = SysUserAdver::where('sys_user_id', $item->id)
  324. ->where('enable', 1)
  325. ->pluck('advertiser_id')
  326. ->all();
  327. $item->advers = JuxingAdAccount::select('nick_name', 'advertiser_id')->where('enable', 1)->whereIn('advertiser_id', $adver_ids)->get();*/
  328. $cust_ids = SysUserCusts::where('sys_user_id', $item->id)
  329. ->where('enable', 1)
  330. ->pluck('customer_id')
  331. ->all();
  332. $item->cust = SysCustomer::select('name', 'remarks')->where('enable', 1)->whereIn('id', $cust_ids)->get();
  333. $item->group_name = $groupListMap->get($item->group_id) ?? null;
  334. }
  335. return [$list, $count];
  336. }
  337. public static function userDetail($id)
  338. {
  339. $user = SysUsers::select('id', 'name', 'desc', 'group_id')->where('id', $id)->first();
  340. if(!isset($user->id)){
  341. return null;
  342. }
  343. #角色
  344. $user->role_ids = SysUserRole::where('sys_user_id', $id)
  345. ->where('enable', 1)
  346. ->pluck('role_id')
  347. ->all();
  348. #广告主
  349. $user->adver_ids = SysUserAdver::where('sys_user_id', $id)
  350. ->where('enable', 1)
  351. ->pluck('advertiser_id')
  352. ->all();
  353. #客户
  354. $user->cust_ids = SysUserCusts::where('sys_user_id', $id)
  355. ->where('enable', 1)
  356. ->pluck('customer_id')
  357. ->all();
  358. #用户组
  359. $user->group_name = SysGroup::where('id', $user->group_id)->where('enable', 1)->value('name') ?? null;
  360. return $user;
  361. }
  362. public static function adverList()
  363. {
  364. $list = JuxingAdAccount::select('nick_name', 'advertiser_id')
  365. ->where('enable', 1)
  366. ->orderBy('id')
  367. ->get();
  368. return $list;
  369. }
  370. public static function doLogin($username, $password)
  371. {
  372. $user = SysUsers::select(['name', 'password', 'id'])
  373. ->where('name', $username)
  374. ->where('enable', 1)
  375. ->first();
  376. if (empty($user)) {
  377. return ['账户不存在', 2002];
  378. }
  379. if (Hash::check($password, $user->password)) {
  380. $redisKey = env('LOGIN_REDIS_KEY') . '-'. $user->id;
  381. $randomKey = RedisModel::get($redisKey);
  382. if(!$randomKey){
  383. $randomKey = SysUsers::random(20);
  384. RedisModel::set($redisKey, $randomKey);
  385. RedisModel::expire($redisKey, 86400*30);
  386. }
  387. $data = array(
  388. 'admin_id' => $user->id,
  389. 'username' => $user->name,
  390. 'random' => $randomKey,
  391. 'ttl' => time()
  392. );
  393. return [$data, 0];
  394. } else {
  395. return ['登录账号或密码不正确', 2003];
  396. }
  397. }
  398. }