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

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";
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
}

@ -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<number[]>([])
const [deps, setDeps] = useState<Manage[]>([])
const [required, setRequired] = useState<number[]>([])
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 (
<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)}/>
<PopPut
key={d.id}

Loading…
Cancel
Save