JM-WechatMini/api/v1/app/user.go
2024-01-11 16:03:51 +08:00

140 lines
3.4 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 (
"git.echol.cn/loser/logger/log"
"github.com/gin-gonic/gin"
"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 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)
}