Refactor token handling and enable MQTT connection

This commit is contained in:
wangqiao
2025-08-24 21:48:14 +08:00
parent 587dbbeca6
commit 43724c4d4d
2 changed files with 17 additions and 13 deletions

View File

@ -67,6 +67,7 @@
import { top } from '~/api/home/index' import { top } from '~/api/home/index'
import type { ProjectDrawStatisticAppRespVO } from '~/api/home/type' import type { ProjectDrawStatisticAppRespVO } from '~/api/home/type'
import { Search } from '@element-plus/icons-vue' import { Search } from '@element-plus/icons-vue'
import refreshToken from "~/utils/RefreshToken";
import useUserStore from '~/store/user' import useUserStore from '~/store/user'
const userStore = useUserStore() const userStore = useUserStore()
const app = useNuxtApp() const app = useNuxtApp()
@ -88,6 +89,8 @@
// 是否登录 // 是否登录
const isLogin = computed(() => { const isLogin = computed(() => {
console.log('userStore.token', userStore);
return !!userStore.token return !!userStore.token
}) })

View File

@ -4,9 +4,9 @@ import { getUserInfo } from "~/api/common/index";
import type { AppMemberUserInfoRespVO } from "~/api/common/types"; import type { AppMemberUserInfoRespVO } from "~/api/common/types";
import MQTTClient from "~/utils/mqttClient"; import MQTTClient from "~/utils/mqttClient";
import { socialLoginByCode } from "~/api/pay"; import { socialLoginByCode } from "~/api/pay";
const app = useNuxtApp();
export default defineStore("useUserStore", { export default defineStore("useUserStore", {
state: () => ({ state: () => ({
token: refreshToken.getToken().token || "", token: refreshToken.getToken().token || "",
refreshToken: refreshToken.getToken().refreshToken || "", refreshToken: refreshToken.getToken().refreshToken || "",
@ -32,7 +32,7 @@ export default defineStore("useUserStore", {
logout() { logout() {
refreshToken.removeToken(); refreshToken.removeToken();
if (self === top) { if (self === top) {
navigateTo("/index"); navigateTo("/");
} else { } else {
window.top?.postMessage({ event: "logout" }, "*"); window.top?.postMessage({ event: "logout" }, "*");
} }
@ -49,19 +49,20 @@ export default defineStore("useUserStore", {
// 建立连接mqtt // 建立连接mqtt
async connectMqtt() { async connectMqtt() {
if (process.client) { if (process.client) {
// this.mqttClient = new MQTTClient("wss://www.tuxixi.net/mqtt", { this.mqttClient = new MQTTClient("wss://www.tuxixi.net/mqtt", {
// clientId: this.userInfoRes.id, clientId: this.userInfoRes.id,
// }); });
// await this.mqttClient.connect(); await this.mqttClient.connect();
// await this.mqttClient?.subscribe( await this.mqttClient?.subscribe(
// `zbjk_message_single/${this.userInfoRes.id}` `zbjk_message_single/${this.userInfoRes.id}`
// ); );
// await this.mqttClient?.subscribe( await this.mqttClient?.subscribe(
// `zbjk_message_kefu/${this.userInfoRes.id}` `zbjk_message_kefu/${this.userInfoRes.id}`
// ); );
} }
}, },
async getToken(row: any) { async getToken(row: any) {
const app = useNuxtApp();
try { try {
// 验证state // 验证state
if ( if (