✨ init project
This commit is contained in:
104
api/app/user.go
Normal file
104
api/app/user.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"Lee-WineList/api"
|
||||
"Lee-WineList/common/constant"
|
||||
"Lee-WineList/core"
|
||||
"Lee-WineList/model/entity"
|
||||
"Lee-WineList/model/param"
|
||||
"Lee-WineList/model/vo"
|
||||
"Lee-WineList/repository"
|
||||
"Lee-WineList/utils"
|
||||
"git.echol.cn/loser/logger/log"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type userApi struct {
|
||||
}
|
||||
|
||||
// UserApi 暴露接口
|
||||
func UserApi() *userApi {
|
||||
return &userApi{}
|
||||
}
|
||||
|
||||
// GetUser 获取当前登录用户信息
|
||||
func (u userApi) GetUser(ctx *gin.Context) {
|
||||
// 取出当前登录用户
|
||||
var ue entity.User
|
||||
if api.GetUser(ctx, &ue, false, true); ctx.IsAborted() {
|
||||
return
|
||||
}
|
||||
// 转换为VO
|
||||
var v vo.UserVO
|
||||
v.ParseOrdinary(ue)
|
||||
core.R(ctx).OkWithData(v)
|
||||
}
|
||||
|
||||
// BindingWeChat 绑定微信
|
||||
func (u userApi) BindingWeChat(ctx *gin.Context) {
|
||||
var p param.BindingWeChat
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
core.R(ctx).FailWithMessage("参数错误" + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 取出当前登录用户
|
||||
var loginUser entity.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())
|
||||
core.R(ctx).FailWithMessage("系统错误,请稍后再试")
|
||||
return
|
||||
}
|
||||
log.Debugf("用户[%v]的UnionId为[%v],OpenId为[%v]", loginUser.Id, unionId, openId)
|
||||
if repository.User().CheckUnionIdIsExist(unionId, openId) {
|
||||
core.R(ctx).FailWithMessage("该微信已绑定其他账号")
|
||||
return
|
||||
}
|
||||
// 解析成功,修改用户信息
|
||||
loginUser.WechatUnionId = &unionId
|
||||
loginUser.WechatOpenId = &openId
|
||||
if err = repository.User().UpdateUserInfo(&loginUser); err != nil {
|
||||
log.Errorf("修改用户信息失败:%s", err.Error())
|
||||
core.R(ctx).FailWithMessage("系统错误,请稍后再试")
|
||||
return
|
||||
}
|
||||
core.R(ctx).Ok()
|
||||
}
|
||||
|
||||
// UpdateUser 修改用户信息
|
||||
func (u userApi) UpdateUser(ctx *gin.Context) {
|
||||
var p param.ChangeUserInfo
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
core.R(ctx).FailWithMessage("参数错误: " + err.Error())
|
||||
return
|
||||
}
|
||||
// 获取当前登录用户
|
||||
var loginUser entity.User
|
||||
if api.GetUser(ctx, &loginUser, false, true); ctx.IsAborted() {
|
||||
return
|
||||
}
|
||||
// 修改资料
|
||||
if p.Sex != constant.UserSexNone {
|
||||
loginUser.Sex = p.Sex
|
||||
}
|
||||
if p.Nickname != "" {
|
||||
loginUser.Nickname = p.Nickname
|
||||
}
|
||||
if p.Avatar != "" && strings.HasPrefix(p.Avatar, "http") {
|
||||
loginUser.Avatar = p.Avatar
|
||||
}
|
||||
// 修改数据
|
||||
if err := repository.User().UpdateUserInfo(&loginUser); err != nil {
|
||||
log.Errorf("修改用户信息失败:%s", err.Error())
|
||||
core.R(ctx).FailWithMessage("修改用户信息失败: " + err.Error())
|
||||
return
|
||||
}
|
||||
core.R(ctx).Ok()
|
||||
}
|
Reference in New Issue
Block a user