Mini.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\common\library\message;
  3. use fast\Http;
  4. use GuzzleHttp\Client;
  5. use GuzzleHttp\Psr7\Response;
  6. use think\Cache;
  7. use think\Log;
  8. use think\Config;
  9. use app\common\Service\ShopConfigService;
  10. use app\common\Enum\ChannelEnum;
  11. class Mini
  12. {
  13. private $appId = '';
  14. private $appSecret = '';
  15. public function __construct($platform = ChannelEnum::CHANNEL_WECHAT_MINI_PROGRAM)
  16. {
  17. // 需要转换 前段穿 DouyinMiniProgram 后端配置是 douyin_mini_program
  18. $platformKey = $this->convertPlatformName($platform);
  19. $platformConfig = ShopConfigService::getConfigs('shop.platform.' . $platformKey);
  20. $this->appId = $platformConfig['app_id'] ?? '';
  21. $this->appSecret = $platformConfig['secret'] ?? '';
  22. }
  23. /**
  24. * 转换平台名称格式
  25. * 将前端驼峰命名转换为后端下划线格式
  26. *
  27. * @param string $platform
  28. * @return string
  29. */
  30. protected function convertPlatformName($platform)
  31. {
  32. // 将驼峰命名转换为下划线格式
  33. return strtolower(preg_replace('/([A-Z])/', '_$1', lcfirst($platform)));
  34. }
  35. /**
  36. * 批量并发发送
  37. * @param array $pushList
  38. * @return bool
  39. */
  40. public function subscribeMessageSendMultiple($pushList)
  41. {
  42. $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $this->getAccessToken();
  43. $concurrentSize = 10;
  44. $promises = [];
  45. $client = new Client();
  46. $promiseProcess = function ($promises) {
  47. $results = \GuzzleHttp\Promise\Utils::unwrap($promises);
  48. foreach ($results as $key => $response) {
  49. $res = (array)json_decode($response->getBody()->getContents(), true);
  50. if (isset($res['errmsg']) && Config::get('app_debug')) {
  51. Log::write($res, 'send_msg');
  52. }
  53. }
  54. };
  55. foreach ($pushList as $index => $item) {
  56. $promises[] = $client->postAsync($url, ['body' => json_encode($item, JSON_UNESCAPED_UNICODE)]);
  57. if (count($promises) == $concurrentSize) {
  58. $promiseProcess($promises);
  59. $promises = [];
  60. }
  61. }
  62. if ($promises) {
  63. $promiseProcess($promises);
  64. }
  65. return true;
  66. }
  67. /**
  68. * 单次异步发送
  69. * @param array $pushData
  70. * @return bool
  71. */
  72. public function send($pushData)
  73. {
  74. $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $this->getAccessToken();
  75. $client = new Client();
  76. $client->postAsync($url, ['body' => json_encode($pushData, JSON_UNESCAPED_UNICODE)])->then(function (Response $response) {
  77. $res = (array)json_decode($response->getBody()->getContents(), true);
  78. if (isset($res['errmsg']) && Config::get('app_debug')) {
  79. Log::write($res, 'send_msg');
  80. return false;
  81. }
  82. })->wait();
  83. return true;
  84. }
  85. private function getAccessToken()
  86. {
  87. $access_token = Cache::get('shop' . $this->appId);
  88. if (!$access_token) {
  89. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->appId . '&secret=' . $this->appSecret;
  90. $res = Http::get($url);
  91. $res = (array)json_decode($res, true);
  92. if (isset($res['access_token'])) {
  93. $access_token = $res['access_token'];
  94. } else if (Config::get('app_debug')) {
  95. Log::write('code:' . $res['errcode'] . ',message:' . $res['errmsg'], 'access_token');
  96. }
  97. Cache::set('shop' . $this->appId, $access_token, 7000);
  98. }
  99. return $access_token;
  100. }
  101. //生成小程序码
  102. public function getWxCodeUnlimited($param)
  103. {
  104. $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $this->getAccessToken();
  105. $data = array_merge([
  106. 'width' => '280'
  107. ], $param);
  108. return Http::post($url, json_encode($data,JSON_UNESCAPED_UNICODE));
  109. }
  110. }