diff --git a/api/handler/chatimpl/azure_handler.go b/api/handler/chatimpl/azure_handler.go index 297fa91a..e800544d 100644 --- a/api/handler/chatimpl/azure_handler.go +++ b/api/handler/chatimpl/azure_handler.go @@ -242,8 +242,7 @@ func (h *ChatHandler) sendAzureMessage( } // 更新用户信息 - h.db.Model(&model.User{}).Where("id = ?", userVo.Id). - UpdateColumn("total_tokens", gorm.Expr("total_tokens + ?", totalTokens)) + h.incUserTokenFee(userVo.Id, totalTokens) } // 保存当前会话 diff --git a/api/handler/chatimpl/baidu_handler.go b/api/handler/chatimpl/baidu_handler.go index 1f8007c5..1ca49e57 100644 --- a/api/handler/chatimpl/baidu_handler.go +++ b/api/handler/chatimpl/baidu_handler.go @@ -9,7 +9,6 @@ import ( "context" "encoding/json" "fmt" - "gorm.io/gorm" "io" "net/http" "strings" @@ -184,8 +183,7 @@ func (h *ChatHandler) sendBaiduMessage( logger.Error("failed to save reply history message: ", res.Error) } // 更新用户信息 - h.db.Model(&model.User{}).Where("id = ?", userVo.Id). - UpdateColumn("total_tokens", gorm.Expr("total_tokens + ?", totalTokens)) + h.incUserTokenFee(userVo.Id, totalTokens) } // 保存当前会话 diff --git a/api/handler/chatimpl/chat_handler.go b/api/handler/chatimpl/chat_handler.go index d56d00aa..c1480c2c 100644 --- a/api/handler/chatimpl/chat_handler.go +++ b/api/handler/chatimpl/chat_handler.go @@ -187,8 +187,14 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session *types.ChatSessio return nil } + if userVo.Calls < session.Model.Weight { + utils.ReplyMessage(ws, fmt.Sprintf("您当前剩余对话次数(%d)已不足以支付当前模型的单次对话需要消耗的对话额度(%d)!", userVo.Calls, session.Model.Weight)) + utils.ReplyMessage(ws, ErrImg) + return nil + } + if userVo.Calls <= 0 && userVo.ChatConfig.ApiKeys[session.Model.Platform] == "" { - utils.ReplyMessage(ws, "您的对话次数已经用尽,请联系管理员或者点击左下角菜单加入众筹获得100次对话!") + utils.ReplyMessage(ws, "您的对话次数已经用尽,请联系管理员或者充值点卡继续对话!") utils.ReplyMessage(ws, ErrImg) return nil } @@ -477,3 +483,10 @@ func (h *ChatHandler) subUserCalls(userVo vo.User, session *types.ChatSession) { h.db.Model(&model.User{}).Where("id = ?", userVo.Id).UpdateColumn("calls", gorm.Expr("calls - ?", num)) } } + +func (h *ChatHandler) incUserTokenFee(userId uint, tokens int) { + h.db.Model(&model.User{}).Where("id = ?", userId). + UpdateColumn("total_tokens", gorm.Expr("total_tokens + ?", tokens)) + h.db.Model(&model.User{}).Where("id = ?", userId). + UpdateColumn("tokens", gorm.Expr("tokens + ?", tokens)) +} diff --git a/api/handler/chatimpl/chatglm_handler.go b/api/handler/chatimpl/chatglm_handler.go index cfd154d5..3f680cac 100644 --- a/api/handler/chatimpl/chatglm_handler.go +++ b/api/handler/chatimpl/chatglm_handler.go @@ -10,7 +10,6 @@ import ( "encoding/json" "fmt" "github.com/golang-jwt/jwt/v5" - "gorm.io/gorm" "io" "strings" "time" @@ -164,8 +163,7 @@ func (h *ChatHandler) sendChatGLMMessage( logger.Error("failed to save reply history message: ", res.Error) } // 更新用户信息 - h.db.Model(&model.User{}).Where("id = ?", userVo.Id). - UpdateColumn("total_tokens", gorm.Expr("total_tokens + ?", totalTokens)) + h.incUserTokenFee(userVo.Id, totalTokens) } // 保存当前会话 diff --git a/api/handler/chatimpl/openai_handler.go b/api/handler/chatimpl/openai_handler.go index a6ed62be..69a2b85e 100644 --- a/api/handler/chatimpl/openai_handler.go +++ b/api/handler/chatimpl/openai_handler.go @@ -241,8 +241,7 @@ func (h *ChatHandler) sendOpenAiMessage( } // 更新用户信息 - h.db.Model(&model.User{}).Where("id = ?", userVo.Id). - UpdateColumn("total_tokens", gorm.Expr("total_tokens + ?", totalTokens)) + h.incUserTokenFee(userVo.Id, totalTokens) } // 保存当前会话 diff --git a/api/handler/chatimpl/xunfei_handler.go b/api/handler/chatimpl/xunfei_handler.go index 19710568..8169aeb4 100644 --- a/api/handler/chatimpl/xunfei_handler.go +++ b/api/handler/chatimpl/xunfei_handler.go @@ -12,7 +12,6 @@ import ( "encoding/json" "fmt" "github.com/gorilla/websocket" - "gorm.io/gorm" "io" "net/http" "net/url" @@ -227,8 +226,7 @@ func (h *ChatHandler) sendXunFeiMessage( logger.Error("failed to save reply history message: ", res.Error) } // 更新用户信息 - h.db.Model(&model.User{}).Where("id = ?", userVo.Id). - UpdateColumn("total_tokens", gorm.Expr("total_tokens + ?", totalTokens)) + h.incUserTokenFee(userVo.Id, totalTokens) } // 保存当前会话 diff --git a/web/src/components/ConfigDialog.vue b/web/src/components/ConfigDialog.vue index 4ca1f568..4fed0a0e 100644 --- a/web/src/components/ConfigDialog.vue +++ b/web/src/components/ConfigDialog.vue @@ -5,55 +5,30 @@ :close-on-click-modal="true" :before-close="close" style="max-width: 600px" - title="用户设置" + title="账户信息" >
- - - - @@ -63,6 +38,7 @@ import {httpGet, httpPost} from "@/utils/http"; import {ElMessage} from "element-plus"; import {Plus} from "@element-plus/icons-vue"; import Compressor from "compressorjs"; +import {dateFormat} from "@/utils/libs"; // eslint-disable-next-line no-undef const props = defineProps({ @@ -74,7 +50,7 @@ const props = defineProps({ const showDialog = computed(() => { return props.show }) -const form = ref({ +const user = ref({ username: '', nickname: '', avatar: '', @@ -87,50 +63,15 @@ const form = ref({ onMounted(() => { // 获取最新用户信息 httpGet('/api/user/profile').then(res => { - form.value = res.data - form.value.chat_config.api_keys = res.data.chat_config.api_keys ?? {OpenAI: "", Azure: "", ChatGLM: ""} + user.value = res.data + user.value.chat_config.api_keys = res.data.chat_config.api_keys ?? {OpenAI: "", Azure: "", ChatGLM: ""} }).catch(e => { ElMessage.error("获取用户信息失败:" + e.message) }); }) -const afterRead = (file) => { - // console.log(file) - // 压缩图片并上传 - new Compressor(file.file, { - quality: 0.6, - success(result) { - const formData = new FormData(); - formData.append('file', result, result.name); - // 执行上传操作 - httpPost('/api/upload', formData).then((res) => { - form.value.avatar = res.data - ElMessage.success({message: "上传成功", duration: 500}) - }).catch((e) => { - ElMessage.error('上传失败:' + e.message) - }) - }, - error(err) { - console.log(err.message); - }, - }); -}; - // eslint-disable-next-line no-undef -const emits = defineEmits(['hide', 'update-user']); -const save = function () { - httpPost('/api/user/profile/update', form.value).then(() => { - ElMessage.success({ - message: '更新成功', - duration: 500, - onClose: () => emits('hide', false) - }) - // 更新用户数据 - emits('update-user', {nickname: form.value['nickname'], avatar: form.value['avatar']}); - }).catch((e) => { - ElMessage.error('更新失败:' + e.message) - }) -} +const emits = defineEmits(['hide']); const close = function () { emits('hide', false); } diff --git a/web/src/components/LoginDialog.vue b/web/src/components/LoginDialog.vue index 1effc4c5..3499046f 100644 --- a/web/src/components/LoginDialog.vue +++ b/web/src/components/LoginDialog.vue @@ -9,7 +9,7 @@ title="用户登录" >