企微短剧业务系统

AuthController.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Log;
  4. use App\Service\CorpAuthService;
  5. use App\Service\HttpService;
  6. use App\Service\JiuZhouService;
  7. use App\Service\TokenService;
  8. use App\Support\qyApi\QyCommon;
  9. use Illuminate\Http\Request;
  10. use App\Http\Controllers\Controller;
  11. class AuthController extends Controller
  12. {
  13. /*
  14. * 获取预授权码
  15. * */
  16. public function getPreAuthCode(Request $request)
  17. {
  18. $userId = $request->input('admin_id');
  19. # 获取redirect_uri
  20. $redirectUri = env('DOMAIN', 'http://dj.wenxingshuju.com/') . urlencode(config('qyWechat.redirect_uri'));
  21. $redirectUri .= '?u='.$userId;
  22. # 获取预授权码
  23. $preAuthCode = TokenService::getPreAuthCode($this->suiteId, $this->suiteSecret);
  24. if(empty($preAuthCode)) {
  25. Log::logError('预授权码获取失败', [
  26. 'corpId' => $this->suiteId,
  27. 'providerSecret' => $this->suiteSecret
  28. ], 'GetPreAuthCode');
  29. return self::returnValue([], 1002);
  30. }
  31. # 为预授权码设置权限
  32. $setSessionInfoUri = config('qyWechat.set_session_info');
  33. $setSessionInfoUri .= TokenService::getAuthToken($this->suiteId, $this->suiteSecret);
  34. $postData = [
  35. 'pre_auth_code' => $preAuthCode,
  36. 'session_info' => array('auth_type' => 1)
  37. ];
  38. $result = HttpService::httpPost($setSessionInfoUri, json_encode($postData), TRUE);
  39. // Log::logInfo($result, [], '0311');
  40. // Todo::测试过程判断是否授权成功
  41. $preAuthCodeUrl = "https://open.work.weixin.qq.com/3rdapp/install?suite_id=" . $this->suiteId . "&pre_auth_code=" . $preAuthCode . "&redirect_uri=" . $redirectUri;
  42. $data = ['url' => $preAuthCodeUrl];
  43. return self::returnValue(['data' => $data]);
  44. }
  45. public function getAuthorize(Request $request)
  46. {
  47. # 根据预授权码获取授权账户信息
  48. $userId = $request->input('u');
  49. $preAuthCode = $request->input('auth_code');
  50. # 获取授权账号相关信息
  51. $result = CorpAuthService::getCorpAuthInfo($this->suiteId, $this->suiteSecret, $preAuthCode, $userId);
  52. if(!$result) {
  53. return self::returnValue([], 1003);
  54. }
  55. $redirectUrl = env('DOMAIN', 'http://dj.wenxingshuju.com/') . 'playlet/index.html#/authorityManage';
  56. return redirect($redirectUrl);
  57. }
  58. /**
  59. * jsapi授权数据
  60. * */
  61. public function authData(Request $request)
  62. {
  63. $validator = \Validator::make($request->all(),[
  64. 'corpid' => 'required|string',
  65. 'user_id' => 'required|string',
  66. 'token' => 'required|string',
  67. 'timestamp' => 'required',
  68. 'url' => 'required'
  69. ]);
  70. if ($validator->fails()) {
  71. return self::returnValue($validator->getMessageBag(), 1102);
  72. }
  73. $corpid = $request->input('corpid');
  74. $userId = $request->input('user_id');
  75. $token = $request->input('token');
  76. $url = $request->input('url');
  77. $timestamp = $request->input('timestamp');
  78. # 获取jsapi需要的参数
  79. $errno = 0;
  80. $data = CorpAuthService::getAuthData($corpid, $userId, $token, $timestamp, $url, $errno);
  81. if($errno) return self::returnValue([], $errno);
  82. return self::returnValue($data);
  83. }
  84. /**
  85. * 企微网页授权回调
  86. * */
  87. public function oauth2Authorize(Request $request)
  88. {
  89. $validator = \Validator::make($request->all(),[
  90. 'corpid' => 'required|string',
  91. 'code' => 'required|string'
  92. ]);
  93. if ($validator->fails()) {
  94. return self::returnValue($validator->getMessageBag(), 1102);
  95. }
  96. $corpid = $request->input('corpid');
  97. $code = $request->input('code');
  98. $state = $request->input('state');
  99. # 根据code获取用户身份
  100. $responseData = QyCommon::getCustomerInfoByCode($corpid, $code);
  101. # 解析用户企微数据【1.企业成员 2.非企业成员】
  102. $externalUserid = $responseData['external_userid'] ?? '';
  103. $deviceId = $responseData['DeviceId'] ?? '';
  104. $openid = $responseData['OpenId'] ?? '';
  105. $userId = $responseData['UserId'] ?? '';
  106. $errCode = $responseData['errcode'] ?? 9999;
  107. # 查询企微用户新关注的客服
  108. $msgId = JiuZhouService::getMsgId($corpid, $externalUserid, null);
  109. # 获取代理商类型
  110. $msgType = JiuZhouService::getMsgTypeByCorpid($corpid);
  111. Log::logInfo('获取用户企微身份信息: ', [
  112. 'corpid' => $corpid,
  113. 'code' => $code,
  114. 'state' => $state,
  115. 'msgId' => $msgId,
  116. 'msgType' => $msgType,
  117. 'response' => $responseData
  118. ], 'Oauth2Authorize');
  119. if($errCode) {
  120. return self::returnValue([], 2801);
  121. }
  122. return self::returnValue([
  123. 'corpid' => $corpid, 'external_userid'=>$externalUserid, 'state' => $state,
  124. 'openid' => $openid, 'device_id' => $deviceId, 'user_id' => $userId,
  125. 'msgId' => $msgId, 'msgType' => $msgType
  126. ]);
  127. }
  128. /**
  129. * 企微网页授权回调过程中异常上报
  130. * */
  131. public function warnReport(Request $request)
  132. {
  133. $corpid = $request->input('corpid');
  134. $step = $request->input('step');
  135. $link = $request->input('link');
  136. $device= $request->header();
  137. Log::logError('企微网页授权回调过程中发生异常', [
  138. 'corpid' => $corpid,
  139. 'link' => $link,
  140. 'step' => $step,
  141. 'device' => $device
  142. ], 'AuthExceptionReport');
  143. }
  144. /**
  145. * 通用 jsapi授权数据
  146. * */
  147. public function commonAuthData(Request $request)
  148. {
  149. $validator = \Validator::make($request->all(),[
  150. 'corpid' => 'required|string',
  151. 'url' => 'required'
  152. ]);
  153. if ($validator->fails()) {
  154. return self::returnValue($validator->getMessageBag(), 1102);
  155. }
  156. $corpid = $request->input('corpid');
  157. $url = $request->input('url');
  158. # 获取jsapi需要的参数
  159. $errno = 0;
  160. $data = CorpAuthService::getCommonAuthData($corpid, $url, $errno);
  161. if($errno) return self::returnValue([], $errno);
  162. return self::returnValue($data);
  163. }
  164. }