添加工具类

This commit is contained in:
zhangyiming
2020-11-19 11:41:58 +08:00
parent 3cc8a1f3fe
commit 854432ea81
17 changed files with 403 additions and 36 deletions

BIN
src/assets/bg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

23
src/assets/index.css Normal file
View File

@@ -0,0 +1,23 @@
/* 公共样式 */
/* 样式重置 */
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
position: relative;
}
.login,
.register {
width: 100%;
height: 100%;
overflow-x: hidden;
position: relative;
background: url("./bg.png") no-repeat center;
background-size: cover;
position: relative;
}

BIN
src/assets/login.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.7 KiB

View File

@@ -4,6 +4,9 @@ import router from './router'
import store from './store'
import EnclosureHttp from "./api/utils/core"
// 导入公共样式
import "./assets/index.css"
const app = createApp(App)
// 全局注册Axios

View File

@@ -1,5 +1,6 @@
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Home from '../views/home.vue'
import { setToken, getToken } from "../utils/token"
const routes: Array<RouteRecordRaw> = [
{
@@ -7,14 +8,22 @@ const routes: Array<RouteRecordRaw> = [
name: 'home',
component: Home
},
// {
// path: '/about',
// name: 'About',
// // route level code-splitting
// // this generates a separate chunk (about.[hash].js) for this route
// // which is lazy-loaded when the route is visited.
// component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
// }
{
path: '/login',
name: 'login',
// route level code-splitting
// this generates a separate chunk (login.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "login" */ '../views/login.vue')
},
{
path: '/register',
name: 'register',
// route level code-splitting
// this generates a separate chunk (register.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "register" */ '../views/register.vue')
}
]
const router = createRouter({
@@ -22,4 +31,11 @@ const router = createRouter({
routes
})
const whiteList = ["/login", "/register"]
router.beforeEach((to, _from, next) => {
const token = getToken()
whiteList.indexOf(to.path) !== -1 || token ? next() : next("/login") // 全部重定向到登录页
})
export default router

19
src/utils/token.ts Normal file
View File

@@ -0,0 +1,19 @@
/**
* 处理token的工具
*/
// 存
export function setToken(token: string): void {
token && sessionStorage.setItem("Token", token)
}
// 取
export function getToken(): string {
let token = sessionStorage.getItem("Token") && JSON.parse(sessionStorage.getItem("Token") || "");
return token;
}
// 删
export function delToken(): void {
sessionStorage.removeItem("Token");
}

View File

@@ -1,18 +1,3 @@
<template>
<div>{{ text }}</div>
</template>
<script lang="ts">
import { ref, onMounted, nextTick } from "vue"
export default {
mounted() {
this.$http.request("get", "/getApi")
},
setup() {
const text = ref("vue-ts")
return {
text,
}
},
}
</script>
<div>Home</div>
</template>

22
src/views/login.vue Normal file
View File

@@ -0,0 +1,22 @@
<template>
<div class="login">{{ text }}</div>
</template>
<script lang="ts">
import { ref } from "vue"
export default {
mounted() {
// @ts-ignore
this.$http.request("get", "/getApi")
},
setup() {
const text = ref("login")
return {
text,
}
},
}
</script>
<style scoped>
</style>

22
src/views/register.vue Normal file
View File

@@ -0,0 +1,22 @@
<template>
<div class="register">{{ text }}</div>
</template>
<script lang="ts">
import { ref } from "vue"
export default {
mounted() {
// @ts-ignore
this.$http.request("get", "/getApi")
},
setup() {
const text = ref("register")
return {
text,
}
},
}
</script>
<style scoped>
</style>