企微短剧业务系统

LicenseService.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\AccountLicense;
  5. use App\Models\AuthorizeCorp;
  6. use App\Models\ChatGroup;
  7. use App\Models\DjUser;
  8. use App\Models\System\AdminManageRole;
  9. use App\Models\System\Role;
  10. use App\RedisModel;
  11. use App\Support\EmailQueue;
  12. use App\Support\qyApi\QyCommon;
  13. use Illuminate\Support\Facades\DB;
  14. class LicenseService
  15. {
  16. /**
  17. * 获取企微可使用的许可数
  18. * */
  19. public static function getLicenseCount($corpid)
  20. {
  21. return AccountLicense::where('corpid', $corpid)
  22. ->where('is_used', 0)
  23. ->where('enable', 1)
  24. ->count();
  25. }
  26. /**
  27. * 分配许可激活账号
  28. * */
  29. public static function activeAccount($corpid, $userid, $adminId, $sysGroupId, $isSysAdmin, &$errMsg)
  30. {
  31. try {
  32. # 判断是否有管理权限
  33. if(!$isSysAdmin) {
  34. if($adminId != $sysGroupId) {
  35. # 获取当前用户的角色信息
  36. $roleIds = AdminManageRole::select('role_id')->where('sys_user_id', $adminId)
  37. ->where('is_delete', 0)->distinct()->pluck('role_id');
  38. if(empty($roleIds))
  39. return 5229;
  40. # 判断当前用户是否有超级权限
  41. $isAdminAuth = Role::whereIn('id', $roleIds)->where('role_type', 10)->exists();
  42. if(!$isAdminAuth) return 5229;
  43. }
  44. }
  45. DB::beginTransaction();
  46. $userInfo = DjUser::where('user_id', $userid)->where('corpid', $corpid)->whereIn('status', [1, 4])->first();
  47. if(empty($userInfo)) {
  48. DB::rollBack();
  49. return 5236;
  50. }
  51. # 查询可用的activeCode
  52. $activeCodeInfo = AccountLicense::where('corpid', $corpid)->where('is_used', 0)->where('enable', 1)->first();
  53. $activeCode = $activeCodeInfo->active_code ?? null;
  54. if(!$activeCode) {
  55. DB::rollBack();
  56. return 5230;
  57. }
  58. $userInfo->is_active = 1;
  59. $userInfo->active_code = $activeCode;
  60. $userInfo->active_time = date('Y-m-d H:i:s');
  61. $userInfo->expire_time = null;
  62. $result = $userInfo->save();
  63. if(!$result) {
  64. DB::rollBack();
  65. EmailQueue::rPush('修改客服激活信息失败', $corpid . '-' . $userid, ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  66. Log::logError('客服激活时调用接口返回错误码', [
  67. 'corpid' => $corpid,
  68. 'userid' => $userid,
  69. 'active_code' => $activeCode,
  70. ], 'ActiveAccountByLicense');
  71. return 5231;
  72. }
  73. # 修改许可状态为已使用
  74. $activeCodeInfo->is_used = 1;
  75. $result = $activeCodeInfo->save();
  76. if(!$result) {
  77. DB::rollBack();
  78. EmailQueue::rPush('修改许可可用状态失败', $activeCode, ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  79. Log::logError('修改许可可用状态失败', [
  80. 'corpid' => $corpid,
  81. 'userid' => $userid,
  82. 'active_code' => $activeCode,
  83. 'result' => $result
  84. ], 'ActiveAccountByLicense');
  85. return 5231;
  86. }
  87. # 激活
  88. $responseData = QyCommon::activeAccountByLicense($activeCode, $corpid, $userid);
  89. if(isset($responseData['errcode']) && $responseData['errcode']) {
  90. DB::rollBack();
  91. $errMsg = $responseData['errmsg'] ?? $responseData['errcode'];
  92. if(!in_array($responseData['errcode'], [701082, 701024])) {
  93. EmailQueue::rPush('客服激活时调用接口返回错误码', $errMsg, ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  94. }
  95. Log::logError('客服激活时调用接口返回错误码', [
  96. 'corpid' => $corpid,
  97. 'userid' => $userid,
  98. 'active_code' => $activeCode,
  99. 'response' => $responseData
  100. ], 'ActiveAccountByLicense');
  101. return 5231;
  102. }
  103. DB::commit();
  104. # 主动在同步客户列表队列头部塞入当前客服
  105. RedisModel::rPush(AuthorizeCorp::SYNC_CORP_CUSTOMER,json_encode([
  106. 'corpid' => $corpid, 'user_list' => [$userid]
  107. ]));
  108. # 主动在同步客户群队列头部塞入当前客服
  109. RedisModel::rPush(ChatGroup::CHAT_GROUP_BASIC_INFO_RDS, json_encode(['corpid' => $corpid, 'user_list' => [$userid], 'source' => '分配许可']));
  110. } catch (\Exception $e) {
  111. DB::rollBack();
  112. EmailQueue::rPush('分配许可激活账号发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  113. Log::logError('分配许可激活账号发生异常', [
  114. 'corpid' => $corpid,
  115. 'userid' => $userid,
  116. 'line' => $e->getLine(),
  117. 'msg' => $e->getMessage()
  118. ], 'ActiveAccountByLicense');
  119. return 5231;
  120. }
  121. return 0;
  122. }
  123. /**
  124. * 批量分配许可
  125. * */
  126. public static function batchActiveAccount($corpid, $useridList, $adminId, $sysGroupId, $isSysAdmin, &$errMsg, &$success, &$failed)
  127. {
  128. try {
  129. # 判断是否有管理权限
  130. if(!$isSysAdmin) {
  131. if($adminId != $sysGroupId) {
  132. # 获取当前用户的角色信息
  133. $roleIds = AdminManageRole::select('role_id')->where('sys_user_id', $adminId)
  134. ->where('is_delete', 0)->distinct()->pluck('role_id');
  135. if(empty($roleIds))
  136. return 5229;
  137. # 判断当前用户是否有超级权限
  138. $isAdminAuth = Role::whereIn('id', $roleIds)->where('role_type', 10)->exists();
  139. if(!$isAdminAuth) return 5229;
  140. }
  141. }
  142. if(!is_array($useridList)) $useridList = explode(',', $useridList);
  143. # 获取需要使用的许可
  144. $activeCodeCount = count(array_unique($useridList));
  145. $activeCodeData = AccountLicense::select(['active_code', 'id'])
  146. ->where('corpid', $corpid)->where('is_used', 0)->where('enable', 1)
  147. ->limit($activeCodeCount)->get();
  148. if($activeCodeCount != $activeCodeData->count())
  149. return 5232;
  150. # 组装批量分配许可的请求体
  151. $activeList = [];
  152. foreach ($activeCodeData as $key=>$activeCodeInfo) {
  153. $activeList[$key] = [
  154. 'active_code' => $activeCodeInfo->active_code,
  155. 'userid' => $useridList[$key],
  156. ];
  157. }
  158. Log::logInfo('本次批量分配组装的请求体为:', [
  159. 'corpid' => $corpid,
  160. 'userid_list' => $useridList,
  161. 'active_list' => $activeList
  162. ], 'BatchActiveAccountByLicense');
  163. # 执行分配
  164. $responseData = QyCommon::batchActiveAccountByLicense($corpid, $activeList);
  165. if(isset($responseData['errcode']) && $responseData['errcode']) {
  166. $errMsg = $responseData['errmsg'] ?? $responseData['errcode'];
  167. EmailQueue::rPush('批量分配客服许可时调用接口返回错误码', $errMsg, ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  168. Log::logError('客服批量激活时调用接口返回错误码', [
  169. 'corpid' => $corpid,
  170. 'active_list' => $activeList,
  171. 'response' => $responseData
  172. ], 'BatchActiveAccountByLicense');
  173. return 5231;
  174. }
  175. $activeResult = $responseData['active_result'];
  176. if(empty($activeResult)) {
  177. EmailQueue::rPush('批量分配客服返回的激活结果为空', $corpid, ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  178. return 5234;
  179. }
  180. $activeCodeUsed = [];
  181. $errData = [701082 => '用户已激活,请勿重复激活'];
  182. foreach ($activeResult as $item) {
  183. if($item['errcode']) {
  184. $failed++;
  185. $errMsgMid['user_id'] = $item['userid'];
  186. $errMsgMid['errcode'] = isset($errData[$item['errcode']]) ? $errData[$item['errcode']] :$item['errcode'];
  187. $errMsgMid['user_name'] = DjUser::where('corpid', $corpid)
  188. ->where('user_id', $item['userid'])->where('status', 1)->value('name');
  189. $errMsg[] = $errMsgMid;
  190. } else {
  191. $success++;
  192. $activeCodeUsed[] = $item['active_code'];
  193. # 更新用户激活状态
  194. DjUser::where('corpid', $corpid)->where('user_id', $item['userid'])->whereIn('status', [1, 4])
  195. ->update(['active_code' => $item['active_code'], 'is_active' => 1, 'active_time' => date('Y-m-d H:i:s'), 'expire_time' => null]);
  196. # 主动在同步客户列表队列头部塞入当前客服
  197. RedisModel::rPush(AuthorizeCorp::SYNC_CORP_CUSTOMER,json_encode([
  198. 'corpid' => $corpid, 'user_list' => [$item['userid']]
  199. ]));
  200. }
  201. }
  202. # 修改许可使用状态
  203. if($activeCodeUsed) {
  204. AccountLicense::where('corpid', $corpid)->whereIn('active_code', $activeCodeUsed)->update(['is_used' => 1]);
  205. }
  206. } catch (\Exception $e) {
  207. EmailQueue::rPush('批量分配许可过程发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  208. Log::logError('批量分配许可过程发生异常', [
  209. 'corpid' => $corpid,
  210. 'userid_list' => $useridList,
  211. 'line' => $e->getLine(),
  212. 'msg' => $e->getMessage(),
  213. ], 'BatchActiveAccountByLicense');
  214. return 5233;
  215. }
  216. return 0;
  217. }
  218. /**
  219. * 许可转移
  220. * */
  221. public static function transfer($corpid, $handoverUserid, $takeoverUserid, $adminId, $sysGroupId, $isSysAdmin, &$errMsg)
  222. {
  223. $transferList = array(
  224. [
  225. 'handover_userid' => $handoverUserid,
  226. 'takeover_userid' => $takeoverUserid
  227. ]
  228. );
  229. try {
  230. # 判断是否有管理权限
  231. if(!$isSysAdmin) {
  232. if($adminId != $sysGroupId) {
  233. # 获取当前用户的角色信息
  234. $roleIds = AdminManageRole::select('role_id')->where('sys_user_id', $adminId)
  235. ->where('is_delete', 0)->distinct()->pluck('role_id');
  236. if(empty($roleIds))
  237. return 5229;
  238. # 判断当前用户是否有超级权限
  239. $isAdminAuth = Role::whereIn('id', $roleIds)->where('role_type', 10)->exists();
  240. if(!$isAdminAuth) return 5229;
  241. }
  242. }
  243. # 转移
  244. $responseData = QyCommon::batchTransferLicense($corpid, $transferList);
  245. if(isset($responseData['errcode']) && $responseData['errcode']) {
  246. $errMsg = $responseData['errmsg'] ?? $responseData['errcode'];
  247. EmailQueue::rPush('批量转移客服许可时调用接口返回错误码', $errMsg, ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  248. Log::logError('客服批量转移时调用接口返回错误码', [
  249. 'corpid' => $corpid,
  250. 'transferList' => $transferList,
  251. 'response' => $responseData
  252. ], 'BatchTransferLicense');
  253. return 5235;
  254. }
  255. $transferResult = $responseData['transfer_result'];
  256. if(empty($transferResult)) {
  257. EmailQueue::rPush('批量转移客服返回的激活结果为空', $corpid, ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  258. return 5234;
  259. }
  260. foreach ($transferResult as $item) {
  261. if($item['errcode']) {
  262. $errMsg = $item['errcode'];
  263. return 5235;
  264. } else {
  265. # 更新用户激活状态
  266. $handoverUserInfo = DjUser::where('corpid', $corpid)->where('user_id', $handoverUserid)->where('status', 1)
  267. ->first();
  268. $handoverUserInfo->is_active = 0;
  269. $handoverUserInfo->save();
  270. DjUser::where('corpid', $corpid)->where('user_id', $takeoverUserid)->whereIn('status', [1, 4])
  271. ->update([
  272. 'active_code' => $handoverUserInfo->active_code,
  273. 'is_active' => 1,
  274. 'active_time' => $handoverUserInfo->active_time,
  275. 'expire_time' => $handoverUserInfo->expire_time]);
  276. # 主动在同步客户列表队列头部塞入当前客服
  277. RedisModel::rPush(AuthorizeCorp::SYNC_CORP_CUSTOMER,json_encode([
  278. 'corpid' => $corpid, 'user_list' => [$takeoverUserid]
  279. ]));
  280. }
  281. }
  282. } catch (\Exception $e) {
  283. EmailQueue::rPush('批量转移客服许可时发生异常', $errMsg, ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  284. Log::logError('批量转移客服许可时发生异常', [
  285. 'corpid' => $corpid,
  286. 'transferList' => $transferList,
  287. 'line' => $e->getLine(),
  288. 'msg' => $e->getMessage()
  289. ], 'BatchTransferLicense');
  290. return 5234;
  291. }
  292. return 0;
  293. }
  294. }