Auto.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\wechat\command;
  3. use app\wechat\service\MediaService;
  4. use app\wechat\service\WechatService;
  5. use think\admin\Command;
  6. use think\console\Input;
  7. use think\console\input\Argument;
  8. use think\console\Output;
  9. /**
  10. * 向指定用户推送消息
  11. * Class Auto
  12. * @package app\wechat\command
  13. */
  14. class Auto extends Command
  15. {
  16. /** @var string */
  17. private $openid;
  18. /**
  19. * 配置消息指令
  20. */
  21. protected function configure()
  22. {
  23. $this->setName('xadmin:fanauto');
  24. $this->addArgument('openid', Argument::OPTIONAL, 'wechat user openid', '');
  25. $this->addArgument('autocode', Argument::OPTIONAL, 'wechat auto message', '');
  26. $this->setDescription('Wechat Users Push AutoMessage for ThinkAdmin');
  27. }
  28. /**
  29. * @param Input $input
  30. * @param Output $output
  31. * @return void
  32. * @throws \WeChat\Exceptions\InvalidResponseException
  33. * @throws \WeChat\Exceptions\LocalCacheException
  34. * @throws \think\admin\Exception
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. protected function execute(Input $input, Output $output)
  40. {
  41. $code = $input->getArgument('autocode');
  42. $this->openid = $input->getArgument('openid');
  43. if (empty($code)) $this->setQueueError("Message Code cannot be empty");
  44. if (empty($this->openid)) $this->setQueueError("Wechat Openid cannot be empty");
  45. // 查询微信消息对象
  46. $map = ['code' => $code, 'status' => 1];
  47. $data = $this->app->db->name('WechatAuto')->where($map)->find();
  48. if (empty($data)) $this->setQueueError("Message Data Query failed");
  49. // 发送微信客服消息
  50. $this->_buildMessage($data);
  51. }
  52. /**
  53. * 关键字处理
  54. * @param array $data
  55. * @return void
  56. * @throws \WeChat\Exceptions\InvalidResponseException
  57. * @throws \WeChat\Exceptions\LocalCacheException
  58. * @throws \think\admin\Exception
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. private function _buildMessage(array $data)
  64. {
  65. $type = strtolower($data['type']);
  66. $result = [0, '待发货的消息不符合规则'];
  67. if ($type === 'text' && !empty($data['content'])) {
  68. $result = $this->_sendMessage('text', ['content' => $data['content']]);
  69. }
  70. if ($type === 'voice' && !empty($data['voice_url'])) {
  71. if ($mediaId = MediaService::instance()->upload($data['voice_url'], 'voice')) {
  72. $result = $this->_sendMessage('voice', ['media_id' => $mediaId]);
  73. }
  74. }
  75. if ($type === 'image' && !empty($data['image_url'])) {
  76. if ($mediaId = MediaService::instance()->upload($data['image_url'], 'image')) {
  77. $result = $this->_sendMessage('image', ['media_id' => $mediaId]);
  78. }
  79. }
  80. if ($type === 'news') {
  81. [$item, $news] = [MediaService::instance()->news($data['news_id']), []];
  82. if (!empty($item['articles'])) {
  83. foreach ($item['articles'] as $vo) array_push($news, [
  84. 'url' => url("@wechat/api.view/item/id/{$vo['id']}", [], false, true)->build(),
  85. 'title' => $vo['title'], 'picurl' => $vo['local_url'], 'description' => $vo['digest'],
  86. ]);
  87. $result = $this->_sendMessage('news', ['articles' => $news]);
  88. }
  89. }
  90. if ($type === 'music' && !empty($data['music_url']) && !empty($data['music_title']) && !empty($data['music_desc'])) {
  91. $mediaId = $data['music_image'] ? MediaService::instance()->upload($data['music_image'], 'image') : '';
  92. $result = $this->_sendMessage('music', [
  93. 'hqmusicurl' => $data['music_url'], 'musicurl' => $data['music_url'],
  94. 'description' => $data['music_desc'], 'title' => $data['music_title'], 'thumb_media_id' => $mediaId,
  95. ]);
  96. }
  97. if ($type === 'video' && !empty($data['video_url']) && !empty($data['video_desc']) && !empty($data['video_title'])) {
  98. $video = ['title' => $data['video_title'], 'introduction' => $data['video_desc']];
  99. if ($mediaId = MediaService::instance()->upload($data['video_url'], 'video', $video)) {
  100. $result = $this->_sendMessage('video', ['media_id' => $mediaId, 'title' => $data['video_title'], 'description' => $data['video_desc']]);
  101. }
  102. }
  103. if (empty($result[0])) {
  104. $this->setQueueError($result[1]);
  105. } else {
  106. $this->setQueueSuccess($result[1]);
  107. }
  108. }
  109. /**
  110. * 推送客服消息
  111. * @param string $type 消息类型
  112. * @param array $data 消息对象
  113. * @return array
  114. */
  115. private function _sendMessage(string $type, array $data): array
  116. {
  117. try {
  118. WechatService::WeChatCustom()->send([
  119. $type => $data, 'touser' => $this->openid, 'msgtype' => $type,
  120. ]);
  121. return [1, '微信消息推送成功'];
  122. } catch (\Exception $exception) {
  123. return [0, $exception->getMessage()];
  124. }
  125. }
  126. }