Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现论坛等级倍率差异化 #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions common/group-ratio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package common
import "encoding/json"

var GroupRatio = map[string]float64{
"default": 1,
"vip": 1,
"svip": 1,
LinuxDoTrustLevel0: 1,
LinuxDoTrustLevel1: 1,
LinuxDoTrustLevel2: 1,
LinuxDoTrustLevel3: 1,
LinuxDoTrustLevel4: 1,
}

func GroupRatio2JSONString() string {
Expand Down
17 changes: 17 additions & 0 deletions common/linuxdo-group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package common

import "fmt"

const (
LinuxDoTrustLevel0 = "Level0"
LinuxDoTrustLevel1 = "level1"
LinuxDoTrustLevel2 = "level2"
LinuxDoTrustLevel3 = "level3"
LinuxDoTrustLevel4 = "level4"
)

type TrustLevel int

func (l TrustLevel) String() string {
return fmt.Sprintf("level%d", l)
}
8 changes: 5 additions & 3 deletions common/topup-ratio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package common
import "encoding/json"

var TopupGroupRatio = map[string]float64{
"default": 1,
"vip": 1,
"svip": 1,
LinuxDoTrustLevel0: 1,
LinuxDoTrustLevel1: 1,
LinuxDoTrustLevel2: 1,
LinuxDoTrustLevel3: 1,
LinuxDoTrustLevel4: 1,
}

func TopupGroupRatio2JSONString() string {
Expand Down
142 changes: 91 additions & 51 deletions controller/linuxdo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"net/http"
"net/url"
"one-api/common"
"one-api/model"
"strconv"
"time"

"one-api/common"
"one-api/model"

"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)

type LinuxDoOAuthResponse struct {
Expand All @@ -23,12 +25,73 @@ type LinuxDoOAuthResponse struct {
}

type LinuxDoUser struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
Active bool `json:"active"`
TrustLevel int `json:"trust_level"`
Silenced bool `json:"silenced"`
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
Active bool `json:"active"`
TrustLevel common.TrustLevel `json:"trust_level"`
Silenced bool `json:"silenced"`
}

type UserHandler interface {
Do() (*model.User, error)
}

type existingUserHandler struct {
*LinuxDoUser
}

func (h existingUserHandler) Do() (*model.User, error) {
user := &model.User{
LinuxDoId: strconv.Itoa(h.ID),
}

err := user.FillUserByLinuxDoId()
if err != nil {
return nil, err
}

trustLevelStr := h.TrustLevel.String()
if user.Group != trustLevelStr {
user.Group = trustLevelStr
err = user.Update(false)
if err != nil {
return nil, fmt.Errorf("更新用户组失败: %w", err)
}
}

return user, err
}

type newUserHandler struct {
ginCtx *gin.Context
linuxDoUser *LinuxDoUser
}

func (h newUserHandler) Do() (*model.User, error) {
if !common.RegisterEnabled {
return nil, errors.New("管理员关闭了新用户注册")
}

affCode := h.ginCtx.Query("aff")

user := new(model.User)
user.LinuxDoId = strconv.Itoa(h.linuxDoUser.ID)
user.InviterId, _ = model.GetUserIdByAffCode(affCode)
user.Username = "linuxdo_" + strconv.Itoa(model.GetMaxUserId()+1)
if h.linuxDoUser.Name != "" {
user.DisplayName = h.linuxDoUser.Name
} else {
user.DisplayName = h.linuxDoUser.Username
}
user.Role = common.RoleCommonUser
user.Status = common.UserStatusEnabled
user.Group = h.linuxDoUser.TrustLevel.String()
if err := user.Insert(user.InviterId); err != nil {
return nil, fmt.Errorf("创建用户失败: %w", err)
}

return user, nil
}

func getLinuxDoUserInfoByCode(code string) (*LinuxDoUser, error) {
Expand Down Expand Up @@ -107,54 +170,29 @@ func LinuxDoOAuth(c *gin.Context) {
return
}
code := c.Query("code")
linuxdoUser, err := getLinuxDoUserInfoByCode(code)
linuxDoUser, err := getLinuxDoUserInfoByCode(code)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
user := model.User{
LinuxDoId: strconv.Itoa(linuxdoUser.ID),
}
if model.IsLinuxDoIdAlreadyTaken(user.LinuxDoId) {
err := user.FillUserByLinuxDoId()
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}

var userHandler UserHandler
if model.IsLinuxDoIdAlreadyTaken(strconv.Itoa(linuxDoUser.ID)) {
userHandler = existingUserHandler{linuxDoUser}
} else {
if common.RegisterEnabled {
affCode := c.Query("aff")
user.InviterId, _ = model.GetUserIdByAffCode(affCode)

user.Username = "linuxdo_" + strconv.Itoa(model.GetMaxUserId()+1)
if linuxdoUser.Name != "" {
user.DisplayName = linuxdoUser.Name
} else {
user.DisplayName = linuxdoUser.Username
}
user.Role = common.RoleCommonUser
user.Status = common.UserStatusEnabled

if err := user.Insert(user.InviterId); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
} else {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "管理员关闭了新用户注册",
})
return
}
userHandler = newUserHandler{c, linuxDoUser}
}

user, err := userHandler.Do()
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}

if user.Status != common.UserStatusEnabled {
Expand All @@ -164,7 +202,8 @@ func LinuxDoOAuth(c *gin.Context) {
})
return
}
setupLogin(&user, c)

setupLogin(user, c)
}

func LinuxDoBind(c *gin.Context) {
Expand Down Expand Up @@ -207,6 +246,7 @@ func LinuxDoBind(c *gin.Context) {
return
}
user.LinuxDoId = strconv.Itoa(linuxdoUser.ID)
user.Group = linuxdoUser.TrustLevel.String()
err = user.Update(false)
if err != nil {
c.JSON(http.StatusOK, gin.H{
Expand Down
5 changes: 3 additions & 2 deletions model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package model
import (
"errors"
"fmt"
"one-api/common"
"strings"
"time"

"one-api/common"

"gorm.io/gorm"
)

Expand All @@ -29,7 +30,7 @@ type User struct {
Quota int `json:"quota" gorm:"type:int;default:0"`
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(64);default:'default'"`
Group string `json:"group" gorm:"type:varchar(64);default:'level0'"`
AffCode string `json:"aff_code" gorm:"type:varchar(32);column:aff_code;uniqueIndex"`
AffCount int `json:"aff_count" gorm:"type:int;default:0;column:aff_count"`
AffQuota int `json:"aff_quota" gorm:"type:int;default:0;column:aff_quota"` // 邀请剩余额度
Expand Down
6 changes: 3 additions & 3 deletions web/src/helpers/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export function renderGroup(group) {
groups.sort();
return <>
{groups.map((group) => {
if (group === 'vip' || group === 'pro') {
if (group === 'level3' || group === 'level4') {
return <Tag size='large' color='yellow'>{group}</Tag>;
} else if (group === 'svip' || group === 'premium') {
} else if (group === 'level2' || group === 'level1') {
return <Tag size='large' color='red'>{group}</Tag>;
}
if (group === 'default') {
if (group === 'level0') {
return <Tag size='large'>{group}</Tag>;
} else {
return <Tag size='large' color={stringToColor(group)}>{group}</Tag>;
Expand Down