perf: version subscribe logic
This commit is contained in:
@@ -116,14 +116,3 @@ func (u *UserApi) GetInfo(context *gin.Context) {
|
|||||||
userInfo, errCode := u.userService.GetInfo(int64(uid))
|
userInfo, errCode := u.userService.GetInfo(int64(uid))
|
||||||
response.Result(errCode, userInfo, context)
|
response.Result(errCode, userInfo, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 订阅个人版
|
|
||||||
func (u *UserApi) Buy(context *gin.Context) {
|
|
||||||
uid, _ := strconv.Atoi(context.Request.Header.Get("uid"))
|
|
||||||
if uid <= 0 {
|
|
||||||
response.Result(response.ErrCodeParamInvalid, nil, context)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
versionInfo, errCode := u.userService.Buy(int64(uid))
|
|
||||||
response.Result(errCode, versionInfo, context)
|
|
||||||
}
|
|
||||||
|
|||||||
+2
-12
@@ -5,8 +5,6 @@ type User struct {
|
|||||||
Email string `gorm:"email"`
|
Email string `gorm:"email"`
|
||||||
Password string `gorm:"password"`
|
Password string `gorm:"password"`
|
||||||
Name string `gorm:"name"`
|
Name string `gorm:"name"`
|
||||||
Version int `gorm:"version"`
|
|
||||||
Expired int64 `gorm:"expired"`
|
|
||||||
Status int `gorm:"status"`
|
Status int `gorm:"status"`
|
||||||
Created int64 `gorm:"created"`
|
Created int64 `gorm:"created"`
|
||||||
Updated int64 `gorm:"updated"`
|
Updated int64 `gorm:"updated"`
|
||||||
@@ -47,18 +45,10 @@ type UserMailParam struct {
|
|||||||
|
|
||||||
type UserInfo struct {
|
type UserInfo struct {
|
||||||
Uid int64 `json:"uid"`
|
Uid int64 `json:"uid"`
|
||||||
Ver int `json:"version"`
|
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserPersonInfo struct {
|
type UserPersonInfo struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Version int `json:"version"`
|
|
||||||
Expired int64 `json:"expired"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type UserVerisonInfo struct {
|
|
||||||
Version int `json:"version"`
|
|
||||||
Expired int64 `json:"expired"`
|
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-31
@@ -47,13 +47,33 @@ func (u *UserService) Register(param *models.UserCreateParam) int {
|
|||||||
newUser := models.User{
|
newUser := models.User{
|
||||||
Email: param.Email,
|
Email: param.Email,
|
||||||
Password: string(password),
|
Password: string(password),
|
||||||
Version: 1,
|
|
||||||
Status: 1,
|
Status: 1,
|
||||||
Created: time.Now().Unix(),
|
Created: time.Now().Unix(),
|
||||||
}
|
}
|
||||||
if err := global.Db.Create(&newUser).Error; err != nil {
|
if err := global.Db.Create(&newUser).Error; err != nil {
|
||||||
return response.ErrCodeFailed
|
return response.ErrCodeFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 新用户默认订阅免费版
|
||||||
|
subscribe := models.Subscribe{
|
||||||
|
Version: 1,
|
||||||
|
Created: time.Now().Unix(),
|
||||||
|
}
|
||||||
|
if global.Db.Table(SUBSCRIBE).Where("uid = ?", newUser.Id).First(&models.Subscribe{}).RowsAffected == 0 {
|
||||||
|
subscribe.Uid = newUser.Id
|
||||||
|
subscribe.Created = time.Now().Unix()
|
||||||
|
err := global.Db.Table(SUBSCRIBE).Create(&subscribe).Error
|
||||||
|
if err != nil {
|
||||||
|
return response.ErrCodeFailed
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
subscribe.Updated = time.Now().Unix()
|
||||||
|
err = global.Db.Model(&models.Subscribe{}).Where("uid = ?", newUser.Id).Updates(&subscribe).Error
|
||||||
|
if err != nil {
|
||||||
|
return response.ErrCodeFailed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return response.ErrCodeSuccess
|
return response.ErrCodeSuccess
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,39 +209,9 @@ func (u *UserService) GetInfo(uid int64) (*models.UserPersonInfo, int) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, response.ErrCodeFailed
|
return nil, response.ErrCodeFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
// 判断用户订阅是否过期
|
|
||||||
if user.Version == 2 && time.Now().Unix() > int64(user.Expired) {
|
|
||||||
err := global.Db.Model(&models.User{}).Where("id = ?", uid).Update("version", 1).Error
|
|
||||||
if err != nil {
|
|
||||||
return nil, response.ErrCodeFailed
|
|
||||||
}
|
|
||||||
return &user, response.ErrCodeSuccess
|
|
||||||
}
|
|
||||||
return &user, response.ErrCodeSuccess
|
return &user, response.ErrCodeSuccess
|
||||||
}
|
}
|
||||||
|
|
||||||
// 订阅个人版
|
|
||||||
func (u *UserService) Buy(uid int64) (*models.UserVerisonInfo, int) {
|
|
||||||
// 订阅一个月按30天计算
|
|
||||||
month := time.Now().Unix() + int64(2592000)
|
|
||||||
user := models.User{
|
|
||||||
Version: 2,
|
|
||||||
Expired: month,
|
|
||||||
Updated: time.Now().Unix(),
|
|
||||||
}
|
|
||||||
err := global.Db.Model(&models.User{}).Where("id = ?", uid).Updates(&user).Error
|
|
||||||
if err != nil {
|
|
||||||
return nil, response.ErrCodeFailed
|
|
||||||
}
|
|
||||||
// 查询版本信息
|
|
||||||
var version models.UserVerisonInfo
|
|
||||||
if err := global.Db.Table(USER).Where("id = ?", uid).First(&version).Error; err != nil {
|
|
||||||
return nil, response.ErrCodeFailed
|
|
||||||
}
|
|
||||||
return &version, response.ErrCodeSuccess
|
|
||||||
}
|
|
||||||
|
|
||||||
// 校验用户Token
|
// 校验用户Token
|
||||||
func (u *UserService) VerifyToken(token string) error {
|
func (u *UserService) VerifyToken(token string) error {
|
||||||
return global.Rdb.Exists(ctx, token).Err()
|
return global.Rdb.Exists(ctx, token).Err()
|
||||||
|
|||||||
Reference in New Issue
Block a user