package user import ( "errors" logger "eta/eta_mini_ht_api/common/component/log" userDao "eta/eta_mini_ht_api/models/user" "gorm.io/gorm" "time" ) type userStatus string const ( UnBind userStatus = "unBind" Login userStatus = "login" Logout userStatus = "logout" ) type UserDTO struct { Id int Username string AreaCode string Mobile string OpenId string UnionId string RiskLevel string RiskValidEndDate string LoginStatus userDao.LogType FollowingGzh int GzhOpenId string } type OfficialUserDTO struct { ID int IdNo string RealName string TemplateUserID int IdKind int IDValid bool AccountStatus string } type FeedbackDTO struct { Mobile string `json:"mobile"` UserId int `json:"userId"` Message string `json:"message"` } func convertUserDTO(user userDao.TemplateUser) UserDTO { return UserDTO{ Id: user.Id, Username: user.Username, AreaCode: user.AreaCode, Mobile: user.Mobile, OpenId: user.OpenId, LoginStatus: user.LoginStatus, RiskLevel: user.RiskLevel, RiskValidEndDate: user.RiskValidEndDate, FollowingGzh: user.FollowingGzh, } } func convertOfficialUserDTO(user userDao.User) OfficialUserDTO { year, month, day := time.Now().Date() today := time.Date(year, month, day, 0, 0, 0, 0, time.Local) return OfficialUserDTO{ ID: user.ID, RealName: user.RealName, IdNo: user.IDNo, TemplateUserID: user.TemplateUserID, IDValid: !user.IDEndDate.Before(today), IdKind: user.IDKind, AccountStatus: user.AccountStatus, } } func GetUserByMobile(mobile string) (dto UserDTO, err error) { templateUser, err := userDao.GetUserByMobile(mobile) if err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { logger.Error("查询用户失败:%v", err) } return } dto = convertUserDTO(templateUser) return } func GetUserAndCountReadTimes(userId int) (err error) { err = userDao.GetUserByIdAndCountReadTimes(userId) if err != nil { logger.Error("更新用户阅读次数失败:%v", err) } return } func UserLogout(userId int) (err error) { err = userDao.UserLogout(userId) if err != nil { logger.Error("用户退出登录失败:%v", err) } return } func UserLogin(userId int) (err error) { err = userDao.UserLogin(userId) if err != nil { logger.Error("用户登录失败:%v", err) } return } func RegisterTemplateUser(dto *UserDTO) (err error) { templateUser := convertToTemplateUser(dto) err = userDao.RegisterTemplateUser(&templateUser) if err != nil { logger.Error("创建临时用户失败:%v", err) return } convertToUserDTO(templateUser, dto) return } func GetTemplateUserByOpenId(openId string) (user UserDTO, err error) { templateUser, err := userDao.GetUserByOpenId(openId) if err != nil { logger.Error("获取用户信息失败:%v", err) return } user = convertUserDTO(templateUser) return } func FeedBack(dto FeedbackDTO) (err error) { feedBackEntity := userDao.FeedBack{ Message: dto.Message, Mobile: dto.Mobile, UserID: dto.UserId, } return userDao.InsertFeedBack(feedBackEntity) } func convertToTemplateUser(dto *UserDTO) userDao.TemplateUser { return userDao.TemplateUser{ Username: dto.Username, Mobile: dto.Mobile, OpenId: dto.OpenId, UnionId: dto.UnionId, GzhOpenId: dto.GzhOpenId, FollowingGzh: dto.FollowingGzh, } } func convertToUserDTO(templateUser userDao.TemplateUser, dto *UserDTO) { dto.Id = templateUser.Id dto.Username = templateUser.Username dto.Mobile = templateUser.Mobile dto.OpenId = templateUser.OpenId dto.UnionId = templateUser.UnionId dto.FollowingGzh = templateUser.FollowingGzh } func BindUserMobile(userId int, mobile string, areaCode string) (err error) { err = userDao.BindMobile(userId, mobile, areaCode) if err != nil { logger.Error("绑定手机号失败:%v", err) } return } func GetUserById(userId int) (userDto UserDTO, err error) { templateUser, err := userDao.GetUserById(userId) if err != nil { logger.Error("查询用户失败:%v", err) return } if templateUser.IsDeleted == 1 { logger.Error("用户已删除:%v", err) err = errors.New("用户已删除") return } userDto = convertUserDTO(templateUser) return } func GetUserByOpenId(openId string) (userDto UserDTO, err error) { templateUser, err := userDao.GetUserByOpenId(openId) if err != nil { logger.Error("查询用户失败:%v", err) return } if templateUser.IsDeleted == 1 { logger.Error("用户已删除:%v", err) err = errors.New("用户已删除") return } userDto = convertUserDTO(templateUser) return } func GetTemplateUserByUnionId(unionId string) (user UserDTO, err error) { templateUser, err := userDao.GetUserByUnionId(unionId) if err != nil { logger.Error("获取用户信息失败:%v", err) return } user = convertUserDTO(templateUser) return } // 绑定小程序OpenId func BindUserXcxOpenId(userId int, openId string) (err error) { err = userDao.BindXcxOpenId(userId, openId) if err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { logger.Error("绑定小程序openId失败:%v", err) } return } return } // 绑定公众号OpenId func BindUserGzhOpenId(userId int, openId string, followingGzh int) (err error) { err = userDao.BindGzhOpenId(userId, openId, followingGzh) if err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { logger.Error("绑定公众号openId失败:%v", err) } return } return } func GetTemplateUserByGzhOpenId(openId string) (user UserDTO, err error) { templateUser, err := userDao.GetUserByGzhOpenId(openId) user = convertUserDTO(templateUser) return } func GetUserByTemplateUserId(templateUserId int) (officialUser OfficialUserDTO, err error) { user, err := userDao.GetUserByTemplateUserId(templateUserId) officialUser = convertOfficialUserDTO(user) return }