123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Log;
- use App\Service\CorpAuthService;
- use App\Service\HttpService;
- use App\Service\JiuZhouService;
- use App\Service\TokenService;
- use App\Support\qyApi\QyCommon;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- class AuthController extends Controller
- {
- /*
- * 获取预授权码
- * */
- public function getPreAuthCode(Request $request)
- {
- $userId = $request->input('admin_id');
- # 获取redirect_uri
- $redirectUri = env('DOMAIN', 'http://dj.wenxingshuju.com/') . urlencode(config('qyWechat.redirect_uri'));
- $redirectUri .= '?u='.$userId;
- # 获取预授权码
- $preAuthCode = TokenService::getPreAuthCode($this->suiteId, $this->suiteSecret);
- if(empty($preAuthCode)) {
- Log::logError('预授权码获取失败', [
- 'corpId' => $this->suiteId,
- 'providerSecret' => $this->suiteSecret
- ], 'GetPreAuthCode');
- return self::returnValue([], 1002);
- }
- # 为预授权码设置权限
- $setSessionInfoUri = config('qyWechat.set_session_info');
- $setSessionInfoUri .= TokenService::getAuthToken($this->suiteId, $this->suiteSecret);
- $postData = [
- 'pre_auth_code' => $preAuthCode,
- 'session_info' => array('auth_type' => 1)
- ];
- $result = HttpService::httpPost($setSessionInfoUri, json_encode($postData), TRUE);
- // Log::logInfo($result, [], '0311');
- // Todo::测试过程判断是否授权成功
- $preAuthCodeUrl = "https://open.work.weixin.qq.com/3rdapp/install?suite_id=" . $this->suiteId . "&pre_auth_code=" . $preAuthCode . "&redirect_uri=" . $redirectUri;
- $data = ['url' => $preAuthCodeUrl];
- return self::returnValue(['data' => $data]);
- }
- public function getAuthorize(Request $request)
- {
- # 根据预授权码获取授权账户信息
- $userId = $request->input('u');
- $preAuthCode = $request->input('auth_code');
- # 获取授权账号相关信息
- $result = CorpAuthService::getCorpAuthInfo($this->suiteId, $this->suiteSecret, $preAuthCode, $userId);
- if(!$result) {
- return self::returnValue([], 1003);
- }
- $redirectUrl = env('DOMAIN', 'http://dj.wenxingshuju.com/') . 'playlet/index.html#/authorityManage';
- return redirect($redirectUrl);
- }
- /**
- * jsapi授权数据
- * */
- public function authData(Request $request)
- {
- $validator = \Validator::make($request->all(),[
- 'corpid' => 'required|string',
- 'user_id' => 'required|string',
- 'token' => 'required|string',
- 'timestamp' => 'required',
- 'url' => 'required'
- ]);
- if ($validator->fails()) {
- return self::returnValue($validator->getMessageBag(), 1102);
- }
- $corpid = $request->input('corpid');
- $userId = $request->input('user_id');
- $token = $request->input('token');
- $url = $request->input('url');
- $timestamp = $request->input('timestamp');
- # 获取jsapi需要的参数
- $errno = 0;
- $data = CorpAuthService::getAuthData($corpid, $userId, $token, $timestamp, $url, $errno);
- if($errno) return self::returnValue([], $errno);
- return self::returnValue($data);
- }
- /**
- * 企微网页授权回调
- * */
- public function oauth2Authorize(Request $request)
- {
- $validator = \Validator::make($request->all(),[
- 'corpid' => 'required|string',
- 'code' => 'required|string'
- ]);
- if ($validator->fails()) {
- return self::returnValue($validator->getMessageBag(), 1102);
- }
- $corpid = $request->input('corpid');
- $code = $request->input('code');
- $state = $request->input('state');
- # 根据code获取用户身份
- $responseData = QyCommon::getCustomerInfoByCode($corpid, $code);
- # 解析用户企微数据【1.企业成员 2.非企业成员】
- $externalUserid = $responseData['external_userid'] ?? '';
- $deviceId = $responseData['DeviceId'] ?? '';
- $openid = $responseData['OpenId'] ?? '';
- $userId = $responseData['UserId'] ?? '';
- $errCode = $responseData['errcode'] ?? 9999;
- # 查询企微用户新关注的客服
- $msgId = JiuZhouService::getMsgId($corpid, $externalUserid, null);
- # 获取代理商类型
- $msgType = JiuZhouService::getMsgTypeByCorpid($corpid);
- Log::logInfo('获取用户企微身份信息: ', [
- 'corpid' => $corpid,
- 'code' => $code,
- 'state' => $state,
- 'msgId' => $msgId,
- 'msgType' => $msgType,
- 'response' => $responseData
- ], 'Oauth2Authorize');
- if($errCode) {
- return self::returnValue([], 2801);
- }
- return self::returnValue([
- 'corpid' => $corpid, 'external_userid'=>$externalUserid, 'state' => $state,
- 'openid' => $openid, 'device_id' => $deviceId, 'user_id' => $userId,
- 'msgId' => $msgId, 'msgType' => $msgType
- ]);
- }
- /**
- * 企微网页授权回调过程中异常上报
- * */
- public function warnReport(Request $request)
- {
- $corpid = $request->input('corpid');
- $step = $request->input('step');
- $link = $request->input('link');
- $device= $request->header();
- Log::logError('企微网页授权回调过程中发生异常', [
- 'corpid' => $corpid,
- 'link' => $link,
- 'step' => $step,
- 'device' => $device
- ], 'AuthExceptionReport');
- }
- /**
- * 通用 jsapi授权数据
- * */
- public function commonAuthData(Request $request)
- {
- $validator = \Validator::make($request->all(),[
- 'corpid' => 'required|string',
- 'url' => 'required'
- ]);
- if ($validator->fails()) {
- return self::returnValue($validator->getMessageBag(), 1102);
- }
- $corpid = $request->input('corpid');
- $url = $request->input('url');
- # 获取jsapi需要的参数
- $errno = 0;
- $data = CorpAuthService::getCommonAuthData($corpid, $url, $errno);
- if($errno) return self::returnValue([], $errno);
- return self::returnValue($data);
- }
- }
|