From 84025a83ab08a411568a32555322a85ba840390e Mon Sep 17 00:00:00 2001 From: Miao_Spring <539943419@qq.com> Date: Tue, 2 Jul 2024 19:27:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96vite=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miaoShareConfig.json | 3 ++- server.js | 32 +++++++++++++++++++++++++++++++- src/App.vue | 2 ++ src/pinia/store.js | 7 +++++++ vite.config.js | 18 ++++++++++++++++++ 5 files changed, 60 insertions(+), 2 deletions(-) diff --git a/miaoShareConfig.json b/miaoShareConfig.json index 4e96ab4..fa9ee41 100644 --- a/miaoShareConfig.json +++ b/miaoShareConfig.json @@ -8,5 +8,6 @@ "cloudCapacity":2147483648 } }, - "enableGuest":false + "enableGuest":false, + "tokenExpiresIn":168 } \ No newline at end of file diff --git a/server.js b/server.js index 35139f7..fc962b1 100644 --- a/server.js +++ b/server.js @@ -12,6 +12,7 @@ import { v4 as uuid4 } from "uuid"; import jwt from "jsonwebtoken"; import sha256 from "crypto-js/sha256.js"; import Config from './miaoShareConfig.json' assert { type: 'json' }; +import { verify } from "crypto"; // function loadJSON(path){ // return JSON.parse(fs.readFileSync(path).toString()); // } @@ -327,13 +328,42 @@ app.post("/user/login", express.json(),function (req, res) { let userGroup = db_getPermissionGroup(id, pwdH); if (userGroup !== null) { res.writeHead(200); - let token = jwt.sign({ id, pwd }, JWT_SK, { expiresIn: "168h" }); + let token = jwt.sign({ id, pwd }, JWT_SK, { expiresIn: `${Config.tokenExpiresIn}h` }); res.end(JSON.stringify({ status: "Success", userGroup, token })); } else { res.end(JSON.stringify({ status: "Error" })); } // console.log(req) }); + + +app.post("/user/refrashToken", express.json(),function (req, res) { + res.header("Access-Control-Allow-Origin", "*"); + res.header("Access-Control-Allow-Headers", "*"); + res.header("Access-Control-Allow-Methods", "POST"); + // console.log(req.body) + let { token } = req.body; + console.log(req.body) + + let verifyData = jwt.verify(token, JWT_SK); + if (verifyData.exp > parseInt(Date.now())) { + return res.status(401).json({ message: "Invalid token" }); + } + let id = verifyData.id + let pwd = verifyData.pwd + let new_token = jwt.sign({ id, pwd }, JWT_SK, { expiresIn: `${Config.tokenExpiresIn}h` }); + res.end(JSON.stringify({ status: "Success", token })); + // let pwdH = sha256(id + pwd).toString(); + // let userGroup = db_getPermissionGroup(id, pwdH); + // if (userGroup !== null) { + // res.writeHead(200); + // let token = jwt.sign({ id, pwd }, JWT_SK, { expiresIn: `${Config.tokenExpiresIn}h` }); + // res.end(JSON.stringify({ status: "Success", userGroup, token })); + // } else { + // res.end(JSON.stringify({ status: "Error" })); + // } + // console.log(req) +}); // app.all() app.post("/file/upload", verifyToken, function (req, res) { res.header("Access-Control-Allow-Origin", "*"); diff --git a/src/App.vue b/src/App.vue index 702ba8e..57be884 100644 --- a/src/App.vue +++ b/src/App.vue @@ -100,6 +100,8 @@ onMounted(()=>{ activePage.value = aP w() }) + }else{ + // 刷新token } }) diff --git a/src/pinia/store.js b/src/pinia/store.js index bde6aab..6e652a5 100644 --- a/src/pinia/store.js +++ b/src/pinia/store.js @@ -4,7 +4,14 @@ import config from "../../miaoShareConfig.json" const userID = localStorage.getItem('userID'); const userGroup = localStorage.getItem('userGroup'); +const tokenExpiresIn = config.tokenExpiresIn +if(!localStorage.getItem('tokenExpiredTime') || + parseInt(localStorage.getItem('tokenExpiredTime')) < parseInt(Date.now())){ + localStorage.removeItem('token') + } + const token = localStorage.getItem('token'); + // console.log(token) var activeFuctionLocal = localStorage.getItem('activeFuction') diff --git a/vite.config.js b/vite.config.js index 4953b3a..d5f3199 100644 --- a/vite.config.js +++ b/vite.config.js @@ -21,4 +21,22 @@ export default defineConfig({ esbuild: { drop: ['console', 'debugger'], }, + build: { + minify: true, + rollupOptions: { + output: { + // 打包输出的配置 + manualChunks: id => { + // 这个ID,就是所有文件的绝对路径 + if (id.includes("node_modules")) { + // 因为 node_modules 中的依赖通常是不会改变的 + // 所以直接单独打包出去 + // 这个return 的值就是打包的名称 + // return id.split('node_modules')[1] + return "modules"; + } + } + } + } + } }) \ No newline at end of file