ToDoReminder.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. $appid = config("wx_msg.f_appid");
  46. TodoList::where('status', '<', TodoList::status_two)
  47. ->where('del_time', 0)
  48. ->where('remind_start', '<=', $now)
  49. ->select(TodoList::$field)
  50. ->orderBy('id')
  51. ->chunkById(10, function ($data) use ($now, $wxService, $appid) {
  52. // 查找关联员工 openid
  53. $wxInfo = WxEmployeeOfficial::where('employee_id', array_column($data,'crt_id'))
  54. ->where('type', WxEmployeeOfficial::login_type_two)
  55. ->where('appid', $appid)
  56. ->pluck('open_id','id')
  57. ->toArray();
  58. foreach ($data as $todo) {
  59. $shouldRemind = false;
  60. if ($todo->remind_interval == 0 && empty($todo->last_remind_time)) {
  61. $shouldRemind = true;
  62. } elseif ($todo->remind_interval > 0 && (empty($todo->last_remind_time) || ($now - $todo->last_remind_time) >= $todo->remind_interval)
  63. ) {
  64. $shouldRemind = true;
  65. }
  66. if (! $shouldRemind) continue;
  67. // 先发送微信消息(接口可能失败)
  68. try {
  69. list($status, $msg) = $this->sendReminder($todo, $wxService, $wxInfo);
  70. if(! $status) {
  71. Log::error("发送待办提醒失败 todo_id: {$todo->id}", ['msg' => $msg]);
  72. }
  73. } catch (\Throwable $e) {
  74. Log::error("发送待办提醒失败 todo_id: {$todo->id}", ['msg' => $e->getMessage()]);
  75. }
  76. // 再更新数据库(单条事务保护即可)
  77. DB::transaction(function () use ($todo, $now) {
  78. $todo->last_remind_time = $now;
  79. $todo->status = TodoList::status_one;
  80. $todo->save();
  81. });
  82. }
  83. });
  84. }
  85. protected function sendReminder($todo, $wxService, $wxInfo)
  86. {
  87. // 查找关联员工 openid
  88. $openid = $wxInfo[$todo->crt_id] ?? "";
  89. if (! $openid) return [false, '无openid'];
  90. // 模板参数
  91. $params = [
  92. 'title' => $todo->title ?? '待办提醒',
  93. 'time' => date('Y-m-d H:i:00', $todo->remind_start),
  94. ];
  95. // 发送消息
  96. list($status, $msg) = $wxService->sendTemplateMessage('to_do', $params, [
  97. 'openid' => $openid,
  98. 'pagepath' => '/pages/todo/detail?id=' . $todo->id, // 小程序路径或URL
  99. ]);
  100. return [$status, $msg];
  101. }
  102. }