企微短剧业务系统

AdminService.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <?php
  2. /**
  3. * Created by:PhpStorm
  4. * Author:chenzhiyuan
  5. * Date: 2022/4/19
  6. * Time: 11:34 上午
  7. */
  8. namespace App\Service\Admin;
  9. use App\Log;
  10. use App\Models\AuthorizeCorp;
  11. use App\Models\DramaUserRela;
  12. use App\Models\OfficialAccount;
  13. use App\Models\System\AdminManageCorp;
  14. use App\Models\System\AdminManageRole;
  15. use App\Models\System\Role;
  16. use App\Models\System\Users;
  17. use App\User;
  18. use Illuminate\Support\Facades\Hash;
  19. class AdminService
  20. {
  21. CONST USER_NAME_EXIST = 4404, //用户名已经存在
  22. PARAM_ERR = 4403, //请求参数错误
  23. SUCCESS_CODE = 0, //请求成功
  24. ERR_CODE = 500; // 更新数据失败
  25. CONST VIEW_TYPE = [
  26. 'helper', //助手看板
  27. 'data', //数据看板
  28. ];
  29. /***
  30. * 查询管理员的列表接口
  31. * @param $view_type
  32. * @param $is_system_admin
  33. * @param $group_admin_id
  34. * @param $page
  35. * @param $page_limit
  36. * @return array
  37. */
  38. public static function admin_list($view_type,$is_system_admin,$admin_id,$sys_group_id,$group_admin_id,$page,$page_limit,&$errno)
  39. {
  40. $userRoleType = 0;
  41. if(!$is_system_admin && ($admin_id != $sys_group_id)) {
  42. # 获取当前登录用户的角色权限
  43. $userRoleIds = AdminManageRole::select(['role_id'])->where('sys_user_id', $admin_id)->where('is_delete', 0)->pluck('role_id');
  44. if(empty($userRoleIds)) return [];
  45. $userRoleType = Role::whereIn('id', $userRoleIds)->where('is_delete', 0)->min('role_type');
  46. if(!$userRoleType) return [];
  47. }
  48. $query = Users::query()->where("enable",1);
  49. /**管理员查看权限**/
  50. if($is_system_admin == Users::SYSTEM_ADMIN){ //系统管理员只看到组管理员
  51. $query->whereRaw("group_admin_id = id");
  52. }else{
  53. $query->where("group_admin_id",$group_admin_id); //非系统管理员只看到当前组的所有成员
  54. }
  55. $total = $query->count();
  56. $list = $query
  57. ->select("id","name","is_system_admin as is_super_admin","is_system_admin","create_time", 'is_promoter')
  58. ->offset(($page-1)*$page_limit)
  59. ->limit($page_limit)
  60. ->orderByRaw("is_system_admin desc,create_time asc")
  61. ->get()->toArray();
  62. foreach ($list as $k=> $item){
  63. $admin_role_name_arr = []; //拥有角色名称集合
  64. $admin_manage_copy_name_arr = []; //管理主体ID
  65. if($item['is_system_admin'] != Users::SYSTEM_ADMIN){
  66. //拥有的角色ID
  67. $manage_role_id_arr = AdminManageRole::query()->where("is_delete",0)
  68. ->where("view_type",$view_type)
  69. ->where("sys_user_id",$item['id'])
  70. ->pluck("role_id")->toArray();
  71. //拥有角色
  72. $adminRoleData = Role::select(['name', 'role_type'])->where("is_delete",0)
  73. ->whereIn("id",$manage_role_id_arr)->get();
  74. $admin_role_name_arr = $adminRoleData->pluck("name")->toArray();
  75. $adminRoleType = $adminRoleData->min('role_type');
  76. //所管理主体
  77. $admin_manage_copy_id_arr = AdminManageCorp::query()->where("is_delete",0)
  78. ->where("view_type",$view_type)
  79. ->where("sys_user_id",$item['id'])
  80. ->pluck("corpid")->toArray();
  81. $admin_manage_copy_name_arr = AuthorizeCorp::query()->whereIn("id",$admin_manage_copy_id_arr)
  82. ->where("enable",1)
  83. ->pluck("corp_name")->toArray();
  84. }else{
  85. /***
  86. * 超级管理员
  87. */
  88. }
  89. if(!$is_system_admin && ($admin_id != $sys_group_id)) {
  90. $list[$k]['can_del'] = $adminRoleType < $userRoleType ? 0: 1;
  91. } else {
  92. $list[$k]['can_del'] = $is_system_admin ? 1 : ($item['id'] == $sys_group_id ? 0 : 1);
  93. }
  94. $list[$k]['role'] = $admin_role_name_arr;
  95. $list[$k]['corp'] = $admin_manage_copy_name_arr;
  96. }
  97. return [$total,$list];
  98. }
  99. /**
  100. * 管理员详情
  101. * @param $view_type
  102. * @param $is_system_admin
  103. * @param $group_admin_id
  104. * @param $admin_id
  105. */
  106. public static function admin_info($view_type, $is_system_admin, $group_admin_id, $admin_id){
  107. $find = Users::where("id",$admin_id)
  108. ->where(function ($query) use ($is_system_admin,$group_admin_id){
  109. if($is_system_admin != Users::SYSTEM_ADMIN) $query->where("group_admin_id",$group_admin_id);
  110. })
  111. ->selectRaw('id, name, is_system_admin as is_super_admin, is_system_admin, create_time, is_promoter, is_all_adq, adq_account, is_all_mp, mp_account, can_export')
  112. ->where("enable",1)->first();
  113. if(empty($find)) return self::PARAM_ERR;
  114. //补充角色
  115. $find->role_id = AdminManageRole::where("is_delete",0)->where("view_type",$view_type)
  116. ->where("sys_user_id",$admin_id)
  117. ->pluck("role_id");
  118. //补充主体
  119. $find->corp_id = AdminManageCorp::query()->where("is_delete",0)
  120. ->where("view_type",$view_type)
  121. ->where("sys_user_id",$admin_id)
  122. ->pluck("corpid");
  123. return $find;
  124. }
  125. /**
  126. * 拥有主体列表
  127. * @param $group_admin_id
  128. * @param $admin_id
  129. * @param $is_system_admin
  130. * @return array
  131. */
  132. public static function corp_list($group_admin_id,$admin_id,$is_system_admin){
  133. $query = AuthorizeCorp::query();
  134. //非超级管理员
  135. if($is_system_admin != Users::SYSTEM_ADMIN){
  136. $corp_id_arr = AdminManageCorp::query()->where("is_delete",0)
  137. ->where("sys_user_id",$admin_id)
  138. ->pluck("corpid")->toArray();
  139. $query->whereIn("id",$corp_id_arr);
  140. }
  141. return $query->where("enable",1)
  142. ->select("id","corp_name")
  143. ->orderBy("id","asc")
  144. ->get()->toArray();
  145. }
  146. /**
  147. * 可选角色
  148. * @param $view_type
  149. */
  150. public static function all_role_list($admin_id,$sys_group_id,$is_system_admin,&$errno)
  151. {
  152. $userRoleType = 0;
  153. if(!$is_system_admin && ($admin_id != $sys_group_id)) {
  154. # 获取当前登录用户的角色权限
  155. $userRoleIds = AdminManageRole::select(['role_id'])->where('sys_user_id', $admin_id)->where('is_delete', 0)->pluck('role_id');
  156. if(empty($userRoleIds)) return [];
  157. $userRoleType = Role::whereIn('id', $userRoleIds)->where('is_delete', 0)->min('role_type');
  158. if(!$userRoleType) return [];
  159. }
  160. return Role::where("is_delete",0)
  161. ->where(function($query) use($is_system_admin, $sys_group_id, $admin_id) {
  162. if(!$is_system_admin) {
  163. $query->where('sys_group_id', $sys_group_id);
  164. } else {
  165. $query->where('sys_group_id', $admin_id);
  166. }
  167. })
  168. ->where('role_type', '>=', $userRoleType)
  169. ->select("id","name", 'role_type')
  170. ->orderBy("role_type")
  171. ->get();
  172. }
  173. /**
  174. * 创建管理员
  175. * @param $view_type
  176. * @param $group_admin_id
  177. * @param $name
  178. * @param $password
  179. * @param $role_id_arr
  180. * @param $corpid_arr
  181. * @param $is_new_group [是否新建分组:true/false]
  182. * @return int
  183. */
  184. public static function admin_create(
  185. $view_type, $group_admin_id, $name, $password, $role_id_arr , $corpid_arr ,$is_new_group, $is_promoter,
  186. $is_all_adq, $adq_account, $is_all_mp, $mp_account, $can_export
  187. ){
  188. //检查用户名是否存在
  189. $find = Users::where("enable",1)->where("name",$name)->first();
  190. if(!empty($find)) return self::USER_NAME_EXIST;
  191. //创建用户
  192. $sys_user_id = Users::create([
  193. 'name' => $name,
  194. 'is_promoter' => $is_promoter,
  195. 'password' => Hash::make($password),
  196. 'group_admin_id' => $group_admin_id,
  197. 'is_all_adq' => $is_all_adq,
  198. 'adq_account' => $adq_account,
  199. 'is_all_mp' => $is_all_mp,
  200. 'mp_account' => $mp_account,
  201. 'can_export' => $can_export
  202. ])->id;
  203. //新建账号为新的分组
  204. if($is_new_group){
  205. Users::query()->where("id",$sys_user_id)->update(['group_admin_id' => $sys_user_id]);
  206. }
  207. //用户绑定所属主体
  208. self::sys_user_bind_corp_id($view_type,$sys_user_id,$corpid_arr);
  209. //用户绑定角色
  210. self::sys_user_bind_role($view_type,$sys_user_id,$role_id_arr);
  211. return self::SUCCESS_CODE;
  212. }
  213. /**
  214. * 系统用户绑定主体
  215. * @param $view_type
  216. * @param $admin_id
  217. * @param $corp_id_arr
  218. */
  219. private static function sys_user_bind_corp_id($view_type,$admin_id,$corp_id_arr){
  220. //绑定主体前释放主体
  221. AdminManageCorp::query()->where("sys_user_id",$admin_id)
  222. ->where("view_type",$view_type)
  223. ->update(['is_delete'=>1]);
  224. $is_system_admin = Users::query()->where("id",$admin_id)->value("is_system_admin");
  225. //非超级管理员就增加主体绑定
  226. if($is_system_admin!=Users::SYSTEM_ADMIN){
  227. foreach ($corp_id_arr as $corpid){
  228. AdminManageCorp::query()->updateOrInsert([
  229. 'sys_user_id' => $admin_id,
  230. 'corpid' => $corpid,
  231. ],[
  232. 'view_type' => $view_type,
  233. 'is_delete' => 0,
  234. ]);
  235. }
  236. }
  237. }
  238. /**
  239. * 系统用户绑定角色
  240. * @param $view_type
  241. * @param $admin_id
  242. * @param $role_id_arr
  243. */
  244. private static function sys_user_bind_role($view_type, $admin_id, $role_id_arr){
  245. //绑定主体前释放主体
  246. AdminManageRole::query()->where("sys_user_id",$admin_id)
  247. ->where("view_type",$view_type)
  248. ->update(['is_delete'=>1]);
  249. $is_system_admin = Users::query()->where("id",$admin_id)->value("is_system_admin");
  250. //非超级管理员就增加主体绑定
  251. if($is_system_admin!=Users::SYSTEM_ADMIN){
  252. foreach ($role_id_arr as $corpid){
  253. AdminManageRole::query()->updateOrInsert([
  254. 'sys_user_id' => $admin_id,
  255. 'role_id' => $corpid,
  256. ],[
  257. 'view_type' => $view_type,
  258. 'is_delete' => 0,
  259. ]);
  260. }
  261. }
  262. }
  263. /**
  264. * 编辑管理员
  265. * @param $view_type
  266. * @param $group_admin_id
  267. * @param $admin_id
  268. * @param $password
  269. * @param $role_id_arr
  270. * @param $corpid_arr
  271. * @return int
  272. */
  273. public static function admin_edit(
  274. $view_type, $group_admin_id, $admin_id, $password, $role_id_arr , $corpid_arr, $is_promoter,
  275. $is_all_adq, $adq_account, $is_all_mp, $mp_account, $can_export
  276. ){
  277. /**检查用户是否正确存在**/
  278. $find = Users::where("id",$admin_id)->where("enable",1)->first();
  279. if(empty($find)) return self::PARAM_ERR;
  280. /**保存修改**/
  281. $save_user_data = [
  282. 'is_promoter' =>$is_promoter,
  283. 'is_all_adq' => $is_all_adq,
  284. 'adq_account' => $adq_account,
  285. 'is_all_mp' => $is_all_mp,
  286. 'mp_account' => $mp_account,
  287. 'can_export' => $can_export,
  288. ];
  289. //是否修改密码
  290. if(!empty($password)){
  291. $save_user_data['password'] = Hash::make($password);
  292. }
  293. Users::where("id",$admin_id)->where("enable",1)->update($save_user_data);
  294. //调整绑定主体
  295. self::sys_user_bind_corp_id($view_type,$admin_id,$corpid_arr);
  296. //调整绑定角色
  297. self::sys_user_bind_role($view_type,$admin_id,$role_id_arr);
  298. return self::SUCCESS_CODE;
  299. }
  300. /**
  301. * 管理员删除
  302. * @param $is_system_admin
  303. * @param $group_admin_id [所属管理员分组]
  304. * @param $admin_id [要删除的管理员ID]
  305. * @return int
  306. */
  307. public static function admin_del($is_system_admin,$group_admin_id, $sys_user_id, $sys_group_id, $admin_id)
  308. {
  309. if(!$is_system_admin && ($admin_id != $sys_group_id)) {
  310. return 4506;
  311. }
  312. $find = Users::query()
  313. ->where("id",$sys_user_id)
  314. ->where(function ($query) use ($is_system_admin,$group_admin_id){
  315. if($is_system_admin != Users::SYSTEM_ADMIN) $query->where("group_admin_id",$group_admin_id);
  316. })
  317. ->where("enable",1)
  318. ->first();
  319. if(empty($find)) return self::PARAM_ERR;
  320. \DB::begintransaction();
  321. $res1 = $find->update(['enable'=>0]);
  322. //将授予角色、主体关系删除
  323. $manageRoleCount = AdminManageRole::query()->where("sys_user_id",$sys_user_id)->count();
  324. if($manageRoleCount > 0) {
  325. $res2 = AdminManageRole::query()->where("sys_user_id",$sys_user_id)->update(['is_delete'=>1]);
  326. } else {
  327. $res2 = true;
  328. }
  329. $manageCorpCount = AdminManageCorp::query()->where("sys_user_id",$sys_user_id)->count();
  330. if($manageCorpCount > 0) {
  331. $res3 = AdminManageCorp::query()->where("sys_user_id",$sys_user_id)->update(['is_delete'=>1]);
  332. } else {
  333. $res3 = true;
  334. }
  335. # 如果为投手,则将投手绑定关系状态修改
  336. $relaCount = DramaUserRela::query()->where('user_id', $sys_user_id)->where('enable', 1)->count();
  337. if($relaCount > 0) {
  338. $res4 = DramaUserRela::query()->where('user_id', $sys_user_id)->where('enable', 1)
  339. ->update(['is_show' => 0, 'disable_date' => date('Y-m-d')]);
  340. } else {
  341. $res4 = true;
  342. }
  343. if($res1 && $res2 && $res3 && $res4) {
  344. \DB::commit();
  345. return self::SUCCESS_CODE;
  346. } else {
  347. \DB::rollBack();
  348. return self::ERR_CODE;
  349. }
  350. }
  351. /**
  352. * 获取拥有"投手"角色的账号列表,不包括超级管理员
  353. * @param false $corp_id_str [非必传参数。不传时查看全部主体。传入corpid时,代表查询具体主体下的投手账号列表]
  354. */
  355. public static function get_toushou_user_list($corp_id_str = false){
  356. //投手角色ID
  357. $role_id = Role::toushou_role_id();
  358. //所有投手角色用户ID
  359. $all_toushou_admin_id_arr = AdminManageRole::query()->where("is_delete",0)
  360. ->where("view_type","helper")
  361. ->where("role_id",$role_id)
  362. ->pluck("sys_user_id")->toArray();
  363. /**查询账号**/
  364. $query = Users::query()->where("enable",1)
  365. ->where("is_system_admin",0)
  366. ->whereIn("id",$all_toushou_admin_id_arr);
  367. //是否查询指定主体
  368. if($corp_id_str!==false){
  369. $corp_id = AuthorizeCorp::query()->where("enable",1)
  370. ->where("corpid",$corp_id_str)
  371. ->value("id");
  372. $manage_corp_admin_id_arr = AdminManageCorp::query()->where("is_delete",0)
  373. ->where("corpid",$corp_id)
  374. ->pluck("sys_user_id")->toArray();
  375. $query->whereIn("id",$manage_corp_admin_id_arr); //查询拥有主体管理权限的用户
  376. }
  377. //返回列表
  378. return $query->select("id","name")
  379. ->get()->toArray();
  380. }
  381. /**
  382. * 获取管理的账号组列表
  383. * @param $group_admin_id
  384. * @param $is_system_admin
  385. */
  386. public static function manage_account_group_list($group_admin_id,$is_system_admin){
  387. /**整理账号组列表**/
  388. $query = Users::query()->where("is_system_admin",0)
  389. ->where("enable",1)
  390. ->whereRaw("id=group_admin_id"); //只查询组管理员
  391. if($is_system_admin != Users::SYSTEM_ADMIN){ //非系统管理员只查询当前组下的管理员列表
  392. $query->where("group_admin_id",$group_admin_id);
  393. }
  394. $admin_group_list = $query->select("id as group_id","name as group_name")->get()->keyBy("group_id")->toArray();
  395. /**账号组列表涉及到的主体列表**/
  396. $manage_corp_id_arr = [];
  397. foreach ($admin_group_list as $group_id =>$item){
  398. $manage_corp_id_arr[$group_id] = AdminManageCorp::query()
  399. ->where("is_delete",0)
  400. ->where("sys_user_id",$group_id)
  401. ->pluck("corpid")->toArray();
  402. }
  403. foreach ($admin_group_list as $group_id=>$item){
  404. $manage_corp_list = [];
  405. if(!empty($manage_corp_id_arr[$group_id])){
  406. $manage_corp_list = AuthorizeCorp::query()
  407. ->whereIn("id",$manage_corp_id_arr[$group_id])
  408. ->where("enable",1)
  409. ->select("corp_name","corpid","id")
  410. ->get()->keyBy("id")->toArray();
  411. foreach ($manage_corp_list as $corpid=>$corpinfo){
  412. $manage_corp_list[$corpid]['corp_name_alias'] = $item['group_name'] . ' — '.$corpinfo['corp_name'];
  413. unset($manage_corp_list[$corpid]['id']);
  414. }
  415. }
  416. $admin_group_list[$group_id]['manage_corp_list'] = array_values($manage_corp_list);
  417. }
  418. return array_values($admin_group_list);
  419. }
  420. public static function isSuperUser($isSystemAdmin, $sysGroupId, $adminId)
  421. {
  422. if(1 == $isSystemAdmin) return true;
  423. if($adminId == $sysGroupId) return true;
  424. # 查询公司账号下所有超级权限的角色ID
  425. $superRoleIdList = Role::getSuperRoleIdList($sysGroupId);
  426. if(empty($superRoleIdList)) return false;
  427. # 查询具有超级权限的账号ID
  428. $superUserIdList = AdminManageRole::getSuperUserIdList($superRoleIdList);
  429. if(empty($superUserIdList)) return false;
  430. # 再次通过账号表查询一次
  431. $finalSuperUserIdList = Users::getCorpUserIdList($sysGroupId, $superUserIdList);
  432. if(empty($finalSuperUserIdList)) return false;
  433. if(in_array($adminId, $finalSuperUserIdList)) return true;
  434. return false;
  435. }
  436. public static function getCorpBindUserList($corpId, $viewType, $groupAdminId, $isSystemAdmin) {
  437. $sysUserIdList = AdminManageCorp::getBindUserIdListByCorpId($viewType, $corpId);
  438. $query = Users::query()->whereIn('id', $sysUserIdList)->where('enable', 1);
  439. /**管理员查看权限**/
  440. if($isSystemAdmin == Users::SYSTEM_ADMIN){ //系统管理员只看到组管理员
  441. $query->whereRaw("group_admin_id = id");
  442. }else{
  443. $query->where("group_admin_id",$groupAdminId); //非系统管理员只看到当前组的所有成员
  444. }
  445. return $query->selectRaw('id as user_id, name as user_name')->get();
  446. }
  447. public static function corpBindMultipleUser($corpId, $userIdList, $viewType) {
  448. try{
  449. $systemAdminList = Users::getSystemAdminIdList();
  450. //绑定主体前释放主体
  451. AdminManageCorp::query()->where("corpid",$corpId)
  452. ->where("view_type",$viewType)
  453. ->update(['is_delete'=>1]);
  454. //非超级管理员就增加主体绑定
  455. foreach ($userIdList as $userId){
  456. if(in_array($userId, $systemAdminList)) {
  457. continue;
  458. }
  459. AdminManageCorp::query()->updateOrInsert([
  460. 'sys_user_id' => $userId,
  461. 'corpid' => $corpId,
  462. ],[
  463. 'view_type' => $viewType,
  464. 'is_delete' => 0,
  465. ]);
  466. }
  467. return ['成功', 0];
  468. } catch (\Exception $exception) {
  469. Log::logError('企微批量绑定成员异常', [
  470. 'file' => $exception->getFile(),
  471. 'line' => $exception->getLine(),
  472. 'msg' => $exception->getMessage(),
  473. 'trace' => $exception->getTraceAsString(),
  474. ], 'interface');
  475. return ['系统异常', 500];
  476. }
  477. }
  478. }