AutoService.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace app\wechat\service;
  3. use think\admin\Service;
  4. use think\admin\service\QueueService;
  5. /**
  6. * 关注自动回复服务
  7. * Class AutoService
  8. * @package app\wechat\service
  9. */
  10. class AutoService extends Service
  11. {
  12. /**
  13. * 注册微信用户推送任务
  14. * @param string $openid
  15. * @throws \think\admin\Exception
  16. * @throws \think\db\exception\DataNotFoundException
  17. * @throws \think\db\exception\DbException
  18. * @throws \think\db\exception\ModelNotFoundException
  19. */
  20. public function register(string $openid)
  21. {
  22. foreach ($this->app->db->name('WechatAuto')->where(['status' => 1])->order('time asc')->cursor() as $vo) {
  23. $time = $this->parseTimeString($vo['time']);
  24. $name = "延迟向 {$openid} 推送 {$vo['code']} 客服消息";
  25. QueueService::instance()->register($name, "xadmin:fanauto {$openid} {$vo['code']}", $time);
  26. }
  27. }
  28. /**
  29. * 解析配置时间格式
  30. * @param string $time
  31. * @return int
  32. */
  33. private function parseTimeString(string $time): int
  34. {
  35. if (preg_match('|^.*?(\d{2}).*?(\d{2}).*?(\d{2}).*?$|', $time, $vars)) {
  36. return intval($vars[1]) * 3600 * intval($vars[2]) * 60 + intval($vars[3]);
  37. } else {
  38. return 0;
  39. }
  40. }
  41. }