Auto.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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:fansmsg');
  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. * @throws \WeChat\Exceptions\InvalidResponseException
  32. * @throws \WeChat\Exceptions\LocalCacheException
  33. * @throws \think\admin\Exception
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. protected function execute(Input $input, Output $output)
  39. {
  40. $code = $input->getArgument('autocode');
  41. $this->openid = $input->getArgument('openid');
  42. if (empty($code)) $this->setQueueError("Message Code cannot be empty");
  43. if (empty($this->openid)) $this->setQueueError("Wechat Openid cannot be empty");
  44. // 查询微信消息对象
  45. $map = ['code' => $code, 'status' => 1];
  46. $data = $this->app->db->name('WechatAuto')->where($map)->find();
  47. if (empty($data)) $this->setQueueError("Message Data Query failed");
  48. // 发送微信客服消息
  49. $this->buildMessage($data);
  50. }
  51. /**
  52. * 关键字处理
  53. * @param array $data
  54. * @throws \WeChat\Exceptions\InvalidResponseException
  55. * @throws \WeChat\Exceptions\LocalCacheException
  56. * @throws \think\admin\Exception
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. */
  61. private function buildMessage(array $data)
  62. {
  63. $type = strtolower($data['type']);
  64. $result = [0, '待发送的消息不符合规则'];
  65. if ($type === 'text' && !empty($data['content'])) {
  66. $result = $this->sendMessage('text', ['content' => $data['content']]);
  67. }
  68. if ($type === 'voice' && !empty($data['voice_url'])) {
  69. if ($mediaId = MediaService::instance()->upload($data['voice_url'], 'voice')) {
  70. $result = $this->sendMessage('voice', ['media_id' => $mediaId]);
  71. }
  72. }
  73. if ($type === 'image' && !empty($data['image_url'])) {
  74. if ($mediaId = MediaService::instance()->upload($data['image_url'], 'image')) {
  75. $result = $this->sendMessage('image', ['media_id' => $mediaId]);
  76. }
  77. }
  78. if ($type === 'news') {
  79. [$item, $news] = [MediaService::instance()->news($data['news_id']), []];
  80. if (isset($item['articles']) && is_array($item['articles'])) {
  81. $host = sysconf('base.site_host') ?: true;
  82. foreach ($item['articles'] as $vo) if (empty($news)) array_push($news, [
  83. 'url' => url("@wechat/api.view/item/id/{$vo['id']}", [], false, $host)->build(),
  84. 'title' => $vo['title'], 'picurl' => $vo['local_url'], 'description' => $vo['digest'],
  85. ]);
  86. $result = $this->sendMessage('news', ['articles' => $news]);
  87. }
  88. }
  89. if ($type === 'music' && !empty($data['music_url']) && !empty($data['music_title']) && !empty($data['music_desc'])) {
  90. $mediaId = $data['music_image'] ? MediaService::instance()->upload($data['music_image'], 'image') : '';
  91. $result = $this->sendMessage('music', [
  92. 'hqmusicurl' => $data['music_url'], 'musicurl' => $data['music_url'],
  93. 'description' => $data['music_desc'], 'title' => $data['music_title'], 'thumb_media_id' => $mediaId,
  94. ]);
  95. }
  96. if ($type === 'video' && !empty($data['video_url']) && !empty($data['video_desc']) && !empty($data['video_title'])) {
  97. $video = ['title' => $data['video_title'], 'introduction' => $data['video_desc']];
  98. if ($mediaId = MediaService::instance()->upload($data['video_url'], 'video', $video)) {
  99. $result = $this->sendMessage('video', ['media_id' => $mediaId, 'title' => $data['video_title'], 'description' => $data['video_desc']]);
  100. }
  101. }
  102. if (empty($result[0])) {
  103. $this->setQueueError($result[1]);
  104. } else {
  105. $this->setQueueSuccess($result[1]);
  106. }
  107. }
  108. /**
  109. * 推送客服消息
  110. * @param string $type 消息类型
  111. * @param array $data 消息对象
  112. * @return array
  113. */
  114. private function sendMessage(string $type, array $data): array
  115. {
  116. try {
  117. WechatService::WeChatCustom()->send([
  118. $type => $data, 'touser' => $this->openid, 'msgtype' => $type,
  119. ]);
  120. return [1, '向微信用户推送消息成功'];
  121. } catch (\Exception $exception) {
  122. return [0, $exception->getMessage()];
  123. }
  124. }
  125. }