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="账户信息" >
- + - {{ form.mobile }} - - - - - - - - + {{ user.mobile }} - {{ form['calls'] }} + {{ user['calls'] }} - {{ form['img_calls'] }} + {{ user['img_calls'] }} - - {{ form['total_tokens'] }} + + {{ user['tokens'] }} - - + + {{ user['total_tokens'] }} - - - - - + + {{ dateFormat(user['expired_time']) }}
- - @@ -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="用户登录" >
- + @@ -33,12 +33,12 @@
- 登录 + 登录
@@ -90,9 +90,17 @@ const close = function () { border-radius 20px .label { + padding-top 3px + .el-icon { - font-size 16px + position relative + font-size 20px margin-right 6px + top 4px + } + + span { + font-size 16px } } diff --git a/web/src/components/UserProfile.vue b/web/src/components/UserProfile.vue index 7e5f8bff..2ba963b3 100644 --- a/web/src/components/UserProfile.vue +++ b/web/src/components/UserProfile.vue @@ -32,6 +32,20 @@ {{ dateFormat(user['expired_time']) }} + + + + + + + + + + + + + 保存 + @@ -43,6 +57,7 @@ import {ElMessage} from "element-plus"; import {Plus} from "@element-plus/icons-vue"; import Compressor from "compressorjs"; import {dateFormat} from "@/utils/libs"; +import {checkSession} from "@/action/session"; const user = ref({ username: '', @@ -55,13 +70,17 @@ const user = ref({ }) onMounted(() => { - // 获取最新用户信息 - httpGet('/api/user/profile').then(res => { - user.value = res.data - user.value.chat_config.api_keys = res.data.chat_config.api_keys ?? {OpenAI: "", Azure: "", ChatGLM: ""} + checkSession().then(() => { + // 获取最新用户信息 + httpGet('/api/user/profile').then(res => { + 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) + }); }).catch(e => { - ElMessage.error("获取用户信息失败:" + e.message) - }); + console.log(e) + }) }) const afterRead = (file) => { @@ -74,14 +93,7 @@ const afterRead = (file) => { // 执行上传操作 httpPost('/api/upload', formData).then((res) => { user.value.avatar = res.data - httpPost('/api/user/profile/update', user.value).then(() => { - ElMessage.success({ - message: '更新成功', - duration: 500, - }) - }).catch((e) => { - ElMessage.error('更新失败:' + e.message) - }) + ElMessage.success({message: "上传成功", duration: 500}) }).catch((e) => { ElMessage.error('图片上传失败:' + e.message) }) @@ -91,6 +103,14 @@ const afterRead = (file) => { }, }); }; + +const save = () => { + httpPost('/api/user/profile/update', user.value).then(() => { + ElMessage.success({message: '更新成功', duration: 500}) + }).catch((e) => { + ElMessage.error('更新失败:' + e.message) + }) +} \ No newline at end of file diff --git a/web/src/main.js b/web/src/main.js index 5db4c31e..3119148a 100644 --- a/web/src/main.js +++ b/web/src/main.js @@ -32,7 +32,7 @@ import { TextEllipsis, Uploader } from "vant"; -import router from "@/router"; +import {router} from "@/router"; import 'v3-waterfall/dist/style.css' import V3waterfall from "v3-waterfall"; diff --git a/web/src/router.js b/web/src/router.js index 6b4ec273..200b0ec1 100644 --- a/web/src/router.js +++ b/web/src/router.js @@ -226,12 +226,14 @@ const router = createRouter({ routes: routes, }) +let prevRoute = null // dynamic change the title when router change router.beforeEach((to, from, next) => { if (to.meta.title) { document.title = `${to.meta.title} | ${process.env.VUE_APP_TITLE}` } + prevRoute = from next() }) -export default router; \ No newline at end of file +export {router, prevRoute}; \ No newline at end of file diff --git a/web/src/views/ChatApps.vue b/web/src/views/ChatApps.vue index 57f1bcbc..78d21188 100644 --- a/web/src/views/ChatApps.vue +++ b/web/src/views/ChatApps.vue @@ -40,7 +40,7 @@