diff --git a/app/api/controller/SendMsgCronJobController.php b/app/api/controller/SendMsgCronJobController.php new file mode 100644 index 0000000..51bc12e --- /dev/null +++ b/app/api/controller/SendMsgCronJobController.php @@ -0,0 +1,46 @@ +with(['teacherAttr', 'subject', 'studentSchedule'])->findOrEmpty(); + + $res = (new SendMsgCronJobService())->teacherScheduleTimePublishMsg($teacher_schedule_time_id); + + return $this->json($res); + }catch (Exception $e){ + + } + + } + + + /** + * @desc 上课提前通知老师 + * @return void + */ + public function classBeginMsgToTeacher() + { + + } + + + +} \ No newline at end of file diff --git a/app/common/service/SendMsgCronJobService.php b/app/common/service/SendMsgCronJobService.php new file mode 100644 index 0000000..2584be5 --- /dev/null +++ b/app/common/service/SendMsgCronJobService.php @@ -0,0 +1,58 @@ +with(['teacherAttr', 'subject', 'studentSchedule'])->findOrEmpty(); + + //给教师发送消息 + $msgInfo = [ + 'teacher'=>$teacher_schedule_time->teacherAttr, + 'subject'=>$teacher_schedule_time->subject, + ]; + + $msg = $send_data = [ + 'touser' => '1231231', + 'template_id' => 'R5kORlczAC4Ltf9RxuesUB8A0ZhEKEh0Zqv9mI8r6u4', + 'data' => [ + 'time2' => [//上课时间 + 'value' => '00:10 - 01:00', + 'color' => '#000000' + ], + 'thing1' => [//课程名称 + 'value' => '历史', + 'color' => '#000000' + ], + 'thing5' => [//上课老师 + 'value' => 'David', + 'color' => '#000000' + ], + 'thing4' => [//学员姓名 + 'value' => '张三', + 'color' => '#000000' + ] + ], + 'miniprogram' => [ + 'pagepath' => 'pages/orders/orders', + 'appid' => getenv('XUN_FU_MA_MINI_APPID'), + ], + "lang" => "zh_CN", + ]; + $result = (new WechatSubscriptService())->sendMsg($msg); + + }catch (Exception $e){ + + } + } + +} \ No newline at end of file diff --git a/app/common/service/WechatSubscriptService.php b/app/common/service/WechatSubscriptService.php new file mode 100644 index 0000000..c2f8f9c --- /dev/null +++ b/app/common/service/WechatSubscriptService.php @@ -0,0 +1,44 @@ +client = new Client(['base_uri' => 'https://api.weixin.qq.com']); + } + + /** + * @desc 发送公众号消息 + * @param $send_data + * @return void + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function sendMsg($send_data) + { + $access_token = WechatUtil::getAccessToken(); + + 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 new file mode 100644 index 0000000..3859b80 --- /dev/null +++ b/app/utils/WechatUtil.php @@ -0,0 +1,94 @@ + self::BASE_URI]); + $response = $client->request('post', 'cgi-bin/stable_token', [ + 'json' => [ + 'grant_type' => 'client_credential', + 'appid' => getenv('APP_ID'), + 'secret' => getenv('APP_SECRET'), + ] + ]); + $response_contents = $response->getBody()->getContents(); + raw_log('wechat/access_token', ['result' => $response_contents]); + if ($response->getStatusCode() == 200) { + $result = json_decode($response_contents, true); + raw_log('wechat/access_token', ['result' => $result]); + if (isset($result['errcode'])) { + throw new Exception($result['errmsg']); + } + Cache::set(self::GENERAL_ACCESS_TOKEN, $result['access_token'], $result['expires_in'] - 60); + + return $result['access_token']; + } + } + } catch (Exception $e) { + return [ + 'code' => ResponseCode::FAIL, + 'msg' => $e->getMessage() + ]; + } + } + + /** + * @desc 获取网页授权code的access_token + * @param $code + * @return array|void + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public static function getCodeAccessToken($code) + { + try { + $client = new Client(['base_uri'=>self::BASE_URI]); + $response = $client->request('get', 'sns/oauth2/access_token', [ + 'query' => [ + 'appid' => getenv('APPID'), + 'secret' => getenv('APPSECRET'), + 'code'=>$code, + 'grant_type'=>'authorization_code' + ] + ]); + + $response_contents = $response->getBody()->getContents(); + raw_log('wechat/code_access_token', ['result' => $response_contents]); + if ($response->getStatusCode() == 200) { + $result = json_decode($response_contents, true); + raw_log('wechat/code_access_token', ['result' => $result]); + if (isset($result['errcode'])) { + throw new Exception($result['errmsg']); + } + return $result; + } + + }catch (Exception $e){ + return [ + 'code'=>ResponseCode::FAIL, + 'msg'=>$e->getMessage() + ]; + } + } + +} \ No newline at end of file diff --git a/config/process.php b/config/process.php index f94d27f..f964c0a 100644 --- a/config/process.php +++ b/config/process.php @@ -38,5 +38,8 @@ return [ 'enable_memory_monitor' => DIRECTORY_SEPARATOR === '/', ] ] - ] + ], + 'task' => [ + 'handler' => process\Task::class + ], ]; diff --git a/process/Task.php b/process/Task.php new file mode 100644 index 0000000..952eef7 --- /dev/null +++ b/process/Task.php @@ -0,0 +1,43 @@ +