Skip to content

Commit

Permalink
fix: some known bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Horbin-Magician committed Apr 15, 2024
1 parent a7e0b96 commit aef87d9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
28 changes: 22 additions & 6 deletions front-end/src/components/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
import { RouterLink, useRouter, useRoute } from 'vue-router'

import '@/assets/icons/iconfont'
import { userlogin, initUser, checkLogin, userlogout } from '@/utils/userUtils';
import { userlogin, initUser, checkLogin, userlogout, addUpdateFun} from '@/utils/userUtils';
import storageUtils from '@/utils/storageUtils';

const route = useRoute()
const router = useRouter()
const docBody = document.body
const message = useMessage()

const router_items = ref({
'首页': {path: '/', shown: true},
'日历': {path: '/calendar', shown: false},
'关于': {path: '/about', shown: true}
})
const now_page = ref(route.path.slice(1).split('/')[0])
const showLoginModal = ref(false)
const showLogoutModal = ref(false)
Expand All @@ -21,16 +26,19 @@
const user_key = ref('')
const nav_track_left = ref('10px')

addUpdateFun(update_routers)
// 初始化User信息
initUser().then(data =>{
if(data && data.status == 0) message.success(data.message)
})

// 初始化theme信息
const theme_storaged = storageUtils.getTheme()
if(theme_storaged != null && theme_storaged != theme){
docBody.setAttribute('theme', theme_storaged)
theme.value = theme_storaged
}

// 定义函数
const switchDocumentTheme = () => {
theme.value = (theme.value == "light") ? "dark" : "light"
Expand All @@ -48,6 +56,7 @@
}
})
}

function onLogoClicked(){
if(checkLogin()){
showLogoutModal.value = true
Expand All @@ -70,8 +79,15 @@
break;
}
}
// 初始化nav_track_left
update_track_left()
update_track_left() // 初始化nav_track_left

function update_routers(){
if(checkLogin()){
router_items.value['日历'].shown = true
} else {
router_items.value['日历'].shown = false
}
}

// 监听路由变化
router.afterEach((to, from) => {
Expand All @@ -89,9 +105,9 @@
</div>
<div class="left-bar">
<div class="nav">
<router-link to="/" class="link"> 首页 </router-link>
<router-link to="/calendar" class="link" v-if="checkLogin()"> 日历 </router-link>
<router-link to="/about" class="link"> 关于 </router-link>
<div v-for="(value, key) in router_items" :key="key">
<router-link :to="value.path" class="link" v-if="value.shown"> {{key}} </router-link>
</div>
<div class="nav-track"> </div>
</div>
<div class="theme" @click="switchDocumentTheme">
Expand Down
16 changes: 9 additions & 7 deletions front-end/src/pages/CalendarPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@
import calendar from '@/utils/calendarUtils'
import { checkLogin } from '@/utils/userUtils';

const message = useMessage()
const router = useRouter()
if(!checkLogin()) {
router.push('/')
message.error("请先登录!")
}

function preYear () {
let n = show_date.value
n.setFullYear(n.getFullYear() - 1)
Expand Down Expand Up @@ -194,6 +187,15 @@

const weeks = ["一", "二", "三", "四", "五", "六", "日"]
const monthData = ref(generateMonth(show_date.value))

onMounted(() => {
const message = useMessage()
const router = useRouter()
if(!checkLogin()) {
router.push('/')
message.error("请先登录!")
}
});
</script>

<template>
Expand Down
14 changes: 7 additions & 7 deletions front-end/src/pages/SecretPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
import { checkLogin } from '@/utils/userUtils';
import { getSecret, updateState, updateMessage } from '@/api/secretAPI'

const message = useMessage()
const router = useRouter()
if(!checkLogin()) {
router.push('/')
message.error("请先登录!")
}

// states
const secret_state = ref(-1); // -1: first see, 0: seen, 1: like, 2: dislike
let secret = null;
Expand Down Expand Up @@ -93,6 +86,13 @@
}

onMounted(() => {
const message = useMessage()
const router = useRouter()
if(!checkLogin()) {
router.push('/')
message.error("请先登录!")
}

const start_words_first =
'欢迎来到秘密树洞!@' +
'从今天到小小语毕业,这里每天都会浮现一条小小槟的“小秘密”。@' +
Expand Down
8 changes: 5 additions & 3 deletions front-end/src/utils/userUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import storageUtils from './storageUtils'
/**
* 用户登录
*/
let updateFuns = [] //用户状态更新回调函数
let updateFuns = [] //用户状态更新回调函数
let isLogining = false //是否正在登录

//添加回调
export const addUpdateFun = (fun) =>{
Expand All @@ -25,24 +26,25 @@ const update = ()=>{
* @returns 结果promise
*/
export const userlogin = (username, password) => {
isLogining = true
return new Promise((resolve) => {
reqLogin(username, password).then(data => {
if (data && data.status === '0') {//登入成功
const authority = data.authority
memoryUtils.userdata = { username, password, authority}
storageUtils.saveUser({ username, password, authority })
update()//更新
data['message'] = "登录成功,欢迎回来~"
} else {//账号或密码错误
const user = storageUtils.getUser()
if (user.username){
storageUtils.removeUser()
memoryUtils.userdata = null
update()//更新
data['message'] = "登录已失效,请重新登陆!"
}
else data['message'] = "登录失败,账号或密码错误!";
}
update()//更新
isLogining = false
resolve(data)
})
})
Expand Down

0 comments on commit aef87d9

Please sign in to comment.