Skip to content

Commit

Permalink
fix: fix unresponsive search
Browse files Browse the repository at this point in the history
  • Loading branch information
lazzzis committed Jul 10, 2022
1 parent 588ed1d commit 4f348c0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 43 deletions.
64 changes: 29 additions & 35 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Axios } from 'axios'
import axios from 'axios'
import urlJoin from 'url-join'
import { useSessionStore } from './store/modules/session'
Expand All @@ -9,41 +8,41 @@ axios.defaults.baseURL = '/api/'
axios.defaults.withCredentials = true
axios.defaults.timeout = 100000 // 100000ms的超时验证
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
const methods = [ 'get', 'post', 'put', 'delete' ] as const
const instance = {} as {
[K in (typeof methods)[number]]: Axios[K]
}
const instance = axios.create()

let errHandler: null | ((err: any) => void) = null
let isSemiRestful = false

export function setErrorHandler (handler: typeof errHandler) {
errHandler = handler
}

// 对异常的基本处理
methods.forEach((key) => {
const axiosMethod = axios[key]
type T = Parameters<typeof axiosMethod>
// A decorator that wraps the passed function and handles errors
instance[key] = async function (...args: T) { // 闭包给原函数捕获异常
try {
const data: {
data: { profile: Profile | null }
} = await Reflect.apply(axiosMethod, axios, args)
if (data.data.profile) {
useSessionStore().setLoginProfile(data.data.profile)
}
return data
} catch (err) {
if (errHandler) {
errHandler(err)
} else {
// eslint-disable-next-line no-alert
window.alert('故障')
}
return Promise.reject(new Error('I throw this on purpose'))
}
} as Axios[typeof key]
instance.interceptors.request.use((config) => {
if (!isSemiRestful) return config
if (config.method === 'put') {
config.method = 'post'
config.url = urlJoin(config.url!, '/update')
} else if (config.method === 'delete') {
config.method = 'post'
config.url = urlJoin(config.url!, '/delete')
}
return config
})

instance.interceptors.response.use((resp) => {
const data: { profile: Profile | null } = resp.data
if (data.profile) {
useSessionStore().setLoginProfile(data.profile)
}
return resp
}, (err) => {
if (errHandler) {
errHandler(err)
} else {
// eslint-disable-next-line no-alert
window.alert('故障')
}
return err
})

const api = {
Expand Down Expand Up @@ -133,10 +132,5 @@ export default {
}

export function semiRestful () {
instance.put = function (url, ...args) {
return instance.post(urlJoin(url, '/update'), ...args)
}
instance.delete = function (url, ...args) {
return instance.post(urlJoin(url, '/delete'), ...args)
}
isSemiRestful = true
}
7 changes: 5 additions & 2 deletions src/views/Solution.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ onBeforeMount(async () => {
<tr>
<td>
{{ item.uuid.slice(0, 8) }}
<a :href="testcaseUrl2(item, 'in')" target="_blank">TestIn</a>
<a :href="testcaseUrl2(item, 'out')" target="_blank">TestOut</a>
<template v-if="isAdmin">
<a :href="testcaseUrl2(item, 'in')" target="_blank">TestIn</a>
{{ " " }}
<a :href="testcaseUrl2(item, 'out')" target="_blank">TestOut</a>
</template>
</td>
<td>{{ item.time }}</td>
<td>{{ item.memory }}</td>
Expand Down
12 changes: 6 additions & 6 deletions src/views/Status.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ const find = solutionStore.find
const route = useRoute()
const router = useRouter()
const uid = $computed(() => route.query.uid || '')
const pid = $computed(() => route.query.pid || '')
const judge = $computed(() => parseInt(route.query.judge) || '')
const language = $computed(() => parseInt(route.query.language) || '')
const page = $computed(() => parseInt(route.query.page) || 1)
const pageSize = $computed(() => parseInt(route.query.pageSize) || 30)
const uid = $ref(route.query.uid || '')
const pid = $ref(route.query.pid || '')
const judge = $ref(parseInt(route.query.judge) || '')
const language = $ref(parseInt(route.query.language) || '')
const page = $ref(parseInt(route.query.page) || 1)
const pageSize = $ref(parseInt(route.query.pageSize) || 30)
const judgeList = $ref(constant.judgeList)
const languageList = $ref(constant.languageList)
Expand Down

0 comments on commit 4f348c0

Please sign in to comment.