123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace Modules\Channel\Services\WechatOpenPlatform;
- use EasyWeChat\OpenPlatform\Application;
- use EasyWeChat\OpenPlatform\Message;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Modules\Common\Errors\Errors;
- use Modules\Common\Exceptions\CommonBusinessException;
- class WechatOpenPlatformService
- {
- public static function getComponentInfoByCompanyUid($companyUid) {
- $componentInfo = DB::table('wechat_open_platform_infos')
- ->where([
- 'company_uid' => $companyUid,
- 'is_enabled' => 1,
- ])->orderBy('id', 'desc')
- ->first();
- if(!$componentInfo) {
- CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
- }
- return $componentInfo;
- }
- public static function getComponentInfoByAppid($componentAppid) {
- $componentInfo = DB::table('wechat_open_platform_infos')
- ->where([
- 'app_id' => $componentAppid,
- 'is_enabled' => 1,
- ])->orderBy('id', 'desc')
- ->first();
- if(!$componentInfo) {
- CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
- }
- return $componentInfo;
- }
-
- public static function buildApplication($componentInfo) {
- $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);
- $app->setCache(Cache::store('redis'));
- return $app;
- }
-
- public static function handleUnauthorized($message) {
- $component_appid = $message['AppId'];
- $authorizer_appid = $message['AuthorizerAppid'];
- DB::table('wechat_authorization_infos')
- ->where([
- 'component_appid' => $component_appid,
- 'authorizer_appid' => $authorizer_appid,
- ])->delete();
- }
-
- public static function getRefreshToken($authorizer_appid, $component_appid)
- {
- return DB::table('wechat_authorization_infos')
- ->where([
- 'component_appid' => $component_appid,
- 'authorizer_appid' => $authorizer_appid,
- 'is_enabled' => 1,
- ])->value('authorizer_refresh_token');
- }
- }
|