feat: 完成在线人数store改造
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -7,15 +7,15 @@
|
|||||||
</template>
|
</template>
|
||||||
<script setup>
|
<script setup>
|
||||||
import signalR from "@/utils/signalR";
|
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,watch } from "vue";
|
import useUserStore from "@/stores/user.js";
|
||||||
const { tokenValue } = useAuths();
|
import { onMounted,watch,computed } from "vue";
|
||||||
|
const userStore = useUserStore();
|
||||||
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();
|
||||||
|
const token = computed(() => useUserStore().token);
|
||||||
// 判断是否有loading有的话去掉
|
// 判断是否有loading有的话去掉
|
||||||
const loading = document.getElementById("Loading");
|
const loading = document.getElementById("Loading");
|
||||||
if (loading !== null) {
|
if (loading !== null) {
|
||||||
@@ -31,16 +31,16 @@ onMounted(async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// watch(
|
watch(
|
||||||
// () => tokenValue,
|
() => token,
|
||||||
// (val,oldValue) => {
|
(val,oldValue) => {
|
||||||
// console.log("token发生改变");
|
console.log("token发生改变");
|
||||||
// if (val) {
|
if (val) {
|
||||||
// signalR.close();
|
signalR.close();
|
||||||
// signalR.init(`main`);
|
signalR.init(`main`);
|
||||||
// }
|
}
|
||||||
// },
|
},
|
||||||
// { deep:true }
|
{immediate:true,deep:true}
|
||||||
// );
|
);
|
||||||
</script>
|
</script>
|
||||||
<style scoped></style>
|
<style scoped></style>
|
||||||
|
|||||||
@@ -8,15 +8,13 @@ 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
|
||||||
loginReUrl: "", // 登录页登陆成功后带重定向redirect=的跳转url 默认为空
|
loginReUrl: "", // 登录页登陆成功后带重定向redirect=的跳转url 默认为空
|
||||||
@@ -29,18 +27,6 @@ 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 = () => {
|
||||||
var token= Local.get(TokenKey);
|
var token= Local.get(TokenKey);
|
||||||
@@ -57,17 +43,12 @@ watch(
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 删除token
|
|
||||||
const removeToken = () => {
|
|
||||||
Local.remove(TokenKey);
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
// 退出登录
|
// 退出登录
|
||||||
const logoutFun = async () => {
|
const logoutFun = async () => {
|
||||||
let flag = true;
|
let flag = true;
|
||||||
try {
|
try {
|
||||||
await userLogout().then((res) => {
|
await userLogout().then((res) => {
|
||||||
|
useUserStore().updateToken(null);
|
||||||
ElMessage({
|
ElMessage({
|
||||||
message: "退出成功",
|
message: "退出成功",
|
||||||
type: "info",
|
type: "info",
|
||||||
@@ -85,6 +66,7 @@ watch(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
useUserStore().updateToken(null);
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
@@ -100,8 +82,6 @@ watch(
|
|||||||
|
|
||||||
// 清空本地存储的信息
|
// 清空本地存储的信息
|
||||||
const clearStorage = () => {
|
const clearStorage = () => {
|
||||||
console.log("token发生改变22清除清除")
|
|
||||||
tokenValue.value='';
|
|
||||||
Session.clear();
|
Session.clear();
|
||||||
Local.clear();
|
Local.clear();
|
||||||
removeToken();
|
removeToken();
|
||||||
@@ -112,6 +92,7 @@ watch(
|
|||||||
const loginFun = async (params) => {
|
const loginFun = async (params) => {
|
||||||
try {
|
try {
|
||||||
const res = await userLogin(params);
|
const res = await userLogin(params);
|
||||||
|
|
||||||
ElMessage({
|
ElMessage({
|
||||||
message: `您好${params.userName},登录成功!`,
|
message: `您好${params.userName},登录成功!`,
|
||||||
type: "success",
|
type: "success",
|
||||||
@@ -138,12 +119,21 @@ watch(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// 删除token
|
||||||
|
const removeToken = () => {
|
||||||
|
// console.log("token发生改变22清除清除")
|
||||||
|
Local.remove(TokenKey);
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// 登录成功之后的操作
|
// 登录成功之后的操作
|
||||||
const loginSuccess = async (res) => {
|
const loginSuccess = async (res) => {
|
||||||
const { token } = res.data;
|
const { token } = res.data;
|
||||||
|
|
||||||
setToken(token);
|
setToken(token);
|
||||||
tokenValue.value=token;
|
useUserStore().updateToken(token);
|
||||||
try {
|
try {
|
||||||
// 存储用户信息
|
// 存储用户信息
|
||||||
await useUserStore().getInfo(); // 用户信息
|
await useUserStore().getInfo(); // 用户信息
|
||||||
@@ -164,6 +154,9 @@ watch(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 注册
|
// 注册
|
||||||
const registerFun = async (params) => {
|
const registerFun = async (params) => {
|
||||||
try {
|
try {
|
||||||
@@ -186,7 +179,6 @@ watch(
|
|||||||
logoutFun,
|
logoutFun,
|
||||||
clearStorage,
|
clearStorage,
|
||||||
registerFun,
|
registerFun,
|
||||||
loginSuccess,
|
loginSuccess
|
||||||
tokenValue
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ const useUserStore = defineStore("user", {
|
|||||||
.then((response) => {
|
.then((response) => {
|
||||||
const res = response.data;
|
const res = response.data;
|
||||||
setToken(res.token);
|
setToken(res.token);
|
||||||
|
|
||||||
this.token = res.token;
|
this.token = res.token;
|
||||||
resolve(response);
|
resolve(response);
|
||||||
})
|
})
|
||||||
@@ -119,6 +120,10 @@ const useUserStore = defineStore("user", {
|
|||||||
this.codeImageURL = "data:image/jpg;base64," + data.img;
|
this.codeImageURL = "data:image/jpg;base64," + data.img;
|
||||||
this.codeUUid = data.uuid;
|
this.codeUUid = data.uuid;
|
||||||
},
|
},
|
||||||
|
updateToken(token)
|
||||||
|
{
|
||||||
|
this.token = token;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
persist: {
|
persist: {
|
||||||
key: "userInfo",
|
key: "userInfo",
|
||||||
|
|||||||
Reference in New Issue
Block a user