openPlatform) { $pt_id = Config::get('wechat.platform_id'); if ($proxy = $this->getProxy($pt_id)) { $httpConfig = array_merge(Config::get('wechat.http'), ['proxy' => $proxy]); Config::set('wechat.http', $httpConfig); } $this->openPlatform = Factory::openPlatform(Config::get('wechat')); $this->openPlatform['cache'] = new RedisCache(Redis::instanceCache()); } return $this->openPlatform; } catch (\Exception $e) { LogService::exception($e); return false; } } /** * @param string $appId * @param string|null $refreshToken * @param AccessToken|null $accessToken * @return \EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\Application * @throws \Exception */ public function getOfficialAccount(string $appId = "", string $refreshToken = null, AccessToken $accessToken = null) { if (!$appId) { if (Config::get('wechat.platform_id')) { if ($proxy = $this->getProxy(Config::get('wechat.platform_id'))) { $httpConfig = array_merge(Config::get('wechat.http'), ['proxy' => $proxy]); Config::set('wechat.http', $httpConfig); } } $this->officialAccount = Factory::officialAccount(Config::get('wechat')); if (isset($proxy) && $proxy) { $httpConfig = array_merge(Config::get('wechat.http'), ['proxy' => $proxy]); $this->officialAccount['http_client'] = new Client($httpConfig); } } else { $this->officialAccount = $this->getOpenPlatform()->officialAccount($appId, $refreshToken, $accessToken); $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoByAppId($appId); $config_platform_id = $pt_id = Config::get('wechat.platform_id'); if ($config_platform_id == $adminConfig['menu_platform_id']) { $pt_id = $adminConfig['menu_platform_id']; } else { $pt_id = $adminConfig['platform_id']; } if ($proxy = $this->getProxy($pt_id)) { $httpConfig = array_merge(Config::get('wechat.http'), ['proxy' => $proxy]); $this->officialAccount['http_client'] = new Client($httpConfig); } } $this->officialAccount['cache'] = new RedisCache(Redis::instanceCache()); return $this->officialAccount; } /** * @param string $sourceDomain * @return string */ public function setSourceDomain(string $sourceDomain) { return $this->sourceDomain = $sourceDomain; } /** * @return string */ public function getSourceDomain() { return $this->sourceDomain; } /** * @return \app\main\model\object\ReturnObject */ public function initWxParams() { try { $adminconfig = UserService::instance()->getRunTimeObject()->adminConfig; switch (UserService::instance()->getRunTimeObject()->urlType) { case OpenPlatformConstants::URL_TYPE_ADMIN: // 后台首页 break; case OpenPlatformConstants::URL_TYPE_PAY: // 充值页 $host = get_host_no_port(); $payHosts = OfficialAccountsService::instance()->getWxpayModel()->getHosts(); if (in_array($host, $payHosts)) { $pay_hosts = OfficialAccountsService::instance()->getWxpayModel()->getInfoByHost($host); $payInfo = OfficialAccountsService::instance()->getWxpayModel()->getInfo($pay_hosts['platform_id'], $pay_hosts['id']); Config::set('wechat.app_id', $payInfo['appid']); Config::set('wechat.secret', $payInfo['secret']); Config::set('wechat.mch_id', $payInfo['mcid']); Config::set('wechat.key', $payInfo['apikey']); Config::set('wechat.platform_id', $payInfo['platform_id']); } else { Config::set('wechat.app_id', $adminconfig['wxpay_appid']); Config::set('wechat.secret', $adminconfig['wxpay_secret']); Config::set('wechat.mch_id', $adminconfig['wxpay_mcid']); Config::set('wechat.key', $adminconfig['wxpay_apikey']); Config::set('wechat.platform_id', $adminconfig['platform_id']); } $arr = Config::get('wechat.http'); $arr['base_uri'] = 'https://api.mch.weixin.qq.com/'; Config::set('wechat.http', $arr); break; case OpenPlatformConstants::URL_TYPE_SPREAD: // 推广页 //获取域名平台,先用顶级查找,顶级找不到使用全域名进行查找 $host = get_host_no_port(); $hostArr = explode(".", $host); array_shift($hostArr); $topHost = implode(".", $hostArr); if (!$ophost = OfficialAccountsService::instance()->getOphostModel()->getInfoByHost($topHost)) { $ophost = OfficialAccountsService::instance()->getOphostModel()->getInfoByHost($host); } //获取微信平台信息 if ($ophost) { $platform_info = OpenPlatformService::instance()->getPlatformModel()->getInfo($ophost['platform_id']); Config::set('wechat.app_id', $platform_info['appid']); Config::set('wechat.secret', $platform_info['secret']); Config::set('wechat.token', $platform_info['token']); Config::set('wechat.aes_key', $platform_info['aes_key']); Config::set('wechat.platform_id', $ophost['platform_id']); } else { Config::set('wechat.app_id', $adminconfig['platform_appid']); Config::set('wechat.secret', $adminconfig['platform_secret']); Config::set('wechat.token', $adminconfig['platform_token']); Config::set('wechat.aes_key', $adminconfig['platform_aes_key']); Config::set('wechat.platform_id', $adminconfig['platform_id']); } break; default: //默认 break; } return $this->getReturn(); } catch (\Exception $e) { return $this->getExceptionReturn($e); } } /** * 获取服务号的refresh_token * @param $channelId * @return mixed * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getRefreshToken($channelId) { $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channelId); $config_platform_id = Config::get('wechat.platform_id'); 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']; } else { $refresh_token = $adminConfig['refresh_token']; } return $refresh_token; } /** * 使用appid初始化平台 * @param $appid * @return \app\main\model\object\ReturnObject */ public function initPlatformByAppId($appid) { $platformData = OpenPlatformService::instance()->getPlatformModel()->where(['appid'=>$appid])->find(); if($platformData){ Config::set('wechat.app_id', $platformData['appid']); Config::set('wechat.secret', $platformData['secret']); Config::set('wechat.token', $platformData['token']); Config::set('wechat.aes_key', $platformData['aes_key']); Config::set('wechat.platform_id', $platformData['id']); LogService::info('API_LOG Platform:' . var_export($platformData, true)); if (!OpenPlatformService::instance()->getOpenPlatform()) { return $this->setMsg('平台配置有误')->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->getReturn(); } }else{ return $this->setMsg('平台配置有误')->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->getReturn(); } return $this->getReturn(); } /** * 使用平台id初始化平台 * @param $pt_id * @return \app\main\model\object\ReturnObject */ public function initPlatformById($pt_id) { $platformData = OpenPlatformService::instance()->getPlatformModel()->getInfo($pt_id); if($platformData){ Config::set('wechat.app_id', $platformData['appid']); Config::set('wechat.secret', $platformData['secret']); Config::set('wechat.token', $platformData['token']); Config::set('wechat.aes_key', $platformData['aes_key']); Config::set('wechat.platform_id', $platformData['id']); LogService::info('API_LOG Platform:' . var_export($platformData, true)); if (!OpenPlatformService::instance()->getOpenPlatform()) { return $this->setMsg('平台配置有误')->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->getReturn(); } }else{ return $this->setMsg('平台配置有误')->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->getReturn(); } return $this->getReturn(); } /** * 返回平台代理IP 多个时随机返回一个 * @param null $pt_id * @return null */ public function getProxyconfigById($pt_id = null, $field = 'proxy_config', $all = false) { //$this->getPlatformModel()->delCacheAll($pt_id); $platformInfo = $this->getPlatformModel()->getInfo($pt_id); if (isset($platformInfo[$field])) { if ($proxyConfig = trim($platformInfo[$field])) { $arr = explode(",", str_replace([" ", " ", "\s", "\r", "\n"], [",", ",", ",", ",", ","], $proxyConfig)); $arr = array_values(array_filter($arr)); if ($all) { return $arr; } $proxy = $arr[array_rand($arr,1)]; Log::info("三方平台代理IP:{$proxy}"); return $proxy; } } return null; } /** * 获取渠道的平台的代理IP * @param null $channel_id * @param null $app_id * @return null|string */ public function getProxyconfigByChannel($channel_id = null, $app_id = null, $field = 'proxy_config', $all = false) { if (empty($channel_id) && empty($app_id)) { return null; } if ($channel_id) { $adminInfo = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channel_id); } else { $adminInfo = AdminService::instance()->getAdminConfigModel()->getAdminInfoByAppId($app_id); } return $this->getProxyconfigById($adminInfo['platform_id'], $field, $all); } /** * 返回平台对应的代理IP * @param $pt_id * @return mixed */ public function getProxy($pt_id) { $modulename = request()->module(); if (isset($this->_proxy[$pt_id]) && $modulename != 'admin') { return $this->_proxy[$pt_id]; } else { $this->_proxy[$pt_id] = $this->getProxyconfigById($pt_id); } return $this->_proxy[$pt_id]; } }