refactor: 移除调试日志,优化代码整洁性
This commit is contained in:
@@ -257,7 +257,7 @@ const handleAttributeClick = (attribute: any) => {
|
|||||||
id: attribute.attributeid,
|
id: attribute.attributeid,
|
||||||
name: attribute.attributename
|
name: attribute.attributename
|
||||||
})
|
})
|
||||||
console.log(attribute)
|
// console.log(attribute)
|
||||||
router.push('/home/aericletype',)
|
router.push('/home/aericletype',)
|
||||||
closeAttributeModal()
|
closeAttributeModal()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,10 @@ import AboutMePage from '../views/aboutme.vue'
|
|||||||
import ArticleContentPage from '../views/articlecontents.vue'
|
import ArticleContentPage from '../views/articlecontents.vue'
|
||||||
import LoginPage from '../views/login.vue'
|
import LoginPage from '../views/login.vue'
|
||||||
import ArticleSavePage from '../views/articlesave.vue'
|
import ArticleSavePage from '../views/articlesave.vue'
|
||||||
|
// 导入全局状态管理
|
||||||
|
import { useGlobalStore } from '@/store/globalStore'
|
||||||
|
// 导入Element Plus消息组件
|
||||||
|
import { ElMessage } from 'element-plus'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 路由配置数组
|
* 路由配置数组
|
||||||
@@ -101,7 +105,8 @@ const routes = [
|
|||||||
name: 'articlesave',
|
name: 'articlesave',
|
||||||
component: ArticleSavePage,
|
component: ArticleSavePage,
|
||||||
meta: {
|
meta: {
|
||||||
title: '保存文章'
|
title: '保存文章',
|
||||||
|
requiresAuth: true // 需要登录才能访问
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -119,15 +124,63 @@ const router = createRouter({
|
|||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 全局路由守卫 - 处理页面标题
|
* 检查token是否过期的工具函数
|
||||||
|
* @param {string} token - JWT token字符串
|
||||||
|
* @returns {boolean} - token是否已过期
|
||||||
|
*/
|
||||||
|
const isTokenExpired = (token) => {
|
||||||
|
try {
|
||||||
|
const payload = JSON.parse(atob(token.split('.')[1]))
|
||||||
|
const exp = payload.exp * 1000 // 转换为毫秒
|
||||||
|
return Date.now() > exp
|
||||||
|
} catch (error) {
|
||||||
|
return true // 解析失败视为过期
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 全局路由守卫 - 处理页面标题和认证
|
||||||
*/
|
*/
|
||||||
router.beforeEach((to, from, next) => {
|
router.beforeEach((to, from, next) => {
|
||||||
|
// 设置页面标题
|
||||||
if (to.meta.title) {
|
if (to.meta.title) {
|
||||||
document.title = to.meta.title + ' - 个人博客'
|
document.title = to.meta.title + ' - 个人博客'
|
||||||
} else {
|
} else {
|
||||||
document.title = '个人博客'
|
document.title = '个人博客'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取全局状态
|
||||||
|
const globalStore = useGlobalStore()
|
||||||
|
|
||||||
|
// 从localStorage获取token
|
||||||
|
const token = localStorage.getItem('token')
|
||||||
|
|
||||||
|
// 如果有token但登录状态为false,自动恢复登录状态
|
||||||
|
if (token && !globalStore.Login && !isTokenExpired(token)) {
|
||||||
|
globalStore.setLoginStatus(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果有token但已过期,清除token和登录状态
|
||||||
|
if (token && isTokenExpired(token)) {
|
||||||
|
localStorage.removeItem('token')
|
||||||
|
globalStore.setLoginStatus(false)
|
||||||
|
ElMessage.error('登录已过期,请重新登录')
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否需要认证
|
||||||
|
if (to.meta.requiresAuth) {
|
||||||
|
// 如果已登录,允许访问
|
||||||
|
if (globalStore.Login) {
|
||||||
next()
|
next()
|
||||||
|
} else {
|
||||||
|
// 未登录,重定向到登录页
|
||||||
|
ElMessage.warning('请先登录')
|
||||||
|
next('/login')
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 不需要认证的路由,直接允许访问
|
||||||
|
next()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
@@ -4,58 +4,17 @@ import { ElMessage } from 'element-plus'
|
|||||||
|
|
||||||
// 创建axios实例
|
// 创建axios实例
|
||||||
const api = axios.create({
|
const api = axios.create({
|
||||||
baseURL: '/api', // API基础URL,使用相对路径,通过Vite代理转发
|
baseURL: '/api',
|
||||||
timeout: 10000, // 请求超时时间
|
timeout: 10000, // 请求超时时间
|
||||||
withCredentials: true // 允许跨域请求携带凭证(如cookies)
|
withCredentials: true // 允许跨域请求携带凭证(如cookies)
|
||||||
})
|
})
|
||||||
|
|
||||||
// 解析JWT token,获取过期时间
|
|
||||||
const parseJwt = (token) => {
|
|
||||||
try {
|
|
||||||
// 移除Bearer前缀(如果有)
|
|
||||||
const pureToken = token.replace('Bearer ', '')
|
|
||||||
// 解析payload部分
|
|
||||||
const base64Url = pureToken.split('.')[1]
|
|
||||||
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/')
|
|
||||||
const jsonPayload = decodeURIComponent(
|
|
||||||
window
|
|
||||||
.atob(base64)
|
|
||||||
.split('')
|
|
||||||
.map((c) => `%${`00${c.charCodeAt(0).toString(16)}`.slice(-2)}`)
|
|
||||||
.join('')
|
|
||||||
)
|
|
||||||
return JSON.parse(jsonPayload)
|
|
||||||
} catch (e) {
|
|
||||||
console.error('解析JWT token失败:', e)
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 验证token是否过期
|
|
||||||
const isTokenExpired = (token) => {
|
|
||||||
const decodedToken = parseJwt(token)
|
|
||||||
if (!decodedToken || !decodedToken.exp) {
|
|
||||||
return true // 如果解析失败或没有过期时间,认为token无效
|
|
||||||
}
|
|
||||||
// 比较过期时间和当前时间(转换为秒)
|
|
||||||
const currentTime = Math.floor(Date.now() / 1000)
|
|
||||||
return decodedToken.exp < currentTime
|
|
||||||
}
|
|
||||||
|
|
||||||
// 请求拦截器
|
// 请求拦截器
|
||||||
api.interceptors.request.use(
|
api.interceptors.request.use(
|
||||||
config => {
|
config => {
|
||||||
// 从localStorage获取token
|
// 从localStorage获取token
|
||||||
let token = localStorage.getItem('token')
|
let token = localStorage.getItem('token')
|
||||||
if (token) {
|
if (token) {
|
||||||
// 验证token是否过期
|
|
||||||
if (isTokenExpired(token)) {
|
|
||||||
// token过期,移除token并跳转到登录页
|
|
||||||
localStorage.removeItem('token')
|
|
||||||
ElMessage.error('登录已过期,请重新登录')
|
|
||||||
// window.location.href = '/login'
|
|
||||||
return Promise.reject(new Error('token已过期'))
|
|
||||||
}
|
|
||||||
// 确保不重复添加Bearer前缀
|
// 确保不重复添加Bearer前缀
|
||||||
if (!token.startsWith('Bearer ')) {
|
if (!token.startsWith('Bearer ')) {
|
||||||
config.headers['Authorization'] = `Bearer ${token}`
|
config.headers['Authorization'] = `Bearer ${token}`
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ class ArticleService {
|
|||||||
* @returns {Promise<import('../types').ApiResponse<import('../types').Article[]>>}
|
* @returns {Promise<import('../types').ApiResponse<import('../types').Article[]>>}
|
||||||
*/
|
*/
|
||||||
getPagedArticles(params = {}) {
|
getPagedArticles(params = {}) {
|
||||||
return api.get(`/articles/status/page?title=${params.title || ''}&categoryid=${params.categoryid || ''}&attributeid=${params.attributeid || ''}&status=${params.status !== undefined ? params.status : 1}&page=${params.page || 0}&size=${params.size || 10}`)
|
return api.get(`/articles/status/page?title=${params.title || ''}&categoryid=${params.categoryid || 0}&attributeid=${params.attributeid || 0}&status=${params.status !== undefined ? params.status : 1}&page=${params.page || 0}&size=${params.size || 10}`)
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 获取已发布文章列表
|
* 获取已发布文章列表
|
||||||
|
|||||||
@@ -151,7 +151,7 @@ const fetchArticleData = async (): Promise<any | null> => {
|
|||||||
try {
|
try {
|
||||||
// 从全局状态获取文章信息
|
// 从全局状态获取文章信息
|
||||||
const response = await globalStore.getValue('articleInfo')
|
const response = await globalStore.getValue('articleInfo')
|
||||||
console.log('获取文章数据:', response)
|
// console.log('获取文章数据:', response)
|
||||||
return response || null
|
return response || null
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('获取文章数据失败:', err)
|
console.error('获取文章数据失败:', err)
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ const fetchArticles = async () => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 3. 为文章列表补充额外信息
|
// 3. 为文章列表补充额外信息
|
||||||
console.log('补充额外信息后的文章列表:', response.data)
|
// console.log('补充额外信息后的文章列表:', response.data)
|
||||||
// 4. 更新文章列表
|
// 4. 更新文章列表
|
||||||
articleList.value = response.data
|
articleList.value = response.data
|
||||||
// 5. 更新总页数
|
// 5. 更新总页数
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ const loadNonsenseList = async () => {
|
|||||||
// 初始化字符样式
|
// 初始化字符样式
|
||||||
// initializeCharStyles()
|
// initializeCharStyles()
|
||||||
// 开始颜色变化定时器
|
// 开始颜色变化定时器
|
||||||
console.log(displayedNonsenseList.value)
|
// console.log(displayedNonsenseList.value)
|
||||||
} else {
|
} else {
|
||||||
ElMessage.error('加载吐槽内容失败')
|
ElMessage.error('加载吐槽内容失败')
|
||||||
error.value = true
|
error.value = true
|
||||||
|
|||||||
@@ -7,8 +7,6 @@ import AutoImport from 'unplugin-auto-import/vite'
|
|||||||
import Components from 'unplugin-vue-components/vite'
|
import Components from 'unplugin-vue-components/vite'
|
||||||
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
|
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
|
||||||
|
|
||||||
|
|
||||||
// https://vite.dev/config/
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [
|
plugins: [
|
||||||
vue(),
|
vue(),
|
||||||
@@ -27,15 +25,6 @@ export default defineConfig({
|
|||||||
},
|
},
|
||||||
server: {
|
server: {
|
||||||
host: '0.0.0.0',
|
host: '0.0.0.0',
|
||||||
proxy: {
|
|
||||||
// 配置API代理
|
|
||||||
'/api': {
|
|
||||||
// target: 'http://www.qf1121.top',
|
|
||||||
target: 'http://localhost:7070',
|
|
||||||
changeOrigin: true,
|
|
||||||
rewrite: (path) => path
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user