fix: 修复在线问题

This commit is contained in:
橙子
2024-01-15 00:25:25 +08:00
parent cf793c198f
commit e64b12097a
6 changed files with 52 additions and 23 deletions

View File

@@ -7,4 +7,4 @@ VITE_APP_URL="http://localhost:19001/api/app"
# ws/开发环境 # ws/开发环境
VITE_APP_BASE_WS = '/dev-ws' VITE_APP_BASE_WS = '/dev-ws'
VITE_APP_BASE_URL_WS="http://localhost:19001/api/app" VITE_APP_BASE_URL_WS="http://localhost:19001/hub"

View File

@@ -6,13 +6,14 @@
</el-config-provider> </el-config-provider>
</template> </template>
<script setup> <script setup>
import signalR from "@/utils/signalR";
import useAuths from "@/hooks/useAuths";
import useConfigStore from "@/stores/config"; import useConfigStore from "@/stores/config";
import { ElConfigProvider } from "element-plus"; import { ElConfigProvider } from "element-plus";
import { onMounted } from "vue"; import { onMounted,watch } from "vue";
const { tokenValue } = useAuths();
import zhCn from "element-plus/dist/locale/zh-cn.mjs"; import zhCn from "element-plus/dist/locale/zh-cn.mjs";
const locale = zhCn; const locale = zhCn;
const configStore = useConfigStore(); const configStore = useConfigStore();
// 判断是否有loading有的话去掉 // 判断是否有loading有的话去掉
@@ -24,6 +25,22 @@ if (loading !== null) {
//加载全局信息 //加载全局信息
onMounted(async () => { onMounted(async () => {
await configStore.getConfig(); await configStore.getConfig();
// setInterval(() => {
// console.log("token的值"+tokenValue.value);
// }, 1000); // 1000毫秒即1秒
}); });
// watch(
// () => tokenValue,
// (val,oldValue) => {
// console.log("token发生改变");
// if (val) {
// signalR.close();
// signalR.init(`main`);
// }
// },
// { deep:true }
// );
</script> </script>
<style scoped></style> <style scoped></style>

View File

@@ -8,11 +8,14 @@ import {
userLogout, userLogout,
userRegister, userRegister,
} from "@/apis/auth"; } from "@/apis/auth";
import { ref, watch } from "vue";
import signalR from "@/utils/signalR";
const TokenKey = "AccessToken"; const TokenKey = "AccessToken";
export const AUTH_MENUS = "AUTH_MENUS"; export const AUTH_MENUS = "AUTH_MENUS";
export const AUTH_USER = "AUTH_USER"; export const AUTH_USER = "AUTH_USER";
export default function useAuths(opt) { export default function useAuths(opt) {
const defaultOpt = { const defaultOpt = {
loginUrl: "/login", // 登录页跳转url 默认: /login loginUrl: "/login", // 登录页跳转url 默认: /login
@@ -26,9 +29,22 @@ export default function useAuths(opt) {
...opt, ...opt,
}; };
const tokenValue=ref('');
watch(
() => tokenValue.value,
(val,oldValue) => {
console.log("token发生改变"+val+"------"+oldValue)
signalR.init(`main`);
},
{ deep:true }
);
// 获取token // 获取token
const getToken = () => { const getToken = () => {
return Local.get(TokenKey); var token= Local.get(TokenKey);
return token;
}; };
// 存储token到cookies // 存储token到cookies
@@ -37,6 +53,7 @@ export default function useAuths(opt) {
return false; return false;
} }
Local.set(TokenKey, token); Local.set(TokenKey, token);
return true; return true;
}; };
@@ -50,7 +67,7 @@ export default function useAuths(opt) {
const logoutFun = async () => { const logoutFun = async () => {
let flag = true; let flag = true;
try { try {
await userLogout().then((res) => { await userLogout().then((res) => {
ElMessage({ ElMessage({
message: "退出成功", message: "退出成功",
type: "info", type: "info",
@@ -75,6 +92,7 @@ export default function useAuths(opt) {
return false; return false;
}); });
} }
if (flag) { if (flag) {
clearStorage(); clearStorage();
} }
@@ -82,9 +100,12 @@ export default function useAuths(opt) {
// 清空本地存储的信息 // 清空本地存储的信息
const clearStorage = () => { const clearStorage = () => {
console.log("token发生改变22清除清除")
tokenValue.value='';
Session.clear(); Session.clear();
Local.clear(); Local.clear();
removeToken(); removeToken();
}; };
// 用户名密码登录 // 用户名密码登录
@@ -122,6 +143,7 @@ export default function useAuths(opt) {
const { token } = res.data; const { token } = res.data;
setToken(token); setToken(token);
tokenValue.value=token;
try { try {
// 存储用户信息 // 存储用户信息
await useUserStore().getInfo(); // 用户信息 await useUserStore().getInfo(); // 用户信息
@@ -165,5 +187,6 @@ export default function useAuths(opt) {
clearStorage, clearStorage,
registerFun, registerFun,
loginSuccess, loginSuccess,
tokenValue
}; };
} }

View File

@@ -84,7 +84,7 @@ import useUserStore from "@/stores/user.js";
import useConfigStore from "@/stores/config"; import useConfigStore from "@/stores/config";
import useAuths from "@/hooks/useAuths"; import useAuths from "@/hooks/useAuths";
import { Session } from "@/utils/storage"; import { Session } from "@/utils/storage";
// import signalR from "@/utils/signalR"; import signalR from "@/utils/signalR";
const { getToken, clearStorage } = useAuths(); const { getToken, clearStorage } = useAuths();
const configStore = useConfigStore(); const configStore = useConfigStore();
@@ -104,7 +104,7 @@ const logout = async () => {
}).then(async () => { }).then(async () => {
//异步 //异步
await userStore.logOut(); await userStore.logOut();
// await signalR.close(); await signalR.close();
//删除成功后,跳转到主页 //删除成功后,跳转到主页
router.push("/login"); router.push("/login");
ElMessage({ ElMessage({

View File

@@ -36,8 +36,6 @@ export default {
connection.state === signalR.HubConnectionState.Disconnected connection.state === signalR.HubConnectionState.Disconnected
); );
// 建议用户重新刷新浏览器 // 建议用户重新刷新浏览器
await this.close();
await this.start();
}); });
connection.onreconnected(() => { connection.onreconnected(() => {
@@ -55,6 +53,7 @@ export default {
try { try {
var that = this; var that = this;
await this.SR.stop(); await this.SR.stop();
this.SR={};
} catch {} } catch {}
}, },

View File

@@ -213,13 +213,11 @@ import RecommendFriend from "./components/RecommendFriend/index.vue";
import ThemeData from "./components/RecommendTheme/index.vue"; import ThemeData from "./components/RecommendTheme/index.vue";
import Skeleton from "@/components/Skeleton/index.vue"; import Skeleton from "@/components/Skeleton/index.vue";
import useSocketStore from "@/stores/socket"; import useSocketStore from "@/stores/socket";
import signalR from "@/utils/signalR";
import useAuths from "@/hooks/useAuths";
const router = useRouter(); const router = useRouter();
const { getToken } = useAuths();
const token = getToken();
const plateList = ref([]); const plateList = ref([]);
const discussList = ref([]); const discussList = ref([]);
@@ -310,15 +308,7 @@ watch(
{ deep: true } { deep: true }
); );
watch(
() => token,
async (val) => {
if (val) {
await signalR.init(`main`);
}
},
{ immediate: true }
);
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
.home-box { .home-box {