This commit is contained in:
2025-03-04 08:52:28 +08:00
parent f91b4ab856
commit 2af1109ced
8 changed files with 29 additions and 13 deletions

View File

@@ -4,6 +4,7 @@ import Login from "./components/Login.vue"
import { onMounted, watch } from "vue" import { onMounted, watch } from "vue"
import { Account } from "./api" import { Account } from "./api"
import { authed, user } from "./store/user" import { authed, user } from "./store/user"
import { STORAGE_KEY } from "./utils/const"
onMounted(async () => { onMounted(async () => {
const data = await Account.getMyProfile() const data = await Account.getMyProfile()
@@ -14,9 +15,9 @@ onMounted(async () => {
watch(authed, (v) => { watch(authed, (v) => {
if (v) { if (v) {
localStorage.setItem("web-isloggedin", "true") localStorage.setItem(STORAGE_KEY.LOGIN, "true")
} else { } else {
localStorage.removeItem("web-isloggedin") localStorage.removeItem(STORAGE_KEY.LOGIN)
} }
}) })
</script> </script>

View File

@@ -1,6 +1,7 @@
import axios from "axios" import axios from "axios"
import { router } from "./router" import { router } from "./router"
import type { TutorialIn } from "./utils/type" import type { TutorialIn } from "./utils/type"
import { STORAGE_KEY } from "./utils/const"
const http = axios.create({ const http = axios.create({
baseURL: baseURL:
@@ -20,8 +21,7 @@ http.interceptors.response.use(
if (err.response) { if (err.response) {
switch (err.response.status) { switch (err.response.status) {
case 401: // 未授权 case 401: // 未授权
localStorage.removeItem("web-isloggedin") localStorage.removeItem(STORAGE_KEY.LOGIN)
alert("未登录")
router.push("/") router.push("/")
break break
case 403: // 禁止访问 case 403: // 禁止访问

View File

@@ -20,7 +20,7 @@
type="error" type="error"
v-if="showMeesage" v-if="showMeesage"
class="message" class="message"
title="登录失败" title="登录失败,请检查用户名和密码"
></n-alert> ></n-alert>
<n-flex> <n-flex>
<n-button block @click="submit" type="primary">登录</n-button> <n-button block @click="submit" type="primary">登录</n-button>

View File

@@ -22,8 +22,9 @@ import { computed, onMounted, ref, useTemplateRef, watch } from "vue"
import { marked } from "marked" import { marked } from "marked"
import { useStorage } from "@vueuse/core" import { useStorage } from "@vueuse/core"
import { Tutorial } from "../api" import { Tutorial } from "../api"
import { STORAGE_KEY } from "../utils/const"
const step = useStorage("web-turtorial-step", 1) const step = useStorage(STORAGE_KEY.STEP, 1)
const displays = ref<number[]>([]) const displays = ref<number[]>([])
const content = ref("") const content = ref("")
const $content = useTemplateRef("$content") const $content = useTemplateRef("$content")

View File

@@ -73,13 +73,14 @@ import { useStorage } from "@vueuse/core"
import { Tutorial } from "../api" import { Tutorial } from "../api"
import type { TutorialSlim } from "../utils/type" import type { TutorialSlim } from "../utils/type"
import { useDialog, useMessage } from "naive-ui" import { useDialog, useMessage } from "naive-ui"
import { STORAGE_KEY } from "../utils/const"
const message = useMessage() const message = useMessage()
const confirm = useDialog() const confirm = useDialog()
const list = ref<TutorialSlim[]>([]) const list = ref<TutorialSlim[]>([])
const content = ref("") const content = ref("")
const tutorial = useStorage("web-tutorial", { const tutorial = useStorage(STORAGE_KEY.TUTORIAL, {
display: 0, display: 0,
title: "", title: "",
content: "", content: "",

View File

@@ -2,6 +2,7 @@ import { createWebHistory, createRouter } from "vue-router"
import { loginModal } from "./store/modal" import { loginModal } from "./store/modal"
import Home from "./pages/Home.vue" import Home from "./pages/Home.vue"
import { STORAGE_KEY } from "./utils/const"
const routes = [ const routes = [
{ path: "/", name: "home", component: Home }, { path: "/", name: "home", component: Home },
@@ -26,7 +27,7 @@ export const router = createRouter({
}) })
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
const isLoggedIn = localStorage.getItem("web-isloggedin") === "true" const isLoggedIn = localStorage.getItem(STORAGE_KEY.LOGIN) === "true"
if (to.meta.auth && !isLoggedIn) { if (to.meta.auth && !isLoggedIn) {
loginModal.value = true loginModal.value = true
next(false) next(false)

View File

@@ -1,4 +1,5 @@
import { useStorage } from "@vueuse/core" import { useStorage } from "@vueuse/core"
import { STORAGE_KEY } from "../utils/const"
const defaultHTML = `<div class="welcome">黄岩一职</div>` const defaultHTML = `<div class="welcome">黄岩一职</div>`
const defaultCSS = `.welcome { const defaultCSS = `.welcome {
@@ -6,12 +7,12 @@ const defaultCSS = `.welcome {
font-size: 24px; font-size: 24px;
}` }`
export const html = useStorage("web-html", defaultHTML) export const html = useStorage(STORAGE_KEY.HTML, defaultHTML)
export const css = useStorage("web-css", defaultCSS) export const css = useStorage(STORAGE_KEY.CSS, defaultCSS)
export const js = useStorage("web-js", "") export const js = useStorage(STORAGE_KEY.JS, "")
export const tab = useStorage("web-tab", "html") export const tab = useStorage(STORAGE_KEY.TAB, "html")
export const size = useStorage("web-fontsize", 24) export const size = useStorage(STORAGE_KEY.FONTSIZE, 24)
export function reset(lang: "html" | "css" | "js") { export function reset(lang: "html" | "css" | "js") {
if (lang === "html") { if (lang === "html") {

View File

@@ -25,3 +25,14 @@ export const alertVariants = [
title: "小心", title: "小心",
}, },
] ]
export const STORAGE_KEY = {
LOGIN: "web-isloggedin",
STEP: "web-turtorial-step",
TUTORIAL: "web-tutorial",
HTML: "web-html",
CSS: "web-css",
JS: "web-js",
TAB: "web-tab",
FONTSIZE: "web-fontsize",
}