Mini.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. class Mini
  10. {
  11. private $appId = '';
  12. private $appSecret = '';
  13. public function __construct()
  14. {
  15. $config = get_addon_config('shop');
  16. $this->appId = $config['wx_appid'];
  17. $this->appSecret = $config['wx_app_secret'];
  18. }
  19. /**
  20. * 批量并发发送
  21. * @param array $pushList
  22. * @return bool
  23. */
  24. public function subscribeMessageSendMultiple($pushList)
  25. {
  26. $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $this->getAccessToken();
  27. $concurrentSize = 10;
  28. $promises = [];
  29. $client = new Client();
  30. $promiseProcess = function ($promises) {
  31. $results = \GuzzleHttp\Promise\Utils::unwrap($promises);
  32. foreach ($results as $key => $response) {
  33. $res = (array)json_decode($response->getBody()->getContents(), true);
  34. if (isset($res['errmsg']) && Config::get('app_debug')) {
  35. Log::write($res, 'send_msg');
  36. }
  37. }
  38. };
  39. foreach ($pushList as $index => $item) {
  40. $promises[] = $client->postAsync($url, ['body' => json_encode($item, JSON_UNESCAPED_UNICODE)]);
  41. if (count($promises) == $concurrentSize) {
  42. $promiseProcess($promises);
  43. $promises = [];
  44. }
  45. }
  46. if ($promises) {
  47. $promiseProcess($promises);
  48. }
  49. return true;
  50. }
  51. /**
  52. * 单次异步发送
  53. * @param array $pushData
  54. * @return bool
  55. */
  56. public function send($pushData)
  57. {
  58. $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $this->getAccessToken();
  59. $client = new Client();
  60. $client->postAsync($url, ['body' => json_encode($pushData, JSON_UNESCAPED_UNICODE)])->then(function (Response $response) {
  61. $res = (array)json_decode($response->getBody()->getContents(), true);
  62. if (isset($res['errmsg']) && Config::get('app_debug')) {
  63. Log::write($res, 'send_msg');
  64. return false;
  65. }
  66. })->wait();
  67. return true;
  68. }
  69. private function getAccessToken()
  70. {
  71. $access_token = Cache::get('shop' . $this->appId);
  72. if (!$access_token) {
  73. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->appId . '&secret=' . $this->appSecret;
  74. $res = Http::get($url);
  75. $res = (array)json_decode($res, true);
  76. if (isset($res['access_token'])) {
  77. $access_token = $res['access_token'];
  78. } else if (Config::get('app_debug')) {
  79. Log::write('code:' . $res['errcode'] . ',message:' . $res['errmsg'], 'access_token');
  80. }
  81. Cache::set('shop' . $this->appId, $access_token, 7000);
  82. }
  83. return $access_token;
  84. }
  85. //生成小程序码
  86. public function getWxCodeUnlimited($param)
  87. {
  88. $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $this->getAccessToken();
  89. $data = array_merge([
  90. 'width' => '280'
  91. ], $param);
  92. return Http::post($url, json_encode($data,JSON_UNESCAPED_UNICODE));
  93. }
  94. }