123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace Modules\Channel\Http\Controllers;
- use Catch\Base\CatchController;
- use EasyWeChat\OpenPlatform\Application;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Modules\Channel\Models\WechatAuthorizationInfo;
- use Modules\Channel\Services\WechatOpenPlatform\WechatOpenPlatformService;
- use Modules\Common\Errors\Errors;
- use Modules\Common\Exceptions\CommonBusinessException;
- use Modules\User\Http\Controllers\UserTrait;
- class WechatOpenPlatformController extends CatchController
- {
- use UserTrait;
-
- public function preauth(Request $request) {
- $currentUser = $this->getCurrentUser();
- $componentInfo = WechatOpenPlatformService::getComponentInfoByCompanyUid($currentUser->pid);
- if(!$componentInfo) {
- CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
- }
- $config = [
- 'app_id' => $componentInfo->app_id,
- 'secret' => $componentInfo->secret,
- 'token' => $componentInfo->token,
- 'aes_key' => $componentInfo->aes_key,
- 'http' => [
- 'throw' => true,
- 'timeout' => 5.0,
- 'retry' => true,
- ],
- ];
- $app = new Application($config);
- $url = $app->createPreAuthorizationUrl(url('/api/channel/openPlatform/auth/'.$componentInfo->app_id), []);
- return $url;
- }
- public function auth(Request $request, $component_appid) {
- $auth_code = $request->input('auth_code');
- $componentInfo = WechatOpenPlatformService::getComponentInfoByCompanyUid($component_appid);
- if(!$componentInfo) {
- CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
- }
- $config = [
- 'app_id' => $componentInfo->app_id,
- 'secret' => $componentInfo->secret,
- 'token' => $componentInfo->token,
- 'aes_key' => $componentInfo->aes_key,
- 'http' => [
- 'throw' => true,
- 'timeout' => 5.0,
- 'retry' => true,
- ],
- ];
- $app = new Application($config);
- $client = $app->getClient();
- $response = $client->post('/cgi-bin/component/api_query_auth', [
- 'component_appid' => $component_appid,
- 'authorization_code' => $auth_code
- ]);
- $authorizer_appid = $response['authorization_info']['authorizer_appid'];
- $authorizer_refresh_token = $response['authorization_info']['authorizer_refresh_token'];
- $currentUser = $this->getCurrentUser();
- WechatAuthorizationInfo::updateOrCreate([
- 'authorizer_appid' => $authorizer_appid,
- 'component_appid' => $component_appid,
- 'user_id' => $currentUser->id,
- 'puser_id' => $currentUser->pid,
- ], [
- 'authorizer_refresh_token' => $authorizer_refresh_token
- ]);
- return view('wechat.openPlatform.authSuccess')->with('url', url('/'));
- }
- }
|