user_serivce.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package user
  2. import (
  3. "errors"
  4. logger "eta/eta_mini_ht_api/common/component/log"
  5. userDao "eta/eta_mini_ht_api/models/user"
  6. "gorm.io/gorm"
  7. "time"
  8. )
  9. type userStatus string
  10. const (
  11. UnBind userStatus = "unBind"
  12. Login userStatus = "login"
  13. Logout userStatus = "logout"
  14. )
  15. type UserDTO struct {
  16. Id int
  17. Username string
  18. AreaCode string
  19. Mobile string
  20. OpenId string
  21. UnionId string
  22. RiskLevel string
  23. RiskValidEndDate string
  24. LoginStatus userDao.LogType
  25. FollowingGzh int
  26. GzhOpenId string
  27. }
  28. type OfficialUserDTO struct {
  29. ID int
  30. IdNo string
  31. RealName string
  32. TemplateUserID int
  33. IdKind int
  34. IDValid bool
  35. AccountStatus string
  36. }
  37. type FeedbackDTO struct {
  38. Mobile string `json:"mobile"`
  39. UserId int `json:"userId"`
  40. Message string `json:"message"`
  41. }
  42. func convertUserDTO(user userDao.TemplateUser) UserDTO {
  43. return UserDTO{
  44. Id: user.Id,
  45. Username: user.Username,
  46. AreaCode: user.AreaCode,
  47. Mobile: user.Mobile,
  48. OpenId: user.OpenId,
  49. LoginStatus: user.LoginStatus,
  50. RiskLevel: user.RiskLevel,
  51. RiskValidEndDate: user.RiskValidEndDate,
  52. FollowingGzh: user.FollowingGzh,
  53. }
  54. }
  55. func convertOfficialUserDTO(user userDao.User) OfficialUserDTO {
  56. year, month, day := time.Now().Date()
  57. today := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
  58. return OfficialUserDTO{
  59. ID: user.ID,
  60. RealName: user.RealName,
  61. IdNo: user.IDNo,
  62. TemplateUserID: user.TemplateUserID,
  63. IDValid: !user.IDEndDate.Before(today),
  64. IdKind: user.IDKind,
  65. AccountStatus: user.AccountStatus,
  66. }
  67. }
  68. func GetUserByMobile(mobile string) (dto UserDTO, err error) {
  69. templateUser, err := userDao.GetUserByMobile(mobile)
  70. if err != nil {
  71. if !errors.Is(err, gorm.ErrRecordNotFound) {
  72. logger.Error("查询用户失败:%v", err)
  73. }
  74. return
  75. }
  76. dto = convertUserDTO(templateUser)
  77. return
  78. }
  79. func GetUserAndCountReadTimes(userId int) (err error) {
  80. err = userDao.GetUserByIdAndCountReadTimes(userId)
  81. if err != nil {
  82. logger.Error("更新用户阅读次数失败:%v", err)
  83. }
  84. return
  85. }
  86. func UserLogout(userId int) (err error) {
  87. err = userDao.UserLogout(userId)
  88. if err != nil {
  89. logger.Error("用户退出登录失败:%v", err)
  90. }
  91. return
  92. }
  93. func UserLogin(userId int) (err error) {
  94. err = userDao.UserLogin(userId)
  95. if err != nil {
  96. logger.Error("用户登录失败:%v", err)
  97. }
  98. return
  99. }
  100. func RegisterTemplateUser(dto *UserDTO) (err error) {
  101. templateUser := convertToTemplateUser(dto)
  102. err = userDao.RegisterTemplateUser(&templateUser)
  103. if err != nil {
  104. logger.Error("创建临时用户失败:%v", err)
  105. return
  106. }
  107. convertToUserDTO(templateUser, dto)
  108. return
  109. }
  110. func GetTemplateUserByOpenId(openId string) (user UserDTO, err error) {
  111. templateUser, err := userDao.GetUserByOpenId(openId)
  112. if err != nil {
  113. logger.Error("获取用户信息失败:%v", err)
  114. return
  115. }
  116. user = convertUserDTO(templateUser)
  117. return
  118. }
  119. func FeedBack(dto FeedbackDTO) (err error) {
  120. feedBackEntity := userDao.FeedBack{
  121. Message: dto.Message,
  122. Mobile: dto.Mobile,
  123. UserID: dto.UserId,
  124. }
  125. return userDao.InsertFeedBack(feedBackEntity)
  126. }
  127. func convertToTemplateUser(dto *UserDTO) userDao.TemplateUser {
  128. return userDao.TemplateUser{
  129. Username: dto.Username,
  130. Mobile: dto.Mobile,
  131. OpenId: dto.OpenId,
  132. UnionId: dto.UnionId,
  133. GzhOpenId: dto.GzhOpenId,
  134. FollowingGzh: dto.FollowingGzh,
  135. }
  136. }
  137. func convertToUserDTO(templateUser userDao.TemplateUser, dto *UserDTO) {
  138. dto.Id = templateUser.Id
  139. dto.Username = templateUser.Username
  140. dto.Mobile = templateUser.Mobile
  141. dto.OpenId = templateUser.OpenId
  142. dto.UnionId = templateUser.UnionId
  143. dto.FollowingGzh = templateUser.FollowingGzh
  144. }
  145. func BindUserMobile(userId int, mobile string, areaCode string) (err error) {
  146. err = userDao.BindMobile(userId, mobile, areaCode)
  147. if err != nil {
  148. logger.Error("绑定手机号失败:%v", err)
  149. }
  150. return
  151. }
  152. func GetUserById(userId int) (userDto UserDTO, err error) {
  153. templateUser, err := userDao.GetUserById(userId)
  154. if err != nil {
  155. logger.Error("查询用户失败:%v", err)
  156. return
  157. }
  158. if templateUser.IsDeleted == 1 {
  159. logger.Error("用户已删除:%v", err)
  160. err = errors.New("用户已删除")
  161. return
  162. }
  163. userDto = convertUserDTO(templateUser)
  164. return
  165. }
  166. func GetUserByOpenId(openId string) (userDto UserDTO, err error) {
  167. templateUser, err := userDao.GetUserByOpenId(openId)
  168. if err != nil {
  169. logger.Error("查询用户失败:%v", err)
  170. return
  171. }
  172. if templateUser.IsDeleted == 1 {
  173. logger.Error("用户已删除:%v", err)
  174. err = errors.New("用户已删除")
  175. return
  176. }
  177. userDto = convertUserDTO(templateUser)
  178. return
  179. }
  180. func GetTemplateUserByUnionId(unionId string) (user UserDTO, err error) {
  181. templateUser, err := userDao.GetUserByUnionId(unionId)
  182. if err != nil {
  183. logger.Error("获取用户信息失败:%v", err)
  184. return
  185. }
  186. user = convertUserDTO(templateUser)
  187. return
  188. }
  189. // 绑定小程序OpenId
  190. func BindUserXcxOpenId(userId int, openId string) (err error) {
  191. err = userDao.BindXcxOpenId(userId, openId)
  192. if err != nil {
  193. if !errors.Is(err, gorm.ErrRecordNotFound) {
  194. logger.Error("绑定小程序openId失败:%v", err)
  195. }
  196. return
  197. }
  198. return
  199. }
  200. // 绑定公众号OpenId
  201. func BindUserGzhOpenId(userId int, openId string, followingGzh int) (err error) {
  202. err = userDao.BindGzhOpenId(userId, openId, followingGzh)
  203. if err != nil {
  204. if !errors.Is(err, gorm.ErrRecordNotFound) {
  205. logger.Error("绑定公众号openId失败:%v", err)
  206. }
  207. return
  208. }
  209. return
  210. }
  211. func GetTemplateUserByGzhOpenId(openId string) (user UserDTO, err error) {
  212. templateUser, err := userDao.GetUserByGzhOpenId(openId)
  213. user = convertUserDTO(templateUser)
  214. return
  215. }
  216. func GetUserByTemplateUserId(templateUserId int) (officialUser OfficialUserDTO, err error) {
  217. user, err := userDao.GetUserByTemplateUserId(templateUserId)
  218. officialUser = convertOfficialUserDTO(user)
  219. return
  220. }