ToDoReminder.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Model\TodoList;
  4. use App\Model\WxEmployeeOfficial;
  5. use App\Service\Weixin\WxTemplateMessageService;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Log;
  9. class ToDoReminder extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'command:todo_reminder';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Command description';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return mixed
  36. */
  37. public function handle()
  38. {
  39. $this->handleReminders();
  40. }
  41. public function handleReminders()
  42. {
  43. $now = time();
  44. $wxService = new WxTemplateMessageService();
  45. TodoList::where('status', '<', TodoList::status_two)
  46. ->where('del_time', 0)
  47. ->where('remind_start', '<=', $now)
  48. ->select(TodoList::$field)
  49. ->orderBy('id')
  50. ->chunkById(10, function ($data) use ($now, $wxService) {
  51. // 查找关联员工 openid
  52. $wxInfo = WxEmployeeOfficial::where('employee_id', array_column($data,'crt_id'))
  53. ->where('type', WxEmployeeOfficial::login_type_two)
  54. ->where('del_time', 0)
  55. ->pluck('open_id','id')
  56. ->toArray();
  57. foreach ($data as $todo) {
  58. $shouldRemind = false;
  59. if ($todo->remind_interval == 0 && empty($todo->last_remind_time)) {
  60. $shouldRemind = true;
  61. } elseif ($todo->remind_interval > 0 && (empty($todo->last_remind_time) || ($now - $todo->last_remind_time) >= $todo->remind_interval)
  62. ) {
  63. $shouldRemind = true;
  64. }
  65. if (! $shouldRemind) continue;
  66. // 先发送微信消息(接口可能失败)
  67. try {
  68. list($status, $msg) = $this->sendReminder($todo, $wxService, $wxInfo);
  69. if(! $status) {
  70. Log::error("发送待办提醒失败 todo_id: {$todo->id}", ['msg' => $msg]);
  71. }
  72. } catch (\Throwable $e) {
  73. Log::error("发送待办提醒失败 todo_id: {$todo->id}", ['msg' => $e->getMessage()]);
  74. }
  75. // 再更新数据库(单条事务保护即可)
  76. DB::transaction(function () use ($todo, $now) {
  77. $todo->last_remind_time = $now;
  78. $todo->status = TodoList::status_one;
  79. $todo->save();
  80. });
  81. }
  82. });
  83. }
  84. protected function sendReminder($todo, $wxService, $wxInfo)
  85. {
  86. // 查找关联员工 openid
  87. $openid = $wxInfo[$todo->crt_id] ?? "";
  88. if (! $openid) return [false, '无openid'];
  89. // 模板参数
  90. $params = [
  91. 'title' => $todo->title ?? '待办提醒',
  92. 'time' => date('Y-m-d H:i:00', $todo->remind_start),
  93. ];
  94. // 发送消息
  95. list($status, $msg) = $wxService->sendTemplateMessage('to_do', $params, [
  96. 'openid' => $openid,
  97. 'pagepath' => '/pages/todo/detail?id=' . $todo->id, // 小程序路径或URL
  98. ]);
  99. return [$status, $msg];
  100. }
  101. }