WechatOpenPlatformController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Modules\Channel\Http\Controllers;
  3. use Catch\Base\CatchController;
  4. use EasyWeChat\OpenPlatform\Application;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Modules\Channel\Models\WechatAuthorizationInfo;
  8. use Modules\Channel\Services\WechatOpenPlatform\WechatOpenPlatformService;
  9. use Modules\Common\Errors\Errors;
  10. use Modules\Common\Exceptions\CommonBusinessException;
  11. use Modules\User\Http\Controllers\UserTrait;
  12. class WechatOpenPlatformController extends CatchController
  13. {
  14. use UserTrait;
  15. // 授权跳转页
  16. public function preauth(Request $request) {
  17. $currentUser = $this->getCurrentUser();
  18. $componentInfo = WechatOpenPlatformService::getComponentInfoByCompanyUid($currentUser->pid);
  19. if(!$componentInfo) {
  20. CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
  21. }
  22. $config = [
  23. 'app_id' => $componentInfo->app_id, // 开放平台账号的 appid
  24. 'secret' => $componentInfo->secret, // 开放平台账号的 secret
  25. 'token' => $componentInfo->token, // 开放平台账号的 token
  26. 'aes_key' => $componentInfo->aes_key, // 明文模式请勿填写 EncodingAESKey
  27. 'http' => [
  28. 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启
  29. 'timeout' => 5.0,
  30. 'retry' => true, // 使用默认重试配置
  31. ],
  32. ];
  33. $app = new Application($config);
  34. $url = $app->createPreAuthorizationUrl(url('/api/channel/openPlatform/auth/'.$componentInfo->app_id), []);
  35. return $url;
  36. }
  37. public function auth(Request $request, $component_appid) {
  38. $auth_code = $request->input('auth_code');
  39. $componentInfo = WechatOpenPlatformService::getComponentInfoByCompanyUid($component_appid);
  40. if(!$componentInfo) {
  41. CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
  42. }
  43. $config = [
  44. 'app_id' => $componentInfo->app_id, // 开放平台账号的 appid
  45. 'secret' => $componentInfo->secret, // 开放平台账号的 secret
  46. 'token' => $componentInfo->token, // 开放平台账号的 token
  47. 'aes_key' => $componentInfo->aes_key, // 明文模式请勿填写 EncodingAESKey
  48. 'http' => [
  49. 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启
  50. 'timeout' => 5.0,
  51. 'retry' => true, // 使用默认重试配置
  52. ],
  53. ];
  54. $app = new Application($config);
  55. $client = $app->getClient();
  56. $response = $client->post('/cgi-bin/component/api_query_auth', [
  57. 'component_appid' => $component_appid,
  58. 'authorization_code' => $auth_code
  59. ]);
  60. $authorizer_appid = $response['authorization_info']['authorizer_appid'];
  61. $authorizer_refresh_token = $response['authorization_info']['authorizer_refresh_token'];
  62. $currentUser = $this->getCurrentUser();
  63. WechatAuthorizationInfo::updateOrCreate([
  64. 'authorizer_appid' => $authorizer_appid,
  65. 'component_appid' => $component_appid,
  66. 'user_id' => $currentUser->id,
  67. 'puser_id' => $currentUser->pid,
  68. ], [
  69. 'authorizer_refresh_token' => $authorizer_refresh_token
  70. ]);
  71. return view('wechat.openPlatform.authSuccess')->with('url', url('/'));
  72. }
  73. }