企微短剧业务系统

ContactService.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\AuthorizeCorp;
  5. use App\Models\CustomerDetails;
  6. use App\Models\DjUser;
  7. use App\RedisModel;
  8. use App\Support\EmailQueue;
  9. use App\Support\qyApi\QyCommon;
  10. use Illuminate\Support\Facades\DB;
  11. class ContactService
  12. {
  13. /**
  14. * 通讯录相关回调时间处理
  15. * */
  16. public static function changeContactEvent($msgObj)
  17. {
  18. $changeType = trim($msgObj->ChangeType);
  19. $corpid = trim($msgObj->AuthCorpId);
  20. if(empty($corpid)) {
  21. $corpid = trim($msgObj->ToUserName);
  22. }
  23. $openUserId = trim($msgObj->OpenUserID);
  24. $userId = trim($msgObj->UserID);
  25. switch($changeType) {
  26. case 'create_user': // 通讯录-新增成员事件
  27. # 获取成员详情
  28. $userDetail = ContactService::getContactUserDetail($corpid, $userId);
  29. if($userDetail === false) {
  30. Log::logError('获取通讯录成员数据回收失败', ['corpid' => $corpid, 'userId'=>$userId], 'ChangeContactEvent-CU');
  31. return false;
  32. }
  33. $insertData = [
  34. 'enable' => 1,
  35. 'is_open_contract' => 1,
  36. ];
  37. $insertData['user_id'] = $userId;
  38. if(isset($userDetail['name']))
  39. $insertData['name'] = $userDetail['name'];
  40. if(isset($userDetail['department']))
  41. $insertData['department'] = implode(',', $userDetail['department']);
  42. if(isset($userDetail['order']))
  43. $insertData['order'] = implode(',', $userDetail['order']);
  44. if(isset($userDetail['gender']))
  45. $insertData['gender'] = $userDetail['gender'];
  46. if(isset($userDetail['avatar']))
  47. $insertData['avatar'] = $userDetail['avatar'];
  48. if(isset($userDetail['status']))
  49. $insertData['status'] = $userDetail['status'];
  50. if(isset($userDetail['main_department']))
  51. $insertData['main_department'] = $userDetail['main_department'];
  52. if(isset($userDetail['is_leader_in_dept']))
  53. $insertData['is_leader_in_dept'] = implode(',', $userDetail['is_leader_in_dept']);
  54. if(isset($userDetail['thumb_avatar']))
  55. $insertData['thumb_avatar'] = $userDetail['thumb_avatar'];
  56. if(isset($userDetail['open_user_id']))
  57. $insertData['open_user_id'] = $userDetail['open_user_id'];
  58. if(isset($userDetail['direct_leader']))
  59. $insertData['direct_leader'] = implode(',', $userDetail['direct_leader']);
  60. if(isset($userDetail['position']))
  61. $insertData['position'] = $userDetail['position'];
  62. if(isset($userDetail['mobile']))
  63. $insertData['mobile'] = $userDetail['mobile'];
  64. if(isset($userDetail['email']))
  65. $insertData['email'] = $userDetail['email'];
  66. if(isset($userDetail['isleader']))
  67. $insertData['is_leader'] = $userDetail['isleader'];
  68. if(isset($userDetail['enable']))
  69. $insertData['wx_enable'] = $userDetail['enable'];
  70. if(isset($userDetail['hide_mobile']))
  71. $insertData['hide_mobile'] = $userDetail['hide_mobile'];
  72. if(isset($userDetail['telephone']))
  73. $insertData['telephone'] = $userDetail['telephone'];
  74. if(isset($userDetail['qr_code']))
  75. $insertData['qr_code'] = $userDetail['qr_code'];
  76. if(isset($userDetail['alias']))
  77. $insertData['alias'] = $userDetail['alias'];
  78. if(isset($userDetail['extattr']))
  79. $insertData['extattr'] = json_encode($userDetail['extattr']);
  80. $result = DjUser::updateOrCreate(['corpid' => $corpid, 'user_id' => $userId], $insertData);
  81. if(!$result) { // 插入数据库失败后将数据插入到Redis
  82. $saveResult = RedisModel::lPush(DjUser::USER_SAVE_FAIL_RDS, json_encode($insertData));
  83. if(!$saveResult) {
  84. Log::logError('添加通讯录成员数据回收失败', $insertData, 'ChangeContactEvent-CU');
  85. return false;
  86. }
  87. }
  88. break;
  89. case 'update_user': // 通讯录-更新成员事件
  90. $insertData['corpid'] = $corpid;
  91. $insertData['enable'] = 1;
  92. $insertData['open_user_id'] = $openUserId;
  93. $insertData['status'] = trim($msgObj->Status);
  94. $insertData['user_id'] = trim($msgObj->NewUserID);
  95. $insertData['gender'] = trim($msgObj->Gender);
  96. $insertData['name'] = trim($msgObj->Name);
  97. $insertData['alias'] = trim($msgObj->Alias);
  98. $insertData['avatar'] = trim($msgObj->Avatar);
  99. $insertData = array_filter($insertData);
  100. $result = DjUser::updateOrCreate([
  101. 'corpid' => $corpid,
  102. 'user_id' => $userId
  103. ], $insertData);
  104. if(!$result) { // 更新至数据库失败后将数据插入到Redis
  105. $saveResult = RedisModel::lPush(DjUser::USER_UPDATE_FAIL_RDS, json_encode($insertData));
  106. if(!$saveResult) {
  107. Log::logError('更新通讯录成员数据回收失败', $insertData, 'ChangeContactEvent-UU');
  108. return false;
  109. }
  110. }
  111. break;
  112. case 'delete_user': // 通讯录-删除成员事件
  113. try {
  114. $result = ContactService::deleteUser($corpid, $userId);
  115. if($result===false) { // 删除成员数据处理失败回收至Redis
  116. $recycleData = ['corpid' => $corpid, 'user_id' => $userId];
  117. $saveResult = RedisModel::lPush(DjUser::USER_DELETE_FAIL_RDS, json_encode($recycleData));
  118. if(!$saveResult) {
  119. Log::logError('更新通讯录成员数据回收失败', $recycleData, 'ChangeContactEvent-DU');
  120. return false;
  121. }
  122. }
  123. } catch (\Exception $e) {
  124. Log::logError('删除成员事件处理发生异常', [
  125. 'line' => $e->getLine(),
  126. 'msg' => $e->getMessage()
  127. ], 'DeleteUserCallBack');
  128. }
  129. break;
  130. }
  131. return true;
  132. }
  133. /**
  134. * 成员删除相关处理
  135. * */
  136. public static function deleteUser($corpid, $userId)
  137. {
  138. try {
  139. DB::beginTransaction();
  140. # 修改成员对应外部联系人关系表
  141. CustomerDetails::suffix($corpid)->where('corpid', $corpid)->where('user_id', $userId)->where('loss_status', 1)
  142. ->update(['loss_status'=>2, 'enable'=>0]);
  143. # 修改成员表
  144. $userInfo = DjUser::where('user_id', $userId)->where('corpid', $corpid)->first();
  145. if(empty($userInfo)) {
  146. EmailQueue::rPush('成员离职事件信息中的成员不存在', json_encode(['corpid' => $corpid, 'user_id' => $userId]), ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  147. return false;
  148. }
  149. if($userInfo->is_active == 1) { // 离职成员已分配的许可未处理,进行自动转移
  150. # 查询一个接替成员的信息
  151. $takeoverUser = DjUser::select('user_id')->where('is_active', 0)->where('enable', 1)->where('status', 1)->first();
  152. if(empty($takeoverUser)) { // 无可用接替成员
  153. EmailQueue::rPush('无可用接替成员', json_encode(['corpid' => $corpid, 'user_id' => $userId]), ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  154. } else {
  155. $transferResult = QyCommon::batchTransferLicense($corpid, array([
  156. 'handover_userid' => $userId, 'takeover_userid' => $takeoverUser->user_id
  157. ]));
  158. EmailQueue::rPush('离职自动转移许可完成', json_encode(['corpid' => $corpid, 'user_id' => $userId, 'takeover_user_id' => $takeoverUser->user_id, 'response' => $transferResult]), ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  159. }
  160. }
  161. $userInfo->status = 5;
  162. $userInfo->new_user_id = null;
  163. $userInfo->enable = 0;
  164. $userInfo->is_active = 0;
  165. $result = $userInfo->save();
  166. if(!$result) {
  167. DB::rollBack();
  168. return false;
  169. }
  170. DB::commit();
  171. } catch (\Exception $e) {
  172. DB::rollBack();
  173. Log::logError('删除成员事件处理发生异常', [
  174. 'line' => $e->getLine(),
  175. 'msg' => $e->getMessage(),
  176. 'corpid' => $corpid,
  177. 'userId' => $userId
  178. ], 'DeleteUserDB');
  179. EmailQueue::rPush('删除成员事件处理发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  180. return false;
  181. }
  182. return true;
  183. }
  184. /**
  185. * 获取企业员工信息
  186. * */
  187. public static function getContactUserDetail($corpid, $userid, $retry=0)
  188. {
  189. # 获取SuiteAccessToken
  190. $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取企业员工信息');
  191. if(empty($accessToken)) { // 令牌获取失败,发送报警
  192. return false;
  193. }
  194. # 获取外部联系人详情信息API
  195. $getExternalContactUri = config('qyWechat.get_user_detail');
  196. $getExternalContactUri .= $accessToken . '&userid=' . $userid;
  197. $response = HttpService::httpGet($getExternalContactUri);
  198. $responseData = json_decode($response, true);
  199. if(isset($responseData['errcode']) && $responseData['errcode']) {
  200. if($retry <=5) {
  201. $retry++;
  202. ContactService::getContactUserDetail($corpid, $userid, $retry);
  203. }
  204. Log::logError('外部联系人详情信息获取失败', $responseData, 'getContactUserDetail');
  205. return false;
  206. }
  207. return $responseData;
  208. }
  209. }