From 9bc90f9297478d4c4f56380b77eb26b6c16be80e Mon Sep 17 00:00:00 2001 From: PeterAlbus Date: Sat, 26 Mar 2022 21:07:33 +0800 Subject: [PATCH] add token --- index.html | 5 +++ src/main.ts | 15 +++++++- src/router/index.ts | 13 +++++++ src/views/Blog.vue | 3 +- src/views/EditBlog.vue | 26 ++++++++----- src/views/Login.vue | 87 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 136 insertions(+), 13 deletions(-) create mode 100644 src/views/Login.vue diff --git a/index.html b/index.html index bc648d2..78996f6 100644 --- a/index.html +++ b/index.html @@ -7,6 +7,11 @@ + + PeterAlbus的博客 diff --git a/src/main.ts b/src/main.ts index 3441354..fabd725 100644 --- a/src/main.ts +++ b/src/main.ts @@ -23,7 +23,20 @@ import 'element-plus/dist/index.css' axios.defaults.withCredentials=false; axios.defaults.baseURL='https://www.peteralbus.com:8089/' - +// axios.defaults.baseURL='https://localhost:8089/' + +axios.interceptors.request.use( + config => { + if (localStorage.getItem("token")) { //判断token是否存在 + // @ts-ignore + config.headers.satoken_peteralbus_blog = localStorage.getItem("token"); //将token设置成请求头 + } + return config; + }, + err => { + return Promise.reject(err); + } +); VMdEditor.use(vuepressTheme, { Prism diff --git a/src/router/index.ts b/src/router/index.ts index c17dad6..122b8e8 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -8,6 +8,7 @@ const Document=()=>import('../views/Document.vue') const EditBlog=()=>import('../views/EditBlog.vue') const UploadPhoto=()=>import('../views/UploadPhoto.vue') const AddFriendLink=()=>import('../views/AddFriendLink.vue') +const Login=()=>import('../views/Login.vue') const routes: Array = [ { @@ -22,6 +23,18 @@ const routes: Array = [ } } }, + { + path: '/login', + name: 'Login', + component: Login, + meta:{ + title:'登录——PeterAlbus的博客', + content:{ + keywords:'PeterAlbus,Vue', + description:'PeterAlbus的博客登录页' + } + } + }, { path: '/blog', name: 'Blog', diff --git a/src/views/Blog.vue b/src/views/Blog.vue index e2516df..4767b52 100644 --- a/src/views/Blog.vue +++ b/src/views/Blog.vue @@ -63,7 +63,6 @@ import {onMounted, ref ,nextTick} from "vue"; import {useRoute} from "vue-router"; import {ArrowRight,Notebook,Share as shareIcon} from "@element-plus/icons-vue"; import axios from "axios"; -import qs from "qs"; import PersonalInfo from '@/components/PersonalInfo.vue' import type {vMdEditor} from "@kangc/v-md-editor" @@ -95,7 +94,7 @@ const getBlog=()=>{ .then(res=>{ blog.value=res.data; blog.value.blogViews+=1; - axios.post('/update',qs.stringify(blog.value)); + axios.get('/visitBlog?blogId='+blog.value.blogId+"&ipAddress="+localStorage.getItem('ipAddress')); document.title = blog.value.blogTitle+'——PeterAlbus的博客' let meta:any=document.querySelector('meta[name="description"]') if(!meta) diff --git a/src/views/EditBlog.vue b/src/views/EditBlog.vue index 156c4b7..c56b000 100644 --- a/src/views/EditBlog.vue +++ b/src/views/EditBlog.vue @@ -165,33 +165,39 @@ const onSubmit= async (formEl: FormInstance | undefined) => { if (valid) { if(blog.value.blogId!==undefined) { - console.log('update:',blog.value); axios.post('/update',qs.stringify(blog.value)) .then(res=>{ - if(res.data!='fail') + if(res.data=='fail') { - ElMessage.success('更新成功'); - router.push('/blog?id='+blog.value.blogId) + ElMessage.error('更新失败'); + } + else if(res.data=='noPermission') + { + ElMessage.error('你没有权限进行此操作!'); } else { - ElMessage.error('更新失败'); + ElMessage.success('更新成功'); + router.push('/blog?id='+blog.value.blogId) } }) } else { - console.log('new:',blog.value); axios.post('/add',qs.stringify(blog.value)) .then(res=>{ - if(res.data!='fail') + if(res.data=='fail') { - ElMessage.success('发布成功'); - router.push('/blog?id='+blog.value.blogId) + ElMessage.error('发布失败'); + } + else if(res.data=='noPermission') + { + ElMessage.error('你没有权限进行此操作!'); } else { - ElMessage.error('发布失败'); + ElMessage.success('发布成功'); + router.push('/blog?id='+blog.value.blogId) } }) } diff --git a/src/views/Login.vue b/src/views/Login.vue new file mode 100644 index 0000000..6e5d49d --- /dev/null +++ b/src/views/Login.vue @@ -0,0 +1,87 @@ + + + + +