自动更新cookie

This commit is contained in:
2025-11-14 20:10:55 +08:00
parent c6c7496449
commit 710d72aefe
14 changed files with 221 additions and 39 deletions
@@ -178,7 +178,6 @@ onMounted(() => {
<div v-else class="nav-item">
<div class="dropdown">
<div
class="nav-link d-flex lh-1 p-0 px-2"
data-bs-toggle="dropdown"
aria-label="Open user menu"
@@ -194,12 +193,60 @@ onMounted(() => {
</div>
</div>
<div class="dropdown-menu dropdown-menu-end dropdown-menu-arrow">
<a href="#" class="dropdown-item">Status</a>
<a href="./profile.html" class="dropdown-item">Profile</a>
<a href="#" class="dropdown-item">Feedback</a>
<router-link to="" class="dropdown-item">{{
t("message.user_home")
}}</router-link>
<router-link to="" class="dropdown-item">{{
t("message.user_settings")
}}</router-link>
<router-link to="" class="dropdown-item">{{
t("message.preferences")
}}</router-link>
<div class="dropdown-divider"></div>
<a href="./settings.html" class="dropdown-item">Settings</a>
<div @click="logOut" class="dropdown-item">Logout</div>
<!-- 如何用户是系统管理员这里显示跳转管理的url -->
<router-link to="/admin" class="dropdown-item">
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="icon icon-tabler icons-tabler-outline icon-tabler-settings"
>
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
<path
d="M10.325 4.317c.426 -1.756 2.924 -1.756 3.35 0a1.724 1.724 0 0 0 2.573 1.066c1.543 -.94 3.31 .826 2.37 2.37a1.724 1.724 0 0 0 1.065 2.572c1.756 .426 1.756 2.924 0 3.35a1.724 1.724 0 0 0 -1.066 2.573c.94 1.543 -.826 3.31 -2.37 2.37a1.724 1.724 0 0 0 -2.572 1.065c-.426 1.756 -2.924 1.756 -3.35 0a1.724 1.724 0 0 0 -2.573 -1.066c-1.543 .94 -3.31 -.826 -2.37 -2.37a1.724 1.724 0 0 0 -1.065 -2.572c-1.756 -.426 -1.756 -2.924 0 -3.35a1.724 1.724 0 0 0 1.066 -2.573c-.94 -1.543 .826 -3.31 2.37 -2.37c1 .608 2.296 .07 2.572 -1.065z"
/>
<path d="M9 12a3 3 0 1 0 6 0a3 3 0 0 0 -6 0" />
</svg>
{{ t("message.administrator") }}</router-link
>
<div @click="logOut" class="dropdown-item">
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="icon icon-tabler icons-tabler-outline icon-tabler-logout-2"
>
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
<path
d="M10 8v-2a2 2 0 0 1 2 -2h7a2 2 0 0 1 2 2v12a2 2 0 0 1 -2 2h-7a2 2 0 0 1 -2 -2v-2"
/>
<path d="M15 12h-12l3 -3" />
<path d="M6 15l-3 -3" />
</svg>
{{ t("message.logout") }}
</div>
</div>
</div>
</div>
+12 -7
View File
@@ -34,13 +34,18 @@
"this_not_email": "This is not an email address.",
"create_new_account": "Create new account",
"already_have_an_account": "Already have an account?",
"network_err":"Network error",
"username_dup":"Duplicate username",
"registration_successful":"Registration successful!",
"server_error":"Server Error",
"user_not_found":"User not found",
"username_or_password_incorrect":"Username or password incorrect.",
"login_successful":"Login successful"
"network_err": "Network error",
"username_dup": "Duplicate username",
"registration_successful": "Registration successful!",
"server_error": "Server Error",
"user_not_found": "User not found",
"username_or_password_incorrect": "Username or password incorrect.",
"login_successful": "Login successful",
"logout": "Logout",
"user_home": "Profile",
"user_settings": "Settings",
"preferences": "Preferences",
"administrator": "Administrator"
},
"button": {
"submit": "Submit",
+14 -9
View File
@@ -32,15 +32,20 @@
"please_enter_your_email": "请输入您的邮箱",
"please_enter_your_username": "请输入您的用户名",
"this_not_email": "这不是一个有效的邮箱地址。",
"create_new_account":"创建新账户",
"already_have_an_account":"已经有账户了?",
"network_err":"网络错误",
"username_dup":"用户名重复",
"registration_successful":"注册成功!",
"server_error":"服务端错误",
"user_not_found":"找不到用户",
"username_or_password_incorrect":"用户或密码错误",
"login_successful":"登录成功"
"create_new_account": "创建新账户",
"already_have_an_account": "已经有账户了?",
"network_err": "网络错误",
"username_dup": "用户名重复",
"registration_successful": "注册成功!",
"server_error": "服务端错误",
"user_not_found": "找不到用户",
"username_or_password_incorrect": "用户或密码错误",
"login_successful": "登录成功",
"logout": "登出",
"user_home": "个人主页",
"user_settings": "个人资料",
"preferences": "偏好设置",
"administrator": "管理员"
},
"button": {
"submit": "提交",
+15 -1
View File
@@ -32,7 +32,7 @@ export const my_network_func = {
//console.log(userstore.cookieValue)
if (userstore.userCookie) {
data["userCookie"] = userstore.userCookie
data["userCookieValue"] = userstore.userCookie.Value
}
var re_data = {};
@@ -50,6 +50,20 @@ export const my_network_func = {
if (response.data) {
re_data["data"] = response.data;
//自动保存服务器发送的cookie
if(response.status==200)
{
if(response.data.err_code==0){
if(response.data.return.cookie){
userstore.cookieUpdata(response.data.return.cookie)
}
}else if(response.data.err_code==-46){
//userCookieExpired
userstore.logout()
}
}
// if (response.data.cookie) {
// if (response.data.cookie.Value == "") {
// myfuncs.dele("cookie");
+11 -2
View File
@@ -1,8 +1,12 @@
import { createRouter, createWebHistory } from "vue-router";
import {
createRouter,
createWebHistory,
createWebHashHistory,
} from "vue-router";
import HomeView from "../views/HomeView.vue";
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
history: createWebHashHistory(import.meta.env.BASE_URL),
routes: [
{
path: "/",
@@ -37,6 +41,11 @@ const router = createRouter({
name: "Register",
component: () => import("../views/registerView.vue"),
},
{
path: "/admin",
name: "admin",
component: () => import("../views/adminView.vue"),
},
],
});
+37 -2
View File
@@ -2,6 +2,7 @@
import { defineStore } from "pinia";
import { ref, computed } from "vue";
import { myfuncs } from "@/myfunc.js";
import { my_network_func } from "@/my_network_func";
// 组合式 API 写法 (推荐)
export const useUserStore = defineStore("user", () => {
@@ -20,6 +21,30 @@ export const useUserStore = defineStore("user", () => {
}
return userCookie.value;
};
const getUserInfoFromCookie = () => {
my_network_func.postJson("/users/getinfo", {}, (r) => {
//console.log(r);
switch (r.statusCode) {
case 200:
switch (r.data.err_code) {
case 0:
if(r.data.return.userInfo){
userInfo.value=r.data.return.userInfo
}else{
userInfo.value=null
}
break;
default:
break;
}
break;
default:
break;
}
});
};
const logout = () => {
userCookie.value = null;
isLoggedIn.value = false;
@@ -31,6 +56,17 @@ export const useUserStore = defineStore("user", () => {
isLoggedIn.value = true;
//这里应该判读cookie的实效性
userCookie.value = cookiesQualified();
//到这里cookie应该是有效的,尝试获取用户info,因为有的info可能是隐藏的 所以用post携带当前cookie去请求用户info
getUserInfoFromCookie();
};
const cookieUpdata = (cookie) => {
userCookie.value = cookie;
myfuncs.saveJsonT("userCookie", cookie);
if (cookie.Remember) {
//长期保存cookie
myfuncs.saveJson("userCookie", cookie);
}
};
const loginFromStoreCookie = () => {
@@ -49,7 +85,6 @@ export const useUserStore = defineStore("user", () => {
}
};
return {
userInfo,
userCookie,
@@ -57,6 +92,6 @@ export const useUserStore = defineStore("user", () => {
logout,
login,
loginFromStoreCookie,
cookieUpdata,
};
});
@@ -30,6 +30,7 @@ function tt(){
<button @click="t">222</button>
{{ user.userCookie }}
<button @click="tt">333</button>
{{ user.userInfo }}
</main>
<MyOffcanvas ref="mos" />
</template>
+2 -6
View File
@@ -60,7 +60,7 @@ function login() {
remember: remember,
},
(r) => {
console.log(r)
//console.log(r)
switch (r.statusCode) {
case 200:
switch (r.data.err_code) {
@@ -84,11 +84,7 @@ function login() {
case 0:
//登录成功,载入cookie
//临时保存cookie
myfuncs.saveJsonT("userCookie",r.data.return.cookie)
if(remember){
//长期保存cookie
myfuncs.saveJson("userCookie",r.data.return.cookie)
}
userStore.cookieUpdata(r.data.return.cookie)
//更新用户信息
userStore.login(r.data.return.cookie)