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)}>