123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace app\common\library;
- use app\common\library\Redis;
- use app\main\service\OpenPlatformService;
- use EasyWeChat\Factory;
- use GuzzleHttp\Client;
- use Symfony\Component\Cache\Simple\RedisCache;
- use think\Config;
- use think\Log;
- class WeChatObject{
- /**
- * @var $channel_info 渠道信息
- */
- private $channel_info;
- /**
- * 构造函数
- * WeChat constructor.
- * @param array $channel_info
- */
- public function __construct($channel_info){
- $this->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(){
- }
- }
|