diff --git a/api/controller/User.go b/api/controller/User.go index 0c4c0a5..262ae49 100644 --- a/api/controller/User.go +++ b/api/controller/User.go @@ -4,7 +4,11 @@ import ( "github.com/gin-gonic/gin" "hepa-calc-api/api/dao" "hepa-calc-api/api/dto" + "hepa-calc-api/api/requests" "hepa-calc-api/api/responses" + "hepa-calc-api/api/service" + "hepa-calc-api/global" + "hepa-calc-api/utils" ) type User struct{} @@ -18,17 +22,66 @@ func (r *User) GetUser(c *gin.Context) { user, err := userDao.GetUserById(userId) if err != nil || user == nil { responses.FailWithMessage("用户数据错误", c) - c.Abort() return } if user.UserStatus == 2 { responses.FailWithMessage("用户已禁用", c) - c.Abort() return } g := dto.GetUserDto(user) + // 加载数据-生日 + g.LoadBirthday(user.Birthday) + responses.OkWithData(g, c) } + +// GetUserInfo 获取用户数据-详情信息 +func (r *User) GetUserInfo(c *gin.Context) { + userId := c.GetInt64("UserId") + + // 获取用户数据 + userInfoDao := dao.UserInfoDao{} + userInfo, err := userInfoDao.GetUserInfoByUserId(userId) + if err != nil || userInfo == nil { + responses.FailWithMessage("用户数据错误", c) + return + } + + g := dto.GetUserInfoDto(userInfo) + + // 加载数据-预产期 + g.LoadExpectedDate(userInfo.ExpectedDate) + + responses.OkWithData(g, c) +} + +// PutUser 修改用户数据-基本信息 +func (r *User) PutUser(c *gin.Context) { + userRequest := requests.UserRequest{} + req := userRequest.PutUser + if err := c.ShouldBind(&req); err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + // 参数验证 + if err := global.Validate.Struct(req); err != nil { + responses.FailWithMessage(utils.Translate(err), c) + return + } + + userId := c.GetInt64("UserId") + + // 业务处理 + userService := service.UserService{} + res, err := userService.PutUser(userId, req) + if err != nil || res != true { + responses.FailWithMessage(err.Error(), c) + return + } + + responses.Ok(c) +} diff --git a/api/dto/User.go b/api/dto/User.go index 0c64315..b8dd5b3 100644 --- a/api/dto/User.go +++ b/api/dto/User.go @@ -4,6 +4,7 @@ import ( "fmt" "hepa-calc-api/api/model" "hepa-calc-api/utils" + "time" ) // UserDto 用户表 @@ -15,7 +16,7 @@ type UserDto struct { RegisterSource int `json:"register_source"` // 注册来源(1:app注册 2:公众号注册) OpenId string `json:"open_id"` // 用户微信标识 UnionId string `json:"union_id"` // 微信开放平台标识 - Birthday *model.LocalTime `json:"birthday"` // 出生日期 + Birthday *string `json:"birthday"` // 出生日期 Age *uint `json:"age"` // 年龄 Sex *int `json:"sex"` // 性别(0:未知 1:男 2:女) Avatar string `json:"avatar"` // 头像 @@ -35,7 +36,6 @@ func GetUserDto(m *model.User) *UserDto { Mobile: m.Mobile, UserStatus: m.UserStatus, RegisterSource: m.RegisterSource, - Birthday: m.Birthday, Age: m.Age, Sex: m.Sex, Avatar: utils.AddOssDomain(m.Avatar), @@ -47,3 +47,13 @@ func GetUserDto(m *model.User) *UserDto { UpdatedAt: m.UpdatedAt, } } + +// LoadBirthday 加载数据-生日 +func (r *UserDto) LoadBirthday(birthday *model.LocalTime) *UserDto { + if birthday != nil { + t := time.Time(*birthday).Format("2006-01-02") + r.Birthday = &t + } + + return r +} diff --git a/api/dto/UserInfo.go b/api/dto/UserInfo.go new file mode 100644 index 0000000..b37e58c --- /dev/null +++ b/api/dto/UserInfo.go @@ -0,0 +1,58 @@ +package dto + +import ( + "fmt" + "hepa-calc-api/api/model" + "time" +) + +// UserInfoDto 用户表-基础信息 +type UserInfoDto struct { + UserInfoId string `json:"user_info_id"` // 主键id + UserId string `json:"user_id"` // 用户id + Height string `json:"height"` // 身高(cm) + Weight string `json:"weight"` // 体重(kg) + NationId string `json:"nation_id"` // 民族id + IsFamilyHistory *int `json:"is_family_history"` // 是否存在家族病史(0:未知 1:是 2:否) + IsPregnant *int `json:"is_pregnant"` // 是否怀孕(1:无计划 2:计划中 3:已怀孕 4:家有宝宝) + ExpectedDate *string `json:"expected_date"` // 预产期 + ProvinceId *int `json:"province_id"` // 省份id + Province string `json:"province"` // 省份 + CityId *int `json:"city_id"` // 城市id + City string `json:"city"` // 城市 + CountyId *int `json:"county_id"` // 区县id + County string `json:"county"` // 区县 + CreatedAt model.LocalTime `json:"created_at"` // 创建时间 + UpdatedAt model.LocalTime `json:"updated_at"` // 更新时间 +} + +// GetUserInfoDto 用户基础信息详情 +func GetUserInfoDto(m *model.UserInfo) *UserInfoDto { + return &UserInfoDto{ + UserInfoId: fmt.Sprintf("%d", m.UserInfoId), + UserId: fmt.Sprintf("%d", m.UserInfoId), + Height: m.Height, + Weight: m.Weight, + NationId: fmt.Sprintf("%d", m.NationId), + IsFamilyHistory: m.IsFamilyHistory, + IsPregnant: m.IsPregnant, + ProvinceId: m.ProvinceId, + Province: m.Province, + CityId: m.CityId, + City: m.City, + CountyId: m.CountyId, + County: m.County, + CreatedAt: m.CreatedAt, + UpdatedAt: m.UpdatedAt, + } +} + +// LoadExpectedDate 加载数据-预产期 +func (r *UserInfoDto) LoadExpectedDate(expectedDate *model.LocalTime) *UserInfoDto { + if expectedDate != nil { + t := time.Time(*expectedDate).Format("2006-01-02") + r.ExpectedDate = &t + } + + return r +} diff --git a/api/requests/User.go b/api/requests/User.go index 9011e7b..6f3ff6a 100644 --- a/api/requests/User.go +++ b/api/requests/User.go @@ -6,10 +6,21 @@ type UserRequest struct { // PutUser 修改用户数据-基本信息 type PutUser struct { - UserName string `json:"user_name" form:"user_name" label:"用户名称" validate:"required"` - Mobile string `json:"mobile" form:"mobile" label:"手机号" validate:"required,Mobile"` - RegisterSource int `json:"register_source" form:"register_source" label:"注册来源" validate:"required,oneof=1 2"` - Age uint `json:"age" form:"age" label:"年龄" validate:"omitempty,min=1,max=120"` - Sex uint `json:"sex" form:"sex" label:"性别" validate:"omitempty,oneof=1 2"` - Avatar string `json:"avatar" form:"avatar" label:"头像"` + UserName string `json:"user_name" form:"user_name" label:"用户名称" validate:"required"` + RegisterSource int `json:"register_source" form:"register_source" label:"注册来源" validate:"required,oneof=1 2"` + Birthday string `json:"birthday" form:"birthday" label:"出生日期" validate:"required,min=1,max=120"` + Sex int `json:"sex" form:"sex" label:"性别" validate:"required,oneof=1 2"` + Avatar string `json:"avatar" form:"avatar" label:"头像"` + Height string `json:"height" form:"height" label:"身高(cm)"` + Weight string `json:"weight" form:"weight" label:"体重(kg)"` + NationId string `json:"nation_id" form:"nation_id" label:"民族id"` // 从int64转换为可选字符串 + IsFamilyHistory *int `json:"is_family_history" form:"is_family_history" label:"是否存在家族病史" validate:"omitempty,oneof=0 1 2"` + IsPregnant *int `json:"is_pregnant" form:"is_pregnant" label:"是否怀孕"` + ExpectedDate string `json:"expected_date" form:"expected_date" label:"预产期"` // 假设转换为可选字符串 + ProvinceId string `json:"province_id" form:"province_id" label:"省份id" validate:"required"` // 从int转换为可选字符串 + Province string `json:"province" form:"province" label:"省份" validate:"required"` + CityId string `json:"city_id" form:"city_id" label:"城市id" validate:"required"` // 从int转换为可选字符串 + City string `json:"city" form:"city" label:"城市" validate:"required"` + CountyId string `json:"county_id" form:"county_id" label:"区县id"` // 从int转换为可选字符串 + County string `json:"county" form:"county" label:"区县"` } diff --git a/api/router/router.go b/api/router/router.go index fa02b84..61aa336 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -174,6 +174,12 @@ func privateRouter(r *gin.Engine, api controller.Api) { { // 获取用户数据-基本信息 centerGroup.GET("", api.User.GetUser) + + // 获取用户数据-详情信息 + centerGroup.GET("/info", api.User.GetUserInfo) + + // 修改用户数据-基本信息 + centerGroup.PUT("", api.User.PutUser) } // 优惠卷 diff --git a/api/service/User.go b/api/service/User.go index 38e37f7..d77d219 100644 --- a/api/service/User.go +++ b/api/service/User.go @@ -6,8 +6,10 @@ import ( "gorm.io/gorm" "hepa-calc-api/api/dao" "hepa-calc-api/api/model" + "hepa-calc-api/api/requests" "hepa-calc-api/extend/aliyun" "hepa-calc-api/extend/app" + "hepa-calc-api/global" "hepa-calc-api/utils" "io" "math/rand" @@ -203,6 +205,12 @@ func (r *UserService) HandleAppUserInfo(tx *gorm.DB, user *model.User, userInfo // 性别 if appUserInfo.Data.Sex != nil { + if *appUserInfo.Data.Sex == 0 { + *appUserInfo.Data.Sex = 1 + } else if *appUserInfo.Data.Sex == 1 { + *appUserInfo.Data.Sex = 2 + } + if user.Sex != nil { if *appUserInfo.Data.Sex != *user.Sex { userData["sex"] = appUserInfo.Data.Sex @@ -606,3 +614,71 @@ func (r *UserService) HandleAppUserCase(tx *gorm.DB, user *model.User) error { return nil } + +// PutUser 修改用户数据-基本信息 +func (r *UserService) PutUser(userId int64, req requests.PutUser) (bool, error) { + //// 获取用户数据 + //userDao := dao.UserDao{} + //user, err := userDao.GetUserById(userId) + //if err != nil { + // return false, errors.New("用户数据错误") + //} + // + //// 获取用户数据 + //userInfoDao := dao.UserInfoDao{} + //userInfo, err := userInfoDao.GetUserInfoByUserId(userId) + //if err != nil { + // return false, errors.New("用户数据错误") + //} + // + //userData := make(map[string]interface{}) + //userInfoData := make(map[string]interface{}) + //appUserData := make(map[string]interface{}) + // + //// 用户名称 + //if req.UserName != user.UserName { + // userData["user_name"] = req.UserName + // appUserData["name"] = req.UserName + //} + // + //// 注册来源 + //if req.RegisterSource != user.RegisterSource { + // userData["register_source"] = req.UserName + //} + // + //// 出生日期 + //birthday := time.Time(*user.Birthday).Format("2006-01-02") + //if req.Birthday != birthday { + // userData["birthday"] = req.Birthday + // appUserData["birthday"] = req.Birthday + // + // // 年龄 + // age, err := utils.CalculateAge(req.Birthday) + // if err != nil { + // return false, errors.New("年龄错误") + // } + // userData["age"] = age + //} + // + //// 性别 + //if req.Sex != *user.Sex { + // userData["sex"] = req.Sex + // + // if req.Sex == 1 { + // appUserData["sex"] = 0 + // } else if req.Sex == 2 { + // appUserData["sex"] = 1 + // } + //} + + // 开始事务 + tx := global.Db.Begin() + defer func() { + if r := recover(); r != nil { + tx.Rollback() + } + }() + + tx.Commit() + return true, nil +}