mirror of
https://github.com/linux-do/new-api.git
synced 2025-09-17 16:06:38 +08:00
feat: 记录自动禁用原因 (close #300)
This commit is contained in:
parent
692455ef2a
commit
eb79880502
@ -1,6 +1,7 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"one-api/common"
|
"one-api/common"
|
||||||
)
|
)
|
||||||
@ -29,6 +30,31 @@ type Channel struct {
|
|||||||
StatusCodeMapping *string `json:"status_code_mapping" gorm:"type:varchar(1024);default:''"`
|
StatusCodeMapping *string `json:"status_code_mapping" gorm:"type:varchar(1024);default:''"`
|
||||||
Priority *int64 `json:"priority" gorm:"bigint;default:0"`
|
Priority *int64 `json:"priority" gorm:"bigint;default:0"`
|
||||||
AutoBan *int `json:"auto_ban" gorm:"default:1"`
|
AutoBan *int `json:"auto_ban" gorm:"default:1"`
|
||||||
|
OtherInfo string `json:"other_info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (channel *Channel) GetOtherInfo() map[string]interface{} {
|
||||||
|
var otherInfo map[string]interface{}
|
||||||
|
if channel.OtherInfo != "" {
|
||||||
|
err := json.Unmarshal([]byte(channel.OtherInfo), &otherInfo)
|
||||||
|
if err != nil {
|
||||||
|
common.SysError("failed to unmarshal other info: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return otherInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func (channel *Channel) SetOtherInfo(otherInfo map[string]interface{}) {
|
||||||
|
otherInfoBytes, err := json.Marshal(otherInfo)
|
||||||
|
if err != nil {
|
||||||
|
common.SysError("failed to marshal other info: " + err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
channel.OtherInfo = string(otherInfoBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (channel *Channel) Save() error {
|
||||||
|
return DB.Save(channel).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllChannels(startIdx int, num int, selectAll bool, idSort bool) ([]*Channel, error) {
|
func GetAllChannels(startIdx int, num int, selectAll bool, idSort bool) ([]*Channel, error) {
|
||||||
@ -213,15 +239,30 @@ func (channel *Channel) Delete() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateChannelStatusById(id int, status int) {
|
func UpdateChannelStatusById(id int, status int, reason string) {
|
||||||
err := UpdateAbilityStatus(id, status == common.ChannelStatusEnabled)
|
err := UpdateAbilityStatus(id, status == common.ChannelStatusEnabled)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.SysError("failed to update ability status: " + err.Error())
|
common.SysError("failed to update ability status: " + err.Error())
|
||||||
}
|
}
|
||||||
|
channel, err := GetChannelById(id, true)
|
||||||
|
if err != nil {
|
||||||
|
// find channel by id error, directly update status
|
||||||
err = DB.Model(&Channel{}).Where("id = ?", id).Update("status", status).Error
|
err = DB.Model(&Channel{}).Where("id = ?", id).Update("status", status).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.SysError("failed to update channel status: " + err.Error())
|
common.SysError("failed to update channel status: " + err.Error())
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// find channel by id success, update status and other info
|
||||||
|
info := channel.GetOtherInfo()
|
||||||
|
info["status_reason"] = reason
|
||||||
|
channel.SetOtherInfo(info)
|
||||||
|
channel.Status = status
|
||||||
|
err = channel.Save()
|
||||||
|
if err != nil {
|
||||||
|
common.SysError("failed to update channel status: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateChannelUsedQuota(id int, quota int) {
|
func UpdateChannelUsedQuota(id int, quota int) {
|
||||||
|
@ -11,14 +11,14 @@ import (
|
|||||||
|
|
||||||
// disable & notify
|
// disable & notify
|
||||||
func DisableChannel(channelId int, channelName string, reason string) {
|
func DisableChannel(channelId int, channelName string, reason string) {
|
||||||
model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled)
|
model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled, reason)
|
||||||
subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelName, channelId)
|
subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelName, channelId)
|
||||||
content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason)
|
content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason)
|
||||||
notifyRootUser(subject, content)
|
notifyRootUser(subject, content)
|
||||||
}
|
}
|
||||||
|
|
||||||
func EnableChannel(channelId int, channelName string) {
|
func EnableChannel(channelId int, channelName string) {
|
||||||
model.UpdateChannelStatusById(channelId, common.ChannelStatusEnabled)
|
model.UpdateChannelStatusById(channelId, common.ChannelStatusEnabled, "")
|
||||||
subject := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
subject := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
||||||
content := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
content := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
||||||
notifyRootUser(subject, content)
|
notifyRootUser(subject, content)
|
||||||
|
@ -225,6 +225,14 @@ const TokensTable = () => {
|
|||||||
onOpenLink('next-mj', record.key);
|
onOpenLink('next-mj', record.key);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
node: 'item',
|
||||||
|
key: 'lobe',
|
||||||
|
name: 'Lobe Chat',
|
||||||
|
onClick: () => {
|
||||||
|
onOpenLink('lobe', record.key);
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
node: 'item',
|
node: 'item',
|
||||||
key: 'ama',
|
key: 'ama',
|
||||||
@ -377,51 +385,6 @@ const TokensTable = () => {
|
|||||||
await loadTokens(activePage - 1);
|
await loadTokens(activePage - 1);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onCopy = async (type, key) => {
|
|
||||||
let status = localStorage.getItem('status');
|
|
||||||
let serverAddress = '';
|
|
||||||
if (status) {
|
|
||||||
status = JSON.parse(status);
|
|
||||||
serverAddress = status.server_address;
|
|
||||||
}
|
|
||||||
if (serverAddress === '') {
|
|
||||||
serverAddress = window.location.origin;
|
|
||||||
}
|
|
||||||
let encodedServerAddress = encodeURIComponent(serverAddress);
|
|
||||||
const nextLink = localStorage.getItem('chat_link');
|
|
||||||
const mjLink = localStorage.getItem('chat_link2');
|
|
||||||
let nextUrl;
|
|
||||||
|
|
||||||
if (nextLink) {
|
|
||||||
nextUrl =
|
|
||||||
nextLink + `/#/?settings={"key":"sk-${key}","url":"${serverAddress}"}`;
|
|
||||||
} else {
|
|
||||||
nextUrl = `https://chat.oneapi.pro/#/?settings={"key":"sk-${key}","url":"${serverAddress}"}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
let url;
|
|
||||||
switch (type) {
|
|
||||||
case 'ama':
|
|
||||||
url =
|
|
||||||
mjLink + `/#/?settings={"key":"sk-${key}","url":"${serverAddress}"}`;
|
|
||||||
break;
|
|
||||||
case 'opencat':
|
|
||||||
url = `opencat://team/join?domain=${encodedServerAddress}&token=sk-${key}`;
|
|
||||||
break;
|
|
||||||
case 'next':
|
|
||||||
url = nextUrl;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
url = `sk-${key}`;
|
|
||||||
}
|
|
||||||
// if (await copy(url)) {
|
|
||||||
// showSuccess('已复制到剪贴板!');
|
|
||||||
// } else {
|
|
||||||
// showWarning('无法复制到剪贴板,请手动复制,已将令牌填入搜索框。');
|
|
||||||
// setSearchKeyword(url);
|
|
||||||
// }
|
|
||||||
};
|
|
||||||
|
|
||||||
const copyText = async (text) => {
|
const copyText = async (text) => {
|
||||||
if (await copy(text)) {
|
if (await copy(text)) {
|
||||||
showSuccess('已复制到剪贴板!');
|
showSuccess('已复制到剪贴板!');
|
||||||
@ -461,6 +424,9 @@ const TokensTable = () => {
|
|||||||
case 'opencat':
|
case 'opencat':
|
||||||
url = `opencat://team/join?domain=${encodedServerAddress}&token=sk-${key}`;
|
url = `opencat://team/join?domain=${encodedServerAddress}&token=sk-${key}`;
|
||||||
break;
|
break;
|
||||||
|
case 'lobe':
|
||||||
|
url = `https://chat-preview.lobehub.com/?settings={"keyVaults":{"openai":{"apiKey":"sk-${key}","baseURL":"${encodedServerAddress}"}}}`;
|
||||||
|
break;
|
||||||
case 'next-mj':
|
case 'next-mj':
|
||||||
url =
|
url =
|
||||||
mjLink + `/#/?settings={"key":"sk-${key}","url":"${serverAddress}"}`;
|
mjLink + `/#/?settings={"key":"sk-${key}","url":"${serverAddress}"}`;
|
||||||
|
Loading…
Reference in New Issue
Block a user