修改了手机号登陆

This commit is contained in:
2024-08-15 14:37:05 +08:00
parent 8ee4dec11d
commit adb565257d
12 changed files with 648 additions and 35 deletions
+70 -19
View File
@@ -47,6 +47,15 @@ func (r *Login) LoginPhone(c *gin.Context) {
}
}
// 开始事务
tx := global.Db.Begin()
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
tx.Rollback()
}
}()
// 检测用户信息
userDao := dao.UserDao{}
maps := make(map[string]interface{})
@@ -54,15 +63,6 @@ func (r *Login) LoginPhone(c *gin.Context) {
user, _ := userDao.GetUser(maps)
// 新用户处理方式
if user == nil {
// 开始事务
tx := global.Db.Begin()
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
tx.Rollback()
}
}()
// 头像
avatar := "www.baidu.com"
@@ -73,18 +73,18 @@ func (r *Login) LoginPhone(c *gin.Context) {
// 新增用户
user = &model.User{
UserName: "",
AppIden: "",
Mobile: req.Mobile,
RegisterSource: req.Source,
OpenId: "",
UnionId: "",
Age: nil,
Sex: 0,
Sex: nil,
Avatar: avatar,
IsMember: 0,
MemberExpireDate: nil,
LoginIp: loginIp,
}
loginAt := model.LocalTime(time.Now())
user.LoginAt = &loginAt
@@ -95,17 +95,61 @@ func (r *Login) LoginPhone(c *gin.Context) {
return
}
fmt.Println(user)
// 新增用户详情
userInfo := &model.UserInfo{
UserId: user.UserId,
Height: "",
Weight: "",
NationId: nil,
IsFamilyHistory: nil,
IsPregnant: nil,
ExpectedDate: nil,
ProvinceId: nil,
Province: "",
CityId: nil,
City: "",
CountyId: nil,
County: "",
}
userInfoDao := dao.UserInfoDao{}
userInfo, err = userInfoDao.AddUserInfo(tx, userInfo)
if err != nil {
tx.Rollback()
responses.FailWithMessage(err.Error(), c)
return
}
// 获取app用户信息
//var result *string
//if result == nil {
// // 新增app用户信息
//}
// 处理app用户数据
userService := service.UserService{}
err = userService.HandleAppUserInfo(tx, user, userInfo)
if err != nil {
tx.Rollback()
responses.FailWithMessage(err.Error(), c)
return
}
tx.Commit()
} else {
// 获取用户详情数据
userInfoDao := dao.UserInfoDao{}
userInfo, err := userInfoDao.GetUserInfoByUserId(user.UserId)
if err != nil {
tx.Rollback()
responses.FailWithMessage(err.Error(), c)
return
}
// 处理app用户数据
userService := service.UserService{}
err = userService.HandleAppUserInfo(tx, user, userInfo)
if err != nil {
tx.Rollback()
responses.FailWithMessage(err.Error(), c)
return
}
}
tx.Commit()
// 下发token
token := &utils.Token{
UserId: fmt.Sprintf("%d", user.UserId),
@@ -118,6 +162,13 @@ func (r *Login) LoginPhone(c *gin.Context) {
return
}
// 获取用户数据
user, err = userDao.GetUser(maps)
if err != nil {
responses.FailWithMessage("登陆失败", c)
return
}
// 处理返回值
g := dto.LoginMobileDto(user)
@@ -194,7 +245,7 @@ func (r *Login) LoginWx(c *gin.Context) {
OpenId: userInfo.OpenId,
UnionId: userInfo.UnionId,
Age: nil,
Sex: userInfo.Sex,
Sex: &userInfo.Sex,
Avatar: avatar,
IsMember: 0,
MemberExpireDate: nil,
+108
View File
@@ -0,0 +1,108 @@
package dao
import (
"gorm.io/gorm"
"gorm.io/gorm/clause"
"hepa-calc-api/api/model"
"hepa-calc-api/global"
)
type UserCaseDao struct {
}
// GetUserCaseById 获取数据-id
func (r *UserCaseDao) GetUserCaseById(UserCaseId int64) (m *model.UserCase, err error) {
err = global.Db.First(&m, UserCaseId).Error
if err != nil {
return nil, err
}
return m, nil
}
// GetUserCasePreloadById 获取数据-加载全部关联-id
func (r *UserCaseDao) GetUserCasePreloadById(UserCaseId int64) (m *model.UserCase, err error) {
err = global.Db.Preload(clause.Associations).First(&m, UserCaseId).Error
if err != nil {
return nil, err
}
return m, nil
}
// DeleteUserCase 删除
func (r *UserCaseDao) DeleteUserCase(tx *gorm.DB, maps interface{}) error {
err := tx.Where(maps).Delete(&model.UserCase{}).Error
if err != nil {
return err
}
return nil
}
// DeleteUserCaseById 删除-id
func (r *UserCaseDao) DeleteUserCaseById(tx *gorm.DB, UserCaseId int64) error {
if err := tx.Delete(&model.UserCase{}, UserCaseId).Error; err != nil {
return err
}
return nil
}
// EditUserCase 修改
func (r *UserCaseDao) EditUserCase(tx *gorm.DB, maps interface{}, data interface{}) error {
err := tx.Model(&model.UserCase{}).Where(maps).Updates(data).Error
if err != nil {
return err
}
return nil
}
// EditUserCaseById 修改-id
func (r *UserCaseDao) EditUserCaseById(tx *gorm.DB, UserCaseId int64, data interface{}) error {
err := tx.Model(&model.UserCase{}).Where("user_case_id = ?", UserCaseId).Updates(data).Error
if err != nil {
return err
}
return nil
}
// GetUserCaseList 获取列表
func (r *UserCaseDao) GetUserCaseList(maps interface{}) (m []*model.UserCase, err error) {
err = global.Db.Where(maps).Find(&m).Error
if err != nil {
return nil, err
}
return m, nil
}
// GetUserCaseCount 获取数量
func (r *UserCaseDao) GetUserCaseCount(maps interface{}) (total int64, err error) {
err = global.Db.Model(&model.UserCase{}).Where(maps).Count(&total).Error
if err != nil {
return 0, err
}
return total, nil
}
// GetUserCaseListRand 获取列表-随机
func (r *UserCaseDao) GetUserCaseListRand(maps interface{}, limit int) (m []*model.UserCase, err error) {
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
if err != nil {
return nil, err
}
return m, nil
}
// AddUserCase 新增
func (r *UserCaseDao) AddUserCase(tx *gorm.DB, model *model.UserCase) (*model.UserCase, error) {
if err := tx.Create(model).Error; err != nil {
return nil, err
}
return model, nil
}
// GetUserCase 获取
func (r *UserCaseDao) GetUserCase(maps interface{}) (m *model.UserCase, err error) {
err = global.Db.Where(maps).First(&m).Error
if err != nil {
return nil, err
}
return m, nil
}
+9
View File
@@ -28,6 +28,15 @@ func (r *UserInfoDao) GetUserInfoPreloadById(UserInfoId int64) (m *model.UserInf
return m, nil
}
// GetUserInfoByUserId 获取数据-user_id
func (r *UserInfoDao) GetUserInfoByUserId(userId int64) (m *model.UserInfo, err error) {
err = global.Db.Where("user_id = ?", userId).First(&m).Error
if err != nil {
return nil, err
}
return m, nil
}
// DeleteUserInfo 删除
func (r *UserInfoDao) DeleteUserInfo(tx *gorm.DB, maps interface{}) error {
err := tx.Where(maps).Delete(&model.UserInfo{}).Error
+4 -2
View File
@@ -15,8 +15,9 @@ 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"` // 出生日期
Age *uint `json:"age"` // 年龄
Sex uint `json:"sex"` // 性别(0:未知 1:男 2:女)
Sex *int `json:"sex"` // 性别(0:未知 1:男 2:女)
Avatar string `json:"avatar"` // 头像
IsMember int `json:"is_member"` // 是否会员(0:否 1:是)
MemberExpireDate *model.LocalTime `json:"member_expire_date"` // 会员到期时间(非会员时为null)
@@ -34,8 +35,9 @@ func GetUserDto(m *model.User) *UserDto {
Mobile: m.Mobile,
UserStatus: m.UserStatus,
RegisterSource: m.RegisterSource,
Birthday: m.Birthday,
Age: m.Age,
Sex: uint(m.Sex),
Sex: m.Sex,
Avatar: utils.AddOssDomain(m.Avatar),
IsMember: m.IsMember,
MemberExpireDate: m.MemberExpireDate,
+3 -1
View File
@@ -9,14 +9,16 @@ import (
// User 用户表
type User struct {
UserId int64 `gorm:"column:user_id;type:bigint(19);primary_key;comment:用户id" json:"user_id"`
AppIden string `gorm:"column:app_iden;type:varchar(50);comment:app唯一标识" json:"app_iden"`
UserName string `gorm:"column:user_name;type:varchar(200);comment:用户名称" json:"user_name"`
Mobile string `gorm:"column:mobile;type:varchar(20);comment:手机号;NOT NULL" json:"mobile"`
UserStatus int `gorm:"column:user_status;type:tinyint(1);default:1;comment:状态(1:正常 2:禁用)" json:"user_status"`
RegisterSource int `gorm:"column:register_source;type:tinyint(1);default:1;comment:注册来源(1app注册 2:公众号注册)" json:"register_source"`
OpenId string `gorm:"column:open_id;type:varchar(100);comment:用户微信标识" json:"open_id"`
UnionId string `gorm:"column:union_id;type:varchar(100);comment:微信开放平台标识" json:"union_id"`
Birthday *LocalTime `gorm:"column:birthday;type:datetime;comment:出生日期" json:"birthday"`
Age *uint `gorm:"column:age;type:int(10) unsigned;comment:年龄" json:"age"`
Sex int `gorm:"column:sex;type:tinyint(1) unsigned;default:0;comment:性别(0:未知 1:男 2:女)" json:"sex"`
Sex *int `gorm:"column:sex;type:tinyint(1) unsigned;comment:性别(0:未知 1:男 2:女)" json:"sex"`
Avatar string `gorm:"column:avatar;type:varchar(255);comment:头像" json:"avatar"`
IsMember int `gorm:"column:is_member;type:tinyint(1);default:0;comment:是否会员(0:否 1:是)" json:"is_member"`
MemberExpireDate *LocalTime `gorm:"column:member_expire_date;type:datetime;comment:会员到期时间(非会员时为null" json:"member_expire_date"`
+19
View File
@@ -0,0 +1,19 @@
package model
// UserCase 用户表-病例
type UserCase struct {
UserCaseId int64 `gorm:"column:user_case_id;type:bigint(19);primary_key;comment:主键id" json:"user_case_id"`
UserId int64 `gorm:"column:user_id;type:bigint(19);comment:用户id;NOT NULL" json:"user_id"`
IsHospital *int `gorm:"column:is_hospital;type:tinyint(1);comment:是否医院就诊(0:否 1:是)" json:"is_hospital"`
LiverStatus string `gorm:"column:liver_status;type:varchar(30);comment:肝脏状态" json:"liver_status"`
IsMedication *int `gorm:"column:is_medication;type:tinyint(1);comment:是否服药(0:否 1:是)" json:"is_medication"`
Medication string `gorm:"column:medication;type:varchar(20);comment:服药名称" json:"medication"`
Disease string `gorm:"column:disease;type:varchar(500);comment:慢性疾病名称(逗号分隔)" json:"disease"`
IsAllergyHistory *int `gorm:"column:is_allergy_history;type:tinyint(1);comment:过敏史(0:否 1:是)" json:"is_allergy_history"`
AllergyHistory string `gorm:"column:allergy_history;type:varchar(100);comment:过敏史描述" json:"allergy_history"`
Model
}
func (m *UserCase) TableName() string {
return "user_case"
}
+14 -13
View File
@@ -8,19 +8,20 @@ import (
// UserInfo 用户表-基础信息
type UserInfo struct {
UserInfoId int64 `gorm:"column:user_info_id;type:bigint(19);primary_key;comment:主键id" json:"user_info_id"`
UserId int64 `gorm:"column:user_id;type:bigint(19);comment:用户id" json:"user_id"`
Height string `gorm:"column:height;type:varchar(20);comment:身高(cm" json:"height"`
Weight string `gorm:"column:weight;type:varchar(20);comment:体重(kg" json:"weight"`
NationId int64 `gorm:"column:nation_id;type:bigint(19);comment:民族id" json:"nation_id"`
FamilyHistoryId int64 `gorm:"column:family_history_id;type:bigint(19);comment:家族病史id" json:"family_history_id"`
ProvinceId int `gorm:"column:province_id;type:int(11);comment:省份id" json:"province_id"`
Province string `gorm:"column:province;type:varchar(40);comment:省份" json:"province"`
CityId int `gorm:"column:city_id;type:int(11);comment:城市id" json:"city_id"`
City string `gorm:"column:city;type:varchar(50);comment:城市" json:"city"`
CountyId int `gorm:"column:county_id;type:int(11);comment:区县id" json:"county_id"`
County string `gorm:"column:county;type:varchar(255);comment:区县" json:"county"`
DiseaseClassId int64 `gorm:"column:disease_class_id;type:bigint(19);comment:疾病分类id" json:"disease_class_id"`
UserInfoId int64 `gorm:"column:user_info_id;type:bigint(19);primary_key;comment:主键id" json:"user_info_id"`
UserId int64 `gorm:"column:user_id;type:bigint(19);comment:用户id" json:"user_id"`
Height string `gorm:"column:height;type:varchar(20);comment:身高(cm" json:"height"`
Weight string `gorm:"column:weight;type:varchar(20);comment:体重(kg" json:"weight"`
NationId *int64 `gorm:"column:nation_id;type:bigint(19);comment:民族id" json:"nation_id"`
IsFamilyHistory *int `gorm:"column:is_family_history;type:tinyint(1);comment:是否存在家族病史(0:未知 1:是 2:否)" json:"is_family_history"`
IsPregnant *int `gorm:"column:is_pregnant;type:tinyint(1);comment:是否怀孕(1:无计划 2:计划中 3:已怀孕 4:家有宝宝)" json:"is_pregnant"`
ExpectedDate *LocalTime `gorm:"column:expected_date;type:datetime;comment:预产期" json:"expected_date"`
ProvinceId *int `gorm:"column:province_id;type:int(11);comment:省份id" json:"province_id"`
Province string `gorm:"column:province;type:varchar(40);comment:省份" json:"province"`
CityId *int `gorm:"column:city_id;type:int(11);comment:城市id" json:"city_id"`
City string `gorm:"column:city;type:varchar(50);comment:城市" json:"city"`
CountyId *int `gorm:"column:county_id;type:int(11);comment:区县id" json:"county_id"`
County string `gorm:"column:county;type:varchar(255);comment:区县" json:"county"`
Model
}
+189
View File
@@ -7,6 +7,8 @@ import (
"hepa-calc-api/api/dao"
"hepa-calc-api/api/model"
"hepa-calc-api/extend/aliyun"
"hepa-calc-api/extend/app"
"hepa-calc-api/utils"
"io"
"math/rand"
"net/http"
@@ -152,3 +154,190 @@ func (r *UserService) AddUserMemberBuyCount(tx *gorm.DB, userId int64) (bool, er
return true, nil
}
// HandleAppUserInfo 处理app用户数据
func (r *UserService) HandleAppUserInfo(tx *gorm.DB, user *model.User, userInfo *model.UserInfo) error {
appUserInfo, err := app.GetInfoByMobile(user.Mobile)
if err != nil {
return err
}
// 对比处理用户数据
userData := make(map[string]interface{}) // 用户数据
userInfoData := make(map[string]interface{}) // 用户详情数据
// 出生日期/年龄
if appUserInfo.Data.Birthday != "" {
if user.Birthday != nil {
birthday := time.Time(*user.Birthday).Format("2006-01-02 15:04:05")
if appUserInfo.Data.Birthday != birthday {
userData["birthday"] = appUserInfo.Data.Birthday
}
} else {
userData["birthday"] = appUserInfo.Data.Birthday
}
// 年龄
age, err := utils.CalculateAge(appUserInfo.Data.Birthday)
if err != nil {
return err
}
userData["age"] = age
}
// 是否怀孕 1无计划 2计划中 3已怀孕 4家有宝宝
if appUserInfo.Data.IsPregnant != nil {
if userInfo.IsPregnant != nil {
if *appUserInfo.Data.IsPregnant != *userInfo.IsPregnant {
userInfoData["is_pregnant"] = appUserInfo.Data.IsPregnant
}
} else {
userInfoData["is_pregnant"] = appUserInfo.Data.IsPregnant
}
// 预产期
if appUserInfo.Data.ExpectedDateOfChildbirth != "" {
userInfoData["expected_date"] = appUserInfo.Data.ExpectedDateOfChildbirth
}
}
// 性别
if appUserInfo.Data.Sex != nil {
if user.Sex != nil {
if *appUserInfo.Data.Sex != *user.Sex {
userData["sex"] = appUserInfo.Data.Sex
}
} else {
userData["sex"] = appUserInfo.Data.Sex
}
}
// 体重
if appUserInfo.Data.Weight != nil {
weight := fmt.Sprintf("%f", appUserInfo.Data.Weight)
if userInfo.Weight != "" {
if weight != userInfo.Weight {
userInfoData["weight"] = weight
}
} else {
userInfoData["weight"] = weight
}
}
// 省份 id
if appUserInfo.Data.ProvinceID != nil {
// 获取省份数据
if userInfo.ProvinceId != nil {
if *appUserInfo.Data.ProvinceID != *userInfo.ProvinceId {
userInfoData["province_id"] = appUserInfo.Data.ProvinceID
}
} else {
userInfoData["province_id"] = appUserInfo.Data.ProvinceID
}
}
// 城市 id
if appUserInfo.Data.CityID != nil {
// 获取城市数据
if userInfo.CityId != nil {
if *appUserInfo.Data.CityID != *userInfo.CityId {
userInfoData["city_id"] = appUserInfo.Data.CityID
}
} else {
userInfoData["city_id"] = appUserInfo.Data.CityID
}
}
// 市区 id
if appUserInfo.Data.CountyID != nil {
// 获取区县数据
if userInfo.CountyId != nil {
if *appUserInfo.Data.CountyID != *userInfo.CountyId {
userInfoData["county_id"] = appUserInfo.Data.CountyID
}
} else {
userInfoData["county_id"] = appUserInfo.Data.CountyID
}
}
// 家族病史
if appUserInfo.Data.IsHBV != nil {
// 有无 肝硬化或肝癌家族史 0无1有2未知
// 是否存在家族病史(0:未知 1:是 2:否)
// 转换双方状态
if *appUserInfo.Data.IsHBV == 0 {
*appUserInfo.Data.IsHBV = 2
} else if *appUserInfo.Data.IsHBV == 1 {
*appUserInfo.Data.IsHBV = 1
} else if *appUserInfo.Data.IsHBV == 2 {
*appUserInfo.Data.IsHBV = 0
}
if userInfo.IsFamilyHistory != nil {
if *appUserInfo.Data.IsHBV != *userInfo.IsFamilyHistory {
userInfoData["is_family_history"] = appUserInfo.Data.IsHBV
}
} else {
userInfoData["is_family_history"] = appUserInfo.Data.IsHBV
}
}
// 民族
if appUserInfo.Data.NationUUID != "" {
// 获取民族数据
var nationId int64
if userInfo.NationId != nil {
if nationId != *userInfo.NationId {
userInfoData["nation_id"] = nationId
}
} else {
userInfoData["nation_id"] = nationId
}
}
// 患者 uuid
if appUserInfo.Data.PatientUUID != "" {
if user.AppIden != "" {
if appUserInfo.Data.PatientUUID != user.AppIden {
userData["app_iden"] = appUserInfo.Data.PatientUUID
}
} else {
userData["app_iden"] = appUserInfo.Data.PatientUUID
}
}
// 姓名
if appUserInfo.Data.Name != "" {
if user.UserName != "" {
if appUserInfo.Data.Name != user.UserName {
userData["user_name"] = appUserInfo.Data.Name
}
} else {
userData["user_name"] = appUserInfo.Data.Name
}
}
// 修改用户数据
if len(userData) > 0 {
userDao := dao.UserDao{}
err := userDao.EditUserById(tx, user.UserId, userData)
if err != nil {
return err
}
}
// 修改用户详情数据
if len(userInfoData) > 0 {
userInfoDao := dao.UserInfoDao{}
err := userInfoDao.EditUserInfoById(tx, userInfo.UserInfoId, userInfoData)
if err != nil {
return err
}
}
return nil
}