WeChatObject.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace app\common\library;
  3. use app\common\library\Redis;
  4. use app\main\service\OpenPlatformService;
  5. use EasyWeChat\Factory;
  6. use GuzzleHttp\Client;
  7. use Symfony\Component\Cache\Simple\RedisCache;
  8. use think\Config;
  9. use think\Log;
  10. class WeChatObject{
  11. /**
  12. * @var $channel_info 渠道信息
  13. */
  14. private $channel_info;
  15. /**
  16. * 构造函数
  17. * WeChat constructor.
  18. * @param array $channel_info
  19. */
  20. public function __construct($channel_info){
  21. $this->channel_info = $channel_info;
  22. return $this;
  23. }
  24. /**
  25. * 获取平台微信
  26. * @param null $platform_id
  27. * @return \EasyWeChat\OpenPlatform\Application | null
  28. */
  29. function getPlatform($platform_id = null){
  30. try{
  31. $weChatConfig = \think\Config::get('wechat');
  32. if(!empty($platform_id)){
  33. $platform = model('Platform')->getInfo($platform_id);
  34. $weChatConfig['app_id'] = $platform['appid'];
  35. $weChatConfig['secret'] = $platform['secret'];
  36. $weChatConfig['token'] = $platform['token'];
  37. $weChatConfig['aes_key'] = $platform['aes_key'];
  38. }else{
  39. $weChatConfig['app_id'] = $this->channel_info['platform_appid'];
  40. $weChatConfig['secret'] = $this->channel_info['platform_secret'];
  41. $weChatConfig['token'] = $this->channel_info['platform_token'];
  42. $weChatConfig['aes_key'] = $this->channel_info['platform_aes_key'];
  43. $platform_id = $this->channel_info['platform_id'];
  44. }
  45. if ($weChatConfig['http']['base_uri'] == 'https://api.mch.weixin.qq.com/') {
  46. $weChatConfig['http']['base_uri'] = 'https://api.weixin.qq.com/';
  47. }
  48. $weChatConfig['http']['timeout'] = 20;
  49. if ($proxy = OpenPlatformService::instance()->getProxy($platform_id)) {
  50. $httpConfig = array_merge($weChatConfig['http'], ['proxy' => $proxy]);
  51. $weChatConfig['http'] = $httpConfig;
  52. Config::set('wechat.http', $httpConfig);
  53. /* $weChatConfig['http']['proxy'] = $proxy;
  54. $weChatConfig['http_client'] = new Client($httpConfig);*/
  55. }
  56. $openPlatform = Factory::openPlatform($weChatConfig);
  57. $openPlatform['cache'] = new RedisCache(Redis::instanceCache());
  58. return $openPlatform;
  59. }catch (\Exception $e){
  60. Log::error('GetWeChatPlatform:'.$e->getMessage());
  61. }
  62. return null;
  63. }
  64. /**
  65. * 获取渠道指定平台的微信
  66. * @param null $platform_id
  67. * @param null $app_id
  68. * @param null $refresh_token
  69. * @return \EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\Application | null
  70. */
  71. function getOfficialAccountByPlatform($platform_id = null,$app_id = null, $refresh_token = null){
  72. try{
  73. if(empty($app_id) && empty($refresh_token)){
  74. $app_id = $this->channel_info['appid'];
  75. $refresh_token = $this->channel_info['refresh_token'];
  76. }
  77. if(!$app_id || !$refresh_token){
  78. throw new \Exception('appid or refresh_token is null');
  79. }
  80. $officialAccount = $this->getPlatform($platform_id)->officialAccount($app_id, $refresh_token);
  81. $officialAccount['cache'] = new RedisCache(Redis::instanceCache());
  82. if ($platform_id) {
  83. if ($proxy = OpenPlatformService::instance()->getProxyconfigById($platform_id)) {
  84. $httpConfig = array_merge(Config::get('wechat.http'), ['proxy' => $proxy]);
  85. $officialAccount['http_client'] = new Client($httpConfig);
  86. }
  87. } else {
  88. if ($proxy = OpenPlatformService::instance()->getProxyconfigByChannel(null, $app_id)) {
  89. $httpConfig = array_merge(Config::get('wechat.http'), ['proxy' => $proxy]);
  90. $officialAccount['http_client'] = new Client($httpConfig);
  91. }
  92. }
  93. return $officialAccount;
  94. }catch (\Exception $e){
  95. Log::info('GetOfficialAccountByPlatform:'.$e->getMessage());
  96. }
  97. return null;
  98. }
  99. /**
  100. * 获取渠道默认微信
  101. * @param null $app_id
  102. * @param null $refresh_token
  103. * @return \EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\Application | null
  104. */
  105. function getOfficialAccount($app_id = null, $refresh_token = null){
  106. try{
  107. if(empty($app_id) && empty($refresh_token)){
  108. $app_id = $this->channel_info['appid'];
  109. $refresh_token = $this->channel_info['refresh_token'];
  110. }
  111. if(!$app_id || !$refresh_token){
  112. throw new \Exception('appid or refresh_token is null');
  113. }
  114. $officialAccount = $this->getPlatform()->officialAccount($app_id, $refresh_token);
  115. $officialAccount['cache'] = new RedisCache(Redis::instanceCache());
  116. if ($proxy = OpenPlatformService::instance()->getProxyconfigByChannel(null, $app_id)) {
  117. $httpConfig = array_merge(Config::get('wechat.http'), ['proxy' => $proxy]);
  118. $officialAccount['http_client'] = new Client($httpConfig);
  119. }
  120. return $officialAccount;
  121. }catch (\Exception $e){
  122. Log::info('GetOfficialAccount:'.$e->getMessage());
  123. }
  124. return null;
  125. }
  126. /**
  127. *
  128. * @param $adminConfig
  129. * @param $config
  130. * @return \EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\Application|null
  131. */
  132. static public function getOfficialAccountByWeChatConfig($adminConfig,$config){
  133. try{
  134. $refresh_token = $adminConfig['refresh_token'] ?? null;
  135. if(!$config || !$adminConfig){
  136. return null;
  137. }
  138. //获取三方平台
  139. if (isset($config['platform_id'])) {
  140. if ($proxy = OpenPlatformService::instance()->getProxy($config['platform_id'])) {
  141. $httpConfig = array_merge($config['http'], ['proxy' => $proxy]);
  142. $weChatConfig['http'] = $httpConfig;
  143. }
  144. } else {
  145. if ($proxy = OpenPlatformService::instance()->getProxy($adminConfig['platform_id'])) {
  146. $httpConfig = array_merge($config['http'], ['proxy' => $proxy]);
  147. $weChatConfig['http'] = $httpConfig;
  148. }
  149. }
  150. $openPlatform = Factory::openPlatform($config);
  151. $openPlatform['cache'] = new RedisCache(Redis::instanceCache());
  152. //检查是否是菜单域名平台
  153. if($config['platform_id'] == $adminConfig['menu_platform_id']){
  154. if(!isset($adminConfig['menu_refresh_token'])){
  155. $adminConfig['menu_refresh_token'] = model('Ptoken')->where(['platform_id'=>$config['platform_id'],'admin_id'=>$adminConfig['admin_id']])->value('refresh_token');
  156. }
  157. $refresh_token = $adminConfig['menu_refresh_token'];
  158. $proxy = OpenPlatformService::instance()->getProxy($adminConfig['menu_platform_id']);
  159. }
  160. //获取officialAccount
  161. if(!$adminConfig['appid'] || !$refresh_token){
  162. Log::write('time: '.date('Y-m-d H:i:s',time()).' getOfficialAccountByWeChatConfig ChannelID:'.$adminConfig['admin_id'].' Error: appid or refresh_token is null','WeChat');
  163. return null;
  164. }
  165. $officialAccount = $openPlatform->officialAccount($adminConfig['appid'], $refresh_token);
  166. $officialAccount['cache'] = new RedisCache(Redis::instanceCache());
  167. if ($proxy) {
  168. $httpConfig = array_merge($config['http'], ['proxy' => $proxy]);
  169. $officialAccount['http_client'] = new Client($httpConfig);
  170. }
  171. return $officialAccount;
  172. }catch (\Exception $e){
  173. Log::error('getOfficialAccountByWeChatConfig ChannelID:'.$adminConfig['admin_id'].' Error: '.$e->getMessage());
  174. return null;
  175. }
  176. }
  177. /**
  178. * 获取微信支付
  179. */
  180. function getPaymentApplication(){
  181. }
  182. /**
  183. * 根据域名获取微信支付配置
  184. */
  185. public function getPaymentConfigByHost(){
  186. }
  187. }