Auto.php 6.1 KB

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