WechatOpenPlatformService.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. ];
  50. $config = array_merge($config, config('easywechat'));
  51. $app = Factory::openPlatform($config);
  52. $app->rebind('cache', Cache::store('redis'));
  53. return $app;
  54. }
  55. /**
  56. * 取消授权
  57. * @param Message $message
  58. */
  59. public static function handleUnauthorized($message) {
  60. $component_appid = $message['AppId'];
  61. $authorizer_appid = $message['AuthorizerAppid'];
  62. WechatAuthorizationInfo::where([
  63. 'component_appid' => $component_appid,
  64. 'authorizer_appid' => $authorizer_appid,
  65. ])->update([
  66. 'is_enabled' => 0
  67. ]);
  68. }
  69. /**
  70. * 获取公众号的refreshToken
  71. * name: getRefreshToken
  72. * @param $authorizer_appid
  73. * @param $component_appid
  74. * date 2023/07/07 10:16
  75. */
  76. public static function getRefreshToken($authorizer_appid, $component_appid)
  77. {
  78. return DB::table('wechat_authorization_infos')
  79. ->where([
  80. 'component_appid' => $component_appid,
  81. 'authorizer_appid' => $authorizer_appid,
  82. 'is_enabled' => 1,
  83. ])->value('authorizer_refresh_token');
  84. }
  85. }