自动更新cookie
This commit is contained in:
@@ -5,6 +5,9 @@
|
|||||||
"jsonErr":-3,
|
"jsonErr":-3,
|
||||||
"userNameDup":-4,
|
"userNameDup":-4,
|
||||||
"userNameNoFund":-41,
|
"userNameNoFund":-41,
|
||||||
"userPassIncorrect":-42
|
"userPassIncorrect":-42,
|
||||||
|
"userCookieError":-44,
|
||||||
|
"userCookieNotFund":-45,
|
||||||
|
"userCookieExpired":-46
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -55,3 +56,28 @@ func HashUserPass(user *TabUser_) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsExpired(expireTime time.Time) bool {
|
||||||
|
return expireTime.Before(time.Now())
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckCookiesAndUpdate(cookie *TabCookie_) bool {
|
||||||
|
if !IsExpired(cookie.ExpiresAt) {
|
||||||
|
if cookie.Remember {
|
||||||
|
cookiewhere := TabCookie_{
|
||||||
|
ID: cookie.ID,
|
||||||
|
}
|
||||||
|
cookieupdata := TabCookie_{
|
||||||
|
UpdatedAt: time.Now(),
|
||||||
|
ExpiresAt: time.Now().Add(time.Duration(ConfigsUser.CookieTimeout) * time.Second),
|
||||||
|
}
|
||||||
|
DB.Where(&cookiewhere).Updates(&cookieupdata)
|
||||||
|
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
//以过期
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
//return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ func SeparateData(ctx *gin.Context) (map[string]interface{}, string) {
|
|||||||
|
|
||||||
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
|
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
|
||||||
//分离数据
|
//分离数据
|
||||||
cookie, ok := jsonData["cookie"].(string)
|
cookie, ok := jsonData["userCookieValue"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
cookie = ""
|
cookie = ""
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,6 +73,47 @@ func ApiUser(r *gin.RouterGroup) {
|
|||||||
r.POST("/test", func(ctx *gin.Context) {
|
r.POST("/test", func(ctx *gin.Context) {
|
||||||
ReturnJson(ctx, "apiOK", nil)
|
ReturnJson(ctx, "apiOK", nil)
|
||||||
})
|
})
|
||||||
|
//通过cookie获取用户info
|
||||||
|
r.POST("/getinfo", func(ctx *gin.Context) {
|
||||||
|
_, cookieval := SeparateData(ctx)
|
||||||
|
//fmt.Println("cookieis" + cookieval)
|
||||||
|
if cookieval != "" {
|
||||||
|
cookie := models.TabCookie_{
|
||||||
|
Value: cookieval,
|
||||||
|
}
|
||||||
|
if models.DB.Where(&cookie).First(&cookie).Error == nil {
|
||||||
|
//找到cookie,验证cookie有效性,以及更新cookie
|
||||||
|
if models.CheckCookiesAndUpdate(&cookie) {
|
||||||
|
//cookie有效
|
||||||
|
//返回最新cookie
|
||||||
|
redata := map[string]interface{}{
|
||||||
|
"cookie": cookie,
|
||||||
|
}
|
||||||
|
//载入用户info
|
||||||
|
userInfo := models.TabFileInfo_{
|
||||||
|
UserID: cookie.UserID,
|
||||||
|
}
|
||||||
|
if models.DB.Where(&userInfo).First(&userInfo).Error == nil {
|
||||||
|
redata["userInfo"] = userInfo
|
||||||
|
} else {
|
||||||
|
redata["userInfo"] = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ReturnJson(ctx, "apiOK", redata)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ReturnJson(ctx, "userCookieExpired", nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ReturnJson(ctx, "userCookieNotFund", nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ReturnJson(ctx, "userCookieError", nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
//用户登陆
|
//用户登陆
|
||||||
r.POST("/login", func(ctx *gin.Context) {
|
r.POST("/login", func(ctx *gin.Context) {
|
||||||
var loginuser From_user_login
|
var loginuser From_user_login
|
||||||
|
|||||||
@@ -6,10 +6,10 @@ func ReturnJson(ctx *gin.Context, errMsg string, data map[string]interface{}) {
|
|||||||
var errCode = ErrorCode[errMsg]
|
var errCode = ErrorCode[errMsg]
|
||||||
returnData := map[string]interface{}{}
|
returnData := map[string]interface{}{}
|
||||||
|
|
||||||
cookie, have_cookie := ctx.Get("cookie")
|
// cookie, have_cookie := ctx.Get("cookie")
|
||||||
if have_cookie {
|
// if have_cookie {
|
||||||
returnData["cookie"] = cookie
|
// returnData["cookie"] = cookie
|
||||||
}
|
// }
|
||||||
|
|
||||||
returnData["err_code"] = errCode
|
returnData["err_code"] = errCode
|
||||||
returnData["err_msg"] = errMsg
|
returnData["err_msg"] = errMsg
|
||||||
|
|||||||
@@ -178,7 +178,6 @@ onMounted(() => {
|
|||||||
<div v-else class="nav-item">
|
<div v-else class="nav-item">
|
||||||
<div class="dropdown">
|
<div class="dropdown">
|
||||||
<div
|
<div
|
||||||
|
|
||||||
class="nav-link d-flex lh-1 p-0 px-2"
|
class="nav-link d-flex lh-1 p-0 px-2"
|
||||||
data-bs-toggle="dropdown"
|
data-bs-toggle="dropdown"
|
||||||
aria-label="Open user menu"
|
aria-label="Open user menu"
|
||||||
@@ -194,12 +193,60 @@ onMounted(() => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="dropdown-menu dropdown-menu-end dropdown-menu-arrow">
|
<div class="dropdown-menu dropdown-menu-end dropdown-menu-arrow">
|
||||||
<a href="#" class="dropdown-item">Status</a>
|
<router-link to="" class="dropdown-item">{{
|
||||||
<a href="./profile.html" class="dropdown-item">Profile</a>
|
t("message.user_home")
|
||||||
<a href="#" class="dropdown-item">Feedback</a>
|
}}</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>
|
<div class="dropdown-divider"></div>
|
||||||
<a href="./settings.html" class="dropdown-item">Settings</a>
|
<!-- 如何用户是系统管理员这里显示跳转管理的url -->
|
||||||
<div @click="logOut" class="dropdown-item">Logout</div>
|
<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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -34,13 +34,18 @@
|
|||||||
"this_not_email": "This is not an email address.",
|
"this_not_email": "This is not an email address.",
|
||||||
"create_new_account": "Create new account",
|
"create_new_account": "Create new account",
|
||||||
"already_have_an_account": "Already have an account?",
|
"already_have_an_account": "Already have an account?",
|
||||||
"network_err":"Network error",
|
"network_err": "Network error",
|
||||||
"username_dup":"Duplicate username",
|
"username_dup": "Duplicate username",
|
||||||
"registration_successful":"Registration successful!",
|
"registration_successful": "Registration successful!",
|
||||||
"server_error":"Server Error",
|
"server_error": "Server Error",
|
||||||
"user_not_found":"User not found",
|
"user_not_found": "User not found",
|
||||||
"username_or_password_incorrect":"Username or password incorrect.",
|
"username_or_password_incorrect": "Username or password incorrect.",
|
||||||
"login_successful":"Login successful"
|
"login_successful": "Login successful",
|
||||||
|
"logout": "Logout",
|
||||||
|
"user_home": "Profile",
|
||||||
|
"user_settings": "Settings",
|
||||||
|
"preferences": "Preferences",
|
||||||
|
"administrator": "Administrator"
|
||||||
},
|
},
|
||||||
"button": {
|
"button": {
|
||||||
"submit": "Submit",
|
"submit": "Submit",
|
||||||
|
|||||||
@@ -32,15 +32,20 @@
|
|||||||
"please_enter_your_email": "请输入您的邮箱",
|
"please_enter_your_email": "请输入您的邮箱",
|
||||||
"please_enter_your_username": "请输入您的用户名",
|
"please_enter_your_username": "请输入您的用户名",
|
||||||
"this_not_email": "这不是一个有效的邮箱地址。",
|
"this_not_email": "这不是一个有效的邮箱地址。",
|
||||||
"create_new_account":"创建新账户",
|
"create_new_account": "创建新账户",
|
||||||
"already_have_an_account":"已经有账户了?",
|
"already_have_an_account": "已经有账户了?",
|
||||||
"network_err":"网络错误",
|
"network_err": "网络错误",
|
||||||
"username_dup":"用户名重复",
|
"username_dup": "用户名重复",
|
||||||
"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": "登出",
|
||||||
|
"user_home": "个人主页",
|
||||||
|
"user_settings": "个人资料",
|
||||||
|
"preferences": "偏好设置",
|
||||||
|
"administrator": "管理员"
|
||||||
},
|
},
|
||||||
"button": {
|
"button": {
|
||||||
"submit": "提交",
|
"submit": "提交",
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export const my_network_func = {
|
|||||||
//console.log(userstore.cookieValue)
|
//console.log(userstore.cookieValue)
|
||||||
|
|
||||||
if (userstore.userCookie) {
|
if (userstore.userCookie) {
|
||||||
data["userCookie"] = userstore.userCookie
|
data["userCookieValue"] = userstore.userCookie.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
var re_data = {};
|
var re_data = {};
|
||||||
@@ -50,6 +50,20 @@ export const my_network_func = {
|
|||||||
if (response.data) {
|
if (response.data) {
|
||||||
re_data["data"] = response.data;
|
re_data["data"] = response.data;
|
||||||
//自动保存服务器发送的cookie
|
//自动保存服务器发送的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) {
|
||||||
// if (response.data.cookie.Value == "") {
|
// if (response.data.cookie.Value == "") {
|
||||||
// myfuncs.dele("cookie");
|
// myfuncs.dele("cookie");
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
import { createRouter, createWebHistory } from "vue-router";
|
import {
|
||||||
|
createRouter,
|
||||||
|
createWebHistory,
|
||||||
|
createWebHashHistory,
|
||||||
|
} from "vue-router";
|
||||||
import HomeView from "../views/HomeView.vue";
|
import HomeView from "../views/HomeView.vue";
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
history: createWebHistory(import.meta.env.BASE_URL),
|
history: createWebHashHistory(import.meta.env.BASE_URL),
|
||||||
routes: [
|
routes: [
|
||||||
{
|
{
|
||||||
path: "/",
|
path: "/",
|
||||||
@@ -37,6 +41,11 @@ const router = createRouter({
|
|||||||
name: "Register",
|
name: "Register",
|
||||||
component: () => import("../views/registerView.vue"),
|
component: () => import("../views/registerView.vue"),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: "/admin",
|
||||||
|
name: "admin",
|
||||||
|
component: () => import("../views/adminView.vue"),
|
||||||
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
import { defineStore } from "pinia";
|
import { defineStore } from "pinia";
|
||||||
import { ref, computed } from "vue";
|
import { ref, computed } from "vue";
|
||||||
import { myfuncs } from "@/myfunc.js";
|
import { myfuncs } from "@/myfunc.js";
|
||||||
|
import { my_network_func } from "@/my_network_func";
|
||||||
|
|
||||||
// 组合式 API 写法 (推荐)
|
// 组合式 API 写法 (推荐)
|
||||||
export const useUserStore = defineStore("user", () => {
|
export const useUserStore = defineStore("user", () => {
|
||||||
@@ -20,6 +21,30 @@ export const useUserStore = defineStore("user", () => {
|
|||||||
}
|
}
|
||||||
return userCookie.value;
|
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 = () => {
|
const logout = () => {
|
||||||
userCookie.value = null;
|
userCookie.value = null;
|
||||||
isLoggedIn.value = false;
|
isLoggedIn.value = false;
|
||||||
@@ -31,6 +56,17 @@ export const useUserStore = defineStore("user", () => {
|
|||||||
isLoggedIn.value = true;
|
isLoggedIn.value = true;
|
||||||
//这里应该判读cookie的实效性
|
//这里应该判读cookie的实效性
|
||||||
userCookie.value = cookiesQualified();
|
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 = () => {
|
const loginFromStoreCookie = () => {
|
||||||
@@ -49,7 +85,6 @@ export const useUserStore = defineStore("user", () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
userInfo,
|
userInfo,
|
||||||
userCookie,
|
userCookie,
|
||||||
@@ -57,6 +92,6 @@ export const useUserStore = defineStore("user", () => {
|
|||||||
logout,
|
logout,
|
||||||
login,
|
login,
|
||||||
loginFromStoreCookie,
|
loginFromStoreCookie,
|
||||||
|
cookieUpdata,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ function tt(){
|
|||||||
<button @click="t">222</button>
|
<button @click="t">222</button>
|
||||||
{{ user.userCookie }}
|
{{ user.userCookie }}
|
||||||
<button @click="tt">333</button>
|
<button @click="tt">333</button>
|
||||||
|
{{ user.userInfo }}
|
||||||
</main>
|
</main>
|
||||||
<MyOffcanvas ref="mos" />
|
<MyOffcanvas ref="mos" />
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ function login() {
|
|||||||
remember: remember,
|
remember: remember,
|
||||||
},
|
},
|
||||||
(r) => {
|
(r) => {
|
||||||
console.log(r)
|
//console.log(r)
|
||||||
switch (r.statusCode) {
|
switch (r.statusCode) {
|
||||||
case 200:
|
case 200:
|
||||||
switch (r.data.err_code) {
|
switch (r.data.err_code) {
|
||||||
@@ -84,11 +84,7 @@ function login() {
|
|||||||
case 0:
|
case 0:
|
||||||
//登录成功,载入cookie
|
//登录成功,载入cookie
|
||||||
//临时保存cookie
|
//临时保存cookie
|
||||||
myfuncs.saveJsonT("userCookie",r.data.return.cookie)
|
userStore.cookieUpdata(r.data.return.cookie)
|
||||||
if(remember){
|
|
||||||
//长期保存cookie
|
|
||||||
myfuncs.saveJson("userCookie",r.data.return.cookie)
|
|
||||||
}
|
|
||||||
|
|
||||||
//更新用户信息
|
//更新用户信息
|
||||||
userStore.login(r.data.return.cookie)
|
userStore.login(r.data.return.cookie)
|
||||||
|
|||||||
Reference in New Issue
Block a user