JM-WechatMini/task/wxMsg.go
2024-04-02 15:02:46 +08:00

136 lines
4.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 {
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)
if jbo.RunCount() == atoi {
WxTask.Remove(jbo)
}
}
}
}
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))
return
}
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",
Page: "/pages/index/done",
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 {
msgData := msg.SendRequest{
ToUser: user.WechatOpenId,
TemplateID: "PgxoZOOSDgBcmIGd_EVLDnYUmL3eu6NQTAZCsHQeuWY",
Page: "/pages/index/done",
MiniprogramState: msg.MiniprogramStateTrial,
Data: msg.SendData{
"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")},
},
}
send, err := sdk.NewSubscribeMessage().Send(&msgData)
if err != nil {
return
}
err = send.GetResponseError()
if err != nil {
global.GVA_LOG.Error("发送失败: %#v", zap.Any("返回结果: %#v", send))
return
}
global.GVA_LOG.Info("发送成功: %#v", zap.Any("返回结果: %#v", send))
}
for i := 0; i < len(msgDatas); i++ {
send, err := sdk.NewSubscribeMessage().Send(&msgDatas[i])
if err != nil {
return
}
err = send.GetResponseError()
if err != nil {
global.GVA_LOG.Error("发送失败: %#v", zap.Any("返回结果: %#v", send))
return
}
global.GVA_LOG.Info("发送成功: %#v", zap.Any("返回结果: %#v", send))
}
}()
}