优化登录逻辑

This commit is contained in:
RockYang 2023-04-08 10:57:46 +08:00
parent df9e587300
commit 208655af5e
5 changed files with 80 additions and 17 deletions

View File

@ -299,8 +299,8 @@ func (s *Server) ListApiKeysHandle(c *gin.Context) {
// GetChatRoleListHandle 获取聊天角色列表 // GetChatRoleListHandle 获取聊天角色列表
func (s *Server) GetChatRoleListHandle(c *gin.Context) { func (s *Server) GetChatRoleListHandle(c *gin.Context) {
var rolesOrder = []string{"gpt", "programmer", "teacher", "red_book", "dou_yin", "weekly_report", "girl_friend", var rolesOrder = []string{"gpt", "teacher", "translator", "english_trainer", "weekly_report", "girl_friend",
"kong_zi", "lu_xun", "steve_jobs", "elon_musk", "translator", "english_trainer", "kong_zi", "lu_xun", "steve_jobs", "elon_musk", "red_book", "dou_yin", "programmer",
"seller", "good_comment", "psychiatrist", "artist"} "seller", "good_comment", "psychiatrist", "artist"}
var res = make([]interface{}, 0) var res = make([]interface{}, 0)
var roles = GetChatRoles() var roles = GetChatRoles()

View File

@ -302,5 +302,8 @@ func (s *Server) LoginHandle(c *gin.Context) {
return return
} }
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Data: sessionId}) c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Data: struct {
User types.User `json:"user"`
SessionId string `json:"session_id"`
}{User: *user, SessionId: sessionId}})
} }

View File

@ -3,13 +3,23 @@
* storage handler * storage handler
*/ */
const SessionIdKey = 'ChatGPT_SESSION_ID'; const SessionUserKey = 'LOGIN_USER';
export const Global = {} export const Global = {}
export function getSessionId() { export function getSessionId() {
return sessionStorage.getItem(SessionIdKey) const user = getLoginUser();
return user ? user['session_id'] : '';
} }
export function setSessionId(value) { export function getLoginUser() {
sessionStorage.setItem(SessionIdKey, value) const value = sessionStorage.getItem(SessionUserKey);
if (value) {
return JSON.parse(value);
} else {
return null;
}
}
export function setLoginUser(value) {
sessionStorage.setItem(SessionUserKey, JSON.stringify(value))
} }

View File

@ -102,12 +102,12 @@
import {defineComponent, nextTick} from 'vue' import {defineComponent, nextTick} from 'vue'
import ChatPrompt from "@/components/ChatPrompt.vue"; import ChatPrompt from "@/components/ChatPrompt.vue";
import ChatReply from "@/components/ChatReply.vue"; import ChatReply from "@/components/ChatReply.vue";
import {randString} from "@/utils/libs"; import {isMobile, randString} from "@/utils/libs";
import {ElMessage, ElMessageBox} from 'element-plus' import {ElMessage, ElMessageBox} from 'element-plus'
import {Tools, Lock, Delete} from '@element-plus/icons-vue' import {Tools, Lock, Delete} from '@element-plus/icons-vue'
import ConfigDialog from '@/components/ConfigDialog.vue' import ConfigDialog from '@/components/ConfigDialog.vue'
import {httpPost, httpGet} from "@/utils/http"; import {httpPost, httpGet} from "@/utils/http";
import {getSessionId, setSessionId} from "@/utils/storage"; import {getSessionId, setLoginUser} from "@/utils/storage";
import hl from 'highlight.js' import hl from 'highlight.js'
import 'highlight.js/styles/a11y-dark.css' import 'highlight.js/styles/a11y-dark.css'
@ -140,6 +140,11 @@ export default defineComponent({
}, },
mounted: function () { mounted: function () {
if (!isMobile()) {
this.$router.push("plus");
return;
}
nextTick(() => { nextTick(() => {
this.chatBoxHeight = window.innerHeight - this.toolBoxHeight; this.chatBoxHeight = window.innerHeight - this.toolBoxHeight;
ElMessage.warning("强烈建议使用PC浏览器访问获的更好的聊天体验") ElMessage.warning("强烈建议使用PC浏览器访问获的更好的聊天体验")
@ -379,7 +384,7 @@ export default defineComponent({
httpPost("/api/login", { httpPost("/api/login", {
token: this.token token: this.token
}).then((res) => { }).then((res) => {
setSessionId(res.data) setLoginUser(res.data)
this.connect(); this.connect();
this.loading = false; this.loading = false;
}).catch(() => { }).catch(() => {

View File

@ -31,6 +31,16 @@
</el-row> </el-row>
<el-row> <el-row>
<div class="left-box"> <div class="left-box">
<div class="search-box">
<el-input v-model="roleName" class="w-50 m-2" size="small" placeholder="搜索聊天角色" @keyup="searchRole">
<template #prefix>
<el-icon class="el-input__icon">
<Search/>
</el-icon>
</template>
</el-input>
</div>
<div class="content" :style="{height: leftBoxHeight+'px'}"> <div class="content" :style="{height: leftBoxHeight+'px'}">
<el-row v-for="item in chatRoles" :key="item.key"> <el-row v-for="item in chatRoles" :key="item.key">
<div :class="item.key === this.role?'chat-role-item active':'chat-role-item'" @click="changeRole(item)"> <div :class="item.key === this.role?'chat-role-item active':'chat-role-item'" @click="changeRole(item)">
@ -129,30 +139,32 @@
import {defineComponent, nextTick} from 'vue' import {defineComponent, nextTick} from 'vue'
import ChatPrompt from "@/components/plus/ChatPrompt.vue"; import ChatPrompt from "@/components/plus/ChatPrompt.vue";
import ChatReply from "@/components/plus/ChatReply.vue"; import ChatReply from "@/components/plus/ChatReply.vue";
import {randString} from "@/utils/libs"; import {isMobile, randString} from "@/utils/libs";
import {ElMessage, ElMessageBox} from 'element-plus' import {ElMessage, ElMessageBox} from 'element-plus'
import {Tools, Lock, Delete, Picture} from '@element-plus/icons-vue' import {Tools, Lock, Delete, Picture, Search} from '@element-plus/icons-vue'
import ConfigDialog from '@/components/ConfigDialog.vue' import ConfigDialog from '@/components/ConfigDialog.vue'
import {httpPost, httpGet} from "@/utils/http"; import {httpPost, httpGet} from "@/utils/http";
import {getSessionId, setSessionId} from "@/utils/storage"; import {getSessionId, setLoginUser} from "@/utils/storage";
import hl from 'highlight.js' import hl from 'highlight.js'
import 'highlight.js/styles/a11y-dark.css' import 'highlight.js/styles/a11y-dark.css'
export default defineComponent({ export default defineComponent({
name: "ChatPlus", name: "ChatPlus",
components: {ChatPrompt, ChatReply, Tools, Lock, Delete, Picture, ConfigDialog}, components: {Search, ChatPrompt, ChatReply, Tools, Lock, Delete, Picture, ConfigDialog},
data() { data() {
return { return {
title: 'ChatGPT 控制台', title: 'ChatGPT 控制台',
logo: 'images/logo.png', logo: 'images/logo.png',
chatData: [], chatData: [],
chatRoles: [], chatRoles: [], //
allChatRoles: [], //
role: 'gpt', role: 'gpt',
inputValue: '', // inputValue: '', //
showConnectDialog: false, showConnectDialog: false,
showLoginDialog: false, showLoginDialog: false,
token: '', // token token: '', // token
replyIcon: 'images/avatar/gpt.png', // replyIcon: 'images/avatar/gpt.png', //
roleName: "", //
lineBuffer: '', // lineBuffer: '', //
connectingMessageBox: null, // connectingMessageBox: null, //
@ -167,6 +179,11 @@ export default defineComponent({
}, },
mounted: function () { mounted: function () {
if (isMobile()) {
this.$router.push("mobile");
return;
}
nextTick(() => { nextTick(() => {
this.resizeElement(); this.resizeElement();
}) })
@ -194,6 +211,7 @@ export default defineComponent({
httpGet("/api/config/chat-roles/get").then((res) => { httpGet("/api/config/chat-roles/get").then((res) => {
// ElMessage.success(''); // ElMessage.success('');
this.chatRoles = res.data; this.chatRoles = res.data;
this.allChatRoles = res.data;
this.loading = false this.loading = false
}).catch(() => { }).catch(() => {
ElMessage.error("获取聊天角色失败"); ElMessage.error("获取聊天角色失败");
@ -391,7 +409,7 @@ export default defineComponent({
httpPost("/api/login", { httpPost("/api/login", {
token: this.token token: this.token
}).then((res) => { }).then((res) => {
setSessionId(res.data) setLoginUser(res.data)
this.connect(); this.connect();
}).catch(() => { }).catch(() => {
ElMessage.error("口令错误"); ElMessage.error("口令错误");
@ -431,7 +449,22 @@ export default defineComponent({
}) })
}).catch(() => { }).catch(() => {
}) })
} },
//
searchRole: function () {
if (this.roleName === '') {
this.chatRoles = this.allChatRoles;
return;
}
const roles = [];
for (let i = 0; i < this.allChatRoles.length; i++) {
if (this.allChatRoles[i].name.indexOf(this.roleName) !== -1) {
roles.push(this.allChatRoles[i]);
}
}
this.chatRoles = roles;
},
}, },
}) })
@ -493,13 +526,25 @@ export default defineComponent({
.left-box { .left-box {
display flex display flex
flex-flow column
min-width 220px; min-width 220px;
max-width 250px; max-width 250px;
background-color: #28292A background-color: #28292A
border-top: 1px solid #2F3032 border-top: 1px solid #2F3032
border-right: 1px solid #2F3032 border-right: 1px solid #2F3032
.search-box {
flex-wrap wrap
padding 10px 15px;
.el-input__wrapper {
background-color: #363535;
box-shadow: none
}
}
// //
::-webkit-scrollbar { ::-webkit-scrollbar {
width: 0; width: 0;
height: 0; height: 0;