Service.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace app\common\library\Wechat;
  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. /**
  10. * 小程序服务类
  11. */
  12. class Service
  13. {
  14. private $appId = '';
  15. private $appSecret = '';
  16. public function __construct()
  17. {
  18. $config = get_addon_config('shop');
  19. $this->appId = $config['wx_appid'];
  20. $this->appSecret = $config['wx_app_secret'];
  21. }
  22. /**
  23. * 批量并发发送
  24. * @param array $pushList
  25. * @return bool
  26. */
  27. public function subscribeMessageSendMultiple($pushList)
  28. {
  29. $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $this->getAccessToken();
  30. $concurrentSize = 10;
  31. $promises = [];
  32. $client = new Client();
  33. $promiseProcess = function ($promises) {
  34. $results = \GuzzleHttp\Promise\Utils::unwrap($promises);
  35. foreach ($results as $key => $response) {
  36. $res = (array)json_decode($response->getBody()->getContents(), true);
  37. if (isset($res['errmsg']) && Config::get('app_debug')) {
  38. Log::write($res, 'send_msg');
  39. }
  40. }
  41. };
  42. foreach ($pushList as $index => $item) {
  43. $promises[] = $client->postAsync($url, ['body' => json_encode($item, JSON_UNESCAPED_UNICODE)]);
  44. if (count($promises) == $concurrentSize) {
  45. $promiseProcess($promises);
  46. $promises = [];
  47. }
  48. }
  49. if ($promises) {
  50. $promiseProcess($promises);
  51. }
  52. return true;
  53. }
  54. /**
  55. * 单次异步发送
  56. * @param array $pushData
  57. * @return bool
  58. */
  59. public function subscribeMessageSend($pushData)
  60. {
  61. $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $this->getAccessToken();
  62. $client = new Client();
  63. $client->postAsync($url, ['body' => json_encode($pushData, JSON_UNESCAPED_UNICODE)])->then(function (Response $response) {
  64. $res = (array)json_decode($response->getBody()->getContents(), true);
  65. if (isset($res['errmsg']) && Config::get('app_debug')) {
  66. Log::write($res, 'send_msg');
  67. return false;
  68. }
  69. })->wait();
  70. return true;
  71. }
  72. //获取access_token
  73. public function getAccessToken()
  74. {
  75. $access_token = Cache::get('shop' . $this->appId);
  76. if (!$access_token) {
  77. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->appId . '&secret=' . $this->appSecret;
  78. $res = Http::get($url);
  79. $res = (array)json_decode($res, true);
  80. if (isset($res['access_token'])) {
  81. $access_token = $res['access_token'];
  82. } elseif (Config::get('app_debug')) {
  83. Log::write('code:' . $res['errcode'] . ',message:' . $res['errmsg'], 'access_token');
  84. }
  85. Cache::set('shop' . $this->appId, $access_token, 7000);
  86. }
  87. return $access_token;
  88. }
  89. //生成小程序码
  90. public function getWxCodeUnlimited($param)
  91. {
  92. $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $this->getAccessToken();
  93. $data = array_merge([
  94. 'width' => '280'
  95. ], $param);
  96. return Http::post($url, json_encode($data, JSON_UNESCAPED_UNICODE));
  97. }
  98. //获取手机号
  99. public function getWechatMobile($code)
  100. {
  101. $access_token = $this->getAccessToken();
  102. $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token={$access_token}";
  103. $res = Http::post($url, json_encode(['code' => $code]));
  104. $res = (array)json_decode($res, true);
  105. if (!isset($res['phone_info']) && config('app_debug')) {
  106. \think\Log::write($res);
  107. }
  108. return $res['phone_info'] ?? [];
  109. }
  110. //获取Session信息
  111. public function getWechatSession($code)
  112. {
  113. $params = [
  114. 'appid' => $this->appId,
  115. 'secret' => $this->appSecret,
  116. 'js_code' => $code,
  117. 'grant_type' => 'authorization_code'
  118. ];
  119. $result = Http::sendRequest("https://api.weixin.qq.com/sns/jscode2session", $params, 'GET');
  120. if ($result['ret']) {
  121. $json = (array)json_decode($result['msg'], true);
  122. return $json;
  123. }
  124. if (config('app_debug')) {
  125. Log::write($result);
  126. }
  127. return ['errmsg' => config('app_debug') ? $result['msg'] : '网络错误'];
  128. }
  129. }