| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 | 
							- <?php
 
- namespace app\common\library\message;
 
- use fast\Http;
 
- use GuzzleHttp\Client;
 
- use GuzzleHttp\Psr7\Response;
 
- use think\Cache;
 
- use think\Log;
 
- use think\Config;
 
- use app\common\Service\ShopConfigService;
 
- use app\common\Enum\ChannelEnum;
 
- class Mini
 
- {
 
-     private $appId = '';
 
-     private $appSecret = '';
 
-     public function __construct($platform = ChannelEnum::CHANNEL_WECHAT_MINI_PROGRAM)
 
-     {
 
-          // 需要转换   前段穿 DouyinMiniProgram  后端配置是   douyin_mini_program
 
-         $platformKey = $this->convertPlatformName($platform);
 
-         $platformConfig = ShopConfigService::getConfigs('shop.platform.' . $platformKey);
 
-  
 
-         $this->appId     = $platformConfig['app_id'] ?? '';
 
-         $this->appSecret = $platformConfig['secret'] ?? '';
 
-     }
 
-          /**
 
-      * 转换平台名称格式
 
-      * 将前端驼峰命名转换为后端下划线格式
 
-      *
 
-      * @param string $platform
 
-      * @return string
 
-      */
 
-     protected function convertPlatformName($platform)
 
-     {
 
-         // 将驼峰命名转换为下划线格式
 
-         return strtolower(preg_replace('/([A-Z])/', '_$1', lcfirst($platform)));
 
-     }
 
-     /**
 
-      * 批量并发发送
 
-      * @param array $pushList
 
-      * @return bool
 
-      */
 
-     public function subscribeMessageSendMultiple($pushList)
 
-     {
 
-         $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $this->getAccessToken();
 
-         $concurrentSize = 10;
 
-         $promises = [];
 
-         $client = new Client();
 
-         $promiseProcess = function ($promises) {
 
-             $results = \GuzzleHttp\Promise\Utils::unwrap($promises);
 
-             foreach ($results as $key => $response) {
 
-                 $res = (array)json_decode($response->getBody()->getContents(), true);
 
-                 if (isset($res['errmsg']) && Config::get('app_debug')) {
 
-                     Log::write($res, 'send_msg');
 
-                 }
 
-             }
 
-         };
 
-         foreach ($pushList as $index => $item) {
 
-             $promises[] = $client->postAsync($url, ['body' => json_encode($item, JSON_UNESCAPED_UNICODE)]);
 
-             if (count($promises) == $concurrentSize) {
 
-                 $promiseProcess($promises);
 
-                 $promises = [];
 
-             }
 
-         }
 
-         if ($promises) {
 
-             $promiseProcess($promises);
 
-         }
 
-         return true;
 
-     }
 
-     /**
 
-      * 单次异步发送
 
-      * @param array $pushData
 
-      * @return bool
 
-      */
 
-     public function send($pushData)
 
-     {
 
-         $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $this->getAccessToken();
 
-         $client = new Client();
 
-         $client->postAsync($url, ['body' => json_encode($pushData, JSON_UNESCAPED_UNICODE)])->then(function (Response $response) {
 
-             $res = (array)json_decode($response->getBody()->getContents(), true);
 
-             if (isset($res['errmsg']) && Config::get('app_debug')) {
 
-                 Log::write($res, 'send_msg');
 
-                 return false;
 
-             }
 
-         })->wait();
 
-         return true;
 
-     }
 
-     private function getAccessToken()
 
-     {
 
-         $access_token = Cache::get('shop' . $this->appId);
 
-         if (!$access_token) {
 
-             $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->appId . '&secret=' . $this->appSecret;
 
-             $res = Http::get($url);
 
-             $res = (array)json_decode($res, true);
 
-             if (isset($res['access_token'])) {
 
-                 $access_token = $res['access_token'];
 
-             } else if (Config::get('app_debug')) {
 
-                 Log::write('code:' . $res['errcode'] . ',message:' . $res['errmsg'], 'access_token');
 
-             }
 
-             Cache::set('shop' . $this->appId, $access_token, 7000);
 
-         }
 
-         return $access_token;
 
-     }
 
-     //生成小程序码
 
-     public function getWxCodeUnlimited($param)
 
-     {
 
-         $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $this->getAccessToken();
 
-         $data = array_merge([
 
-             'width' => '280'
 
-         ], $param);       
 
-         return Http::post($url, json_encode($data,JSON_UNESCAPED_UNICODE));    
 
-     }
 
- }
 
 
  |