WechatOpenPlatformService.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Modules\Channel\Services\WechatOpenPlatform;
  3. use EasyWeChat\Factory;
  4. use EasyWeChat\OpenPlatform\Application;
  5. use EasyWeChat\OpenPlatform\Message;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\DB;
  8. use Modules\Channel\Models\WechatAuthorizationInfo;
  9. use Modules\Common\Errors\Errors;
  10. use Modules\Common\Exceptions\CommonBusinessException;
  11. class WechatOpenPlatformService
  12. {
  13. public static function getComponentInfoByCompanyUid($companyUid) {
  14. $componentInfo = DB::table('wechat_open_platform_infos')
  15. ->where([
  16. 'company_uid' => $companyUid,
  17. 'is_enabled' => 1,
  18. ])->orderBy('id', 'desc')
  19. ->first();
  20. if(!$componentInfo) {
  21. CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
  22. }
  23. return $componentInfo;
  24. }
  25. public static function getComponentInfoByAppid($componentAppid) {
  26. $componentInfo = DB::table('wechat_open_platform_infos')
  27. ->where([
  28. 'app_id' => $componentAppid,
  29. 'is_enabled' => 1,
  30. ])->orderBy('id', 'desc')
  31. ->first();
  32. if(!$componentInfo) {
  33. CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
  34. }
  35. return $componentInfo;
  36. }
  37. /**
  38. * 构造app
  39. * @param $componentInfo
  40. * @return Application
  41. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  42. */
  43. public static function buildApplication($componentInfo) {
  44. $config = [
  45. 'app_id' => $componentInfo->app_id, // 开放平台账号的 appid
  46. 'secret' => $componentInfo->secret, // 开放平台账号的 secret
  47. 'token' => $componentInfo->token, // 开放平台账号的 token
  48. 'aes_key' => $componentInfo->aes_key, // 明文模式请勿填写 EncodingAESKey
  49. 'response_type' => 'array',
  50. 'http' => [
  51. 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启
  52. 'timeout' => 5.0,
  53. 'retry' => true, // 使用默认重试配置
  54. ],
  55. ];
  56. $app = Factory::openPlatform($config);
  57. $app->rebind('cache', Cache::store('redis'));
  58. return $app;
  59. }
  60. /**
  61. * 取消授权
  62. * @param Message $message
  63. */
  64. public static function handleUnauthorized($message) {
  65. $component_appid = $message['AppId'];
  66. $authorizer_appid = $message['AuthorizerAppid'];
  67. WechatAuthorizationInfo::where([
  68. 'component_appid' => $component_appid,
  69. 'authorizer_appid' => $authorizer_appid,
  70. ])->update([
  71. 'is_enabled' => 0
  72. ]);
  73. }
  74. /**
  75. * 获取公众号的refreshToken
  76. * name: getRefreshToken
  77. * @param $authorizer_appid
  78. * @param $component_appid
  79. * date 2023/07/07 10:16
  80. */
  81. public static function getRefreshToken($authorizer_appid, $component_appid)
  82. {
  83. return DB::table('wechat_authorization_infos')
  84. ->where([
  85. 'component_appid' => $component_appid,
  86. 'authorizer_appid' => $authorizer_appid,
  87. 'is_enabled' => 1,
  88. ])->value('authorizer_refresh_token');
  89. }
  90. }