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/开发环境
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>
</template>
<script setup>
import signalR from "@/utils/signalR";
import useAuths from "@/hooks/useAuths";
import useConfigStore from "@/stores/config";
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";
const locale = zhCn;
const configStore = useConfigStore();
// 判断是否有loading有的话去掉
@@ -24,6 +25,22 @@ if (loading !== null) {
//加载全局信息
onMounted(async () => {
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>
<style scoped></style>

View File

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

View File

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

View File

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

View File

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