123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- 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
- }
|