Auto.php 6.1 KB

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