JM-WechatMini/task/wxMsg.go

136 lines
4.1 KiB
Go
Raw Normal View History

2024-03-21 21:08:45 +08:00
package task
import (
"github.com/medivhzhan/weapp/v3"
msg "github.com/medivhzhan/weapp/v3/subscribemessage"
"go.uber.org/zap"
"miniapp/global"
"miniapp/model/app"
"miniapp/model/common"
"strconv"
"strings"
"time"
)
func SendMsg(userId int) {
var user app.User
err := global.GVA_DB.Model(&user).Where("id = ?", userId).Find(&user).Error
if err != nil {
global.GVA_LOG.Error("获取用户信息失败:%s", zap.Error(err))
return
}
// 获取用户待办列表
var userTodo []common.UserTodo
if user.IsSurgery == 0 {
err = global.GVA_DB.Model(&userTodo).Where("user_id = ? and is_finish = ? and remind_period = 0", userId, 0).Find(&userTodo).Error
if err != nil {
global.GVA_LOG.Error("获取用户待办列表:%s", zap.Error(err))
return
}
} else {
err = global.GVA_DB.Model(&userTodo).Where("user_id = ? and is_finish = ? and remind_period = 1", userId, 0).Find(&userTodo).Error
if err != nil {
global.GVA_LOG.Error("获取用户待办列表:%s", zap.Error(err))
return
}
}
// 根据用户手术信息 发送提醒消息
parse, _ := time.Parse("2006-01-02", user.SurgeryTime)
if time.Now().Sub(parse).Hours()/24 <= -3 {
for _, todo := range userTodo {
2024-03-30 22:58:42 +08:00
jbo, _ := WxTask.Every(1).Day().At(todo.RemindTime).Do(MiniappSendMsg, user.WechatOpenId, todo.Content, todo.RemindPeriod, todo.RemindTime) // 检查用户待办事项
2024-03-21 21:08:45 +08:00
s := strings.Split("一天3次", "")[2]
// 将s转为int类型
atoi, _ := strconv.Atoi(s)
if jbo.RunCount() == atoi {
WxTask.Remove(jbo)
}
}
}
}
2024-03-30 22:58:42 +08:00
func MiniappSendMsg(userId string, utId string) {
2024-03-21 21:08:45 +08:00
sdk := weapp.NewClient(global.GVA_CONFIG.MiniApp.AppId, global.GVA_CONFIG.MiniApp.AppSecret)
2024-03-30 22:58:42 +08:00
go func() {
2024-03-21 21:08:45 +08:00
2024-03-30 22:58:42 +08:00
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))
2024-04-02 15:02:46 +08:00
return
2024-03-30 22:58:42 +08:00
}
var msgDatas []msg.SendRequest
//判断context是否超过20个字符
if len(ut.Content)/3 >= 19 {
text := strings.Split(ut.Content, "药品")
for i := 1; i < len(text); i++ {
msgData := msg.SendRequest{
ToUser: user.WechatOpenId,
TemplateID: "PgxoZOOSDgBcmIGd_EVLDnYUmL3eu6NQTAZCsHQeuWY",
2024-03-31 03:52:36 +08:00
Page: "/pages/index/done",
2024-03-30 22:58:42 +08:00
MiniprogramState: msg.MiniprogramStateTrial,
Data: msg.SendData{
"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")},
},
}
msgDatas = append(msgDatas, msgData)
}
} else {
2024-03-29 02:32:26 +08:00
msgData := msg.SendRequest{
2024-03-30 22:58:42 +08:00
ToUser: user.WechatOpenId,
2024-03-29 02:32:26 +08:00
TemplateID: "PgxoZOOSDgBcmIGd_EVLDnYUmL3eu6NQTAZCsHQeuWY",
2024-03-31 03:52:36 +08:00
Page: "/pages/index/done",
2024-03-29 02:32:26 +08:00
MiniprogramState: msg.MiniprogramStateTrial,
Data: msg.SendData{
2024-03-30 22:58:42 +08:00
"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")},
2024-03-29 02:32:26 +08:00
},
}
2024-03-21 21:08:45 +08:00
2024-03-30 22:58:42 +08:00
send, err := sdk.NewSubscribeMessage().Send(&msgData)
if err != nil {
return
}
2024-03-29 02:32:26 +08:00
2024-03-30 22:58:42 +08:00
err = send.GetResponseError()
if err != nil {
2024-04-02 15:02:46 +08:00
global.GVA_LOG.Error("发送失败: %#v", zap.Any("返回结果: %#v", send))
2024-03-30 22:58:42 +08:00
return
}
global.GVA_LOG.Info("发送成功: %#v", zap.Any("返回结果: %#v", send))
2024-03-29 02:32:26 +08:00
}
2024-03-21 21:08:45 +08:00
2024-03-30 22:58:42 +08:00
for i := 0; i < len(msgDatas); i++ {
send, err := sdk.NewSubscribeMessage().Send(&msgDatas[i])
if err != nil {
return
}
2024-03-29 02:32:26 +08:00
2024-03-30 22:58:42 +08:00
err = send.GetResponseError()
if err != nil {
2024-04-02 15:02:46 +08:00
global.GVA_LOG.Error("发送失败: %#v", zap.Any("返回结果: %#v", send))
2024-03-30 22:58:42 +08:00
return
}
global.GVA_LOG.Info("发送成功: %#v", zap.Any("返回结果: %#v", send))
2024-03-29 02:32:26 +08:00
}
2024-03-30 22:58:42 +08:00
}()
2024-03-21 21:08:45 +08:00
}