user.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package model
  2. import (
  3. "context"
  4. "database/sql/driver"
  5. "encoding/json"
  6. "fmt"
  7. "gorm.io/gorm"
  8. "project_management/app/e"
  9. "project_management/global"
  10. "project_management/utils"
  11. )
  12. type User struct {
  13. gorm.Model
  14. Phone string `gorm:"type:varchar(50);" json:"phone" validate:"required" min:"11" max:"11"`
  15. Collection collection `gorm:"type:json" json:"collection"` // 收藏系统
  16. }
  17. type CollectionAppDto struct {
  18. Phone string `gorm:"type:varchar(50);" json:"phone" validate:"required" min:"11" max:"11"`
  19. Collection collection `gorm:"type:json" json:"collection"` // 收藏系统
  20. State int `gorm:"type:int;" json:"state"`
  21. }
  22. func (u User) TableName() string {
  23. return "users"
  24. }
  25. type UserRegist struct {
  26. Phone string `json:"phone" validate:"required" min:"11" max:"11"`
  27. Code string `json:"code" validate:"required" min:"6" max:"6"`
  28. Token string `json:"token"`
  29. }
  30. type collection []string
  31. func (lo collection) Value() (driver.Value, error) {
  32. return json.Marshal(lo)
  33. }
  34. func (fn *collection) Scan(src interface{}) error {
  35. if src == nil {
  36. *fn = make(collection, 0)
  37. return nil
  38. }
  39. var u []byte
  40. switch v := src.(type) {
  41. case string:
  42. u = []byte(v)
  43. case []byte:
  44. u = v
  45. default:
  46. return fmt.Errorf("unsupported type: %T", src)
  47. }
  48. return json.Unmarshal(u, (*[]string)(fn))
  49. }
  50. // Login 登录
  51. func (u User) Login(user UserRegist) (UserRegist, string) {
  52. //TODO implement me
  53. var usere UserRegist
  54. tx := global.DBLink.Where("phone = ?", user.Phone).First(&u)
  55. ctx := context.Background()
  56. result, err := global.Rdb.Get(ctx, user.Phone).Result()
  57. if err != nil {
  58. return UserRegist{}, "验证验证码失败请联系管理员"
  59. } else if result != user.Code {
  60. return UserRegist{}, "验证码错误"
  61. }
  62. token, err := utils.CreateToken(u.ID, u.Phone, "user")
  63. token = "interior:" + token
  64. if err != nil {
  65. return UserRegist{}, "创建令牌失败"
  66. }
  67. usere.Phone = user.Phone
  68. usere.Token = token
  69. if tx.RowsAffected == 0 {
  70. u.Phone = user.Phone
  71. create := global.DBLink.Create(&u)
  72. if create.Error != nil {
  73. return UserRegist{}, "登录失败"
  74. }
  75. return usere, ""
  76. } else {
  77. return usere, ""
  78. }
  79. }
  80. func (u User) CollectionApp(user CollectionAppDto) e.Rescode {
  81. //TODO implement me
  82. tx := global.DBLink.Table(u.TableName())
  83. if tx.Error != nil {
  84. return e.CollectionFAIL
  85. }
  86. tx.Where("phone = ?", user.Phone).First(&u)
  87. u.Collection = append(u.Collection)
  88. capIdsMap := make(map[string]bool)
  89. if user.State == 1 {
  90. for _, v := range u.Collection {
  91. capIdsMap[v] = true
  92. }
  93. for _, s := range user.Collection {
  94. if _, ok := capIdsMap[s]; !ok {
  95. u.Collection = append(u.Collection, s)
  96. }
  97. }
  98. } else if user.State == 0 {
  99. strings := removeDuplicates(u.Collection, user.Collection)
  100. u.Collection = strings
  101. }
  102. updates := tx.Where("phone = ?", user.Phone).
  103. Updates(map[string]interface{}{"collection": u.Collection})
  104. if updates.Error != nil {
  105. return e.CollectionFAIL
  106. }
  107. if updates.RowsAffected > 0 {
  108. return e.SUCCESS
  109. }
  110. return e.CollectionFAIL
  111. }
  112. func (u User) CollectionList(phone string) (e.Rescode, []Apply) {
  113. //TODO implement me
  114. var applys []Apply
  115. tx := global.DBLink.Table(u.TableName())
  116. if tx.Error != nil {
  117. return e.FINDFAIL, applys
  118. }
  119. tx.Where("phone = ?", phone).First(&u)
  120. for _, s := range u.Collection {
  121. var apply Apply
  122. find := global.DBLink.Table(Apply{}.TableName()).Where("app_id = ?", s).Find(&apply)
  123. if find.Error != nil {
  124. return e.FINDFAIL, applys
  125. } else if find.RowsAffected > 0 {
  126. applys = append(applys, apply)
  127. }
  128. }
  129. return e.SUCCESS, applys
  130. }
  131. func removeDuplicates(slice1, slice2 []string) []string {
  132. exclusionMap := make(map[string]bool)
  133. for _, item := range slice2 {
  134. exclusionMap[item] = true
  135. }
  136. var result []string
  137. for _, item := range slice1 {
  138. if !exclusionMap[item] {
  139. result = append(result, item)
  140. }
  141. }
  142. return result
  143. }