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) var msgDatas []msg.SendRequest //判断context是否超过20个字符 if len(context)/3 >= 19 { text := strings.Split(context, "药品") for i := 1; i < len(text); i++ { msgData := msg.SendRequest{ ToUser: openId, 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}, }, } msgDatas = append(msgDatas, msgData) } } else { 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 } 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 { fmt.Printf("微信返回错误: %#v", err) return } global.GVA_LOG.Info("发送成功: %#v", zap.Any("返回结果: %#v", send)) } }