Skip to content

Commit

Permalink
feat: split schools
Browse files Browse the repository at this point in the history
  • Loading branch information
JingBh committed Jul 29, 2024
1 parent d0cc9b8 commit f757fb9
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 26 deletions.
5 changes: 4 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { computed, reactive } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useDark } from '@vueuse/core'
import { useSchool } from './lib/school'
import { useStore } from './lib/store'
const school = useSchool()
const store = useStore()
const router = useRouter()
Expand All @@ -24,7 +27,7 @@ const isTabBarRoute = computed(() => {
const hasTitle = computed(() => !!route.meta.title)
const title = computed<string>(() => {
return route.meta?.title ?? '信息学部权益墙'
return route.meta?.title ?? (school.value.name + '权益墙')
})
const isNotHome = computed(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let instance: AxiosInstance | null = null
export const useAxiosInstance = (): AxiosInstance => {
if (instance === null) {
instance = axios.create({
baseURL: import.meta.env.VITE_API_URL
baseURL: '/api'
})

instance.interceptors.request.use((config) => {
Expand Down
39 changes: 28 additions & 11 deletions src/lib/router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { nextTick } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'

import { useSchool } from './school'
import { useStore } from './store'
import About from '../pages/About.vue'
import AdminAdminList from '../pages/AdminAdminList.vue'
Expand All @@ -14,6 +14,7 @@ import QuestionCreate from '../pages/QuestionCreate.vue'
import QuestionCreateSuccess from '../pages/QuestionCreateSuccess.vue'
import QuestionIndex from '../pages/QuestionIndex.vue'
import QuestionShow from '../pages/QuestionShow.vue'
import SchoolSwitch from '../pages/SchoolSwitch.vue'
import UserIndex from '../pages/UserIndex.vue'
import UserLogin from '../pages/UserLogin.vue'

Expand Down Expand Up @@ -63,7 +64,7 @@ const routes: RouteRecordRaw[] = [
path: '/announcements',
component: AnnouncementIndex,
meta: {
title: '信息学部权益墙'
title: '{school}权益墙'
}
},
{
Expand Down Expand Up @@ -110,6 +111,13 @@ const routes: RouteRecordRaw[] = [
title: '反馈问题'
}
},
{
path: '/schools',
component: SchoolSwitch,
meta: {
title: '切换学院'
}
},
{
path: '/user',
component: UserIndex,
Expand Down Expand Up @@ -139,6 +147,18 @@ const router = createRouter({
routes
})

/**
* Format page title
*/
router.beforeEach((to) => {
if (to.meta?.title) {
while (to.meta.title.includes('{school}')) {
to.meta.title = to.meta.title.replace('{school}', useSchool().value.name)
}
}
return true
})

/**
* Fetch user information
*/
Expand Down Expand Up @@ -185,17 +205,14 @@ router.beforeEach(async (to) => {
* Change page title
*/
router.afterEach((to) => {
const school = useSchool()
const store = useStore()

if (store.isWeixin) {
nextTick(() => {
const appTitle = '信息学部权益墙'
if (to.meta?.title && to.meta.title !== appTitle) {
document.title = `${to.meta.title} - ${appTitle}`
} else {
document.title = appTitle
}
})
const appTitle = school.value.name + '权益墙'
if (store.isWeixin && to.meta?.title && to.meta.title !== appTitle) {
document.title = `${to.meta.title} - ${appTitle}`
} else {
document.title = appTitle
}
})

Expand Down
49 changes: 49 additions & 0 deletions src/lib/school.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { computed } from 'vue'
import type { ComputedRef } from 'vue'

import type { SchoolInfo, SchoolInfoBase } from '../types/school'

import logoDefault from '../assets/images/bjut-logo.svg?url'

export const schoolDefault: SchoolInfoBase = {
name: '信息学部',
logo: logoDefault
}

export const schools: SchoolInfo[] = [
{
domain: 'https://appeal.bjut.tech',
name: '信息科学技术学院'
},
{
domain: 'https://appeal-cs.bjut.tech',
name: '计算机学院'
}
]

const schoolRef = computed<SchoolInfoBase>(() => {
const domain = window.location.origin
const school = schools.find(school => school.domain === domain)
return Object.assign({}, schoolDefault, school)
})

export const useSchool = (): ComputedRef<SchoolInfoBase> => {
return schoolRef
}

type SchoolListItem = SchoolInfoBase & SchoolInfo & {
active: boolean
}

const schoolListRef = computed<SchoolListItem[]>(() => {
const domain = window.location.origin
return schools.map((school) => {
return Object.assign({}, schoolDefault, school, {
active: school.domain === domain
})
})
})

export const useSchoolList = (): ComputedRef<SchoolListItem[]> => {
return schoolListRef
}
11 changes: 6 additions & 5 deletions src/pages/About.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { computed } from 'vue'
import { useAxios } from '@vueuse/integrations/useAxios'
import { useAxiosInstance } from '../lib/axios'
import logo from '../assets/images/bjut-fit-logo.svg?inline'
import { useSchool } from '../lib/school'
interface ActuatorInfo {
build?: {
Expand All @@ -16,6 +15,8 @@ interface ActuatorInfo {
}
}
const school = useSchool()
const { data } = useAxios<ActuatorInfo>('actuator/info', useAxiosInstance(), {
immediate: true
})
Expand All @@ -36,20 +37,20 @@ const version = computed(() => {
class="w-32 h-32 bg-white"
round
fit="fill"
:src="logo"
:src="school.logo"
/>
</div>
<div class="flex flex-col items-center gap-2">
<h1 class="text-2xl">
<span class="font-bold">信息学部权益墙</span>
<span class="font-bold">{{ school.name }}权益墙</span>
<span
v-if="version"
class="ml-2 text-sm text-gray-500 dark:text-neutral-400"
v-text="version"
/>
</h1>
<p class="text-gray-500 dark:text-neutral-400">
北京工业大学信息学部学生会生活权益部
北京工业大学{{ school.name }}学生会权益部
</p>
</div>
</div>
Expand Down
46 changes: 46 additions & 0 deletions src/pages/SchoolSwitch.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<script setup lang="ts">
import { useSchoolList } from '../lib/school'
import type { SchoolInfo } from '../types/school'
const schools = useSchoolList()
const navigate = (school: SchoolInfo): void => {
location.href = school.domain
}
</script>

<template>
<div class="flex flex-col items-stretch py-6 gap-4">
<van-cell-group inset>
<van-cell
v-for="school of schools"
:key="school.name"
size="large"
:title="school.name"
:label="school.domain"
:is-link="!school.active"
center
@click="navigate(school)"
>
<template #icon>
<img
:src="school.logo"
:alt="'logo of ' + school.name"
class="w-12 h-12 mr-3 rounded-full"
>
</template>
<template
v-if="school.active"
#right-icon
>
<van-icon
size="18"
class="text-green-500"
class-prefix="bi"
name="check2-circle"
/>
</template>
</van-cell>
</van-cell-group>
</div>
</template>
8 changes: 8 additions & 0 deletions src/pages/UserIndex.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ const { data: count } = useAxios<{
:to="store.loggedIn ? '/user/history' : ''"
:aria-disabled="!store.loggedIn"
/>
<van-cell
icon-prefix="bi"
icon="arrow-left-right"
title="切换学院"
center
is-link
to="/schools"
/>
</van-cell-group>

<van-cell-group
Expand Down
19 changes: 13 additions & 6 deletions src/pages/UserLogin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useSchool } from '../lib/school'
import { useAxiosInstance } from '../lib/axios'
import { useStore } from '../lib/store'
const school = useSchool()
const store = useStore()
const router = useRouter()
Expand Down Expand Up @@ -63,14 +66,18 @@ const submit = (): void => {

<template>
<div class="flex flex-col items-stretch mx-4 py-6 gap-4">
<h1 class="text-2xl font-semibold text-center">
统一身份认证登录
</h1>
<!--<img
src="../assets/images/bjut-fit-logo-horizontal.png"
<img
v-if="school.logoHorizontal"
:src="school.logoHorizontal"
alt="logo"
class="mx-auto h-[4.5rem] mt-4 mb-6"
>-->
>
<h1
v-else
class="text-2xl font-semibold text-center"
>
统一身份认证登录
</h1>
<van-cell-group
class="!mx-0"
inset
Expand Down
13 changes: 13 additions & 0 deletions src/types/school.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface SchoolInfoBase {
domain?: string
name: string
logo: string
logoHorizontal?: string
}

export interface SchoolInfo {
domain: string
name: string
logo?: string
logoHorizontal?: string
}
2 changes: 1 addition & 1 deletion src/utils/attachment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Attachment } from '../types/Attachment'

export const getUrl = (attachment: Attachment): string => {
return `${import.meta.env.VITE_API_URL}/attachments/${attachment.id}`
return `/api/attachments/${attachment.id}`
}
1 change: 0 additions & 1 deletion src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/// <reference types="vite/client" />

interface ImportMetaEnv {
readonly VITE_API_URL: string
readonly VITE_ANALYTICS_CF_TOKEN?: string
}

Expand Down

0 comments on commit f757fb9

Please sign in to comment.