Mp.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 Mp
  10. {
  11. private $appId = '';
  12. private $appSecret = '';
  13. public function __construct()
  14. {
  15. $config = get_addon_config('shop');
  16. $this->appId = $config['mp_appid'];
  17. $this->appSecret = $config['mp_app_secret'];
  18. }
  19. private function getAccessToken()
  20. {
  21. $access_token = Cache::get('shop' . $this->appId);
  22. if (!$access_token) {
  23. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->appId . '&secret=' . $this->appSecret;
  24. $res = Http::get($url);
  25. $res = (array)json_decode($res, true);
  26. if (isset($res['access_token'])) {
  27. $access_token = $res['access_token'];
  28. } else if (Config::get('app_debug')) {
  29. Log::write('code:' . $res['errcode'] . ',message:' . $res['errmsg'], 'access_token');
  30. }
  31. Cache::set('shop' . $this->appId, $access_token, 7000);
  32. }
  33. return $access_token;
  34. }
  35. /**
  36. * 批量并发发送
  37. * @param array $pushList
  38. * @return bool
  39. */
  40. public function sendTemplateMessageMultiple($pushList)
  41. {
  42. $url = 'https://api.weixin.qq.com/cgi-bin/message/template/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/template/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. }