123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2018/11/29
- * Time: 上午10:41
- */
- namespace app\main\service;
- use app\common\library\Redis;
- use app\common\model\Platform;
- use app\main\constants\ErrorCodeConstants;
- use app\main\constants\OpenPlatformConstants;
- use EasyWeChat\Factory;
- use EasyWeChat\OpenPlatform\Authorizer\Auth\AccessToken;
- use GuzzleHttp\Client;
- use Symfony\Component\Cache\Simple\RedisCache;
- use think\Config;
- use think\Log;
- /**
- * 公众平台
- * Class OpenPlatformService
- * @package app\main\service
- */
- class OpenPlatformService extends BaseService
- {
- /**
- * @var OpenPlatformService
- */
- protected static $self = NULL;
- /**
- * @var \EasyWeChat\OpenPlatform\Application
- */
- public $openPlatform = NULL;
- /**
- * @var \EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\Application
- */
- public $officialAccount;
- /**
- * @var string
- */
- public $sourceDomain;
- //平台ID对应的代理IP
- private $_proxy = [];
- /**
- * @return $this|OpenPlatformService
- */
- public static function instance()
- {
- if (self::$self == NULL) {
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * @return Platform
- */
- public function getPlatformModel()
- {
- return model('Platform');
- }
- /**
- * @return bool|\EasyWeChat\OpenPlatform\Application
- */
- public function getOpenPlatform()
- {
- try {
- if (!$this->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];
- }
- }
|