feat: 登录与websocket 初始化相关的调整
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
// 登录成功
|
||||
export const NOTICE_EVENT_LOGIN_SUCCESS = "NOTICE_EVENT_LOGIN_SUCCESS";
|
||||
// 退出登录
|
||||
export const NOTICE_EVENT_LOGOUT = "NOTICE_EVENT_LOGOUT";
|
||||
|
||||
|
||||
@@ -6,8 +6,13 @@ import {
|
||||
import { getWeChatAuthCode } from "./AuthManager";
|
||||
import { useAppStore } from "@/store";
|
||||
import { clientId } from "@/constant/base";
|
||||
import { NOTICE_EVENT_LOGIN_SUCCESS } from "@/constant/constant";
|
||||
|
||||
const loginAuth = () => {
|
||||
uni.setStorageSync("token", "");
|
||||
const appStore = useAppStore();
|
||||
appStore.setHasToken(false);
|
||||
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const openIdCode = await getWeChatAuthCode();
|
||||
console.log("获取到的微信授权code:", openIdCode);
|
||||
@@ -25,6 +30,8 @@ const loginAuth = () => {
|
||||
uni.setStorageSync("token", response.access_token);
|
||||
const appStore = useAppStore();
|
||||
appStore.setHasToken(true);
|
||||
// 登录成功后,触发登录成功事件
|
||||
uni.$emit(NOTICE_EVENT_LOGIN_SUCCESS);
|
||||
resolve();
|
||||
} else {
|
||||
reject(response.message || "登录失败");
|
||||
|
||||
@@ -152,6 +152,7 @@ import {
|
||||
RECOMMEND_POSTS_TITLE,
|
||||
SEND_COMMAND_TEXT,
|
||||
NOTICE_EVENT_LOGOUT,
|
||||
NOTICE_EVENT_LOGIN_SUCCESS,
|
||||
} from "@/constant/constant";
|
||||
import { WSS_URL } from "@/request/base/baseUrl";
|
||||
import { MessageRole, MessageType, CompName } from "@/model/ChatModel";
|
||||
@@ -336,6 +337,12 @@ const sendMessageAction = (inputText) => {
|
||||
|
||||
/// 添加通知
|
||||
const addNoticeListener = () => {
|
||||
uni.$on(NOTICE_EVENT_LOGIN_SUCCESS, () => {
|
||||
if (!webSocketConnectStatus) {
|
||||
initHandler();
|
||||
}
|
||||
});
|
||||
|
||||
uni.$on(NOTICE_EVENT_LOGOUT, () => {
|
||||
resetConfig();
|
||||
uni.showToast({
|
||||
@@ -384,18 +391,6 @@ const initHandler = () => {
|
||||
initWebSocket();
|
||||
};
|
||||
|
||||
// 绑定成功,监听token变化,初始化
|
||||
|
||||
watch(
|
||||
() => appStore.hasToken,
|
||||
(newValue) => {
|
||||
if (newValue) {
|
||||
console.log("token存在,初始化数据");
|
||||
initHandler();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
onMounted(() => {
|
||||
try {
|
||||
getMainPageData();
|
||||
@@ -772,6 +767,7 @@ const stopRequest = () => {
|
||||
|
||||
// 组件销毁时清理资源
|
||||
onUnmounted(() => {
|
||||
uni.$off(NOTICE_EVENT_LOGIN_SUCCESS);
|
||||
uni.$off(SCROLL_TO_BOTTOM);
|
||||
uni.$off(RECOMMEND_POSTS_TITLE);
|
||||
uni.$off(SEND_COMMAND_TEXT);
|
||||
@@ -785,6 +781,7 @@ const resetConfig = () => {
|
||||
if (webSocketManager) {
|
||||
webSocketManager.destroy();
|
||||
webSocketManager = null;
|
||||
webSocketConnectStatus = false;
|
||||
}
|
||||
|
||||
// 清理打字机管理器
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { goLogin } from "../../hooks/useGoLogin";
|
||||
import { BASE_URL } from "./baseUrl";
|
||||
import { getCurrentConfig } from "@/constant/base";
|
||||
import { useAppStore } from "@/store";
|
||||
import { NOTICE_EVENT_LOGOUT } from "@/constant/constant";
|
||||
|
||||
const clientId = getCurrentConfig().clientId;
|
||||
const defaultConfig = {
|
||||
@@ -55,6 +57,10 @@ function request(url, args = {}, method = "POST", customConfig = {}) {
|
||||
resolve(res.data);
|
||||
if (res.statusCode && res.statusCode === 424) {
|
||||
console.log("424错误,重新登录");
|
||||
uni.setStorageSync("token", "");
|
||||
const appStore = useAppStore();
|
||||
appStore.setHasToken(false);
|
||||
uni.$emit(NOTICE_EVENT_LOGOUT);
|
||||
goLogin();
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user