2023-04-24 17:19:41 +08:00
|
|
|
|
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)
|
2023-04-27 15:56:12 +08:00
|
|
|
|
|
2023-04-24 17:19:41 +08:00
|
|
|
|
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
|
|
|
|
|
}
|
2023-04-27 15:56:12 +08:00
|
|
|
|
//log.Debugf("用户[%v]的UnionId为[%v],OpenId为[%v]", loginUser.Id, unionId, openId)
|
|
|
|
|
//if repository.User().CheckUnionIdIsExist(unionId, openId) {
|
|
|
|
|
// core.R(ctx).FailWithMessage("该微信已绑定其他账号")
|
|
|
|
|
// return
|
|
|
|
|
//}
|
2023-04-24 17:19:41 +08:00
|
|
|
|
// 解析成功,修改用户信息
|
|
|
|
|
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
|
|
|
|
|
}
|
2023-04-27 15:56:12 +08:00
|
|
|
|
// 操作成功,更新头像和昵称
|
2023-04-24 17:19:41 +08:00
|
|
|
|
core.R(ctx).Ok()
|
|
|
|
|
}
|
2023-04-27 17:36:07 +08:00
|
|
|
|
|
|
|
|
|
// GetLikeWineList 获取我喜欢的酒单列表
|
|
|
|
|
func (u userApi) GetLikeWineList(ctx *gin.Context) {
|
|
|
|
|
var p param.GetLikeWineList
|
|
|
|
|
if err := ctx.ShouldBind(&p); err != nil {
|
|
|
|
|
core.R(ctx).FailWithMessage("参数错误: " + err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取我喜欢的酒单列表
|
|
|
|
|
wines, err := repository.User().GetLikeWineList(&p)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorf("获取我喜欢的酒单列表失败:%s", err.Error())
|
|
|
|
|
core.R(ctx).FailWithMessage("系统错误,请稍后再试")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
core.R(ctx).OkWithData(wines)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteLikeWine 删除我喜欢的酒单
|
|
|
|
|
func (u userApi) DeleteLikeWine(ctx *gin.Context) {
|
|
|
|
|
var p param.DeleteLikeWine
|
|
|
|
|
if err := ctx.ShouldBind(&p); err != nil {
|
|
|
|
|
core.R(ctx).FailWithMessage("参数错误: " + err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除我喜欢的酒单
|
|
|
|
|
if err := repository.User().DeleteLikeWine(&p); err != nil {
|
|
|
|
|
log.Errorf("删除我喜欢的酒单失败:%s", err.Error())
|
|
|
|
|
core.R(ctx).FailWithMessage("系统错误,请稍后再试")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
core.R(ctx).Ok()
|
|
|
|
|
}
|