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