JM-WechatMini/task/wxMsg.go
2024-03-21 21:08:45 +08:00

84 lines
2.4 KiB
Go

package task
import (
"fmt"
"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(openId string, context string, frequency string, remindTime string) {
sdk := weapp.NewClient(global.GVA_CONFIG.MiniApp.AppId, global.GVA_CONFIG.MiniApp.AppSecret)
msgData := msg.SendRequest{
ToUser: openId,
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},
},
}
send, err := sdk.NewSubscribeMessage().Send(&msgData)
if err != nil {
return
}
err = send.GetResponseError()
if err != nil {
fmt.Printf("微信返回错误: %#v", err)
return
}
fmt.Printf("返回结果: %#v", send)
}