ToDoReminder.php 4.1 KB

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