企微短剧业务系统

LicenseService.php 14KB

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