公众号通知定时任务
This commit is contained in:
parent
eb4a73bd0b
commit
088ebac333
46
app/api/controller/SendMsgCronJobController.php
Normal file
46
app/api/controller/SendMsgCronJobController.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\BaseController;
|
||||
use app\common\model\TeacherScheduleTime;
|
||||
use app\common\service\SendMsgCronJobService;
|
||||
use think\Exception;
|
||||
|
||||
class SendMsgCronJobController extends BaseController
|
||||
{
|
||||
|
||||
protected $noNeedLogin = ['*'];
|
||||
|
||||
/**
|
||||
* @desc 排课发布成功之后,发送通知
|
||||
* @param $teacher_schedule_time
|
||||
* @return void
|
||||
*/
|
||||
public function teacherScheduleTimePublishMsg($teacher_schedule_time_id = 1)
|
||||
{
|
||||
try {
|
||||
// $teacher_schedule_time = TeacherScheduleTime::where('id', $teacher_schedule_time_id)->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()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
58
app/common/service/SendMsgCronJobService.php
Normal file
58
app/common/service/SendMsgCronJobService.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use app\common\model\TeacherScheduleTime;
|
||||
use think\Exception;
|
||||
|
||||
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';
|
||||
|
||||
public function teacherScheduleTimePublishMsg($teacher_schedule_time_id)
|
||||
{
|
||||
try {
|
||||
$teacher_schedule_time = TeacherScheduleTime::where('id', $teacher_schedule_time_id)->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){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
44
app/common/service/WechatSubscriptService.php
Normal file
44
app/common/service/WechatSubscriptService.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use app\constant\ResponseCode;
|
||||
use app\utils\WechatUtil;
|
||||
use GuzzleHttp\Client;
|
||||
use support\Cache;
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
* 微信公众号
|
||||
*/
|
||||
class WechatSubscriptService
|
||||
{
|
||||
const SUBSCRIPT_ACCESS_TOKEN = 'subscript_access_token';
|
||||
|
||||
protected $client;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->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 '<pre>';
|
||||
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]);
|
||||
}
|
||||
}
|
94
app/utils/WechatUtil.php
Normal file
94
app/utils/WechatUtil.php
Normal file
@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace app\utils;
|
||||
|
||||
use app\constant\ResponseCode;
|
||||
use GuzzleHttp\Client;
|
||||
use support\Cache;
|
||||
use think\Exception;
|
||||
|
||||
class WechatUtil
|
||||
{
|
||||
const GENERAL_ACCESS_TOKEN = 'general_access_token';
|
||||
const CODE_ACCESS_TOKEN = 'code_access_token';
|
||||
const BASE_URI = 'https://api.weixin.qq.com';
|
||||
|
||||
/**
|
||||
* @desc 获取access token
|
||||
* @return array|mixed|void
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public static function getAccessToken()
|
||||
{
|
||||
try {
|
||||
if (Cache::has(self::GENERAL_ACCESS_TOKEN)) {
|
||||
return Cache::get(self::GENERAL_ACCESS_TOKEN);
|
||||
} else {
|
||||
$client = new Client(['base_uri' => 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()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -38,5 +38,8 @@ return [
|
||||
'enable_memory_monitor' => DIRECTORY_SEPARATOR === '/',
|
||||
]
|
||||
]
|
||||
]
|
||||
],
|
||||
'task' => [
|
||||
'handler' => process\Task::class
|
||||
],
|
||||
];
|
||||
|
43
process/Task.php
Normal file
43
process/Task.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace process;
|
||||
|
||||
use Workerman\Crontab\Crontab;
|
||||
|
||||
class Task
|
||||
{
|
||||
public function onWorkerStart()
|
||||
{
|
||||
|
||||
// 每秒钟执行一次
|
||||
new Crontab('*/1 * * * * *', function(){
|
||||
echo date('Y-m-d H:i:s')."\n";
|
||||
});
|
||||
|
||||
// 每5秒执行一次
|
||||
new Crontab('*/5 * * * * *', function(){
|
||||
echo date('Y-m-d H:i:s')."\n";
|
||||
});
|
||||
|
||||
// 每分钟执行一次
|
||||
new Crontab('0 */1 * * * *', function(){
|
||||
echo date('Y-m-d H:i:s')."\n";
|
||||
});
|
||||
|
||||
// 每5分钟执行一次
|
||||
new Crontab('0 */5 * * * *', function(){
|
||||
echo date('Y-m-d H:i:s')."\n";
|
||||
});
|
||||
|
||||
// 每分钟的第一秒执行
|
||||
new Crontab('1 * * * * *', function(){
|
||||
echo date('Y-m-d H:i:s')."\n";
|
||||
});
|
||||
|
||||
// 每天的7点50执行,注意这里省略了秒位
|
||||
new Crontab('50 7 * * *', function(){
|
||||
echo date('Y-m-d H:i:s')."\n";
|
||||
});
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user