JM-WechatMini/api/v1/app/user.go
2024-03-30 22:58:42 +08:00

193 lines
4.9 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 app
import (
"fmt"
"git.echol.cn/loser/logger/log"
"github.com/gin-gonic/gin"
"github.com/medivhzhan/weapp/v3"
msg "github.com/medivhzhan/weapp/v3/subscribemessage"
"miniapp/api"
"miniapp/model/app"
"miniapp/model/app/request"
"miniapp/model/app/response"
r "miniapp/model/common/response"
"miniapp/utils"
"strconv"
"strings"
)
type UserApi struct {
}
// GetUser 获取当前登录用户信息
func (u *UserApi) GetUser(ctx *gin.Context) {
// 取出当前登录用户
var ue app.User
if api.GetUser(ctx, &ue, false, true); ctx.IsAborted() {
return
}
// 转换为VO
var v response.UserVO
v.ParseOrdinary(ue)
r.OkWithData(v, ctx)
}
// BindingWeChat 绑定微信
func (u *UserApi) BindingWeChat(ctx *gin.Context) {
var p request.BindingWeChat
if err := ctx.ShouldBind(&p); err != nil {
r.FailWithMessage("参数错误"+err.Error(), ctx)
return
}
// 取出当前登录用户
var loginUser app.User
if api.GetUser(ctx, &loginUser, true, true); ctx.IsAborted() {
return
}
// 解析出UnionId和OpenId
unionId, openId, _, err := utils.WeChatUtils().GetWechatUnionId(p.Code)
if err != nil {
log.Errorf("获取微信UnionId失败%s", err.Error())
r.FailWithMessage("系统错误,请稍后再试", ctx)
return
}
// 解析成功,修改用户信息
loginUser.WechatUnionId = &unionId
loginUser.WechatOpenId = openId
if err = userService.UpdateUserInfo(&loginUser); err != nil {
log.Errorf("修改用户信息失败:%s", err.Error())
r.FailWithMessage("系统错误,请稍后再试", ctx)
return
}
r.Ok(ctx)
}
// UpdateUser 修改用户信息
func (u *UserApi) UpdateUser(ctx *gin.Context) {
var p request.ChangeUserInfo
if err := ctx.ShouldBind(&p); err != nil {
r.FailWithMessage("参数错误: "+err.Error(), ctx)
return
}
// 获取当前登录用户
var loginUser app.User
if api.GetUser(ctx, &loginUser, false, true); ctx.IsAborted() {
return
}
// 修改资料
if p.Nickname != "" {
loginUser.Nickname = p.Nickname
}
if p.Avatar != "" && strings.HasPrefix(p.Avatar, "http") {
loginUser.Avatar = p.Avatar
}
if p.Phone != "" {
loginUser.Phone = p.Phone
}
if p.SurgeryTime != "" {
//t, _ := time.Parse("2006-01-02", p.SurgeryTime)
//format := t.UTC().Format("2006-01-02")
loginUser.SurgeryTime = p.SurgeryTime
}
loginUser.IsSurgery = p.IsSurgery
loginUser.HospitalId = p.HospitalId
// 修改数据
if err := userService.UpdateUserInfo(&loginUser); err != nil {
log.Errorf("修改用户信息失败:%s", err.Error())
r.FailWithMessage("修改用户信息失败"+err.Error(), ctx)
return
}
// 操作成功,更新头像和昵称
r.Ok(ctx)
}
// UpdateUserHospital 修改用户医院信息
func (u *UserApi) UpdateUserHospital(ctx *gin.Context) {
var p request.ChangeUserHospital
if err := ctx.ShouldBind(&p); err != nil {
r.FailWithMessage("参数错误: "+err.Error(), ctx)
return
}
// 修改数据
if err := userService.UpdateUserHospital(&p); err != nil {
log.Errorf("修改用户信息失败:%s", err.Error())
r.FailWithMessage("修改用户信息失败"+err.Error(), ctx)
return
}
// 操作成功,更新头像和昵称
r.Ok(ctx)
}
// GetInfo 获取用户信息
func (u *UserApi) GetInfo(ctx *gin.Context) {
//id := ctx.Param("id")
//if id == "" {
// global.GVA_LOG.Error("参数错误")
// r.FailWithMessage("参数错误", ctx)
// return
//}
var ue app.User
if api.GetUser(ctx, &ue, false, true); ctx.IsAborted() {
return
}
// 获取用户信息
userInfo, err := userService.GetUserInfo(strconv.Itoa(int(ue.ID)))
if err != nil {
log.Errorf("获取用户信息失败:%s", err.Error())
r.FailWithMessage("获取用户信息失败"+err.Error(), ctx)
return
}
r.OkWithData(userInfo, ctx)
}
// SendMedicineRemind 发送用药提醒消息
func (u *UserApi) SendMedicineRemind(ctx *gin.Context) {
var req utils.SendRequest
if err := ctx.ShouldBind(&req); err != nil {
r.FailWithMessage("参数错误: "+err.Error(), ctx)
return
}
//app-id: wxaaf66dbb5c3983b3
//app-secret: 0abba24dbff43febba1e551651f693b4
//template-id: PgxoZOOSDgBcmIGd_EVLDnYUmL3eu6NQTAZCsHQeuWY
sdk := weapp.NewClient("wxaaf66dbb5c3983b3", "0abba24dbff43febba1e551651f693b4")
msgData := msg.SendRequest{
ToUser: "o9Fq_6_cYKvOWnyUM3McC11hWsTI",
TemplateID: "PgxoZOOSDgBcmIGd_EVLDnYUmL3eu6NQTAZCsHQeuWY",
Page: "page/index/todo",
MiniprogramState: msg.MiniprogramStateTrial,
Data: msg.SendData{
"thing1": msg.SendValue{Value: "眼药水"},
"time2": msg.SendValue{Value: "20:00"},
"short_thing17": msg.SendValue{Value: "一天4次"},
"time15": msg.SendValue{Value: "2024-03-20 20:00"},
},
}
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)
r.Ok(ctx)
}