🐛 fix cron bug
This commit is contained in:
parent
b2eb4edbd5
commit
6608d8e966
@ -34,7 +34,7 @@ func GetUser(ctx *gin.Context, u *app.User, dontResponse, dontCheck bool) {
|
||||
// 需要跳过微信绑定检验
|
||||
if !dontCheck {
|
||||
// 检查微信绑定
|
||||
if u.WechatOpenId == nil || *u.WechatOpenId == "" {
|
||||
if u.WechatOpenId == "" {
|
||||
log.Errorf("%v 未绑定微信", u.Nickname)
|
||||
response.FailWithMessage("请先绑定微信", ctx)
|
||||
ctx.Abort()
|
||||
|
@ -57,7 +57,7 @@ func (u *UserApi) BindingWeChat(ctx *gin.Context) {
|
||||
|
||||
// 解析成功,修改用户信息
|
||||
loginUser.WechatUnionId = &unionId
|
||||
loginUser.WechatOpenId = &openId
|
||||
loginUser.WechatOpenId = openId
|
||||
if err = userService.UpdateUserInfo(&loginUser); err != nil {
|
||||
log.Errorf("修改用户信息失败:%s", err.Error())
|
||||
r.FailWithMessage("系统错误,请稍后再试", ctx)
|
||||
|
@ -10,7 +10,7 @@ type User struct {
|
||||
global.GVA_MODEL
|
||||
Phone string `json:"phone" gorm:"index:deleted;type:varchar(255) not null comment '手机号'"`
|
||||
WechatUnionId *string `json:"wechat_union_id" gorm:"type:varchar(255) comment '微信UnionId'"`
|
||||
WechatOpenId *string `json:"wechat_open_id" gorm:"type:varchar(255) comment '微信OpenId'"`
|
||||
WechatOpenId string `json:"wechat_open_id" gorm:"type:varchar(255) comment '微信OpenId'"`
|
||||
Nickname string `json:"nickname" gorm:"type:varchar(255) comment '昵称'"`
|
||||
Avatar string `json:"avatar" gorm:"type:varchar(255) comment '头像'"`
|
||||
Status constant.UserStatus `json:"status" gorm:"type:enum('NORMAL','DISABLE'); default:'NORMAL'; not null; comment:'状态 NORMAL-正常;DISABLE-禁用'"`
|
||||
|
17
task/task.go
17
task/task.go
@ -13,10 +13,23 @@ func InitTask() {
|
||||
WxTask = gocron.NewScheduler(time.Local)
|
||||
|
||||
// 每天凌晨1点执行
|
||||
_, _ = WxTask.Every(1).Day().At("01:00").Do(CheckUserSurgeryDate) // 检查用户是否已到手术日期
|
||||
_, _ = WxTask.Every(1).Day().At("01:00").Do(SendMsg2User)
|
||||
_, err := WxTask.Every(1).Day().At("01:00").Do(CheckUserSurgeryDate) // 检查用户是否已到手术日期
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("检查用户是否已到手术日期定时任务创建失败", zap.Error(err))
|
||||
}
|
||||
_, err = WxTask.Every(1).Day().At("01:00").Do(SendMsg2User)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("发送消息定时任务创建失败", zap.Error(err))
|
||||
}
|
||||
|
||||
//_, err = WxTask.Every("1m").Do(MiniappSendMsg, "o9Fq_6_cYKvOWnyUM3McC11hWsTI", "测试", "测试", "测试")
|
||||
_, err = WxTask.Every("1m").Do(SendMsg2User)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("检查用户是否已到手术日期定时任务创建失败", zap.Error(err))
|
||||
}
|
||||
|
||||
// 开启定时任务
|
||||
WxTask.StartAsync()
|
||||
global.GVA_LOG.Info("定时任务已开启", zap.Any("定时任务状态:", WxTask.IsRunning()))
|
||||
global.GVA_LOG.Info("定时任务已开启", zap.Any("任务数量:", WxTask.Len()))
|
||||
}
|
||||
|
17
task/user.go
17
task/user.go
@ -5,11 +5,15 @@ import (
|
||||
"miniapp/global"
|
||||
"miniapp/model/app"
|
||||
"miniapp/model/common"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// CheckUserSurgeryDate 检查用户是否已到手术日期
|
||||
func CheckUserSurgeryDate() {
|
||||
go func() {
|
||||
global.GVA_LOG.Info("检查用户是否已到手术日期")
|
||||
var users []app.User
|
||||
global.GVA_DB.Model(&app.User{}).Find(&users)
|
||||
for _, user := range users {
|
||||
@ -18,11 +22,12 @@ func CheckUserSurgeryDate() {
|
||||
global.GVA_DB.Model(&app.User{}).Where("id = ?", user.ID).Updates(app.User{IsSurgery: 1})
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func SendMsg2User() {
|
||||
WxTask.Clear()
|
||||
|
||||
go func() {
|
||||
global.GVA_LOG.Info("执行发送消息定时任务")
|
||||
var ut []common.UserTodo
|
||||
err := global.GVA_DB.Model(&common.UserTodo{}).Find(&ut).Error
|
||||
if err != nil {
|
||||
@ -30,8 +35,7 @@ func SendMsg2User() {
|
||||
}
|
||||
|
||||
for _, todo := range ut {
|
||||
parse, _ := time.Parse("2006-01-02", todo.RemindDay)
|
||||
if time.Now().Sub(parse).Hours()/24 == 0 {
|
||||
if strings.Compare(todo.RemindDay, time.Now().Format("2006-01-02")) == 0 {
|
||||
// 创建定时任务
|
||||
//WxTask := gocron.NewScheduler(time.Local)
|
||||
//Send(todo.UserId, todo.Content, todo.RemindTime, todo.Frequency)
|
||||
@ -45,7 +49,9 @@ func SendMsg2User() {
|
||||
// 根据用户手术信息 发送提醒消息
|
||||
if user.IsSurgery == 0 && todo.RemindPeriod == 0 && todo.IsFinish == 0 {
|
||||
// 根据年月日时发送消息
|
||||
_, err := WxTask.Every(1).Day().At(todo.RemindTime).Do(MiniappSendMsg, *user.WechatOpenId, todo.Content, todo.RemindPeriod, todo.RemindTime)
|
||||
//job, err := WxTask.Every(1).Day().At(todo.RemindTime).Do(MiniappSendMsg, *user.WechatOpenId, todo.Content, todo.RemindPeriod, todo.RemindTime)
|
||||
job, err := WxTask.Every(1).Day().At(todo.RemindTime).Do(MiniappSendMsg, strconv.Itoa(int(user.ID)), strconv.Itoa(int(todo.ID)))
|
||||
global.GVA_LOG.Info("定时任务创建成功", zap.Any("job", job))
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("定时任务创建失败:%s", zap.Error(err))
|
||||
return
|
||||
@ -53,5 +59,6 @@ func SendMsg2User() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ func SendMsg(userId int) {
|
||||
parse, _ := time.Parse("2006-01-02", user.SurgeryTime)
|
||||
if time.Now().Sub(parse).Hours()/24 <= -3 {
|
||||
for _, todo := range userTodo {
|
||||
jbo, _ := WxTask.Every(1).Day().At(todo.RemindTime).Do(MiniappSendMsg, *user.WechatOpenId, todo.Content, todo.RemindPeriod, todo.RemindTime) // 检查用户待办事项
|
||||
jbo, _ := WxTask.Every(1).Day().At(todo.RemindTime).Do(MiniappSendMsg, user.WechatOpenId, todo.Content, todo.RemindPeriod, todo.RemindTime) // 检查用户待办事项
|
||||
s := strings.Split("一天3次", "")[2]
|
||||
// 将s转为int类型
|
||||
atoi, _ := strconv.Atoi(s)
|
||||
@ -52,24 +52,39 @@ func SendMsg(userId int) {
|
||||
}
|
||||
}
|
||||
|
||||
func MiniappSendMsg(openId string, context string, frequency string, remindTime string) {
|
||||
func MiniappSendMsg(userId string, utId string) {
|
||||
sdk := weapp.NewClient(global.GVA_CONFIG.MiniApp.AppId, global.GVA_CONFIG.MiniApp.AppSecret)
|
||||
go func() {
|
||||
|
||||
user := app.User{}
|
||||
err := global.GVA_DB.Model(&app.User{}).Where("id = ?", userId).Find(&user).Error
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("定时任务获取用户信息失败:%s", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
var ut common.UserTodo
|
||||
err = global.GVA_DB.Model(&common.UserTodo{}).Where("id = ?", utId).First(&ut).Error
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("定时任务获取用户Todo列表失败:", zap.Error(err))
|
||||
}
|
||||
|
||||
var msgDatas []msg.SendRequest
|
||||
|
||||
//判断context是否超过20个字符
|
||||
if len(context)/3 >= 19 {
|
||||
text := strings.Split(context, "药品")
|
||||
if len(ut.Content)/3 >= 19 {
|
||||
text := strings.Split(ut.Content, "药品")
|
||||
for i := 1; i < len(text); i++ {
|
||||
msgData := msg.SendRequest{
|
||||
ToUser: openId,
|
||||
ToUser: user.WechatOpenId,
|
||||
TemplateID: "PgxoZOOSDgBcmIGd_EVLDnYUmL3eu6NQTAZCsHQeuWY",
|
||||
Page: "/page/index/todo",
|
||||
MiniprogramState: msg.MiniprogramStateTrial,
|
||||
Data: msg.SendData{
|
||||
"thing1": msg.SendValue{Value: text[0] + text[i]},
|
||||
"time2": msg.SendValue{Value: remindTime},
|
||||
"short_thing17": msg.SendValue{Value: frequency},
|
||||
"time15": msg.SendValue{Value: time.DateTime},
|
||||
"thing1": msg.SendValue{Value: text[i]},
|
||||
"time2": msg.SendValue{Value: ut.RemindTime},
|
||||
"short_thing17": msg.SendValue{Value: ut.Frequency},
|
||||
"time15": msg.SendValue{Value: time.Now().Format("2006-01-02 15:04:05")},
|
||||
},
|
||||
}
|
||||
|
||||
@ -77,15 +92,15 @@ func MiniappSendMsg(openId string, context string, frequency string, remindTime
|
||||
}
|
||||
} else {
|
||||
msgData := msg.SendRequest{
|
||||
ToUser: openId,
|
||||
ToUser: user.WechatOpenId,
|
||||
TemplateID: "PgxoZOOSDgBcmIGd_EVLDnYUmL3eu6NQTAZCsHQeuWY",
|
||||
Page: "/page/index/todo",
|
||||
MiniprogramState: msg.MiniprogramStateTrial,
|
||||
Data: msg.SendData{
|
||||
"thing1": msg.SendValue{Value: context},
|
||||
"time2": msg.SendValue{Value: remindTime},
|
||||
"short_thing17": msg.SendValue{Value: frequency},
|
||||
"time15": msg.SendValue{Value: time.DateTime},
|
||||
"thing1": msg.SendValue{Value: ut.Content},
|
||||
"time2": msg.SendValue{Value: ut.RemindTime},
|
||||
"short_thing17": msg.SendValue{Value: ut.Frequency},
|
||||
"time15": msg.SendValue{Value: time.Now().Format("2006-01-02 15:04:05")},
|
||||
},
|
||||
}
|
||||
|
||||
@ -115,5 +130,6 @@ func MiniappSendMsg(openId string, context string, frequency string, remindTime
|
||||
}
|
||||
global.GVA_LOG.Info("发送成功: %#v", zap.Any("返回结果: %#v", send))
|
||||
}
|
||||
}()
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user