package model import ( "context" "database/sql/driver" "encoding/json" "fmt" "gorm.io/gorm" "project_management/app/e" "project_management/global" "project_management/utils" ) type User struct { gorm.Model Phone string `gorm:"type:varchar(50);" json:"phone" validate:"required" min:"11" max:"11"` Collection collection `gorm:"type:json" json:"collection"` // 收藏系统 } type CollectionAppDto struct { Phone string `gorm:"type:varchar(50);" json:"phone" validate:"required" min:"11" max:"11"` Collection collection `gorm:"type:json" json:"collection"` // 收藏系统 State int `gorm:"type:int;" json:"state"` } func (u User) TableName() string { return "users" } type UserRegist struct { Phone string `json:"phone" validate:"required" min:"11" max:"11"` Code string `json:"code" validate:"required" min:"6" max:"6"` Token string `json:"token"` } type collection []string func (lo collection) Value() (driver.Value, error) { return json.Marshal(lo) } func (fn *collection) Scan(src interface{}) error { if src == nil { *fn = make(collection, 0) return nil } var u []byte switch v := src.(type) { case string: u = []byte(v) case []byte: u = v default: return fmt.Errorf("unsupported type: %T", src) } return json.Unmarshal(u, (*[]string)(fn)) } // Login 登录 func (u User) Login(user UserRegist) (UserRegist, string) { //TODO implement me var usere UserRegist tx := global.DBLink.Where("phone = ?", user.Phone).First(&u) ctx := context.Background() result, err := global.Rdb.Get(ctx, user.Phone).Result() if err != nil { return UserRegist{}, "验证验证码失败请联系管理员" } else if result != user.Code { return UserRegist{}, "验证码错误" } token, err := utils.CreateToken(u.ID, u.Phone, "user") token = "interior:" + token if err != nil { return UserRegist{}, "创建令牌失败" } usere.Phone = user.Phone usere.Token = token if tx.RowsAffected == 0 { u.Phone = user.Phone create := global.DBLink.Create(&u) if create.Error != nil { return UserRegist{}, "登录失败" } return usere, "" } else { return usere, "" } } func (u User) CollectionApp(user CollectionAppDto) e.Rescode { //TODO implement me tx := global.DBLink.Table(u.TableName()) if tx.Error != nil { return e.CollectionFAIL } tx.Where("phone = ?", user.Phone).First(&u) u.Collection = append(u.Collection) capIdsMap := make(map[string]bool) if user.State == 1 { for _, v := range u.Collection { capIdsMap[v] = true } for _, s := range user.Collection { if _, ok := capIdsMap[s]; !ok { u.Collection = append(u.Collection, s) } } } else if user.State == 0 { strings := removeDuplicates(u.Collection, user.Collection) u.Collection = strings } updates := tx.Where("phone = ?", user.Phone). Updates(map[string]interface{}{"collection": u.Collection}) if updates.Error != nil { return e.CollectionFAIL } if updates.RowsAffected > 0 { return e.SUCCESS } return e.CollectionFAIL } func (u User) CollectionList(phone string) (e.Rescode, []Apply) { //TODO implement me var applys []Apply tx := global.DBLink.Table(u.TableName()) if tx.Error != nil { return e.FINDFAIL, applys } tx.Where("phone = ?", phone).First(&u) for _, s := range u.Collection { var apply Apply find := global.DBLink.Table(Apply{}.TableName()).Where("app_id = ?", s).Find(&apply) if find.Error != nil { return e.FINDFAIL, applys } else if find.RowsAffected > 0 { applys = append(applys, apply) } } return e.SUCCESS, applys } func removeDuplicates(slice1, slice2 []string) []string { exclusionMap := make(map[string]bool) for _, item := range slice2 { exclusionMap[item] = true } var result []string for _, item := range slice1 { if !exclusionMap[item] { result = append(result, item) } } return result }