企微短剧业务系统

CorpAuthService.php 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\AuthorizeCorp;
  5. class CorpAuthService
  6. {
  7. /**
  8. * 获取授权企业信息
  9. * */
  10. public static function getCorpAuthInfo($suiteId, $suiteSecret, $preAuthCode, $userId)
  11. {
  12. # 获取企业信息
  13. $corpData = CorpAuthService::getPermanentCode($suiteId, $suiteSecret, $preAuthCode);
  14. # 验证是否为正常信息
  15. if(isset($corpData['errcode']) && $corpData['errcode']) {
  16. Log::logError('永久授权码接口返回数据异常', $corpData, 'CorpAuthInfo');
  17. return false;
  18. }
  19. # 处理数据
  20. $corpid = $corpData['auth_corp_info']['corpid'];
  21. $insertData['corp_name'] = isset($corpData['auth_corp_info']['corp_name']) ? $corpData['auth_corp_info']['corp_name'] : null;
  22. $insertData['corp_type'] = isset($corpData['auth_corp_info']['corp_type']) ? $corpData['auth_corp_info']['corp_type'] : null;
  23. $insertData['corp_square_logo_url'] = isset($corpData['auth_corp_info']['corp_square_logo_url']) ?
  24. $corpData['auth_corp_info']['corp_square_logo_url'] : null;
  25. $insertData['corp_square_logo_url'] = isset($corpData['auth_corp_info']['corp_square_logo_url']) ? $corpData['auth_corp_info']['corp_square_logo_url'] : null;
  26. $insertData['corp_user_max'] = isset($corpData['auth_corp_info']['corp_user_max']) ? $corpData['auth_corp_info']['corp_user_max'] : null;
  27. $insertData['corp_full_name'] = isset($corpData['auth_corp_info']['corp_full_name']) ? $corpData['auth_corp_info']['corp_full_name'] : null;
  28. $insertData['subject_type'] = isset($corpData['auth_corp_info']['subject_type']) ? $corpData['auth_corp_info']['subject_type'] : null;
  29. $insertData['verified_end_time'] = isset($corpData['auth_corp_info']['verified_end_time']) ? $corpData['auth_corp_info']['verified_end_time'] : null;
  30. $insertData['corp_wx_qrcode'] = isset($corpData['auth_corp_info']['corp_wxqrcode']) ? $corpData['auth_corp_info']['corp_wxqrcode'] : null;
  31. $insertData['corp_scale'] = isset($corpData['auth_corp_info']['corp_scale']) ? $corpData['auth_corp_info']['corp_scale'] : null;
  32. $insertData['corp_industry'] = isset($corpData['auth_corp_info']['corp_industry']) ? $corpData['auth_corp_info']['corp_industry'] : null;
  33. $insertData['corp_sub_industry'] = isset($corpData['auth_corp_info']['corp_sub_industry']) ? $corpData['auth_corp_info']['corp_sub_industry'] : null;
  34. $insertData['access_token'] = isset($corpData['access_token']) ? $corpData['access_token'] : '';
  35. $insertData['expires_in'] = isset($corpData['expires_in']) ? time() + $corpData['expires_in'] : null;
  36. $insertData['permanent_code'] = $corpData['permanent_code'];
  37. $insertData['auth_info'] = isset($corpData['auth_info']) ? json_encode($corpData['auth_info']) : null;
  38. $insertData['auth_user_info'] = isset($corpData['auth_user_info']) ? json_encode($corpData['auth_user_info']) : null;
  39. $insertData['dealer_corp_info'] = isset($corpData['dealer_corp_info']) ? json_encode($corpData['dealer_corp_info']) : null;
  40. $insertData['register_code_info'] = isset($corpData['register_code_info']) ? json_encode($corpData['register_code_info']) : null;
  41. $corpData['auth_info'] = json_decode($insertData['auth_info'], true);
  42. $insertData['agent_id'] = isset($corpData['auth_info']['agent'][0]['agentid']) ? $corpData['auth_info']['agent'][0]['agentid'] : null;
  43. $insertData['is_customized_app'] = isset($corpData['auth_info']['agent'][0]['is_customized_app']) ? $corpData['auth_info']['agent'][0]['is_customized_app'] : 0;
  44. if($userId) {
  45. $insertData['admin_id'] = $userId;
  46. }
  47. $insertData['enable'] = 1;
  48. # 存储企业信息
  49. return AuthorizeCorp::updateOrCreate(['corpid' => $corpid], $insertData);
  50. }
  51. /**
  52. * 获取企业永久授权码
  53. * */
  54. public static function getPermanentCode($suiteId, $suiteTicket, $preAuthCode)
  55. {
  56. # 获取SuiteAccessToken
  57. // $suiteAccessToken = TokenService::getSuiteAccessToken($suiteId, $suiteTicket);
  58. $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteTicket);
  59. # 获取永久授权码链接
  60. $permanentCodeUri = config('qyWechat.get_permanent_code');
  61. $permanentCodeUri .= $suiteAccessToken;
  62. $postData = array('auth_code' => $preAuthCode);
  63. $response = HttpService::httpPost($permanentCodeUri, json_encode($postData));
  64. $responseData = json_decode($response, true);
  65. return $responseData;
  66. }
  67. /**
  68. * corpid转换
  69. * */
  70. public static function getOpenCorpid($corpid, $suiteId, $suiteSecret)
  71. {
  72. # 获取SuiteAccessToken
  73. $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret);
  74. // $suiteAccessToken = TokenService::getSuiteAccessToken($suiteId, $suiteSecret);
  75. $requestUri = config('qyWechat.get_open_corpid');
  76. $requestUri .= $suiteAccessToken;
  77. $response = HttpService::httpPost($requestUri, json_encode(['corpid' => $corpid]));
  78. $responseData = json_decode($response, true);
  79. return $responseData;
  80. }
  81. /**
  82. * jsapi授权信息获取
  83. * */
  84. public static function getAuthData($corpid, $userId, $token, $timestamp, $url, &$errno)
  85. {
  86. # 验证合法性
  87. $authToken = get_token($corpid, $userId, $timestamp);
  88. if($authToken != $token) {
  89. Log::logError('验签失败', [
  90. 'corpid' => $corpid,
  91. 'user_id' => $userId,
  92. 'token' => $token,
  93. 'timestamp' => $timestamp,
  94. 'auth_token' => $authToken
  95. ], 'JsapiAuthData');
  96. $errno = 1004;
  97. return [];
  98. }
  99. # 获取agentId
  100. $agentId = AuthorizeCorp::where('corpid', $corpid)->where('enable', 1)->value('agent_id');
  101. if(empty($agentId)) {
  102. Log::logError('agentId获取失败', [
  103. 'corpid' => $corpid,
  104. 'user_id' => $userId,
  105. 'token' => $token,
  106. 'timestamp' => $timestamp,
  107. 'auth_token' => $authToken
  108. ], 'JsapiAuthData');
  109. $errno = 1004;
  110. return [];
  111. }
  112. # 获取企业jsapiTicket
  113. $jsapiTicket = AccessTokenService::getJsapiTicket($corpid);
  114. # 获取应用jsapiTicket
  115. $appJsapiTicket = AccessTokenService::getAppJsapiTicket($corpid);
  116. # 获取随机字符串和时间戳
  117. $nonceStr = get_random_str(16);
  118. $timestamp = time();
  119. $jsapiTicketStr = 'jsapi_ticket=' . $jsapiTicket . '&noncestr=' .$nonceStr. '&timestamp='.$timestamp.'&url=' .$url;
  120. $appJsapiTicketStr = 'jsapi_ticket=' . $appJsapiTicket . '&noncestr=' .$nonceStr. '&timestamp='.$timestamp.'&url=' .$url;
  121. $signature = sha1($jsapiTicketStr);
  122. $signatureApp = sha1($appJsapiTicketStr);
  123. return [
  124. 'corpid' => $corpid,
  125. 'agent_id' => $agentId,
  126. 'timestamp' => $timestamp,
  127. 'nonce_str' => $nonceStr,
  128. 'signature' => $signature,
  129. 'app_signature' => $signatureApp
  130. ];
  131. }
  132. /**
  133. * jsapi信息获取-公用
  134. */
  135. public static function getCommonAuthData($corpid, $url, &$errno)
  136. {
  137. # 获取agentId
  138. $agentId = AuthorizeCorp::where('corpid', $corpid)->where('enable', 1)->value('agent_id');
  139. if(empty($agentId)) {
  140. Log::logError('agentId获取失败', [
  141. 'corpid' => $corpid,
  142. 'url' => $url,
  143. ], 'getCommonAuthData');
  144. $errno = 1004;
  145. return [];
  146. }
  147. # 获取企业jsapiTicket
  148. $jsapiTicket = AccessTokenService::getJsapiTicket($corpid);
  149. # 获取应用jsapiTicket
  150. $appJsapiTicket = AccessTokenService::getAppJsapiTicket($corpid);
  151. # 获取随机字符串和时间戳
  152. $nonceStr = get_random_str(16);
  153. $timestamp = time();
  154. $jsapiTicketStr = 'jsapi_ticket=' . $jsapiTicket . '&noncestr=' .$nonceStr. '&timestamp='.$timestamp.'&url=' .$url;
  155. $appJsapiTicketStr = 'jsapi_ticket=' . $appJsapiTicket . '&noncestr=' .$nonceStr. '&timestamp='.$timestamp.'&url=' .$url;
  156. $signature = sha1($jsapiTicketStr);
  157. $signatureApp = sha1($appJsapiTicketStr);
  158. return [
  159. 'corpid' => $corpid,
  160. 'agent_id' => $agentId,
  161. 'timestamp' => $timestamp,
  162. 'nonce_str' => $nonceStr,
  163. 'signature' => $signature,
  164. 'app_signature' => $signatureApp
  165. ];
  166. }
  167. }