From 639f2aa3808ae0cc3b91354b567d6531510d1743 Mon Sep 17 00:00:00 2001 From: Dai Date: Mon, 5 Aug 2024 21:59:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E4=BC=97=E5=8F=B7=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/SendMsgCronJobController.php | 17 +++++ app/common/service/SendMsgCronJobService.php | 76 +++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/app/api/controller/SendMsgCronJobController.php b/app/api/controller/SendMsgCronJobController.php index 349c04a..07dbc3d 100644 --- a/app/api/controller/SendMsgCronJobController.php +++ b/app/api/controller/SendMsgCronJobController.php @@ -5,6 +5,8 @@ namespace app\api\controller; use app\BaseController; use app\common\model\TeacherScheduleTime; use app\common\service\SendMsgCronJobService; +use app\constant\ResponseCode; +use app\utils\WechatUtil; use support\Request; use think\Exception; @@ -13,6 +15,19 @@ class SendMsgCronJobController extends BaseController protected $noNeedLogin = ['*']; + + public function getToken() + { + $token = WechatUtil::getAccessToken(); + + return $this->json([ + 'code' => ResponseCode::SUCCESS, + 'data' => $token, + 'msg' => 'success' + ]); + } + + /** * @desc 排课发布成功之后,发送通知 * @param $teacher_schedule_time @@ -39,7 +54,9 @@ class SendMsgCronJobController extends BaseController */ public function classBeginMsgToTeacher() { + $res = (new SendMsgCronJobService())->classBeginMsgToTeacher(); + return $this->json($res); } diff --git a/app/common/service/SendMsgCronJobService.php b/app/common/service/SendMsgCronJobService.php index 0a39165..02bd14b 100644 --- a/app/common/service/SendMsgCronJobService.php +++ b/app/common/service/SendMsgCronJobService.php @@ -13,6 +13,13 @@ class SendMsgCronJobService const TEACHER_SCHEDULE_TIME_PUBLISH_TEACHER = 'teacher_schedule_time_publish_to_teacher'; const TEACHER_SCHEDULE_TIME_PUBLISH_STUDENT = 'teacher_schedule_time_publish_to_teacher'; + + /** + * 排课成功通知 + * @param $teacher_schedule_time_id + * @return void + * @throws \GuzzleHttp\Exception\GuzzleException + */ public function teacherScheduleTimePublishMsg($teacher_schedule_time_id) { try { @@ -144,4 +151,73 @@ class SendMsgCronJobService } } + + /** + * 上课时间 提前一天通知老师 + * @return void + */ + public function classBeginMsgToTeacher() + { + try { + $teacher_schedule_time = TeacherScheduleTime::where(['is_publish'=>1]) + ->whereTime('start_time', '>=', date('Y-m-d 00:00:00', strtotime('+21 day'))) + ->whereTime('start_time', '<=', date('Y-m-d 23:59:59', strtotime('+21 day'))) + ->with(['teacherAttr', 'subject', 'studentSchedule']) + ->select(); + + foreach ($teacher_schedule_time as $item) { + + $studentSchedule = $item->studentSchedule->toArray(); + if (empty($studentSchedule)) { + throw new Exception('未找到课程分配的学生'); + } + + $student_info = []; + foreach ($studentSchedule as $student) { + if (!$student['is_publish']) { +// throw new Exception($student['student_name'] . '的排课未发布'); + } + + $student = Student::where(['id' => $student['student_id']])->findOrEmpty(); + array_push($student_info, ['id' => $student['id'], 'student_name' => $student['student_name'], 'openid' => $student['openid']]); + } + $student_name = implode(',',array_column($student_info, 'student_name')); + $send_teacher_data = [ + 'touser' => 'olfLh6o4CG9xb6_tA3y29shOj_Bo',//@todo:发送人 + 'template_id' => 'gTtXWz35mjbtbOZdq6uNBIqy2_W-gu7F4H6s5N-vNAI', + 'data' => [ + 'thing8' => [//课程名称 + 'value' => $item->english_name, + 'color' => '#000000' + ], + 'thing1' => [//上课时间 + 'time5' => $item->end_time, + 'color' => '#000000' + ], + 'thing5' => [//任课教师 + 'value' => $item->teacherAttr->teacher_name, + 'color' => '#000000' + ], + 'thing4' => [//学员姓名 + 'value' => $student_name, + 'color' => '#000000' + ] + ], + 'miniprogram' => [ + + ], + "lang" => "zh_CN", + ]; + $result = (new WechatSubscriptService())->sendMsg($send_teacher_data); + } + + }catch (Exception $e) { + return [ + 'code' => ResponseCode::FAIL, + 'msg' => $e->getMessage() + ]; + } + } + + } \ No newline at end of file