channel_info = $channel_info; return $this; } /** * 获取平台微信 * @param null $platform_id * @return \EasyWeChat\OpenPlatform\Application | null */ function getPlatform($platform_id = null){ try{ $weChatConfig = \think\Config::get('wechat'); if(!empty($platform_id)){ $platform = model('Platform')->getInfo($platform_id); $weChatConfig['app_id'] = $platform['appid']; $weChatConfig['secret'] = $platform['secret']; $weChatConfig['token'] = $platform['token']; $weChatConfig['aes_key'] = $platform['aes_key']; }else{ $weChatConfig['app_id'] = $this->channel_info['platform_appid']; $weChatConfig['secret'] = $this->channel_info['platform_secret']; $weChatConfig['token'] = $this->channel_info['platform_token']; $weChatConfig['aes_key'] = $this->channel_info['platform_aes_key']; $platform_id = $this->channel_info['platform_id']; } if ($weChatConfig['http']['base_uri'] == 'https://api.mch.weixin.qq.com/') { $weChatConfig['http']['base_uri'] = 'https://api.weixin.qq.com/'; } $weChatConfig['http']['timeout'] = 20; if ($proxy = OpenPlatformService::instance()->getProxy($platform_id)) { $httpConfig = array_merge($weChatConfig['http'], ['proxy' => $proxy]); $weChatConfig['http'] = $httpConfig; Config::set('wechat.http', $httpConfig); /* $weChatConfig['http']['proxy'] = $proxy; $weChatConfig['http_client'] = new Client($httpConfig);*/ } $openPlatform = Factory::openPlatform($weChatConfig); $openPlatform['cache'] = new RedisCache(Redis::instanceCache()); return $openPlatform; }catch (\Exception $e){ Log::error('GetWeChatPlatform:'.$e->getMessage()); } return null; } /** * 获取渠道指定平台的微信 * @param null $platform_id * @param null $app_id * @param null $refresh_token * @return \EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\Application | null */ function getOfficialAccountByPlatform($platform_id = null,$app_id = null, $refresh_token = null){ try{ if(empty($app_id) && empty($refresh_token)){ $app_id = $this->channel_info['appid']; $refresh_token = $this->channel_info['refresh_token']; } if(!$app_id || !$refresh_token){ throw new \Exception('appid or refresh_token is null'); } $officialAccount = $this->getPlatform($platform_id)->officialAccount($app_id, $refresh_token); $officialAccount['cache'] = new RedisCache(Redis::instanceCache()); if ($platform_id) { if ($proxy = OpenPlatformService::instance()->getProxyconfigById($platform_id)) { $httpConfig = array_merge(Config::get('wechat.http'), ['proxy' => $proxy]); $officialAccount['http_client'] = new Client($httpConfig); } } else { if ($proxy = OpenPlatformService::instance()->getProxyconfigByChannel(null, $app_id)) { $httpConfig = array_merge(Config::get('wechat.http'), ['proxy' => $proxy]); $officialAccount['http_client'] = new Client($httpConfig); } } return $officialAccount; }catch (\Exception $e){ Log::info('GetOfficialAccountByPlatform:'.$e->getMessage()); } return null; } /** * 获取渠道默认微信 * @param null $app_id * @param null $refresh_token * @return \EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\Application | null */ function getOfficialAccount($app_id = null, $refresh_token = null){ try{ if(empty($app_id) && empty($refresh_token)){ $app_id = $this->channel_info['appid']; $refresh_token = $this->channel_info['refresh_token']; } if(!$app_id || !$refresh_token){ throw new \Exception('appid or refresh_token is null'); } $officialAccount = $this->getPlatform()->officialAccount($app_id, $refresh_token); $officialAccount['cache'] = new RedisCache(Redis::instanceCache()); if ($proxy = OpenPlatformService::instance()->getProxyconfigByChannel(null, $app_id)) { $httpConfig = array_merge(Config::get('wechat.http'), ['proxy' => $proxy]); $officialAccount['http_client'] = new Client($httpConfig); } return $officialAccount; }catch (\Exception $e){ Log::info('GetOfficialAccount:'.$e->getMessage()); } return null; } /** * * @param $adminConfig * @param $config * @return \EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\Application|null */ static public function getOfficialAccountByWeChatConfig($adminConfig,$config){ try{ $refresh_token = $adminConfig['refresh_token'] ?? null; if(!$config || !$adminConfig){ return null; } //获取三方平台 if (isset($config['platform_id'])) { if ($proxy = OpenPlatformService::instance()->getProxy($config['platform_id'])) { $httpConfig = array_merge($config['http'], ['proxy' => $proxy]); $weChatConfig['http'] = $httpConfig; } } else { if ($proxy = OpenPlatformService::instance()->getProxy($adminConfig['platform_id'])) { $httpConfig = array_merge($config['http'], ['proxy' => $proxy]); $weChatConfig['http'] = $httpConfig; } } $openPlatform = Factory::openPlatform($config); $openPlatform['cache'] = new RedisCache(Redis::instanceCache()); //检查是否是菜单域名平台 if($config['platform_id'] == $adminConfig['menu_platform_id']){ if(!isset($adminConfig['menu_refresh_token'])){ $adminConfig['menu_refresh_token'] = model('Ptoken')->where(['platform_id'=>$config['platform_id'],'admin_id'=>$adminConfig['admin_id']])->value('refresh_token'); } $refresh_token = $adminConfig['menu_refresh_token']; $proxy = OpenPlatformService::instance()->getProxy($adminConfig['menu_platform_id']); } //获取officialAccount if(!$adminConfig['appid'] || !$refresh_token){ Log::write('time: '.date('Y-m-d H:i:s',time()).' getOfficialAccountByWeChatConfig ChannelID:'.$adminConfig['admin_id'].' Error: appid or refresh_token is null','WeChat'); return null; } $officialAccount = $openPlatform->officialAccount($adminConfig['appid'], $refresh_token); $officialAccount['cache'] = new RedisCache(Redis::instanceCache()); if ($proxy) { $httpConfig = array_merge($config['http'], ['proxy' => $proxy]); $officialAccount['http_client'] = new Client($httpConfig); } return $officialAccount; }catch (\Exception $e){ Log::error('getOfficialAccountByWeChatConfig ChannelID:'.$adminConfig['admin_id'].' Error: '.$e->getMessage()); return null; } } /** * 获取微信支付 */ function getPaymentApplication(){ } /** * 根据域名获取微信支付配置 */ public function getPaymentConfigByHost(){ } }