From 0a75a36f352cb4cde951911dcf6be4d8ef5b8354 Mon Sep 17 00:00:00 2001 From: Dai Date: Sun, 4 Aug 2024 23:31:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E4=BC=97=E5=8F=B7=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/SendMsgCronJobController.php | 5 +- app/common/model/CronJob.php | 11 + app/common/service/SendMsgCronJobService.php | 123 +++++- app/common/service/WechatSubscriptService.php | 31 +- app/utils/WechatUtil.php | 2 + .../app/controller/CronJobController.php | 68 ++++ plugin/admin/app/model/CronJob.php | 47 +++ plugin/admin/app/view/cron-job/index.html | 385 ++++++++++++++++++ plugin/admin/app/view/cron-job/insert.html | 271 ++++++++++++ plugin/admin/app/view/cron-job/update.html | 309 ++++++++++++++ process/Task.php | 2 +- 11 files changed, 1224 insertions(+), 30 deletions(-) create mode 100644 app/common/model/CronJob.php create mode 100644 plugin/admin/app/controller/CronJobController.php create mode 100644 plugin/admin/app/model/CronJob.php create mode 100644 plugin/admin/app/view/cron-job/index.html create mode 100644 plugin/admin/app/view/cron-job/insert.html create mode 100644 plugin/admin/app/view/cron-job/update.html diff --git a/app/api/controller/SendMsgCronJobController.php b/app/api/controller/SendMsgCronJobController.php index 51bc12e..349c04a 100644 --- a/app/api/controller/SendMsgCronJobController.php +++ b/app/api/controller/SendMsgCronJobController.php @@ -5,6 +5,7 @@ namespace app\api\controller; use app\BaseController; use app\common\model\TeacherScheduleTime; use app\common\service\SendMsgCronJobService; +use support\Request; use think\Exception; class SendMsgCronJobController extends BaseController @@ -17,12 +18,12 @@ class SendMsgCronJobController extends BaseController * @param $teacher_schedule_time * @return void */ - public function teacherScheduleTimePublishMsg($teacher_schedule_time_id = 1) + public function teacherScheduleTimePublishMsg(Request $request) { try { // $teacher_schedule_time = TeacherScheduleTime::where('id', $teacher_schedule_time_id)->with(['teacherAttr', 'subject', 'studentSchedule'])->findOrEmpty(); - $res = (new SendMsgCronJobService())->teacherScheduleTimePublishMsg($teacher_schedule_time_id); + $res = (new SendMsgCronJobService())->teacherScheduleTimePublishMsg(75); return $this->json($res); }catch (Exception $e){ diff --git a/app/common/model/CronJob.php b/app/common/model/CronJob.php new file mode 100644 index 0000000..f712ff7 --- /dev/null +++ b/app/common/model/CronJob.php @@ -0,0 +1,11 @@ +with(['teacherAttr', 'subject', 'studentSchedule'])->findOrEmpty(); + $teacher_schedule_time = TeacherScheduleTime::where(['id' => $teacher_schedule_time_id])->with(['teacherAttr', 'subject', 'studentSchedule']) + ->findOrEmpty(); + + if ($teacher_schedule_time->isEmpty()) { + throw new Exception('教师排课不存在'); + } + $studentSchedule = $teacher_schedule_time->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']]); + } + //给教师发送消息 - $msgInfo = [ - 'teacher'=>$teacher_schedule_time->teacherAttr, - 'subject'=>$teacher_schedule_time->subject, - ]; - - $msg = $send_data = [ - 'touser' => '1231231', - 'template_id' => 'R5kORlczAC4Ltf9RxuesUB8A0ZhEKEh0Zqv9mI8r6u4', + $msgInfo = $teacher_schedule_time->toArray(); + $student_name = implode(',',array_column($student_info, 'student_name')); + $send_teacher_data = [ + 'touser' => 'olfLh6o4CG9xb6_tA3y29shOj_Bo', + 'template_id' => 'gTtXWz35mjbtbOZdq6uNBIqy2_W-gu7F4H6s5N-vNAI', 'data' => [ 'time2' => [//上课时间 - 'value' => '00:10 - 01:00', + 'value' => $teacher_schedule_time->en_start_time, 'color' => '#000000' ], 'thing1' => [//课程名称 - 'value' => '历史', + 'value' => $teacher_schedule_time->english_name, 'color' => '#000000' ], 'thing5' => [//上课老师 - 'value' => 'David', + 'value' => $teacher_schedule_time->teacherAttr->teacher_name, 'color' => '#000000' ], 'thing4' => [//学员姓名 - 'value' => '张三', + 'value' => $student_name, 'color' => '#000000' ] ], 'miniprogram' => [ - 'pagepath' => 'pages/orders/orders', - 'appid' => getenv('XUN_FU_MA_MINI_APPID'), + ], "lang" => "zh_CN", ]; - $result = (new WechatSubscriptService())->sendMsg($msg); + $result = (new WechatSubscriptService())->sendMsg($send_teacher_data); - }catch (Exception $e){ + CronJob::create([ + 'msg_type' => self::TEACHER_SCHEDULE_TIME_PUBLISH_TEACHER, + 'teacher_id' => $teacher_schedule_time->teacher_id, + 'subject_id' => $teacher_schedule_time->subject_id, + 'student_id' => '', + 'teacher_schedule_time_id'=>$teacher_schedule_time->id, + 'time'=>$teacher_schedule_time->time, + 'en_time'=>$teacher_schedule_time->en_time, + 'start_time'=>$teacher_schedule_time->start_time, + 'end_time'=>$teacher_schedule_time->end_time, + 'en_start_time'=>$teacher_schedule_time->en_start_time, + 'en_end_time'=>$teacher_schedule_time->en_end_time, + 'send_role'=> 'teacher', + 'send_data'=> json_encode($send_teacher_data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), + 'msg_info' => json_encode($msgInfo, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), + 'send_result'=> $result['code'] == ResponseCode::SUCCESS ? 1 : 0, + 'send_result_msg'=> $result['msg'], + ]); + + foreach ($student_info as $student) { + + $send_teacher_data = [ + 'touser' => 'olfLh6o4CG9xb6_tA3y29shOj_Bo',//@todo:发送人 + 'template_id' => 'gTtXWz35mjbtbOZdq6uNBIqy2_W-gu7F4H6s5N-vNAI', + 'data' => [ + 'time2' => [//上课时间 + 'value' => $teacher_schedule_time->start_time, + 'color' => '#000000' + ], + 'thing1' => [//课程名称 + 'value' => $teacher_schedule_time->subject_name, + 'color' => '#000000' + ], + 'thing5' => [//上课老师 + 'value' => $teacher_schedule_time->teacherAttr->teacher_name, + 'color' => '#000000' + ], + 'thing4' => [//学员姓名 + 'value' => $student_name, + 'color' => '#000000' + ] + ], + 'miniprogram' => [ + + ], + "lang" => "zh_CN", + ]; + $result = (new WechatSubscriptService())->sendMsg($send_teacher_data); + + CronJob::create([ + 'msg_type' => self::TEACHER_SCHEDULE_TIME_PUBLISH_TEACHER, + 'teacher_id' => $teacher_schedule_time->teacher_id, + 'subject_id' => $teacher_schedule_time->subject_id, + 'student_id' => '', + 'teacher_schedule_time_id'=>$teacher_schedule_time->id, + 'time'=>$teacher_schedule_time->time, + 'en_time'=>$teacher_schedule_time->en_time, + 'start_time'=>$teacher_schedule_time->start_time, + 'end_time'=>$teacher_schedule_time->end_time, + 'en_start_time'=>$teacher_schedule_time->en_start_time, + 'en_end_time'=>$teacher_schedule_time->en_end_time, + 'send_role'=> 'student', + 'send_data'=> json_encode($send_teacher_data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), + 'msg_info' => json_encode($msgInfo, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), + 'send_result'=> $result['code'] == ResponseCode::SUCCESS ? 1 : 0, + 'send_result_msg'=> $result['msg'], + ]); + + } + + } catch (Exception $e) { } } diff --git a/app/common/service/WechatSubscriptService.php b/app/common/service/WechatSubscriptService.php index c2f8f9c..1a17f20 100644 --- a/app/common/service/WechatSubscriptService.php +++ b/app/common/service/WechatSubscriptService.php @@ -25,20 +25,31 @@ class WechatSubscriptService /** * @desc 发送公众号消息 * @param $send_data - * @return void * @throws \GuzzleHttp\Exception\GuzzleException */ public function sendMsg($send_data) { - $access_token = WechatUtil::getAccessToken(); + try { + $access_token = WechatUtil::getAccessToken(); + $result = $this->client->request('post', 'cgi-bin/message/template/send?access_token=' . $access_token, [ + 'json' => $send_data, + ]); + $result = json_decode($result->getBody()->getContents(), true); + + raw_log('wechat/send_msg', ['send_data'=>$send_data, 'result'=>$result]); + if(isset($result['errcode']) && $result['errcode'] != 0){ + throw new Exception($result['errmsg']); + } + return [ + 'code' => ResponseCode::SUCCESS, + 'msg'=> $result['errmsg'] + ]; + }catch (Exception $e){ + return [ + 'code' => ResponseCode::FAIL, + 'msg' => $e->getMessage() + ]; + } - print '
';
-        print_r($access_token);
-        die;
-        $result = $this->client->request('post', 'cgi-bin/message/template/send?access_token=' . $access_token, [
-            'json' => $send_data,
-        ]);
-        $result = json_decode($result->getBody()->getContents(), true);
-        raw_log('wechat/send_msg', ['send_data'=>$send_data, 'result'=>$result]);
     }
 }
\ No newline at end of file
diff --git a/app/utils/WechatUtil.php b/app/utils/WechatUtil.php
index 3859b80..3f23683 100644
--- a/app/utils/WechatUtil.php
+++ b/app/utils/WechatUtil.php
@@ -21,6 +21,8 @@ class WechatUtil
     public static function getAccessToken()
     {
         try {
+//            $str = '83_5nmVfFogqz0nUC-sXhNJNRAcD6doq1N3E-g2fb3ty1RqqZq2kXazJDA2-z5TibuSmmUdFh6RlEzojOx1hWMp1dkKKpAQOH7GIuvz0RWTRfYAdeTiDaeRcDXx6TsDOUdABATEK';
+//            Cache::set(self::GENERAL_ACCESS_TOKEN, $str, 3500);
             if (Cache::has(self::GENERAL_ACCESS_TOKEN)) {
                 return Cache::get(self::GENERAL_ACCESS_TOKEN);
             } else {
diff --git a/plugin/admin/app/controller/CronJobController.php b/plugin/admin/app/controller/CronJobController.php
new file mode 100644
index 0000000..dfb91d6
--- /dev/null
+++ b/plugin/admin/app/controller/CronJobController.php
@@ -0,0 +1,68 @@
+model = new CronJob;
+    }
+    
+    /**
+     * 浏览
+     * @return Response
+     */
+    public function index(): Response
+    {
+        return view('cron-job/index');
+    }
+
+    /**
+     * 插入
+     * @param Request $request
+     * @return Response
+     * @throws BusinessException
+     */
+    public function insert(Request $request): Response
+    {
+        if ($request->method() === 'POST') {
+            return parent::insert($request);
+        }
+        return view('cron-job/insert');
+    }
+
+    /**
+     * 更新
+     * @param Request $request
+     * @return Response
+     * @throws BusinessException
+    */
+    public function update(Request $request): Response
+    {
+        if ($request->method() === 'POST') {
+            return parent::update($request);
+        }
+        return view('cron-job/update');
+    }
+
+}
diff --git a/plugin/admin/app/model/CronJob.php b/plugin/admin/app/model/CronJob.php
new file mode 100644
index 0000000..0d614b1
--- /dev/null
+++ b/plugin/admin/app/model/CronJob.php
@@ -0,0 +1,47 @@
+
+
+    
+        
+        浏览页面
+        
+        
+    
+    
+    
+        
+        
+
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ + + +
+
+ 展开 + 收起 +
+
+
+
+ + +
+
+
+
+
+ + + + + + + + + + + + + + + diff --git a/plugin/admin/app/view/cron-job/insert.html b/plugin/admin/app/view/cron-job/insert.html new file mode 100644 index 0000000..9a10536 --- /dev/null +++ b/plugin/admin/app/view/cron-job/insert.html @@ -0,0 +1,271 @@ + + + + + 新增页面 + + + + + + +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+ +
+
+ + +
+
+ +
+ + + + + + + + + + diff --git a/plugin/admin/app/view/cron-job/update.html b/plugin/admin/app/view/cron-job/update.html new file mode 100644 index 0000000..02cf2e1 --- /dev/null +++ b/plugin/admin/app/view/cron-job/update.html @@ -0,0 +1,309 @@ + + + + + 更新页面 + + + + + + + +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+ +
+
+ + +
+
+ +
+ + + + + + + + + + + diff --git a/process/Task.php b/process/Task.php index ead8a78..f465f27 100644 --- a/process/Task.php +++ b/process/Task.php @@ -8,7 +8,7 @@ class Task { public function onWorkerStart() { - + // 每天的7点50执行,注意这里省略了秒位 new Crontab('50 7 * * *', function(){ echo date('Y-m-d H:i:s')."\n";