优化登录逻辑

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

View File

@ -302,5 +302,8 @@ func (s *Server) LoginHandle(c *gin.Context) {
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
*/
const SessionIdKey = 'ChatGPT_SESSION_ID';
const SessionUserKey = 'LOGIN_USER';
export const Global = {}
export function getSessionId() {
return sessionStorage.getItem(SessionIdKey)
const user = getLoginUser();
return user ? user['session_id'] : '';
}
export function setSessionId(value) {
sessionStorage.setItem(SessionIdKey, value)
export function getLoginUser() {
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 ChatPrompt from "@/components/ChatPrompt.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 {Tools, Lock, Delete} from '@element-plus/icons-vue'
import ConfigDialog from '@/components/ConfigDialog.vue'
import {httpPost, httpGet} from "@/utils/http";
import {getSessionId, setSessionId} from "@/utils/storage";
import {getSessionId, setLoginUser} from "@/utils/storage";
import hl from 'highlight.js'
import 'highlight.js/styles/a11y-dark.css'
@ -140,6 +140,11 @@ export default defineComponent({
},
mounted: function () {
if (!isMobile()) {
this.$router.push("plus");
return;
}
nextTick(() => {
this.chatBoxHeight = window.innerHeight - this.toolBoxHeight;
ElMessage.warning("强烈建议使用PC浏览器访问获的更好的聊天体验")
@ -379,7 +384,7 @@ export default defineComponent({
httpPost("/api/login", {
token: this.token
}).then((res) => {
setSessionId(res.data)
setLoginUser(res.data)
this.connect();
this.loading = false;
}).catch(() => {

View File

@ -31,6 +31,16 @@
</el-row>
<el-row>
<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'}">
<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)">
@ -129,30 +139,32 @@
import {defineComponent, nextTick} from 'vue'
import ChatPrompt from "@/components/plus/ChatPrompt.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 {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 {httpPost, httpGet} from "@/utils/http";
import {getSessionId, setSessionId} from "@/utils/storage";
import {getSessionId, setLoginUser} from "@/utils/storage";
import hl from 'highlight.js'
import 'highlight.js/styles/a11y-dark.css'
export default defineComponent({
name: "ChatPlus",
components: {ChatPrompt, ChatReply, Tools, Lock, Delete, Picture, ConfigDialog},
components: {Search, ChatPrompt, ChatReply, Tools, Lock, Delete, Picture, ConfigDialog},
data() {
return {
title: 'ChatGPT 控制台',
logo: 'images/logo.png',
chatData: [],
chatRoles: [],
chatRoles: [], //
allChatRoles: [], //
role: 'gpt',
inputValue: '', //
showConnectDialog: false,
showLoginDialog: false,
token: '', // token
replyIcon: 'images/avatar/gpt.png', //
roleName: "", //
lineBuffer: '', //
connectingMessageBox: null, //
@ -167,6 +179,11 @@ export default defineComponent({
},
mounted: function () {
if (isMobile()) {
this.$router.push("mobile");
return;
}
nextTick(() => {
this.resizeElement();
})
@ -194,6 +211,7 @@ export default defineComponent({
httpGet("/api/config/chat-roles/get").then((res) => {
// ElMessage.success('');
this.chatRoles = res.data;
this.allChatRoles = res.data;
this.loading = false
}).catch(() => {
ElMessage.error("获取聊天角色失败");
@ -391,7 +409,7 @@ export default defineComponent({
httpPost("/api/login", {
token: this.token
}).then((res) => {
setSessionId(res.data)
setLoginUser(res.data)
this.connect();
}).catch(() => {
ElMessage.error("口令错误");
@ -431,7 +449,22 @@ export default defineComponent({
})
}).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 {
display flex
flex-flow column
min-width 220px;
max-width 250px;
background-color: #28292A
border-top: 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 {
width: 0;
height: 0;