新增选择部门必修选修功能

main
king 1 year ago
parent f31ee9534b
commit 467dbccdd9
  1. 43
      src/hooks/storageDep.ts
  2. 23
      src/pages/manage/selectDep/selectDep.tsx

@ -1,26 +1,57 @@
import Taro from "@tarojs/taro"; import Taro from "@tarojs/taro";
const KET = 'SELECTDEP' const KET = 'SELECT_DEP'
const KET_REQUIRED = 'SELECT_REQUIRED'
/** 部门 */
function set(data: number[]) { function set(data: number[]) {
Taro.setStorageSync(KET, data) Taro.setStorageSync(KET, data)
} }
function remove() {
Taro.removeStorageSync(KET)
}
function get(): number[] { function get(): number[] {
const deps = Taro.getStorageSync(KET) const deps = Taro.getStorageSync(KET)
if (deps && deps.length) { if (deps && deps.length) {
remove() removeDeps()
return deps return deps
} }
return [] 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 { export default {
set, set,
get, get,
remove remove,
setRequired,
getRequired
} }

@ -7,16 +7,22 @@ import folder from "@/static/img/folder.png";
import MyButton from "@/components/button/MyButton"; import MyButton from "@/components/button/MyButton";
import storageDep from "@/hooks/storageDep"; import storageDep from "@/hooks/storageDep";
/**
* depIds JSON = number[]
* required
*/
const SelectDep: FC = () => { 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<number[]>([]) const [ids, setIds] = useState<number[]>([])
const [deps, setDeps] = useState<Manage[]>([]) const [deps, setDeps] = useState<Manage[]>([])
const [required, setRequired] = useState<number[]>([])
useEffect(() => { useEffect(() => {
curriculum.department().then(res => { curriculum.department().then(res => {
setDeps(res.data) setDeps(res.data)
}) })
setIds(JSON.parse(depIds)) setIds(JSON.parse(params.depIds))
setRequired(JSON.parse(params.required || "[]"))
}, []) }, [])
const onChange = useCallback((id: number) => { const onChange = useCallback((id: number) => {
@ -26,10 +32,20 @@ const SelectDep: FC = () => {
...ids, ...ids,
id id
]) ])
if (params.required) {
setRequired([...required, 0])
}
} else { } else {
const oldIds: number[] = JSON.parse(JSON.stringify(ids)) const oldIds: number[] = JSON.parse(JSON.stringify(ids))
oldIds.splice(index, 1) oldIds.splice(index, 1)
setIds(oldIds) setIds(oldIds)
if (params.required) {
const oldRequired: number[] = JSON.parse(JSON.stringify(required))
oldRequired.splice(index, 1)
setRequired(oldRequired)
}
} }
}, [ids]) }, [ids])
@ -39,12 +55,13 @@ const SelectDep: FC = () => {
return return
} }
storageDep.set(ids) storageDep.set(ids)
storageDep.setRequired(required)
Taro.navigateBack({delta: 1}) Taro.navigateBack({delta: 1})
} }
return ( return (
<View className='px-2 bg-white'> <View className='px-2 bg-white'>
{deps.map(d => <View className='flex align-center' key={d.id} onClick={() => onChange(d.id)}> {deps.map((d) => <View className='flex align-center' key={d.id} onClick={() => onChange(d.id)}>
<Checkbox value={d.id + ''} checked={ids.includes(d.id)}/> <Checkbox value={d.id + ''} checked={ids.includes(d.id)}/>
<PopPut <PopPut
key={d.id} key={d.id}

Loading…
Cancel
Save