WechatOpenPlatformService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\Database\Eloquent\Model;
  7. use Illuminate\Database\Query\Builder;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Support\Facades\DB;
  10. use Modules\Channel\Models\WechatAuthorizationInfo;
  11. use Modules\Common\Errors\Errors;
  12. use Modules\Common\Exceptions\CommonBusinessException;
  13. use Modules\Common\Services\BaseService;
  14. class WechatOpenPlatformService extends BaseService
  15. {
  16. public static function getComponentInfoByCompanyUid($companyUid) {
  17. $componentInfo = DB::table('wechat_open_platform_infos')
  18. ->where([
  19. 'company_uid' => $companyUid,
  20. 'is_enabled' => 1,
  21. ])->orderBy('id', 'desc')
  22. ->first();
  23. if(!$componentInfo) {
  24. CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
  25. }
  26. return $componentInfo;
  27. }
  28. public static function getComponentInfoByAppid($componentAppid) {
  29. $componentInfo = DB::table('wechat_open_platform_infos')
  30. ->where([
  31. 'app_id' => $componentAppid,
  32. 'is_enabled' => 1,
  33. ])->orderBy('id', 'desc')
  34. ->first();
  35. if(!$componentInfo) {
  36. CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
  37. }
  38. return $componentInfo;
  39. }
  40. /**
  41. * 构造app
  42. * @param $componentInfo
  43. * @return Application
  44. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  45. */
  46. public static function buildApplication($componentInfo) {
  47. $config = [
  48. 'app_id' => $componentInfo->app_id, // 开放平台账号的 appid
  49. 'secret' => $componentInfo->secret, // 开放平台账号的 secret
  50. 'token' => $componentInfo->token, // 开放平台账号的 token
  51. 'aes_key' => $componentInfo->aes_key, // 明文模式请勿填写 EncodingAESKey
  52. ];
  53. $config = array_merge($config, config('easywechat'));
  54. $app = Factory::openPlatform($config);
  55. $app->rebind('cache', Cache::store('redis'));
  56. return $app;
  57. }
  58. /**
  59. * 取消授权
  60. * @param Message $message
  61. */
  62. public static function handleUnauthorized($message) {
  63. $component_appid = $message['AppId'];
  64. $authorizer_appid = $message['AuthorizerAppid'];
  65. WechatAuthorizationInfo::where([
  66. 'component_appid' => $component_appid,
  67. 'authorizer_appid' => $authorizer_appid,
  68. ])->update([
  69. 'is_enabled' => 0
  70. ]);
  71. }
  72. /**
  73. * 获取公众号的refreshToken
  74. * name: getRefreshToken
  75. * @param $authorizer_appid
  76. * @param $component_appid
  77. * date 2023/07/07 10:16
  78. */
  79. public static function getRefreshToken($authorizer_appid, $component_appid)
  80. {
  81. return DB::table('wechat_authorization_infos')
  82. ->where([
  83. 'component_appid' => $component_appid,
  84. 'authorizer_appid' => $authorizer_appid,
  85. 'is_enabled' => 1,
  86. ])->first();
  87. }
  88. /**
  89. * 获取公众号的refreshToken
  90. * name:getAppInfoById
  91. * @param $id 公众号授权id
  92. * @return Model|Builder|object|null
  93. */
  94. public static function getAppInfoById($id)
  95. {
  96. $info = DB::table('wechat_authorization_infos')
  97. ->where([
  98. 'id' => $id,
  99. 'is_enabled' => 1,
  100. ])->first();
  101. if (is_empty($info)){
  102. self::throwErrMsg("公众号不存在或已取消授权");
  103. }
  104. return $info;
  105. }
  106. }