处理余额问题OpenAi余额显示问题

This commit is contained in:
jau1 2023-12-13 11:16:52 +08:00
parent 461f5dab56
commit 4414c982d1
2 changed files with 35 additions and 2 deletions

View File

@ -34,15 +34,28 @@ func GetAllChannels(startIdx int, num int, selectAll bool) ([]*Channel, error) {
} else { } else {
err = DB.Order("id desc").Limit(num).Offset(startIdx).Omit("key").Find(&channels).Error err = DB.Order("id desc").Limit(num).Offset(startIdx).Omit("key").Find(&channels).Error
} }
for i, v := range channels {
if v.Type == common.ChannelTypeOpenAI {
channels[i].Balance = v.Balance - float64(v.UsedQuota)/500000.0
if channels[i].Balance < 0 {
channels[i].Balance = 0
}
}
}
return channels, err return channels, err
} }
func SearchChannels(keyword string) (channels []*Channel, err error) { func SearchChannels(keyword string) (channels []*Channel, err error) {
keyCol := "`key`" keyCol := "`key`"
if common.UsingPostgreSQL { if common.UsingPostgreSQL {
keyCol = `"key"` keyCol = `"key"`
} }
err = DB.Omit("key").Where("id = ? or name LIKE ? or "+keyCol+" = ?", common.String2Int(keyword), keyword+"%", keyword).Find(&channels).Error err = DB.Omit("key").Where("id = ? or name LIKE ? or "+keyCol+" = ?", common.String2Int(keyword), keyword+"%", keyword).Find(&channels).Error
for i, v := range channels {
channels[i].Balance = v.Balance - float64(v.UsedQuota)/500000.0
if channels[i].Balance < 0 {
channels[i].Balance = 0
}
}
return channels, err return channels, err
} }
@ -54,6 +67,10 @@ func GetChannelById(id int, selectAll bool) (*Channel, error) {
} else { } else {
err = DB.Omit("key").First(&channel, "id = ?", id).Error err = DB.Omit("key").First(&channel, "id = ?", id).Error
} }
channel.Balance = channel.Balance - float64(channel.UsedQuota)/500000.0
if channel.Balance < 0 {
channel.Balance = 0
}
return &channel, err return &channel, err
} }

View File

@ -44,7 +44,8 @@ const EditChannel = () => {
other: '', other: '',
model_mapping: '', model_mapping: '',
models: [], models: [],
groups: ['default'] groups: ['default'],
balance: 0.0
}; };
const [batch, setBatch] = useState(false); const [batch, setBatch] = useState(false);
const [inputs, setInputs] = useState(originInputs); const [inputs, setInputs] = useState(originInputs);
@ -194,6 +195,7 @@ const EditChannel = () => {
let res; let res;
localInputs.models = localInputs.models.join(','); localInputs.models = localInputs.models.join(',');
localInputs.group = localInputs.groups.join(','); localInputs.group = localInputs.groups.join(',');
localInputs.balance = parseFloat(localInputs.balance);
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 {
@ -375,6 +377,20 @@ const EditChannel = () => {
options={modelOptions} options={modelOptions}
/> />
</Form.Field> </Form.Field>
{
!isEdit && (
<Form.Field>
<Form.Input
label='余额'
name='balance'
placeholder={'请输入余额'}
onChange={handleInputChange}
value={inputs.balance}
autoComplete='new-password'
/>
</Form.Field>
)
}
<div style={{ lineHeight: '40px', marginBottom: '12px' }}> <div style={{ lineHeight: '40px', marginBottom: '12px' }}>
<Button type={'button'} onClick={() => { <Button type={'button'} onClick={() => {
handleInputChange(null, { name: 'models', value: basicModels }); handleInputChange(null, { name: 'models', value: basicModels });