From 467dbccdd96e361dc2d73c0a01f04a6599822de3 Mon Sep 17 00:00:00 2001 From: king <2229249788@qq.com> Date: Thu, 10 Aug 2023 17:00:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=80=89=E6=8B=A9=E9=83=A8?= =?UTF-8?q?=E9=97=A8=E5=BF=85=E4=BF=AE=E9=80=89=E4=BF=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/storageDep.ts | 43 ++++++++++++++++++++---- src/pages/manage/selectDep/selectDep.tsx | 23 +++++++++++-- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/hooks/storageDep.ts b/src/hooks/storageDep.ts index b70704a..b0e9c89 100644 --- a/src/hooks/storageDep.ts +++ b/src/hooks/storageDep.ts @@ -1,26 +1,57 @@ import Taro from "@tarojs/taro"; -const KET = 'SELECTDEP' +const KET = 'SELECT_DEP' +const KET_REQUIRED = 'SELECT_REQUIRED' +/** 部门 */ function set(data: number[]) { Taro.setStorageSync(KET, data) } -function remove() { - Taro.removeStorageSync(KET) -} function get(): number[] { const deps = Taro.getStorageSync(KET) if (deps && deps.length) { - remove() + removeDeps() return deps } return [] } + +function removeDeps() { + Taro.removeStorageSync(KET) +} + +/** 必修选修 */ +function removeRequired() { + Taro.removeStorageSync(KET_REQUIRED) +} + +function setRequired(data: number[]) { + Taro.setStorageSync(KET_REQUIRED, data) +} + + +/** getRequired 比 set后调用 */ +function getRequired(): (0 | 1)[] { + const deps = Taro.getStorageSync(KET_REQUIRED) + if (deps && deps.length) { + removeRequired() + return deps + } + return [] +} + +function remove() { + removeRequired() + removeDeps() +} + export default { set, get, - remove + remove, + setRequired, + getRequired } diff --git a/src/pages/manage/selectDep/selectDep.tsx b/src/pages/manage/selectDep/selectDep.tsx index eb7597a..f5bbf4f 100644 --- a/src/pages/manage/selectDep/selectDep.tsx +++ b/src/pages/manage/selectDep/selectDep.tsx @@ -7,16 +7,22 @@ import folder from "@/static/img/folder.png"; import MyButton from "@/components/button/MyButton"; import storageDep from "@/hooks/storageDep"; +/** + * 选择部门 depIds 是 JSON = number[] + * required 是 必修选修 + */ const SelectDep: FC = () => { - const {depIds} = Taro.getCurrentInstance()?.router?.params as { depIds: string } + const params = Taro.getCurrentInstance()?.router?.params as { depIds: string, required: string | undefined } const [ids, setIds] = useState([]) const [deps, setDeps] = useState([]) + const [required, setRequired] = useState([]) useEffect(() => { curriculum.department().then(res => { setDeps(res.data) }) - setIds(JSON.parse(depIds)) + setIds(JSON.parse(params.depIds)) + setRequired(JSON.parse(params.required || "[]")) }, []) const onChange = useCallback((id: number) => { @@ -26,10 +32,20 @@ const SelectDep: FC = () => { ...ids, id ]) + + if (params.required) { + setRequired([...required, 0]) + } } else { const oldIds: number[] = JSON.parse(JSON.stringify(ids)) oldIds.splice(index, 1) setIds(oldIds) + + if (params.required) { + const oldRequired: number[] = JSON.parse(JSON.stringify(required)) + oldRequired.splice(index, 1) + setRequired(oldRequired) + } } }, [ids]) @@ -39,12 +55,13 @@ const SelectDep: FC = () => { return } storageDep.set(ids) + storageDep.setRequired(required) Taro.navigateBack({delta: 1}) } return ( - {deps.map(d => onChange(d.id)}> + {deps.map((d) => onChange(d.id)}>