mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-10 18:43:41 +08:00
Compare commits
18 Commits
v0.4.2
...
v0.4.4-alp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e87ad1f402 | ||
|
|
07cccdc8c0 | ||
|
|
f71f01662c | ||
|
|
54d7a1c2e8 | ||
|
|
f426f31bd7 | ||
|
|
2930577cd6 | ||
|
|
e09512177a | ||
|
|
d6dbaff3c2 | ||
|
|
7f9577a386 | ||
|
|
38668e7331 | ||
|
|
323f3d263a | ||
|
|
0c34ed4c61 | ||
|
|
7c7eb6b7ec | ||
|
|
8b2ef666ef | ||
|
|
955d5f8707 | ||
|
|
47ca449e32 | ||
|
|
39481eb6c0 | ||
|
|
69153e7231 |
23
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
23
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
---
|
||||||
|
name: 报告问题
|
||||||
|
about: 使用简练详细的语言描述你遇到的问题
|
||||||
|
title: ''
|
||||||
|
labels: bug
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**例行检查**
|
||||||
|
+ [ ] 我已确认目前没有类似 issue
|
||||||
|
+ [ ] 我已确认我已升级到最新版本
|
||||||
|
+ [ ] 我理解并愿意跟进此 issue,协助测试和提供反馈
|
||||||
|
+ [ ] 我理解并认可上述内容,并理解项目维护者精力有限,不遵循规则的 issue 可能会被无视或直接关闭
|
||||||
|
|
||||||
|
**问题描述**
|
||||||
|
|
||||||
|
**复现步骤**
|
||||||
|
|
||||||
|
**预期结果**
|
||||||
|
|
||||||
|
**相关截图**
|
||||||
|
如果没有的话,请删除此节。
|
||||||
11
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
11
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
blank_issues_enabled: false
|
||||||
|
contact_links:
|
||||||
|
- name: 项目群聊
|
||||||
|
url: https://openai.justsong.cn/
|
||||||
|
about: 演示站首页有官方群聊信息
|
||||||
|
- name: 赞赏支持
|
||||||
|
url: https://iamazing.cn/page/reward
|
||||||
|
about: 请作者喝杯咖啡,以激励作者持续开发
|
||||||
|
- name: 付费部署或定制功能
|
||||||
|
url: https://openai.justsong.cn/
|
||||||
|
about: 加群后联系群主
|
||||||
18
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
18
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
---
|
||||||
|
name: 功能请求
|
||||||
|
about: 使用简练详细的语言描述希望加入的新功能
|
||||||
|
title: ''
|
||||||
|
labels: enhancement
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**例行检查**
|
||||||
|
+ [ ] 我已确认目前没有类似 issue
|
||||||
|
+ [ ] 我已确认我已升级到最新版本
|
||||||
|
+ [ ] 我理解并愿意跟进此 issue,协助测试和提供反馈
|
||||||
|
+ [ ] 我理解并认可上述内容,并理解项目维护者精力有限,不遵循规则的 issue 可能会被无视或直接关闭
|
||||||
|
|
||||||
|
**功能描述**
|
||||||
|
|
||||||
|
**应用场景**
|
||||||
10
README.md
10
README.md
@@ -117,6 +117,8 @@ sudo certbot --nginx
|
|||||||
sudo service nginx restart
|
sudo service nginx restart
|
||||||
```
|
```
|
||||||
|
|
||||||
|
初始账号用户名为 `root`,密码为 `123456`。
|
||||||
|
|
||||||
### 手动部署
|
### 手动部署
|
||||||
1. 从 [GitHub Releases](https://github.com/songquanpeng/one-api/releases/latest) 下载可执行文件或者从源码编译:
|
1. 从 [GitHub Releases](https://github.com/songquanpeng/one-api/releases/latest) 下载可执行文件或者从源码编译:
|
||||||
```shell
|
```shell
|
||||||
@@ -201,3 +203,11 @@ https://openai.justsong.cn
|
|||||||
+ 令牌额度仅供用户设置最大使用量,用户可自由设置。
|
+ 令牌额度仅供用户设置最大使用量,用户可自由设置。
|
||||||
2. 宝塔部署后访问出现空白页面?
|
2. 宝塔部署后访问出现空白页面?
|
||||||
+ 自动配置的问题,详见[#97](https://github.com/songquanpeng/one-api/issues/97)。
|
+ 自动配置的问题,详见[#97](https://github.com/songquanpeng/one-api/issues/97)。
|
||||||
|
3. 提示无可用渠道?
|
||||||
|
+ 请检查的用户分组和渠道分组设置。
|
||||||
|
+ 以及渠道的模型设置。
|
||||||
|
|
||||||
|
## 注意
|
||||||
|
本项目为开源项目,请在遵循 OpenAI 的[使用条款](https://openai.com/policies/terms-of-use)以及法律法规的情况下使用,不得用于非法用途。
|
||||||
|
|
||||||
|
本项目依据 MIT 协议开源,请以某种方式保留 One API 的版权信息。
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ func GroupRatio2JSONString() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UpdateGroupRatioByJSONString(jsonStr string) error {
|
func UpdateGroupRatioByJSONString(jsonStr string) error {
|
||||||
|
GroupRatio = make(map[string]float64)
|
||||||
return json.Unmarshal([]byte(jsonStr), &GroupRatio)
|
return json.Unmarshal([]byte(jsonStr), &GroupRatio)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,16 +2,23 @@ package common
|
|||||||
|
|
||||||
import "encoding/json"
|
import "encoding/json"
|
||||||
|
|
||||||
|
// ModelRatio
|
||||||
// https://platform.openai.com/docs/models/model-endpoint-compatibility
|
// https://platform.openai.com/docs/models/model-endpoint-compatibility
|
||||||
// https://openai.com/pricing
|
// https://openai.com/pricing
|
||||||
// TODO: when a new api is enabled, check the pricing here
|
// TODO: when a new api is enabled, check the pricing here
|
||||||
|
// 1 === $0.002 / 1K tokens
|
||||||
var ModelRatio = map[string]float64{
|
var ModelRatio = map[string]float64{
|
||||||
"gpt-4": 15,
|
"gpt-4": 15,
|
||||||
"gpt-4-0314": 15,
|
"gpt-4-0314": 15,
|
||||||
|
"gpt-4-0613": 15,
|
||||||
"gpt-4-32k": 30,
|
"gpt-4-32k": 30,
|
||||||
"gpt-4-32k-0314": 30,
|
"gpt-4-32k-0314": 30,
|
||||||
"gpt-3.5-turbo": 1, // $0.002 / 1K tokens
|
"gpt-4-32k-0613": 30,
|
||||||
"gpt-3.5-turbo-0301": 1,
|
"gpt-3.5-turbo": 0.75, // $0.0015 / 1K tokens
|
||||||
|
"gpt-3.5-turbo-0301": 0.75,
|
||||||
|
"gpt-3.5-turbo-0613": 0.75,
|
||||||
|
"gpt-3.5-turbo-16k": 1.5, // $0.003 / 1K tokens
|
||||||
|
"gpt-3.5-turbo-16k-0613": 1.5,
|
||||||
"text-ada-001": 0.2,
|
"text-ada-001": 0.2,
|
||||||
"text-babbage-001": 0.25,
|
"text-babbage-001": 0.25,
|
||||||
"text-curie-001": 1,
|
"text-curie-001": 1,
|
||||||
@@ -39,6 +46,7 @@ func ModelRatio2JSONString() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UpdateModelRatioByJSONString(jsonStr string) error {
|
func UpdateModelRatioByJSONString(jsonStr string) error {
|
||||||
|
ModelRatio = make(map[string]float64)
|
||||||
return json.Unmarshal([]byte(jsonStr), &ModelRatio)
|
return json.Unmarshal([]byte(jsonStr), &ModelRatio)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -37,6 +37,58 @@ type OpenAIUsageResponse struct {
|
|||||||
TotalUsage float64 `json:"total_usage"` // unit: 0.01 dollar
|
TotalUsage float64 `json:"total_usage"` // unit: 0.01 dollar
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OpenAISBUsageResponse struct {
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
Data *struct {
|
||||||
|
Credit string `json:"credit"`
|
||||||
|
} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetResponseBody(method, url string, channel *model.Channel) ([]byte, error) {
|
||||||
|
client := &http.Client{}
|
||||||
|
req, err := http.NewRequest(method, url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
auth := fmt.Sprintf("Bearer %s", channel.Key)
|
||||||
|
req.Header.Add("Authorization", auth)
|
||||||
|
res, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
body, err := io.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = res.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return body, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateChannelOpenAISBBalance(channel *model.Channel) (float64, error) {
|
||||||
|
url := fmt.Sprintf("https://api.openai-sb.com/sb-api/user/status?api_key=%s", channel.Key)
|
||||||
|
body, err := GetResponseBody("GET", url, channel)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
response := OpenAISBUsageResponse{}
|
||||||
|
err = json.Unmarshal(body, &response)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if response.Data == nil {
|
||||||
|
return 0, errors.New(response.Msg)
|
||||||
|
}
|
||||||
|
balance, err := strconv.ParseFloat(response.Data.Credit, 64)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
channel.UpdateBalance(balance)
|
||||||
|
return balance, nil
|
||||||
|
}
|
||||||
|
|
||||||
func updateChannelBalance(channel *model.Channel) (float64, error) {
|
func updateChannelBalance(channel *model.Channel) (float64, error) {
|
||||||
baseURL := common.ChannelBaseURLs[channel.Type]
|
baseURL := common.ChannelBaseURLs[channel.Type]
|
||||||
switch channel.Type {
|
switch channel.Type {
|
||||||
@@ -48,27 +100,14 @@ func updateChannelBalance(channel *model.Channel) (float64, error) {
|
|||||||
return 0, errors.New("尚未实现")
|
return 0, errors.New("尚未实现")
|
||||||
case common.ChannelTypeCustom:
|
case common.ChannelTypeCustom:
|
||||||
baseURL = channel.BaseURL
|
baseURL = channel.BaseURL
|
||||||
|
case common.ChannelTypeOpenAISB:
|
||||||
|
return updateChannelOpenAISBBalance(channel)
|
||||||
default:
|
default:
|
||||||
return 0, errors.New("尚未实现")
|
return 0, errors.New("尚未实现")
|
||||||
}
|
}
|
||||||
url := fmt.Sprintf("%s/v1/dashboard/billing/subscription", baseURL)
|
url := fmt.Sprintf("%s/v1/dashboard/billing/subscription", baseURL)
|
||||||
|
|
||||||
client := &http.Client{}
|
body, err := GetResponseBody("GET", url, channel)
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
auth := fmt.Sprintf("Bearer %s", channel.Key)
|
|
||||||
req.Header.Add("Authorization", auth)
|
|
||||||
res, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
body, err := io.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
err = res.Body.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@@ -84,20 +123,7 @@ func updateChannelBalance(channel *model.Channel) (float64, error) {
|
|||||||
startDate = now.AddDate(0, 0, -100).Format("2006-01-02")
|
startDate = now.AddDate(0, 0, -100).Format("2006-01-02")
|
||||||
}
|
}
|
||||||
url = fmt.Sprintf("%s/v1/dashboard/billing/usage?start_date=%s&end_date=%s", baseURL, startDate, endDate)
|
url = fmt.Sprintf("%s/v1/dashboard/billing/usage?start_date=%s&end_date=%s", baseURL, startDate, endDate)
|
||||||
req, err = http.NewRequest("GET", url, nil)
|
body, err = GetResponseBody("GET", url, channel)
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
req.Header.Add("Authorization", auth)
|
|
||||||
res, err = client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
body, err = io.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
err = res.Body.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,6 +71,33 @@ func init() {
|
|||||||
Root: "gpt-3.5-turbo-0301",
|
Root: "gpt-3.5-turbo-0301",
|
||||||
Parent: nil,
|
Parent: nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Id: "gpt-3.5-turbo-0613",
|
||||||
|
Object: "model",
|
||||||
|
Created: 1677649963,
|
||||||
|
OwnedBy: "openai",
|
||||||
|
Permission: permission,
|
||||||
|
Root: "gpt-3.5-turbo-0613",
|
||||||
|
Parent: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Id: "gpt-3.5-turbo-16k",
|
||||||
|
Object: "model",
|
||||||
|
Created: 1677649963,
|
||||||
|
OwnedBy: "openai",
|
||||||
|
Permission: permission,
|
||||||
|
Root: "gpt-3.5-turbo-16k",
|
||||||
|
Parent: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Id: "gpt-3.5-turbo-16k-0613",
|
||||||
|
Object: "model",
|
||||||
|
Created: 1677649963,
|
||||||
|
OwnedBy: "openai",
|
||||||
|
Permission: permission,
|
||||||
|
Root: "gpt-3.5-turbo-16k-0613",
|
||||||
|
Parent: nil,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Id: "gpt-4",
|
Id: "gpt-4",
|
||||||
Object: "model",
|
Object: "model",
|
||||||
@@ -89,6 +116,15 @@ func init() {
|
|||||||
Root: "gpt-4-0314",
|
Root: "gpt-4-0314",
|
||||||
Parent: nil,
|
Parent: nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Id: "gpt-4-0613",
|
||||||
|
Object: "model",
|
||||||
|
Created: 1677649963,
|
||||||
|
OwnedBy: "openai",
|
||||||
|
Permission: permission,
|
||||||
|
Root: "gpt-4-0613",
|
||||||
|
Parent: nil,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Id: "gpt-4-32k",
|
Id: "gpt-4-32k",
|
||||||
Object: "model",
|
Object: "model",
|
||||||
@@ -107,6 +143,15 @@ func init() {
|
|||||||
Root: "gpt-4-32k-0314",
|
Root: "gpt-4-32k-0314",
|
||||||
Parent: nil,
|
Parent: nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Id: "gpt-4-32k-0613",
|
||||||
|
Object: "model",
|
||||||
|
Created: 1677649963,
|
||||||
|
OwnedBy: "openai",
|
||||||
|
Permission: permission,
|
||||||
|
Root: "gpt-4-32k-0613",
|
||||||
|
Parent: nil,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Id: "text-embedding-ada-002",
|
Id: "text-embedding-ada-002",
|
||||||
Object: "model",
|
Object: "model",
|
||||||
|
|||||||
@@ -58,6 +58,20 @@ func countTokenMessages(messages []Message, model string) int {
|
|||||||
return tokenNum
|
return tokenNum
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func countTokenInput(input any, model string) int {
|
||||||
|
switch input.(type) {
|
||||||
|
case string:
|
||||||
|
return countTokenText(input.(string), model)
|
||||||
|
case []string:
|
||||||
|
text := ""
|
||||||
|
for _, s := range input.([]string) {
|
||||||
|
text += s
|
||||||
|
}
|
||||||
|
return countTokenText(text, model)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
func countTokenText(text string, model string) int {
|
func countTokenText(text string, model string) int {
|
||||||
tokenEncoder := getTokenEncoder(model)
|
tokenEncoder := getTokenEncoder(model)
|
||||||
token := tokenEncoder.Encode(text, nil, nil)
|
token := tokenEncoder.Encode(text, nil, nil)
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ type GeneralOpenAIRequest struct {
|
|||||||
Temperature float64 `json:"temperature"`
|
Temperature float64 `json:"temperature"`
|
||||||
TopP float64 `json:"top_p"`
|
TopP float64 `json:"top_p"`
|
||||||
N int `json:"n"`
|
N int `json:"n"`
|
||||||
Input string `json:"input"`
|
Input any `json:"input"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatRequest struct {
|
type ChatRequest struct {
|
||||||
@@ -177,6 +177,7 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|||||||
// https://github.com/songquanpeng/one-api/issues/67
|
// https://github.com/songquanpeng/one-api/issues/67
|
||||||
model_ = strings.TrimSuffix(model_, "-0301")
|
model_ = strings.TrimSuffix(model_, "-0301")
|
||||||
model_ = strings.TrimSuffix(model_, "-0314")
|
model_ = strings.TrimSuffix(model_, "-0314")
|
||||||
|
model_ = strings.TrimSuffix(model_, "-0613")
|
||||||
fullRequestURL = fmt.Sprintf("%s/openai/deployments/%s/%s", baseURL, model_, task)
|
fullRequestURL = fmt.Sprintf("%s/openai/deployments/%s/%s", baseURL, model_, task)
|
||||||
} else if channelType == common.ChannelTypePaLM {
|
} else if channelType == common.ChannelTypePaLM {
|
||||||
err := relayPaLM(textRequest, c)
|
err := relayPaLM(textRequest, c)
|
||||||
@@ -189,7 +190,7 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|||||||
case RelayModeCompletions:
|
case RelayModeCompletions:
|
||||||
promptTokens = countTokenText(textRequest.Prompt, textRequest.Model)
|
promptTokens = countTokenText(textRequest.Prompt, textRequest.Model)
|
||||||
case RelayModeModeration:
|
case RelayModeModeration:
|
||||||
promptTokens = countTokenText(textRequest.Input, textRequest.Model)
|
promptTokens = countTokenInput(textRequest.Input, textRequest.Model)
|
||||||
}
|
}
|
||||||
preConsumedTokens := common.PreConsumedQuota
|
preConsumedTokens := common.PreConsumedQuota
|
||||||
if textRequest.MaxTokens != 0 {
|
if textRequest.MaxTokens != 0 {
|
||||||
@@ -239,16 +240,15 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|||||||
defer func() {
|
defer func() {
|
||||||
if consumeQuota {
|
if consumeQuota {
|
||||||
quota := 0
|
quota := 0
|
||||||
usingGPT4 := strings.HasPrefix(textRequest.Model, "gpt-4")
|
completionRatio := 1.34 // default for gpt-3
|
||||||
completionRatio := 1
|
if strings.HasPrefix(textRequest.Model, "gpt-4") {
|
||||||
if usingGPT4 {
|
|
||||||
completionRatio = 2
|
completionRatio = 2
|
||||||
}
|
}
|
||||||
if isStream {
|
if isStream {
|
||||||
responseTokens := countTokenText(streamResponseText, textRequest.Model)
|
responseTokens := countTokenText(streamResponseText, textRequest.Model)
|
||||||
quota = promptTokens + responseTokens*completionRatio
|
quota = promptTokens + int(float64(responseTokens)*completionRatio)
|
||||||
} else {
|
} else {
|
||||||
quota = textResponse.Usage.PromptTokens + textResponse.Usage.CompletionTokens*completionRatio
|
quota = textResponse.Usage.PromptTokens + int(float64(textResponse.Usage.CompletionTokens)*completionRatio)
|
||||||
}
|
}
|
||||||
quota = int(float64(quota) * ratio)
|
quota = int(float64(quota) * ratio)
|
||||||
if ratio != 0 && quota <= 0 {
|
if ratio != 0 && quota <= 0 {
|
||||||
@@ -260,7 +260,7 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|||||||
common.SysError("Error consuming token remain quota: " + err.Error())
|
common.SysError("Error consuming token remain quota: " + err.Error())
|
||||||
}
|
}
|
||||||
userId := c.GetInt("id")
|
userId := c.GetInt("id")
|
||||||
model.RecordLog(userId, model.LogTypeConsume, fmt.Sprintf("使用模型 %s 消耗 %d 点额度(模型倍率 %.2f,分组倍率 %.2f)", textRequest.Model, quota, modelRatio, groupRatio))
|
model.RecordLog(userId, model.LogTypeConsume, fmt.Sprintf("使用模型 %s 消耗 %d 点额度(模型倍率 %.2f,分组倍率 %.2f,补全倍率 %.2f)", textRequest.Model, quota, modelRatio, groupRatio, completionRatio))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
@@ -30,16 +30,19 @@ func GetRandomSatisfiedChannel(group string, model string) (*Channel, error) {
|
|||||||
|
|
||||||
func (channel *Channel) AddAbilities() error {
|
func (channel *Channel) AddAbilities() error {
|
||||||
models_ := strings.Split(channel.Models, ",")
|
models_ := strings.Split(channel.Models, ",")
|
||||||
|
groups_ := strings.Split(channel.Group, ",")
|
||||||
abilities := make([]Ability, 0, len(models_))
|
abilities := make([]Ability, 0, len(models_))
|
||||||
for _, model := range models_ {
|
for _, model := range models_ {
|
||||||
|
for _, group := range groups_ {
|
||||||
ability := Ability{
|
ability := Ability{
|
||||||
Group: channel.Group,
|
Group: group,
|
||||||
Model: model,
|
Model: model,
|
||||||
ChannelId: channel.Id,
|
ChannelId: channel.Id,
|
||||||
Enabled: channel.Status == common.ChannelStatusEnabled,
|
Enabled: channel.Status == common.ChannelStatusEnabled,
|
||||||
}
|
}
|
||||||
abilities = append(abilities, ability)
|
abilities = append(abilities, ability)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return DB.Create(&abilities).Error
|
return DB.Create(&abilities).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,13 @@ function renderType(type) {
|
|||||||
return <Label basic color={type2label[type].color}>{type2label[type].text}</Label>;
|
return <Label basic color={type2label[type].color}>{type2label[type].text}</Label>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function renderBalance(type, balance) {
|
||||||
|
if (type === 5) {
|
||||||
|
return <span>¥{(balance / 10000).toFixed(2)}</span>
|
||||||
|
}
|
||||||
|
return <span>${balance.toFixed(2)}</span>
|
||||||
|
}
|
||||||
|
|
||||||
const ChannelsTable = () => {
|
const ChannelsTable = () => {
|
||||||
const [channels, setChannels] = useState([]);
|
const [channels, setChannels] = useState([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
@@ -336,7 +343,7 @@ const ChannelsTable = () => {
|
|||||||
<Popup
|
<Popup
|
||||||
content={channel.balance_updated_time ? renderTimestamp(channel.balance_updated_time) : '未更新'}
|
content={channel.balance_updated_time ? renderTimestamp(channel.balance_updated_time) : '未更新'}
|
||||||
key={channel.id}
|
key={channel.id}
|
||||||
trigger={<span>${channel.balance.toFixed(2)}</span>}
|
trigger={renderBalance(channel.type, channel.balance)}
|
||||||
basic
|
basic
|
||||||
/>
|
/>
|
||||||
</Table.Cell>
|
</Table.Cell>
|
||||||
|
|||||||
@@ -10,10 +10,17 @@ export function renderText(text, limit) {
|
|||||||
export function renderGroup(group) {
|
export function renderGroup(group) {
|
||||||
if (group === "") {
|
if (group === "") {
|
||||||
return <Label>default</Label>
|
return <Label>default</Label>
|
||||||
} else if (group === "vip" || group === "pro") {
|
}
|
||||||
|
let groups = group.split(",");
|
||||||
|
groups.sort();
|
||||||
|
return <>
|
||||||
|
{groups.map((group) => {
|
||||||
|
if (group === "vip" || group === "pro") {
|
||||||
return <Label color='yellow'>{group}</Label>
|
return <Label color='yellow'>{group}</Label>
|
||||||
} else if (group === "svip" || group === "premium") {
|
} else if (group === "svip" || group === "premium") {
|
||||||
return <Label color='red'>{group}</Label>
|
return <Label color='red'>{group}</Label>
|
||||||
}
|
}
|
||||||
return <Label>{group}</Label>
|
return <Label>{group}</Label>
|
||||||
|
})}
|
||||||
|
</>
|
||||||
}
|
}
|
||||||
@@ -15,8 +15,8 @@ const EditChannel = () => {
|
|||||||
key: '',
|
key: '',
|
||||||
base_url: '',
|
base_url: '',
|
||||||
other: '',
|
other: '',
|
||||||
group: 'default',
|
|
||||||
models: [],
|
models: [],
|
||||||
|
groups: ['default']
|
||||||
};
|
};
|
||||||
const [batch, setBatch] = useState(false);
|
const [batch, setBatch] = useState(false);
|
||||||
const [inputs, setInputs] = useState(originInputs);
|
const [inputs, setInputs] = useState(originInputs);
|
||||||
@@ -37,6 +37,11 @@ const EditChannel = () => {
|
|||||||
} else {
|
} else {
|
||||||
data.models = data.models.split(",")
|
data.models = data.models.split(",")
|
||||||
}
|
}
|
||||||
|
if (data.group === "") {
|
||||||
|
data.groups = []
|
||||||
|
} else {
|
||||||
|
data.groups = data.group.split(",")
|
||||||
|
}
|
||||||
setInputs(data);
|
setInputs(data);
|
||||||
} else {
|
} else {
|
||||||
showError(message);
|
showError(message);
|
||||||
@@ -61,7 +66,7 @@ const EditChannel = () => {
|
|||||||
|
|
||||||
const fetchGroups = async () => {
|
const fetchGroups = async () => {
|
||||||
try {
|
try {
|
||||||
let res = await API.get(`/api/group`);
|
let res = await API.get(`/api/group/`);
|
||||||
setGroupOptions(res.data.data.map((group) => ({
|
setGroupOptions(res.data.data.map((group) => ({
|
||||||
key: group,
|
key: group,
|
||||||
text: group,
|
text: group,
|
||||||
@@ -94,6 +99,7 @@ const EditChannel = () => {
|
|||||||
}
|
}
|
||||||
let res;
|
let res;
|
||||||
localInputs.models = localInputs.models.join(",")
|
localInputs.models = localInputs.models.join(",")
|
||||||
|
localInputs.group = localInputs.groups.join(",")
|
||||||
if (isEdit) {
|
if (isEdit) {
|
||||||
res = await API.put(`/api/channel/`, { ...localInputs, id: parseInt(channelId) });
|
res = await API.put(`/api/channel/`, { ...localInputs, id: parseInt(channelId) });
|
||||||
} else {
|
} else {
|
||||||
@@ -185,14 +191,14 @@ const EditChannel = () => {
|
|||||||
<Form.Dropdown
|
<Form.Dropdown
|
||||||
label='分组'
|
label='分组'
|
||||||
placeholder={'请选择分组'}
|
placeholder={'请选择分组'}
|
||||||
name='group'
|
name='groups'
|
||||||
fluid
|
fluid
|
||||||
search
|
multiple
|
||||||
selection
|
selection
|
||||||
allowAdditions
|
allowAdditions
|
||||||
additionLabel={'请在系统设置页面编辑分组倍率以添加新的分组:'}
|
additionLabel={'请在系统设置页面编辑分组倍率以添加新的分组:'}
|
||||||
onChange={handleInputChange}
|
onChange={handleInputChange}
|
||||||
value={inputs.group}
|
value={inputs.groups}
|
||||||
autoComplete='new-password'
|
autoComplete='new-password'
|
||||||
options={groupOptions}
|
options={groupOptions}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ const EditUser = () => {
|
|||||||
};
|
};
|
||||||
const fetchGroups = async () => {
|
const fetchGroups = async () => {
|
||||||
try {
|
try {
|
||||||
let res = await API.get(`/api/group`);
|
let res = await API.get(`/api/group/`);
|
||||||
setGroupOptions(res.data.data.map((group) => ({
|
setGroupOptions(res.data.data.map((group) => ({
|
||||||
key: group,
|
key: group,
|
||||||
text: group,
|
text: group,
|
||||||
|
|||||||
Reference in New Issue
Block a user