index.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. import { api_schoolAreaDetail, api_schoolAreaList, api_sysAreaQueryAllProvince, api_userBeneficiarySave, api_userBeneficiaryUpdate } from "../../api/new";
  2. import { GRADE_ENUM } from "../../utils/util";
  3. const classList: any = [];
  4. for (let i = 1; i <= 40; i++) {
  5. classList.push({ text: i + '班', value: i });
  6. }
  7. /** 获取年级 */
  8. const getGradeList = (gradeYear?: string, instrumentCode?: string) => {
  9. let tempList: any = [];
  10. const five = [
  11. { text: '一年级', value: 1, instrumentCode },
  12. { text: '二年级', value: 2, instrumentCode },
  13. { text: '三年级', value: 3, instrumentCode },
  14. { text: '四年级', value: 4, instrumentCode },
  15. { text: '五年级', value: 5, instrumentCode }
  16. ];
  17. const one = [{ text: '六年级', value: 6, instrumentCode }];
  18. const three = [
  19. { text: '七年级', value: 7, instrumentCode },
  20. { text: '八年级', value: 8, instrumentCode },
  21. { text: '九年级', value: 9, instrumentCode }
  22. ];
  23. if (gradeYear === 'FIVE_YEAR_SYSTEM') {
  24. tempList.push(...[...five]);
  25. } else if (gradeYear === 'SIX_YEAR_SYSTEM') {
  26. tempList.push(...[...five, ...one]);
  27. } else if (gradeYear === 'THREE_YEAR_SYSTEM') {
  28. tempList.push(...[...three]);
  29. } else if (gradeYear === 'FORE_YEAR_SYSTEM') {
  30. tempList.push(...[...one, ...three]);
  31. } else {
  32. tempList.push(...[...five, ...one, ...three]);
  33. }
  34. return tempList;
  35. };
  36. // pages/buyerInformation/index.ts
  37. Page({
  38. /**
  39. * 页面的初始数据
  40. */
  41. data: {
  42. phone: '',
  43. name: '',
  44. gender: '1',
  45. schoolAreaId: '',
  46. schoolAreaName: '',
  47. currentGradeClassTxt: '',
  48. currentClass: null,
  49. currentGradeNum: null,
  50. cityCode: null,
  51. cityName: "",
  52. provinceCode: null,
  53. provinceName: "",
  54. regionCode: null,
  55. regionName: "",
  56. userBeneficiaryId: '', // 选中用户的编号
  57. showArea: false,
  58. areaList: [] as any,
  59. showSchool: false,
  60. showSchoolAfterLeave: false, // 离开后
  61. /** 学校列表 */
  62. schoolAreaList: [] as any,
  63. schoolAreaIndex: 0,
  64. /** 临时切换时选择的学校编号 */
  65. tempChangeSchoolAreaId: '',
  66. schoolLoading: false,
  67. /** 搜索学校 */
  68. searchName: '',
  69. /** 学校类型 */
  70. schoolInstrumentSetType: '',
  71. /** 年级班级 */
  72. gradeClassList: [] as any,
  73. gradeClassIndexs: [0, 0],
  74. showGradeClass: false,
  75. showGradeClassAfterLeave: false, // 离开后
  76. },
  77. /**
  78. * 生命周期函数--监听页面加载
  79. */
  80. onLoad(options: any) {
  81. if (options.userBeneficiaryId) {
  82. this.setData({
  83. userBeneficiaryId: options.userBeneficiaryId
  84. })
  85. }
  86. // 从缓存里面获取用户信息
  87. this.getUserDetail()
  88. this.getAreas()
  89. this.getSchools()
  90. },
  91. async getUserDetail() {
  92. try {
  93. const information = wx.getStorageSync('buyerInfomation')
  94. const users = information ? JSON.parse(information) : null
  95. if (users) {
  96. this.setData({
  97. phone: users.phone,
  98. name: users.name,
  99. gender: users.gender,
  100. schoolAreaId: users.schoolAreaId,
  101. schoolAreaName: users.schoolAreaName,
  102. currentGradeClassTxt: users.currentGradeClassTxt,
  103. currentClass: users.currentClass,
  104. currentGradeNum: users.currentGradeNum,
  105. cityCode: users.cityCode,
  106. cityName: users.cityName,
  107. provinceCode: users.provinceCode,
  108. provinceName: users.provinceName,
  109. regionCode: users.regionCode,
  110. regionName: users.regionName,
  111. schoolAreaIndex: users.schoolAreaIndex || 0,
  112. gradeClassIndexs: users.gradeClassIndexs || [0, 0]
  113. }, () => {
  114. this.getSchoolAreaDetail()
  115. })
  116. }
  117. } catch {
  118. //
  119. }
  120. },
  121. onBack() {
  122. // 如果有购买人编号,返回时则不缓存数据
  123. if (!this.data.userBeneficiaryId) {
  124. this.onSetCatch(this.data)
  125. }
  126. wx.navigateBack()
  127. },
  128. /**
  129. * 生命周期函数--监听页面初次渲染完成
  130. */
  131. onReady() {
  132. },
  133. /**
  134. * 生命周期函数--监听页面显示
  135. */
  136. onShow() {
  137. },
  138. /** 获取省市区 */
  139. async getAreas() {
  140. try {
  141. const { data } = await api_sysAreaQueryAllProvince({})
  142. this.setData({
  143. areaList: this.formateArea(data.data)
  144. }, () => {
  145. console.log(this.data.areaList)
  146. })
  147. } catch {
  148. //
  149. }
  150. },
  151. formateArea(area: any[]) {
  152. const province_list: { [_: string]: string } = {};
  153. const city_list: { [_: string]: string } = {};
  154. const county_list: { [_: string]: string } = {};
  155. area.forEach((item: any) => {
  156. province_list[item.code] = item.name;
  157. });
  158. area.forEach((item: any) => {
  159. item.areas && item.areas.forEach((city: any) => {
  160. // console.log(city.code, city.code.toString().split(''))
  161. city_list[city.code] = city.name;
  162. });
  163. });
  164. area.forEach((item: any) => {
  165. item.areas && item.areas.forEach((city: any) => {
  166. city.areas && city.areas.forEach((county: any) => {
  167. county_list[county.code] = county.name;
  168. });
  169. });
  170. });
  171. return {
  172. province_list,
  173. city_list,
  174. county_list
  175. };
  176. },
  177. /** 获取学校列表 */
  178. async getSchools(name?: string) {
  179. this.setData({
  180. schoolLoading: true
  181. })
  182. try {
  183. // 判断是否有地区信息
  184. if (!this.data.provinceCode || !this.data.cityCode || !this.data.regionCode) {
  185. return
  186. }
  187. const { data } = await api_schoolAreaList({
  188. name,
  189. testFlag: true,
  190. provinceCode: this.data.provinceCode,
  191. cityCode: this.data.cityCode,
  192. regionCode: this.data.regionCode
  193. })
  194. const result = data.data || []
  195. const tempList: any[] = []
  196. result.forEach((item: any) => {
  197. tempList.push({
  198. text: item.name,
  199. value: item.id
  200. })
  201. })
  202. let tempSchoolId = ''
  203. if (tempList.length > 0) {
  204. const first = tempList[0]
  205. tempSchoolId = first.value || ''
  206. }
  207. this.setData({
  208. schoolAreaList: tempList,
  209. tempChangeSchoolAreaId: tempSchoolId
  210. })
  211. } catch {
  212. //
  213. }
  214. this.setData({
  215. schoolLoading: false
  216. })
  217. },
  218. /** 获取学校详情 */
  219. async getSchoolAreaDetail() {
  220. const { data } = await api_schoolAreaDetail({ id: this.data.schoolAreaId })
  221. const result = data.data || {}
  222. let gradeClassList: any = []
  223. let schoolInstrumentSetType = ''
  224. if (result.school) {
  225. const schoolInfo = result.school || {};
  226. const schoolInstrumentList = schoolInfo.schoolInstrumentList || [];
  227. // forms.schoolInstrumentSetType = schoolInfo.instrumentSetType;
  228. if (schoolInfo.instrumentSetType === 'SCHOOL') {
  229. gradeClassList = [{
  230. values: getGradeList(schoolInfo.gradeYear),
  231. defaultIndex: this.data.gradeClassIndexs[0]
  232. }, {
  233. values: classList,
  234. defaultIndex: this.data.gradeClassIndexs[1]
  235. }]
  236. schoolInstrumentSetType = schoolInfo.instrumentSetType
  237. } else if (schoolInfo.instrumentSetType === 'GRADE') {
  238. const gradeList: any = []
  239. schoolInstrumentList.forEach((item: any) => {
  240. gradeList.push({
  241. text: GRADE_ENUM[item.gradeNum],
  242. value: item.gradeNum,
  243. instrumentId: item.instrumentId
  244. })
  245. });
  246. gradeList.sort((a: any, b: any) => a.value - b.value);
  247. gradeClassList = [{
  248. values: gradeList,
  249. defaultIndex: this.data.gradeClassIndexs[0]
  250. }, {
  251. values: classList,
  252. defaultIndex: this.data.gradeClassIndexs[1]
  253. }]
  254. schoolInstrumentSetType = schoolInfo.instrumentSetType
  255. } else if (schoolInfo.instrumentSetType === 'CLASS') {
  256. // // 班级
  257. const tempGradeList: any[] = [];
  258. schoolInstrumentList.forEach((item: any) => {
  259. if (!tempGradeList.includes(item.gradeNum)) {
  260. tempGradeList.push(item.gradeNum);
  261. }
  262. });
  263. const lastGradeList: any[] = [];
  264. tempGradeList.forEach((temp: any) => {
  265. const list = {
  266. text: GRADE_ENUM[temp],
  267. value: temp,
  268. instrumentId: '',
  269. instrumentCode: '',
  270. instrumentName: '',
  271. classList: [] as any
  272. };
  273. schoolInstrumentList.forEach((item: any) => {
  274. if (temp === item.gradeNum) {
  275. list.instrumentId = item.instrumentId;
  276. list.instrumentCode = item.instrumentCode;
  277. list.instrumentName = item.instrumentName;
  278. list.classList.push({
  279. text: item.classNum + '班',
  280. value: item.classNum,
  281. instrumentCode: item.instrumentCode
  282. });
  283. }
  284. });
  285. // 排序班级
  286. list.classList.sort((a: any, b: any) => a.value - b.value);
  287. lastGradeList.push(list);
  288. });
  289. lastGradeList.sort((a: any, b: any) => a.value - b.value);
  290. // console.log(lastGradeList, 'lastGradeList')
  291. gradeClassList = [{
  292. values: lastGradeList,
  293. defaultIndex: this.data.gradeClassIndexs[0]
  294. }, {
  295. values: lastGradeList[this.data.gradeClassIndexs[0]]?.classList || [],
  296. defaultIndex: this.data.gradeClassIndexs[1]
  297. }]
  298. schoolInstrumentSetType = schoolInfo.instrumentSetType
  299. } else {
  300. gradeClassList = [{
  301. values: getGradeList(),
  302. defaultIndex: this.data.gradeClassIndexs[0]
  303. }, {
  304. values: classList,
  305. defaultIndex: this.data.gradeClassIndexs[1]
  306. }]
  307. schoolInstrumentSetType = ''
  308. }
  309. } else {
  310. gradeClassList = [{
  311. values: getGradeList(),
  312. defaultIndex: this.data.gradeClassIndexs[0]
  313. }, {
  314. values: classList,
  315. defaultIndex: this.data.gradeClassIndexs[1]
  316. }]
  317. schoolInstrumentSetType = ''
  318. }
  319. // 格式化年级班级 - 如果后台改了学校配置,本地保存了缓存,判断年级、班级是否存在
  320. const grade = this.data.gradeClassIndexs
  321. const tempGradeList = gradeClassList[0]
  322. const tempClassList = gradeClassList[1]
  323. if ((tempGradeList?.values?.length || 0) - 1 < grade[0] || (tempClassList?.values?.length || 0) - 1 < grade[1]) {
  324. gradeClassList[0].defaultIndex = 0
  325. gradeClassList[1].defaultIndex = 0
  326. this.setData({
  327. gradeClassList,
  328. gradeClassIndexs: [0, 0],
  329. currentClass: null,
  330. currentGradeNum: null,
  331. currentGradeClassTxt: '',
  332. schoolInstrumentSetType
  333. })
  334. } else {
  335. this.setData({
  336. gradeClassList,
  337. schoolInstrumentSetType
  338. })
  339. }
  340. },
  341. /** 选择男女 */
  342. onCheckGender(e: any) {
  343. const { dataset } = e.target
  344. this.setData({
  345. gender: dataset.gender
  346. })
  347. },
  348. /** 显示选择地区 */
  349. onShowAreaList() {
  350. this.setData({
  351. showArea: true
  352. })
  353. },
  354. /** 关闭选择地区 */
  355. onCloseAreaList() {
  356. this.setData({
  357. showArea: false
  358. })
  359. },
  360. onChangeAreaList(e: any) {
  361. // console.log(e, '3')
  362. },
  363. /** 确定选择地区 */
  364. submitArea(e: any) {
  365. const selectedOptions: any = e.detail.values
  366. this.setData({
  367. provinceCode: selectedOptions[0].code,
  368. cityCode: selectedOptions[1].code,
  369. regionCode: selectedOptions[2]?.code,
  370. provinceName: selectedOptions[0].name,
  371. cityName: selectedOptions[1].name,
  372. regionName: selectedOptions[2]?.name,
  373. showArea: false,
  374. searchName: '',
  375. schoolAreaId: '',
  376. schoolAreaName: '',
  377. schoolAreaIndex: 0,
  378. gradeClassIndexs: [0, 0],
  379. 'gradeClassList[0].defaultIndex': 0,
  380. 'gradeClassList[1].defaultIndex': 0,
  381. currentGradeNum: null,
  382. currentClass: null,
  383. currentGradeClassTxt: '',
  384. }, () => {
  385. this.getSchools()
  386. })
  387. },
  388. /** 关闭选择学校 */
  389. onCloseSchool() {
  390. this.setData({
  391. showSchool: false
  392. })
  393. },
  394. /** 选择学校关闭后 */
  395. onSchoolAfterLeave() {
  396. this.setData({
  397. showSchoolAfterLeave: true
  398. })
  399. },
  400. /** 选择学校打开前 */
  401. onSchoolBeforeEnter() {
  402. this.setData({
  403. showSchoolAfterLeave: false
  404. })
  405. },
  406. /** 选择学校 */
  407. onSelectSchool() {
  408. if (!this.data.provinceName) {
  409. wx.showToast({
  410. title: '请选择地区',
  411. icon: 'none'
  412. })
  413. return
  414. }
  415. this.setData({
  416. showSchool: true
  417. })
  418. },
  419. /** 确定选择学校 */
  420. onSubmitSchool() {
  421. const detail = this.data.schoolAreaList.find((item: any) => item.value === this.data.tempChangeSchoolAreaId)
  422. const detailIndex = this.data.schoolAreaList.findIndex((item: any) => item.value === this.data.tempChangeSchoolAreaId)
  423. // console.log(detail, detailIndex, this.data.tempChangeSchoolAreaId)
  424. if (detailIndex === -1) return
  425. this.setData({
  426. schoolAreaName: detail.text,
  427. schoolAreaId: detail.value,
  428. schoolAreaIndex: detailIndex,
  429. showSchool: false,
  430. gradeClassIndexs: [0, 0],
  431. 'gradeClassList[0].defaultIndex': 0,
  432. 'gradeClassList[1].defaultIndex': 0,
  433. currentGradeNum: null,
  434. currentClass: null,
  435. currentGradeClassTxt: '',
  436. }, () => {
  437. this.getSchoolAreaDetail()
  438. })
  439. },
  440. onChangeSchool(e: any) {
  441. const { value } = e.detail.value
  442. this.setData({
  443. tempChangeSchoolAreaId: value
  444. })
  445. },
  446. onSearch() {
  447. this.setData({
  448. schoolAreaIndex: 0
  449. }, () => {
  450. this.getSchools(this.data.searchName);
  451. })
  452. },
  453. onSearchChange(e: any) {
  454. this.setData({
  455. searchName: e.detail
  456. })
  457. },
  458. /** 选择年级班级 */
  459. onSelectGradeClass() {
  460. if (!this.data.schoolAreaId) {
  461. wx.showToast({
  462. title: '请选择学校',
  463. icon: 'none'
  464. })
  465. return
  466. }
  467. this.setData({
  468. showGradeClass: true
  469. })
  470. },
  471. /** 年级班级 */
  472. onCloseGradeClass() {
  473. this.setData({
  474. showGradeClass: false
  475. })
  476. },
  477. onGradeClassBeforeEnter() {
  478. this.setData({
  479. showGradeClassAfterLeave: false
  480. })
  481. },
  482. onGradeClassAfterLeave() {
  483. this.setData({
  484. showGradeClassAfterLeave: true
  485. })
  486. },
  487. /** 确认选择年级班级 */
  488. onSubmitGradeClass(e: any) {
  489. const selectedOptions: any = e.detail.value
  490. const selectedIndexs: any = e.detail.index
  491. // console.log(selectedOptions, e, 'selectedOptions')
  492. this.setData({
  493. currentGradeClassTxt: selectedOptions[0].text + selectedOptions[1].text,
  494. currentGradeNum: selectedOptions[0].value,
  495. currentClass: selectedOptions[1].value,
  496. 'gradeClassList[0].defaultIndex': selectedIndexs[0],
  497. 'gradeClassList[1].defaultIndex': selectedIndexs[1],
  498. gradeClassIndexs: selectedIndexs,
  499. showGradeClass: false
  500. })
  501. },
  502. /** 年级班级切换时 */
  503. onGradeClassChange(event: any) {
  504. const { picker, index, value } = event.detail;
  505. if (index !== 0) return
  506. // 第一列滚动时
  507. // console.log(this.data.gradeClassList, "this.data.gradeClassList")
  508. if (this.data.schoolInstrumentSetType === "CLASS") {
  509. const firstColumn = value[index]
  510. const gradeDetail = this.data.gradeClassList[0]?.values
  511. const classList = gradeDetail?.find((item: any) => item.value === firstColumn.value)
  512. if (classList) {
  513. picker.setColumnIndex(1, '0')
  514. picker.setColumnValues(1, classList.classList);
  515. }
  516. }
  517. },
  518. /** 最终提交 */
  519. async onSubmit() {
  520. try {
  521. const params = this.data
  522. if (!params.phone || !/^1[3456789]\d{9}$/.test(params.phone)) {
  523. wx.showToast({
  524. title: '请输入正确的手机号',
  525. icon: "none"
  526. })
  527. return
  528. }
  529. if (!params.name) {
  530. wx.showToast({
  531. title: '请输入学生姓名',
  532. icon: "none"
  533. })
  534. return
  535. }
  536. if (!params.provinceCode || !params.cityCode || !params.regionCode) {
  537. wx.showToast({
  538. title: '请选择地区',
  539. icon: "none"
  540. })
  541. return
  542. }
  543. if (!params.schoolAreaId) {
  544. wx.showToast({
  545. title: '请选择学校',
  546. icon: "none"
  547. })
  548. return
  549. }
  550. if (!params.currentGradeNum) {
  551. wx.showToast({
  552. title: '请选择年级',
  553. icon: "none"
  554. })
  555. return
  556. }
  557. const pages = getCurrentPages();
  558. const prevPage = pages[pages.length - 2]; // 获取上一个页面实例
  559. if (params.userBeneficiaryId) {
  560. const { data } = await api_userBeneficiaryUpdate({
  561. id: params.userBeneficiaryId,
  562. phone: params.phone,
  563. name: params.name,
  564. gender: params.gender,
  565. currentGradeNum: params.currentGradeNum,
  566. currentClass: params.currentClass,
  567. schoolAreaId: params.schoolAreaId,
  568. defaultStatus: false
  569. })
  570. wx.showToast({
  571. title: '提交成功',
  572. icon: 'none'
  573. })
  574. prevPage?.setData({ backParams: { userBeneficiaryId: data.data.id, name: params.name, phone: params.phone, schoolInfo: params.schoolAreaName + params.currentGradeClassTxt } });
  575. } else {
  576. const { data } = await api_userBeneficiarySave({
  577. "phone": params.phone,
  578. "name": params.name,
  579. "gender": params.gender,
  580. "currentGradeNum": params.currentGradeNum,
  581. "currentClass": params.currentClass,
  582. "schoolAreaId": params.schoolAreaId,
  583. defaultStatus: false
  584. })
  585. wx.showToast({
  586. title: '提交成功',
  587. icon: 'none'
  588. })
  589. prevPage?.setData({ backParams: { userBeneficiaryId: data.data.id, name: params.name, phone: params.phone, schoolInfo: params.schoolAreaName + params.currentGradeClassTxt } });
  590. }
  591. this.onSetCatch(params)
  592. wx.navigateBack()
  593. } catch {
  594. //
  595. }
  596. },
  597. /** 设置缓存 */
  598. onSetCatch(params: any) {
  599. wx.setStorageSync('buyerInfomation', JSON.stringify({
  600. phone: params.phone,
  601. name: params.name,
  602. gender: params.gender,
  603. schoolAreaId: params.schoolAreaId,
  604. schoolAreaName: params.schoolAreaName,
  605. currentGradeClassTxt: params.currentGradeClassTxt,
  606. currentClass: params.currentClass,
  607. currentGradeNum: params.currentGradeNum,
  608. cityCode: params.cityCode,
  609. cityName: params.cityName,
  610. provinceCode: params.provinceCode,
  611. provinceName: params.provinceName,
  612. regionCode: params.regionCode,
  613. regionName: params.regionName,
  614. schoolAreaIndex: params.schoolAreaIndex,
  615. gradeClassIndexs: params.gradeClassIndexs
  616. }))
  617. }
  618. })