feat: 写了点后端
This commit is contained in:
1 parent
e2079c5388
commit
79cc9c34d6
20 files changed
+219
-20
No files matched your search
+3
-1
@@ -4,4 +4,6 @@
|
|||||||
if [ -d ".output" ]; then
|
if [ -d ".output" ]; then
|
||||||
tar -czvf ./output.tar.gz .output
|
tar -czvf ./output.tar.gz .output
|
||||||
mv output.tar.gz .output/output.tar.gz
|
mv output.tar.gz .output/output.tar.gz
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
echo "You can preview this build using node .output/server/index.mjs"
|
||||||
@@ -38,7 +38,10 @@ const navList: NavItem[] = [
|
|||||||
{
|
{
|
||||||
title: '文章',
|
title: '文章',
|
||||||
path: '/posts',
|
path: '/posts',
|
||||||
otherPath: ['/post']
|
},
|
||||||
|
{
|
||||||
|
title: '后台',
|
||||||
|
path: '/user',
|
||||||
},
|
},
|
||||||
// {
|
// {
|
||||||
// title: '测试',
|
// title: '测试',
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
import { defineStore } from "pinia";
|
||||||
|
|
||||||
|
export const useUserStore = defineStore("user", () => {
|
||||||
|
const isLogin = ref<boolean>(false);
|
||||||
|
const _token = ref<string>();
|
||||||
|
const _tokenExpireTime = ref<number>();
|
||||||
|
|
||||||
|
const tokenExpireTime = computed(() => {
|
||||||
|
if (_tokenExpireTime.value) {
|
||||||
|
return _tokenExpireTime.value;
|
||||||
|
}
|
||||||
|
const res = parseInt(localStorage.getItem("miao-token-expire-time") || "0")
|
||||||
|
_tokenExpireTime.value = res;
|
||||||
|
return res;
|
||||||
|
});
|
||||||
|
|
||||||
|
const setTokenExpireTime = (value: number) => {
|
||||||
|
_tokenExpireTime.value = value;
|
||||||
|
localStorage.setItem("miao-token-expire-time", value.toString());
|
||||||
|
};
|
||||||
|
|
||||||
|
const token = computed(() => {
|
||||||
|
if (_token.value) {
|
||||||
|
return _token.value;
|
||||||
|
}
|
||||||
|
const res = localStorage.getItem("miao-token") || ""
|
||||||
|
_token.value = res;
|
||||||
|
return res;
|
||||||
|
});
|
||||||
|
|
||||||
|
const setLoginStatus = (value: boolean) => {
|
||||||
|
isLogin.value = value;
|
||||||
|
};
|
||||||
|
const setToken = (value: string) => {
|
||||||
|
_token.value = value;
|
||||||
|
localStorage.setItem("miao-token", value);
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
isLogin,
|
||||||
|
setLoginStatus,
|
||||||
|
token,
|
||||||
|
setToken,
|
||||||
|
tokenExpireTime,
|
||||||
|
setTokenExpireTime,
|
||||||
|
_tokenExpireTime,
|
||||||
|
_token
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
export default useUserStore;
|
||||||
+1
-14
@@ -8,7 +8,6 @@ export default defineNuxtConfig({
|
|||||||
devtools: { enabled: false },
|
devtools: { enabled: false },
|
||||||
alias: {
|
alias: {
|
||||||
"@": "/"
|
"@": "/"
|
||||||
// 'dayjs': 'dayjs/esm/'
|
|
||||||
},
|
},
|
||||||
app: {
|
app: {
|
||||||
pageTransition: {
|
pageTransition: {
|
||||||
@@ -18,14 +17,7 @@ export default defineNuxtConfig({
|
|||||||
},
|
},
|
||||||
vite: {
|
vite: {
|
||||||
server: {},
|
server: {},
|
||||||
plugins: [
|
plugins: [],
|
||||||
// AutoImport({
|
|
||||||
// resolvers: [ElementPlusResolver()],
|
|
||||||
// }),
|
|
||||||
// Components({
|
|
||||||
// resolvers: [ElementPlusResolver()],
|
|
||||||
// })
|
|
||||||
],
|
|
||||||
test: {
|
test: {
|
||||||
deps: {
|
deps: {
|
||||||
inline: ['element-plus']
|
inline: ['element-plus']
|
||||||
@@ -34,11 +26,6 @@ export default defineNuxtConfig({
|
|||||||
},
|
},
|
||||||
modules: ["@pinia/nuxt", '@element-plus/nuxt'],
|
modules: ["@pinia/nuxt", '@element-plus/nuxt'],
|
||||||
compatibilityDate: "2025-01-12",
|
compatibilityDate: "2025-01-12",
|
||||||
build: {
|
|
||||||
analyze: {
|
|
||||||
filename: "stats.html",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
nitro: {
|
nitro: {
|
||||||
compressPublicAssets: true,
|
compressPublicAssets: true,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pinia/nuxt": "^0.9.0",
|
"@pinia/nuxt": "^0.9.0",
|
||||||
|
"@types/node": "^22.10.5",
|
||||||
"dayjs": "^1.11.13",
|
"dayjs": "^1.11.13",
|
||||||
"markdown-it": "^14.1.0",
|
"markdown-it": "^14.1.0",
|
||||||
"node-fetch": "^3.3.2",
|
"node-fetch": "^3.3.2",
|
||||||
|
|||||||
+2
-2
@@ -1,5 +1,5 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="post-wrapper">
|
<div class="user-wrapper">
|
||||||
<NuxtPage />
|
<NuxtPage />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
.post-wrapper {
|
.user-wrapper {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<template>
|
||||||
|
<div class="user-wrapper">
|
||||||
|
<NuxtPage />
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style lang="scss" scoped>
|
||||||
|
.user-wrapper {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
margin: 0 auto;
|
||||||
|
max-width: 800px;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
index
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style lang="scss" scoped>
|
||||||
|
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
login
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style lang="scss" scoped>
|
||||||
|
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
用户主界面
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style lang="scss" scoped>
|
||||||
|
|
||||||
|
</style>
|
||||||
Generated
+3
@@ -11,6 +11,9 @@ importers:
|
|||||||
'@pinia/nuxt':
|
'@pinia/nuxt':
|
||||||
specifier: ^0.9.0
|
specifier: ^0.9.0
|
||||||
version: 0.9.0(magicast@0.3.5)(pinia@2.3.0(typescript@5.7.3)(vue@3.5.13(typescript@5.7.3)))(rollup@4.30.1)
|
version: 0.9.0(magicast@0.3.5)(pinia@2.3.0(typescript@5.7.3)(vue@3.5.13(typescript@5.7.3)))(rollup@4.30.1)
|
||||||
|
'@types/node':
|
||||||
|
specifier: ^22.10.5
|
||||||
|
version: 22.10.5
|
||||||
dayjs:
|
dayjs:
|
||||||
specifier: ^1.11.13
|
specifier: ^1.11.13
|
||||||
version: 1.11.13
|
version: 1.11.13
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const body = await readBody(event)
|
||||||
|
return
|
||||||
|
})
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import checkUserData from '~/server/utils/checkUserData';
|
||||||
|
|
||||||
export default defineEventHandler((event) => {
|
export default defineEventHandler((event) => {
|
||||||
const res = fs.readdirSync('./')
|
checkUserData()
|
||||||
|
const res = fs.readdirSync('userData')
|
||||||
return {
|
return {
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import serverConfig from "../server.config"
|
||||||
|
|
||||||
|
const pathNeedAuth = serverConfig.auth.api
|
||||||
|
|
||||||
|
export default defineEventHandler((event) => {
|
||||||
|
// console.log('New request: ' + getRequestURL(event))
|
||||||
|
const path = getRequestURL(event).pathname
|
||||||
|
if(pathNeedAuth.has(path)){
|
||||||
|
// console.log('Auth needed')
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
})
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
export default defineEventHandler((event) => {
|
||||||
|
checkUserData()
|
||||||
|
// console.log('New request: ' + getRequestURL(event))
|
||||||
|
})
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
export default {
|
||||||
|
auth: {
|
||||||
|
// 需要进行鉴权的api
|
||||||
|
api: new Set([
|
||||||
|
'/api/test/testDir'
|
||||||
|
])
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import mkfileIfNotExist from "./mkfileIfNotExist"
|
||||||
|
|
||||||
|
let checked = false
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查并完善userData的目录结构,若不存在密钥则生成随机的密钥
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
|
const checkUserData = () => {
|
||||||
|
if (checked) return
|
||||||
|
checked = true
|
||||||
|
// console.log('check userData')
|
||||||
|
const dirList = [
|
||||||
|
'./userData/',
|
||||||
|
'./userData/db/',
|
||||||
|
'./userData/md/',
|
||||||
|
]
|
||||||
|
dirList.forEach(mkdirIfNotExist)
|
||||||
|
mkfileIfNotExist('./userData/key.txt', new Array(2048).fill(0).map(() => Math.floor(Math.random() * 16).toString(16)).join(''))
|
||||||
|
}
|
||||||
|
|
||||||
|
export default checkUserData
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import fs from 'fs';
|
||||||
|
|
||||||
|
export default function mkdirIfNotExists(dir: string | string[]): void {
|
||||||
|
if (typeof dir !== 'string') {
|
||||||
|
dir.forEach((d) => mkdirIfNotExists(d));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!fs.existsSync(dir)) {
|
||||||
|
try {
|
||||||
|
fs.mkdirSync(dir, { recursive: true });
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
import mkdirIfNotExist from './mkdirIfNotExist';
|
||||||
|
|
||||||
|
export default function mkfileIfNotExist(filePath: string | string[], content?: string): void {
|
||||||
|
content = content || '';
|
||||||
|
if (Array.isArray(filePath)) {
|
||||||
|
filePath.forEach((file) => mkfileIfNotExist(file, content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (typeof filePath !== 'string') {
|
||||||
|
console.error('Invalid file path:', filePath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (fs.existsSync(filePath)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const dir = path.dirname(filePath);
|
||||||
|
mkdirIfNotExist(dir);
|
||||||
|
try {
|
||||||
|
fs.writeFileSync(filePath, content);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error creating file:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-1
@@ -9,6 +9,7 @@
|
|||||||
"style/**/*",
|
"style/**/*",
|
||||||
"utils/**/*",
|
"utils/**/*",
|
||||||
".nuxt/*.d.ts",
|
".nuxt/*.d.ts",
|
||||||
"types/**/*"
|
"types/**/*",
|
||||||
|
"plugins/**/*",
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user