|
|
@@ -26,6 +26,8 @@ type User struct {
|
|
|
UsedQuota int `json:"used_quota" gorm:"type:int;default:0;column:used_quota"` // used quota
|
|
|
RequestCount int `json:"request_count" gorm:"type:int;default:0;"` // request number
|
|
|
Group string `json:"group" gorm:"type:varchar(32);default:'default'"`
|
|
|
+ AffCode string `json:"aff_code" gorm:"type:varchar(32);column:aff_code;uniqueIndex"`
|
|
|
+ InviterId int `json:"inviter_id" gorm:"type:int;column:inviter_id;index"`
|
|
|
}
|
|
|
|
|
|
func GetMaxUserId() int {
|
|
|
@@ -58,6 +60,15 @@ func GetUserById(id int, selectAll bool) (*User, error) {
|
|
|
return &user, err
|
|
|
}
|
|
|
|
|
|
+func GetUserIdByAffCode(affCode string) (int, error) {
|
|
|
+ if affCode == "" {
|
|
|
+ return 0, errors.New("affCode 为空!")
|
|
|
+ }
|
|
|
+ var user User
|
|
|
+ err := DB.Select("id").First(&user, "aff_code = ?", affCode).Error
|
|
|
+ return user.Id, err
|
|
|
+}
|
|
|
+
|
|
|
func DeleteUserById(id int) (err error) {
|
|
|
if id == 0 {
|
|
|
return errors.New("id 为空!")
|
|
|
@@ -66,7 +77,7 @@ func DeleteUserById(id int) (err error) {
|
|
|
return user.Delete()
|
|
|
}
|
|
|
|
|
|
-func (user *User) Insert() error {
|
|
|
+func (user *User) Insert(inviterId int) error {
|
|
|
var err error
|
|
|
if user.Password != "" {
|
|
|
user.Password, err = common.Password2Hash(user.Password)
|
|
|
@@ -76,6 +87,7 @@ func (user *User) Insert() error {
|
|
|
}
|
|
|
user.Quota = common.QuotaForNewUser
|
|
|
user.AccessToken = common.GetUUID()
|
|
|
+ user.AffCode = common.GetRandomString(4)
|
|
|
result := DB.Create(user)
|
|
|
if result.Error != nil {
|
|
|
return result.Error
|
|
|
@@ -83,6 +95,16 @@ func (user *User) Insert() error {
|
|
|
if common.QuotaForNewUser > 0 {
|
|
|
RecordLog(user.Id, LogTypeSystem, fmt.Sprintf("新用户注册赠送 %d 点额度", common.QuotaForNewUser))
|
|
|
}
|
|
|
+ if inviterId != 0 {
|
|
|
+ if common.QuotaForInvitee > 0 {
|
|
|
+ _ = IncreaseUserQuota(user.Id, common.QuotaForInvitee)
|
|
|
+ RecordLog(user.Id, LogTypeSystem, fmt.Sprintf("使用邀请码赠送 %d 点额度", common.QuotaForInvitee))
|
|
|
+ }
|
|
|
+ if common.QuotaForInviter > 0 {
|
|
|
+ _ = IncreaseUserQuota(inviterId, common.QuotaForInviter)
|
|
|
+ RecordLog(inviterId, LogTypeSystem, fmt.Sprintf("邀请用户赠送 %d 点额度", common.QuotaForInviter))
|
|
|
+ }
|
|
|
+ }
|
|
|
return nil
|
|
|
}
|
|
|
|