addMember.ts 18 KB

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