import { api_schoolAreaDetail, api_schoolAreaList, api_sysAreaQueryAllProvince, api_userBeneficiarySave, api_userBeneficiaryUpdate } from "../../api/new"; import { GRADE_ENUM } from "../../utils/util"; const classList: any = []; for (let i = 1; i <= 40; i++) { classList.push({ text: i + '班', value: i }); } /** 获取年级 */ const getGradeList = (gradeYear?: string, instrumentCode?: string) => { let tempList: any = []; const five = [ { text: '一年级', value: 1, instrumentCode }, { text: '二年级', value: 2, instrumentCode }, { text: '三年级', value: 3, instrumentCode }, { text: '四年级', value: 4, instrumentCode }, { text: '五年级', value: 5, instrumentCode } ]; const one = [{ text: '六年级', value: 6, instrumentCode }]; const three = [ { text: '七年级', value: 7, instrumentCode }, { text: '八年级', value: 8, instrumentCode }, { text: '九年级', value: 9, instrumentCode } ]; if (gradeYear === 'FIVE_YEAR_SYSTEM') { tempList.push(...[...five]); } else if (gradeYear === 'SIX_YEAR_SYSTEM') { tempList.push(...[...five, ...one]); } else if (gradeYear === 'THREE_YEAR_SYSTEM') { tempList.push(...[...three]); } else if (gradeYear === 'FORE_YEAR_SYSTEM') { tempList.push(...[...one, ...three]); } else { tempList.push(...[...five, ...one, ...three]); } return tempList; }; // pages/buyerInformation/index.ts Page({ /** * 页面的初始数据 */ data: { cacheArea: [] as { cityCode: string, shiftCityCode: string }[], // 临时存储的对应关系 phone: '', name: '', gender: '', schoolAreaId: '', schoolAreaName: '', currentClassTxt: '', // 班级 currentClass: null, currentGradeTxt: '', // 年级 currentGradeNum: null, cityCode: null, cityName: "", provinceCode: null, provinceName: "", regionCode: null, regionName: "", userBeneficiaryId: '', // 选中用户的编号 userBeneficiaryInfo: { name: '', phoneNumber: '', schoolInfo: '' }, showArea: false, showAreaAfterLeave: false, areaList: [] as any, showSchool: false, showSchoolAfterLeave: false, // 离开后 /** 学校列表 */ schoolAreaList: [] as any, schoolAreaIndex: 0, /** 临时切换时选择的学校编号 */ tempChangeSchoolAreaId: '', schoolLoading: false, /** 搜索学校 */ searchName: '', /** 学校类型 */ schoolInstrumentSetType: '', /** 年级 */ gradeGradeList: [] as any, gradeGradeIndex: 0, showGradeClass: false, showGradeClassAfterLeave: false, // 离开后 /** 班级 */ classList: [] as any, classIndex: 0, showClass: false, showClassAfterLeave: false, // 离开后 buyerLoading: false, currentIndex: 0, }, /** * 生命周期函数--监听页面加载 */ onLoad(options: any) { if (options.userBeneficiaryId) { this.setData({ userBeneficiaryId: options.userBeneficiaryId }) } // 从缓存里面获取用户信息 this.getUserDetail() this.getAreas() this.getSchools() }, onBack() { // 如果有购买人编号,返回时则不缓存数据 if (!this.data.userBeneficiaryId) { this.onSetCatch(this.data) } wx.navigateBack() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, async getUserDetail() { try { // const { data } = await api_userBeneficiaryDetail({ // id: this.data.userBeneficiaryId // }) // console.log(data, 'data') const information = wx.getStorageSync('buyerInfomation') const users = information ? JSON.parse(information) : null if (users) { this.setData({ phone: users.phone, name: users.name, gender: users.gender, schoolAreaId: users.schoolAreaId, schoolAreaName: users.schoolAreaName, currentGradeTxt: users.currentGradeTxt, currentClass: users.currentClass, currentClassTxt: users.currentClassTxt, currentGradeNum: users.currentGradeNum, cityCode: users.cityCode, cityName: users.cityName, provinceCode: users.provinceCode, provinceName: users.provinceName, regionCode: users.regionCode, regionName: users.regionName, schoolAreaIndex: users.schoolAreaIndex, gradeGradeIndex: users.gradeGradeIndex, classIndex: users.classIndex }, () => { this.getSchoolAreaDetail() }) } } catch { // } }, /** 获取省市区 */ async getAreas() { try { const { data } = await api_sysAreaQueryAllProvince({}) this.setData({ areaList: this.formateArea(data.data) }) } catch { // } }, formateArea(area: any[]) { const province_list: { [_: string]: string } = {}; const city_list: { [_: string]: string } = {}; const county_list: { [_: string]: string } = {}; area.forEach((item: any) => { province_list[item.code] = item.name; }); area.forEach((item: any) => { item.areas && item.areas.forEach((city: any, index: number) => { let code = city.code + "" // 某些数据不标准 这里需要转换一下 if (code[4] !== "0" || code[5] !== "0") { // 现在把区域的数据改为市的 const newCode = code.substring(0, 2) + (index < 10 ? `a${index}` : index < 20 ? `b${index - 10}` : index < 30 ? `c${index - 20}` : `d${index - 30}`) + "00"; this.data.cacheArea.push({ cityCode: code, shiftCityCode: newCode }) code = newCode } city_list[code] = city.name; }); }); area.forEach((item: any) => { item.areas && item.areas.forEach((city: any) => { city.areas && city.areas.forEach((county: any) => { county_list[county.code] = county.name; }); }); }); return { province_list, city_list, county_list }; }, // 转换 formateCityCode(reverse?: boolean) { if (!this.data.regionCode && this.data.cityCode) { const cityCodeObj = this.data.cacheArea.find((item: any) => { return item[reverse ? "cityCode" : "shiftCityCode"] == this.data.cityCode }) return cityCodeObj ? cityCodeObj[reverse ? "shiftCityCode" : "cityCode"] : "" } return this.data.cityCode }, /** 获取学校列表 */ async getSchools(name?: string) { this.setData({ schoolLoading: true }) try { // 判断是否有地区信息 if (!this.data.provinceCode || !this.data.cityCode) { return } const citycode = this.formateCityCode() const { data } = await api_schoolAreaList({ name, testFlag: true, provinceCode: this.data.provinceCode, cityCode: citycode, regionCode: this.data.regionCode }) const result = data.data || [] const tempList: any[] = [] result.forEach((item: any) => { tempList.push({ text: item.name, value: item.id }) }) let tempSchoolId = '' let areaIndex = 0 if (tempList.length > 0) { const childIndex = tempList.findIndex((item: any) => this.data.schoolAreaId === item.value) console.log(childIndex, 'childIndex') if (childIndex !== -1) { const first = tempList[childIndex] tempSchoolId = first.value || '' areaIndex = childIndex } else { const first = tempList[0] tempSchoolId = first.value || '' areaIndex = 0 } } this.setData({ schoolAreaIndex: areaIndex, schoolAreaList: tempList, tempChangeSchoolAreaId: tempSchoolId }, () => { const schoolArea = this.selectComponent('#schoolArea') schoolArea?.setIndexes([areaIndex]) }) } catch { // } this.setData({ schoolLoading: false }) }, /** 获取学校详情 */ async getSchoolAreaDetail() { if (!this.data.schoolAreaId) return const { data } = await api_schoolAreaDetail({ id: this.data.schoolAreaId }) const result = data.data || {} let tempGradeGradeList: any = [] let tempClassList: any = [] let schoolInstrumentSetType = '' if (result.school) { const schoolInfo = result.school || {}; const schoolInstrumentList = schoolInfo.schoolInstrumentList || []; // forms.schoolInstrumentSetType = schoolInfo.instrumentSetType; if (schoolInfo.instrumentSetType === 'SCHOOL') { tempGradeGradeList = getGradeList(schoolInfo.gradeYear) tempClassList = classList schoolInstrumentSetType = schoolInfo.instrumentSetType } else if (schoolInfo.instrumentSetType === 'GRADE') { const gradeList: any = [] schoolInstrumentList.forEach((item: any) => { gradeList.push({ text: GRADE_ENUM[item.gradeNum], value: item.gradeNum, instrumentId: item.instrumentId }) }); gradeList.sort((a: any, b: any) => a.value - b.value); tempGradeGradeList = gradeList tempClassList = classList schoolInstrumentSetType = schoolInfo.instrumentSetType } else if (schoolInfo.instrumentSetType === 'CLASS') { // // 班级 const tempGradeList: any[] = []; schoolInstrumentList.forEach((item: any) => { if (!tempGradeList.includes(item.gradeNum)) { tempGradeList.push(item.gradeNum); } }); const lastGradeList: any[] = []; tempGradeList.forEach((temp: any) => { const list = { text: GRADE_ENUM[temp], value: temp, instrumentId: '', instrumentCode: '', instrumentName: '', classList: [] as any }; schoolInstrumentList.forEach((item: any) => { if (temp === item.gradeNum) { list.instrumentId = item.instrumentId; list.instrumentCode = item.instrumentCode; list.instrumentName = item.instrumentName; list.classList.push({ text: item.classNum + '班', value: item.classNum, instrumentCode: item.instrumentCode }); } }); // 排序班级 list.classList.sort((a: any, b: any) => a.value - b.value); lastGradeList.push(list); }); lastGradeList.sort((a: any, b: any) => a.value - b.value); tempGradeGradeList = lastGradeList tempClassList = lastGradeList[this.data.gradeGradeIndex]?.classList || [] schoolInstrumentSetType = schoolInfo.instrumentSetType } else { tempGradeGradeList = getGradeList() tempClassList = classList schoolInstrumentSetType = '' } } else { tempGradeGradeList = getGradeList() tempClassList = classList schoolInstrumentSetType = '' } // 格式化年级班级 - 如果后台改了学校配置,本地保存了缓存,判断年级、班级是否存在 const gradeIndex = this.data.gradeGradeIndex const classIndex = this.data.classIndex if ((tempGradeGradeList.length || 0) - 1 < gradeIndex) { tempClassList = tempGradeGradeList[0]?.classList || [] this.setData({ gradeGradeList: tempGradeGradeList, classList: tempClassList, schoolInstrumentSetType: schoolInstrumentSetType, gradeGradeIndex: 0, classIndex: 0, // currentClass: null, // currentClassTxt: '', // currentGradeNum: null, // currentGradeTxt: '' }) } else if ((tempClassList.length || 0) - 1 < classIndex) { this.setData({ gradeGradeList: tempGradeGradeList, classList: tempClassList, schoolInstrumentSetType: schoolInstrumentSetType, classIndex: 0, // currentClass: null, // currentClassTxt: '', }) } else { this.setData({ gradeGradeList: tempGradeGradeList, classList: tempClassList, schoolInstrumentSetType: schoolInstrumentSetType }) } }, /** 选择男女 */ onCheckGender(e: any) { const { dataset } = e.target this.setData({ gender: dataset.gender }) }, /** 显示选择地区 */ onShowAreaList() { this.setData({ showArea: true }) }, /** 关闭选择地区 */ onCloseAreaList() { this.setData({ showArea: false }) }, /** 选择地区关闭后 */ onAreaAfterLeave() { this.setData({ showAreaAfterLeave: true }) }, /** 选择地区打开前 */ onAreaBeforeEnter() { this.setData({ showAreaAfterLeave: false }) }, /** 确定选择地区 */ submitArea(e: any) { const selectedOptions: any = e.detail.values this.setData({ provinceCode: selectedOptions[0].code, cityCode: selectedOptions[1].code, regionCode: selectedOptions[2]?.code || null, provinceName: selectedOptions[0].name || '', cityName: selectedOptions[1].name || '', regionName: selectedOptions[2]?.name || '', showArea: false, searchName: '', schoolAreaId: '', schoolAreaName: '', schoolAreaIndex: 0, currentGradeNum: null, currentGradeTxt: '', gradeGradeIndex: 0, currentClass: null, currentClassTxt: '', classIndex: 0 }, () => { this.getSchools() }) }, /** 关闭选择学校 */ onCloseSchool() { this.setData({ showSchool: false }) }, /** 选择学校关闭后 */ onSchoolAfterLeave() { this.setData({ showSchoolAfterLeave: true }) }, /** 选择学校打开前 */ onSchoolBeforeEnter() { this.setData({ showSchoolAfterLeave: false }) }, /** 选择学校 */ onSelectSchool() { if (!this.data.provinceName) { wx.showToast({ title: '请选择地区', icon: 'none' }) return } this.setData({ showSchool: true }) }, /** 确定选择学校 */ onSubmitSchool() { if (this.data.tempChangeSchoolAreaId === this.data.schoolAreaId) { this.setData({ showSchool: false }) return } const detail = this.data.schoolAreaList.find((item: any) => item.value === this.data.tempChangeSchoolAreaId) const detailIndex = this.data.schoolAreaList.findIndex((item: any) => item.value === this.data.tempChangeSchoolAreaId) // console.log(detail, detailIndex, this.data.tempChangeSchoolAreaId) if (detailIndex === -1) return this.setData({ schoolAreaName: detail.text, schoolAreaId: detail.value, schoolAreaIndex: detailIndex, showSchool: false, currentGradeNum: null, currentGradeTxt: '', gradeGradeIndex: 0, currentClass: null, currentClassTxt: '', classIndex: 0 }, () => { this.getSchoolAreaDetail() }) }, onChangeSchool(e: any) { const { value } = e.detail.value this.setData({ tempChangeSchoolAreaId: value }) }, onSearch() { this.getSchools(this.data.searchName); }, onSearchChange(e: any) { this.setData({ searchName: e.detail }) }, /** 选择年级班级 */ onSelectGradeClass() { if (!this.data.schoolAreaId) { wx.showToast({ title: '请选择学校', icon: 'none' }) return } this.setData({ showGradeClass: true }) }, /** 年级班级 */ onCloseGradeClass() { this.setData({ showGradeClass: false }) }, onGradeClassBeforeEnter() { this.setData({ showGradeClassAfterLeave: false }) }, onGradeClassAfterLeave() { this.setData({ showGradeClassAfterLeave: true }) }, /** 确认选择年级班级 */ onSubmitGradeClass(e: any) { const selectedOptions: any = e.detail.value const selectedIndexs: any = e.detail.index if (this.data.schoolInstrumentSetType === "CLASS") { const gradeDetail = this.data.gradeGradeList; const classList = gradeDetail?.find((item: any) => item.value === selectedOptions.value) console.log(classList, "classList") if (classList) { this.setData({ classIndex: 0, classList: classList.classList }) } } this.setData({ currentGradeTxt: selectedOptions.text, currentGradeNum: selectedOptions.value, gradeGradeIndex: selectedIndexs, showGradeClass: false, currentClass: null, currentClassTxt: '' }) }, /** 选择班级 */ onSelectClass() { if (!this.data.currentGradeNum) { wx.showToast({ title: '请选择所在年级', icon: 'none' }) return } this.setData({ showClass: true }) }, /** 班级 */ onCloseClass() { this.setData({ showClass: false }) }, onClassBeforeEnter() { this.setData({ showClassAfterLeave: false }) }, onClassAfterLeave() { this.setData({ showClassAfterLeave: true }) }, /** 确认选择班级 */ onSubmitClass(e: any) { const selectedOptions: any = e.detail.value const selectedIndexs: any = e.detail.index this.setData({ currentClassTxt: selectedOptions.text, currentClass: selectedOptions.value, classIndex: selectedIndexs, showClass: false }) }, messageName(value: string) { const nameReg = /^[\u4E00-\u9FA5]+$/ if (!value) { return '请填写享用者姓名'; } else if (!nameReg.test(value)) { return '享用者姓名必须为中文'; } else if (value.length < 2 || value.length > 14) { return '享用者姓名必须为2~14个字'; } else { return '' } }, /** 最终提交 */ async onSubmit() { try { const params = this.data if (!params.provinceCode || !params.cityCode) { wx.showToast({ title: '请选择学校地区', icon: "none" }) return } if (!params.schoolAreaId) { wx.showToast({ title: '请选择所在学校', icon: "none" }) return } if (!params.phone || !/^1[3456789]\d{9}$/.test(params.phone)) { wx.showToast({ title: '请填写正确的电话号码', icon: "none" }) return } if (this.messageName(params.name)) { wx.showToast({ title: this.messageName(params.name), icon: "none" }) return } if (!params.gender) { wx.showToast({ title: '请选择性别', icon: 'none' }) return } if (!params.currentGradeNum) { wx.showToast({ title: '请选择所在年级', icon: "none" }) return } if (!params.currentClass) { wx.showToast({ title: '请选择所在班级', icon: "none" }) return } const objs = { phone: params.phone, name: params.name, gender: params.gender, currentGradeNum: params.currentGradeNum, currentClass: params.currentClass, schoolAreaId: params.schoolAreaId, defaultStatus: false } // const userBeneficiary = { // name: params.name, // phoneNumber: params.phone, // schoolInfo: (params.provinceName || '') + (params.cityName || '') + (params.regionName || '') + params.schoolAreaName + params.currentGradeTxt + params.currentClassTxt // } this.setData({ buyerLoading: true }) const pages = getCurrentPages(); const prevPage = pages[pages.length - 2]; // 获取上一个页面实例 if (params.userBeneficiaryId) { const { data } = await api_userBeneficiaryUpdate({ id: params.userBeneficiaryId, ...objs }) wx.showToast({ title: '保存成功', icon: 'none' }) prevPage?.setData({ backParams: { userBeneficiaryId: data.data.id, name: params.name, phone: params.phone, schoolInfo: (params.provinceName || '') + (params.cityName || '') + (params.regionName || '') + params.schoolAreaName + params.currentGradeTxt + params.currentClassTxt } }); } else { const { data } = await api_userBeneficiarySave({ ...objs }) wx.showToast({ title: '保存成功', icon: 'none' }) prevPage?.setData({ backParams: { userBeneficiaryId: data.data.id, name: params.name, phone: params.phone, schoolInfo: (params.provinceName || '') + (params.cityName || '') + (params.regionName || '') + params.schoolAreaName + params.currentGradeTxt + params.currentClassTxt } }); } this.onSetCatch(params) setTimeout(() => { wx.navigateBack() this.setData({ buyerLoading: false }) }, 1000); } catch { // this.setData({ buyerLoading: false }) } }, /** 设置缓存 */ onSetCatch(params: any) { wx.setStorageSync('buyerInfomation', JSON.stringify({ phone: params.phone, name: params.name, gender: params.gender, schoolAreaId: params.schoolAreaId, schoolAreaName: params.schoolAreaName, currentGradeTxt: params.currentGradeTxt, currentClass: params.currentClass, currentClassTxt: params.currentClassTxt, currentGradeNum: params.currentGradeNum, cityCode: params.cityCode, cityName: params.cityName, provinceCode: params.provinceCode, provinceName: params.provinceName, regionCode: params.regionCode, regionName: params.regionName, schoolAreaIndex: params.schoolAreaIndex, gradeGradeIndex: params.gradeGradeIndex, classIndex: params.classIndex })) } })