-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(exam): rewrite in hooks (#158)
- Loading branch information
Showing
6 changed files
with
171 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { useRequestNext } from "@/hooks"; | ||
import { ZFService } from "@/services"; | ||
import useGeneralInfoStore from "@/store/system/generalInfo"; | ||
import { Exam } from "@/types/Exam"; | ||
import { persistedStorage, RequestError } from "@/utils"; | ||
import { withRespDataNeverNull } from "@/utils/promise"; | ||
import Taro from "@tarojs/taro"; | ||
import { defineStore, storeToRefs } from "pinia"; | ||
import { ref } from "vue"; | ||
|
||
type TermExamCollection = { | ||
exams: Exam[], | ||
year: string; | ||
term: "上" | "下" | "短"; | ||
updateTime: string; | ||
}; | ||
|
||
const useExamStore = defineStore("exam/collections", () => { | ||
const { info: generalInfo } = storeToRefs(useGeneralInfoStore()); | ||
const collections = ref<TermExamCollection[]>([]); | ||
|
||
const { error, loading, run: fetchExam } = useRequestNext( | ||
withRespDataNeverNull(ZFService.getExamInfo), { | ||
defaultParams: { | ||
year: generalInfo.value.termYear, | ||
term: generalInfo.value.term | ||
}, | ||
initialData: [], | ||
onSuccess: (exams, params) => { | ||
const { year, term } = params!; | ||
const existedIndex = collections.value.findIndex(_ => _.year === year && _.term === term); | ||
|
||
if (existedIndex !== -1) { | ||
collections.value[existedIndex] = { | ||
...collections.value[existedIndex], | ||
exams, | ||
updateTime: Date().toString() | ||
}; | ||
} else { | ||
collections.value.push({ exams, year, term, updateTime: Date().toString() }); | ||
} | ||
}, | ||
onError: (e) => { | ||
if (e instanceof RequestError) { | ||
Taro.showToast({ title: `更新成绩失败: ${e.message}`, icon: "none" }); | ||
} | ||
} | ||
} | ||
); | ||
|
||
function queryByTermSync(options?: { year: string, term: "上" | "下" | "短" }) { | ||
const { year, term } = options ?? { term: generalInfo.value.term, year: generalInfo.value.termYear }; | ||
const existed = collections.value.find(_ => _.year === year && _.term === term); | ||
|
||
return existed; | ||
} | ||
|
||
return { | ||
loading, | ||
collections, | ||
fetchExam, | ||
error, | ||
queryByTermSync | ||
}; | ||
}, { | ||
persist: { | ||
storage: persistedStorage, | ||
pick: ["collections"] | ||
} | ||
}); | ||
|
||
export default useExamStore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import useGeneralInfoStore from "@/store/system/generalInfo"; | ||
import { defineStore, storeToRefs } from "pinia"; | ||
import { ref } from "vue"; | ||
|
||
const useExamQueryOptionsStore = defineStore("exam/queryOptions", () => { | ||
const { info: generalInfo } = storeToRefs(useGeneralInfoStore()); | ||
|
||
const term = ref<"上" | "下" | "短">(generalInfo.value.scoreTerm); | ||
const year = ref(generalInfo.value.scoreYear); | ||
|
||
function setOption(value: { | ||
term: "上" | "下" | "短"; | ||
year: string; | ||
}) { | ||
term.value = value.term; | ||
year.value = value.year; | ||
} | ||
|
||
return { | ||
term, | ||
year, | ||
setOption | ||
}; | ||
}); | ||
|
||
export default useExamQueryOptionsStore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import RequestError, { MPErrorCode } from "./request/requestError"; | ||
|
||
/** | ||
* 当 promise 返回的结果为 null 时抛出异常 | ||
* | ||
* 正方的每个接口都有较大的概率返回 data: null | ||
*/ | ||
export function withRespDataNeverNull<T, Args extends any[]>( | ||
fetcher: (...args: Args) => Promise<T>, | ||
options?: { | ||
errMsg?: string | ||
} | ||
) { | ||
const { errMsg = "响应数据格式异常" } = options ?? {}; | ||
|
||
return async (...args: Args) => { | ||
const resp = await fetcher(...args); | ||
if (resp === null) | ||
throw new RequestError(errMsg, MPErrorCode.MP_INVALID_DATA_VALUE); | ||
|
||
return resp as T; | ||
}; | ||
} |