You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.2 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package app
import (
"git.echol.cn/loser/logger/log"
"github.com/gin-gonic/gin"
"miniapp/api"
"miniapp/global"
"miniapp/model/app"
"miniapp/model/app/request"
"miniapp/model/app/response"
r "miniapp/model/common/response"
"miniapp/utils"
"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
}
// 获取用户信息
userInfo, err := userService.GetUserInfo(id)
if err != nil {
log.Errorf("获取用户信息失败:%s", err.Error())
r.FailWithMessage("获取用户信息失败"+err.Error(), ctx)
return
}
r.OkWithData(userInfo, ctx)
}