index.ts 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. // index.ts
  2. import { api_shopInstruments, api_shopProduct } from "../../api/login";
  3. import { api_schoolAreaDetail, api_schoolAreaList, api_sysAreaQueryAllProvince, api_userBeneficiarySave, api_userBeneficiaryUpdate } from "../../api/new";
  4. import { debounce, formatPrice, GRADE_ENUM } from '../../utils/util'
  5. const classList: any = [];
  6. for (let i = 1; i <= 40; i++) {
  7. classList.push({ text: i + '班', value: i });
  8. }
  9. /** 获取年级 */
  10. const getGradeList = (gradeYear?: string, instrumentCode?: string) => {
  11. let tempList: any = [];
  12. const five = [
  13. { text: '一年级', value: 1, instrumentCode },
  14. { text: '二年级', value: 2, instrumentCode },
  15. { text: '三年级', value: 3, instrumentCode },
  16. { text: '四年级', value: 4, instrumentCode },
  17. { text: '五年级', value: 5, instrumentCode }
  18. ];
  19. const one = [{ text: '六年级', value: 6, instrumentCode }];
  20. const three = [
  21. { text: '七年级', value: 7, instrumentCode },
  22. { text: '八年级', value: 8, instrumentCode },
  23. { text: '九年级', value: 9, instrumentCode }
  24. ];
  25. if (gradeYear === 'FIVE_YEAR_SYSTEM') {
  26. tempList.push(...[...five]);
  27. } else if (gradeYear === 'SIX_YEAR_SYSTEM') {
  28. tempList.push(...[...five, ...one]);
  29. } else if (gradeYear === 'THREE_YEAR_SYSTEM') {
  30. tempList.push(...[...three]);
  31. } else if (gradeYear === 'FORE_YEAR_SYSTEM') {
  32. tempList.push(...[...one, ...three]);
  33. } else {
  34. tempList.push(...[...five, ...one, ...three]);
  35. }
  36. return tempList;
  37. };
  38. // 获取应用实例
  39. const app = getApp<IAppOption>()
  40. // pages/orders/orders.ts
  41. Page({
  42. /**
  43. * 页面的初始数据
  44. */
  45. data: {
  46. goodsImgList: [
  47. 'https://oss.dayaedu.com/ktyq/1739523537634.png',
  48. 'https://oss.dayaedu.com/ktyq/1739523564827.png',
  49. 'https://oss.dayaedu.com/ktyq/1739523574402.png',
  50. 'https://oss.dayaedu.com/ktyq/1739523583463.png'
  51. ],
  52. current: 0,
  53. popupShow: false,
  54. instrumentList: [] as any,
  55. list: [] as any,
  56. isOverSaled: false, // 是否所有商品都没有库存
  57. smallGoods: {}, // 最小金额商品
  58. selected: {} as any,
  59. selectInstrumentId: '', // 选中的乐器
  60. selectedInstrument: {} as any,
  61. formatSelectGood: {
  62. typeName: '',
  63. showSalePrice: '', // 显示的现价
  64. originalPrice: 0, // 原价
  65. salePrice: 0, // 现价
  66. discountPrice: '' // 已省
  67. } as any, // 格式化所有选中的数据
  68. showService: false,
  69. isFromPreviewImage: false,
  70. showBuyer: false, // 收益人
  71. cacheArea: [] as { cityCode: string, shiftCityCode: string }[], // 临时存储的对应关系
  72. phone: '',
  73. name: '',
  74. gender: '',
  75. schoolAreaId: '',
  76. schoolAreaName: '',
  77. currentClassTxt: '', // 班级
  78. currentClass: null,
  79. currentGradeTxt: '', // 年级
  80. currentGradeNum: null,
  81. cityCode: null,
  82. cityName: "",
  83. provinceCode: null,
  84. provinceName: "",
  85. regionCode: null,
  86. regionName: "",
  87. showGender: false,
  88. genderList: [{
  89. name: '男',
  90. value: '1',
  91. className: 'one-gender cancel-gender'
  92. }, {
  93. name: '女',
  94. value: '0',
  95. className: 'two-gender cancel-gender'
  96. }],
  97. userBeneficiaryId: '', // 选中用户的编号
  98. userBeneficiaryInfo: {
  99. name: '',
  100. phoneNumber: '',
  101. schoolInfo: ''
  102. },
  103. showArea: false,
  104. showAreaAfterLeave: false,
  105. areaList: [] as any,
  106. showSchool: false,
  107. showSchoolAfterLeave: false, // 离开后
  108. /** 学校列表 */
  109. schoolAreaList: [] as any,
  110. schoolAreaIndex: 0,
  111. /** 临时切换时选择的学校编号 */
  112. tempChangeSchoolAreaId: '',
  113. schoolLoading: false,
  114. /** 搜索学校 */
  115. searchName: '',
  116. /** 学校类型 */
  117. schoolInstrumentSetType: '',
  118. /** 年级 */
  119. gradeGradeList: [] as any,
  120. gradeGradeIndex: 0,
  121. showGradeClass: false,
  122. showGradeClassAfterLeave: false, // 离开后
  123. /** 班级 */
  124. classList: [] as any,
  125. classIndex: 0,
  126. showClass: false,
  127. showClassAfterLeave: false, // 离开后
  128. buyerLoading: false,
  129. currentIndex: 0,
  130. },
  131. /**
  132. * 生命周期函数--监听页面加载
  133. */
  134. onLoad() {
  135. // this.onInit()
  136. },
  137. /**
  138. * 获取基础信息
  139. */
  140. async onInit() {
  141. try {
  142. const result = await api_shopInstruments({ appId: app.globalData.appId })
  143. const instrumentList = result.data.data || []
  144. instrumentList?.forEach((item: any) => {
  145. item.showSalePrice = formatPrice(item.salePrice || 0, 'ALL')
  146. })
  147. const { data } = await api_shopProduct({ appId: app.globalData.appId });
  148. const list = data.data || []
  149. let selected: any = {}
  150. let isOverSaled = true // 是否销售完
  151. // 最少金额商品
  152. let smallGoods: any = {}
  153. list.forEach((item: any) => {
  154. item.originalPrice = formatPrice(item.originalPrice, "ALL");
  155. item.showSalePrice = formatPrice(item.salePrice, "ALL");
  156. item.typeName = this.formatPeriod(item.num, item.period);
  157. item.discountPrice = formatPrice(
  158. item.originalPrice - item.salePrice,
  159. "ALL"
  160. );
  161. const prices: any = formatPrice(item.salePrice)
  162. item.integerPart = prices.integerPart
  163. item.decimalPart = prices.decimalPart
  164. if (item.stockNum > 0) {
  165. isOverSaled = false
  166. if (!selected.id) {
  167. selected = item
  168. }
  169. }
  170. // 获取最小金额
  171. if (smallGoods?.salePrice) {
  172. smallGoods = smallGoods.salePrice <= item.salePrice ? smallGoods : item
  173. } else {
  174. smallGoods = item
  175. }
  176. });
  177. if (isOverSaled) {
  178. // 没有可购买商品则默认选中第一个商品
  179. selected = list[0]
  180. }
  181. this.setData({
  182. list,
  183. instrumentList, // 乐器列表
  184. isOverSaled,
  185. selected,
  186. smallGoods,
  187. selectInstrumentId: '',
  188. selectedInstrument: {},
  189. userBeneficiaryId: '',
  190. userBeneficiaryInfo: {
  191. name: '',
  192. phoneNumber: '',
  193. schoolInfo: ''
  194. }
  195. }, () => {
  196. this.onFormatGoods()
  197. })
  198. } catch (e) {
  199. console.log(e, 'e')
  200. }
  201. },
  202. // 格式化类型
  203. formatPeriod(num: number, type: string) {
  204. const template: any = {
  205. DAY: "天卡",
  206. MONTH: "月卡",
  207. YEAR: "年卡"
  208. }
  209. if (type === "YEAR" && num >= 99) {
  210. return '永久卡'
  211. }
  212. return num + (template[type] || '')
  213. },
  214. /** 切换swiper */
  215. onSwiperChange(e: any) {
  216. console.log(e, 'e')
  217. return false
  218. },
  219. // 选择
  220. onSelectGoods(e: any) {
  221. const { dataset } = e.currentTarget
  222. const item = this.data.list.find((item: any) => item.id === dataset.id)
  223. // 判断是否有库存
  224. if (item.stockNum <= 0) {
  225. return
  226. }
  227. this.setData({
  228. selected: item || {}
  229. }, () => {
  230. this.onFormatGoods()
  231. })
  232. },
  233. /** 选中乐器 */
  234. onSelectInstrument(e: any) {
  235. const { dataset } = e.currentTarget;
  236. if (dataset.id === this.data.selectInstrumentId) {
  237. this.setData({
  238. selectInstrumentId: '',
  239. selectedInstrument: {}
  240. }, () => {
  241. this.onFormatGoods()
  242. })
  243. } else {
  244. const item = this.data.instrumentList.find((item: any) => item.id === dataset.id);
  245. this.setData({
  246. selectInstrumentId: dataset.id,
  247. selectedInstrument: item || {}
  248. }, () => {
  249. this.onFormatGoods()
  250. })
  251. }
  252. },
  253. /** 格式化选中的商品 */
  254. onFormatGoods() {
  255. const selected = this.data.selected;
  256. const selectedInstrument = this.data.selectedInstrument
  257. const params = {
  258. typeName: '',
  259. showSalePrice: '' as any, // 显示的现价
  260. originalPrice: 0, // 原价
  261. salePrice: 0, // 现价
  262. discountPrice: '' as any, // 已省
  263. integerPart: '',
  264. decimalPart: '',
  265. }
  266. // 选中期限
  267. if (selected.id) {
  268. params.typeName = selected.typeName
  269. params.showSalePrice = selected.showSalePrice
  270. params.originalPrice = selected.originalPrice
  271. params.salePrice = selected.salePrice
  272. params.discountPrice = selected.discountPrice
  273. const prices: any = formatPrice(params.salePrice);
  274. params.integerPart = prices.integerPart
  275. params.decimalPart = prices.decimalPart
  276. }
  277. // 选中乐器
  278. if (selectedInstrument.id) {
  279. params.typeName = selected.typeName ? selected.typeName + '+' + selectedInstrument.name : selectedInstrument.name
  280. params.originalPrice = Number(selected.originalPrice) + Number(selectedInstrument.originalPrice)
  281. params.salePrice = Number(selected.salePrice) + Number(selectedInstrument.salePrice)
  282. params.showSalePrice = formatPrice(params.salePrice, "ALL");
  283. params.discountPrice = formatPrice(
  284. params.originalPrice - params.salePrice,
  285. "ALL"
  286. );
  287. const prices: any = formatPrice(params.salePrice);
  288. params.integerPart = prices.integerPart
  289. params.decimalPart = prices.decimalPart
  290. }
  291. this.setData({
  292. formatSelectGood: params
  293. })
  294. },
  295. isLogin() {
  296. // 判断是否登录
  297. if (!app.globalData.isLogin) {
  298. wx.navigateTo({
  299. url: '../login/login',
  300. })
  301. return false
  302. }
  303. return true
  304. },
  305. /** 我的订单 */
  306. onOrder() {
  307. // 判断是否登录
  308. if (!this.isLogin()) {
  309. return
  310. }
  311. wx.navigateTo({
  312. url: '../orders/orders',
  313. })
  314. },
  315. /** 客服 */
  316. onService() {
  317. this.setData({
  318. showService: true
  319. })
  320. },
  321. changePop(event: { detail: any }) {
  322. this.setData({
  323. showService: event.detail
  324. })
  325. },
  326. onBuyShop() {
  327. // 判断是否登录
  328. if (!this.isLogin()) {
  329. return
  330. }
  331. this.setData({
  332. popupShow: true
  333. })
  334. },
  335. onClose() {
  336. this.setData({
  337. popupShow: false,
  338. currentIndex: 1,
  339. userBeneficiaryId: '',
  340. userBeneficiaryInfo: {
  341. name: '',
  342. phoneNumber: '',
  343. schoolInfo: ''
  344. }
  345. })
  346. },
  347. onSubmit() {
  348. // 判断是否登录
  349. const that = this
  350. if (!this.data.userBeneficiaryId) {
  351. wx.showToast({
  352. title: '请填写享用者的个人信息',
  353. icon: 'none'
  354. })
  355. return
  356. }
  357. debounce(function () {
  358. if (!that.isLogin()) {
  359. return
  360. }
  361. const params = [] as any
  362. const selected = that.data.selected
  363. if (selected.id) {
  364. params.push({
  365. pic: selected.pic,
  366. name: selected.name,
  367. period: selected.period,
  368. num: selected.num,
  369. originalPrice: selected.originalPrice,
  370. salePrice: selected.salePrice,
  371. shopId: selected.shopId,
  372. id: selected.id,
  373. goodsType: 'ACTIVATION_CODE', // INSTRUMENTS
  374. })
  375. }
  376. const selectedInstrument = that.data.selectedInstrument
  377. if (selectedInstrument.id) {
  378. params.push({
  379. pic: selectedInstrument.pic,
  380. name: selectedInstrument.name,
  381. period: selectedInstrument?.period,
  382. num: selectedInstrument?.num || 0,
  383. originalPrice: selectedInstrument.originalPrice,
  384. salePrice: selectedInstrument.salePrice,
  385. shopId: selectedInstrument.shopId,
  386. id: selectedInstrument.id,
  387. goodsType: 'INSTRUMENTS', // INSTRUMENTS
  388. })
  389. }
  390. let info = JSON.stringify({
  391. ...params
  392. });
  393. info = encodeURIComponent(info);
  394. console.log(params, "params")
  395. wx.navigateTo({
  396. url: `../orders/order-detail?orderInfo=${info}&userBeneficiaryId=${that.data.userBeneficiaryId}`,
  397. success: () => {
  398. that.setData({
  399. popupShow: false,
  400. currentIndex: 1,
  401. })
  402. }
  403. })
  404. }, 500)()
  405. },
  406. onPreivewGoodsImg(e: { currentTarget: { dataset: any } }) {
  407. wx.previewImage({
  408. current: e.currentTarget.dataset.src,
  409. urls: this.data.goodsImgList,
  410. success: () => {
  411. this.setData({
  412. isFromPreviewImage: true
  413. })
  414. }
  415. })
  416. },
  417. onPreivewGoods(e: { currentTarget: { dataset: any } }) {
  418. wx.previewImage({
  419. current: e.currentTarget.dataset.src,
  420. urls: [e.currentTarget.dataset.src],
  421. success: () => {
  422. this.setData({
  423. isFromPreviewImage: true
  424. })
  425. }
  426. })
  427. },
  428. /**
  429. * 生命周期函数--监听页面显示
  430. */
  431. onShow() {
  432. if (!this.data.isFromPreviewImage) {
  433. this.onInit()
  434. } else {
  435. this.setData({
  436. isFromPreviewImage: false
  437. })
  438. }
  439. },
  440. onShareAppMessage() {
  441. return {
  442. title: '器乐数字AI工具',
  443. path: '/pages/index/index',
  444. imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
  445. }
  446. },
  447. onShareTimeline() {
  448. return {
  449. title: '器乐数字AI工具',
  450. path: '/pages/index/index',
  451. imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
  452. }
  453. },
  454. onSelectBuyer() {
  455. // 从缓存里面获取用户信息
  456. this.getUserDetail()
  457. if (this.data.areaList.length <= 0) {
  458. this.getAreas()
  459. }
  460. this.getSchools()
  461. this.setData({
  462. showBuyer: true
  463. })
  464. },
  465. onCloseBuyer() {
  466. this.setData({
  467. showBuyer: false
  468. }, () => {
  469. if (!this.data.userBeneficiaryId) {
  470. this.onSetCatch(this.data)
  471. }
  472. })
  473. },
  474. async getUserDetail() {
  475. try {
  476. // const { data } = await api_userBeneficiaryDetail({
  477. // id: this.data.userBeneficiaryId
  478. // })
  479. // console.log(data, 'data')
  480. const information = wx.getStorageSync('buyerInfomation')
  481. const users = information ? JSON.parse(information) : null
  482. if (users) {
  483. this.setData({
  484. phone: users.phone,
  485. name: users.name,
  486. gender: users.gender,
  487. schoolAreaId: users.schoolAreaId,
  488. schoolAreaName: users.schoolAreaName,
  489. currentGradeTxt: users.currentGradeTxt,
  490. currentClass: users.currentClass,
  491. currentClassTxt: users.currentClassTxt,
  492. currentGradeNum: users.currentGradeNum,
  493. cityCode: users.cityCode,
  494. cityName: users.cityName,
  495. provinceCode: users.provinceCode,
  496. provinceName: users.provinceName,
  497. regionCode: users.regionCode,
  498. regionName: users.regionName,
  499. schoolAreaIndex: users.schoolAreaIndex,
  500. gradeGradeIndex: users.gradeGradeIndex,
  501. classIndex: users.classIndex
  502. }, () => {
  503. this.getSchoolAreaDetail()
  504. })
  505. }
  506. } catch {
  507. //
  508. }
  509. },
  510. /** 获取省市区 */
  511. async getAreas() {
  512. try {
  513. const { data } = await api_sysAreaQueryAllProvince({})
  514. this.setData({
  515. areaList: this.formateArea(data.data)
  516. })
  517. } catch {
  518. //
  519. }
  520. },
  521. formateArea(area: any[]) {
  522. const province_list: { [_: string]: string } = {};
  523. const city_list: { [_: string]: string } = {};
  524. const county_list: { [_: string]: string } = {};
  525. area.forEach((item: any) => {
  526. province_list[item.code] = item.name;
  527. });
  528. area.forEach((item: any) => {
  529. item.areas && item.areas.forEach((city: any, index: number) => {
  530. let code = city.code + ""
  531. // 某些数据不标准 这里需要转换一下
  532. if (code[4] !== "0" || code[5] !== "0") {
  533. // 现在把区域的数据改为市的
  534. const newCode = code.substring(0, 2) + (index < 10 ? `a${index}` : index < 20 ? `b${index - 10}` : index < 30 ? `c${index - 20}` : `d${index - 30}`) + "00";
  535. this.data.cacheArea.push({
  536. cityCode: code,
  537. shiftCityCode: newCode
  538. })
  539. code = newCode
  540. }
  541. city_list[code] = city.name;
  542. });
  543. });
  544. area.forEach((item: any) => {
  545. item.areas && item.areas.forEach((city: any) => {
  546. city.areas && city.areas.forEach((county: any) => {
  547. county_list[county.code] = county.name;
  548. });
  549. });
  550. });
  551. return {
  552. province_list,
  553. city_list,
  554. county_list
  555. };
  556. },
  557. // 转换
  558. formateCityCode(reverse?: boolean) {
  559. if (!this.data.regionCode && this.data.cityCode) {
  560. const cityCodeObj = this.data.cacheArea.find((item: any) => {
  561. return item[reverse ? "cityCode" : "shiftCityCode"] == this.data.cityCode
  562. })
  563. return cityCodeObj ? cityCodeObj[reverse ? "shiftCityCode" : "cityCode"] : ""
  564. }
  565. return this.data.cityCode
  566. },
  567. /** 获取学校列表 */
  568. async getSchools(name?: string) {
  569. this.setData({
  570. schoolLoading: true
  571. })
  572. try {
  573. // 判断是否有地区信息
  574. if (!this.data.provinceCode || !this.data.cityCode) {
  575. return
  576. }
  577. const citycode = this.formateCityCode()
  578. const { data } = await api_schoolAreaList({
  579. name,
  580. testFlag: true,
  581. provinceCode: this.data.provinceCode,
  582. cityCode: citycode,
  583. regionCode: this.data.regionCode
  584. })
  585. const result = data.data || []
  586. const tempList: any[] = []
  587. result.forEach((item: any) => {
  588. tempList.push({
  589. text: item.name,
  590. value: item.id
  591. })
  592. })
  593. let tempSchoolId = ''
  594. if (tempList.length > 0) {
  595. const first = tempList[0]
  596. tempSchoolId = first.value || ''
  597. }
  598. this.setData({
  599. schoolAreaList: tempList,
  600. tempChangeSchoolAreaId: tempSchoolId
  601. })
  602. } catch {
  603. //
  604. }
  605. this.setData({
  606. schoolLoading: false
  607. })
  608. },
  609. onCheckedGender() {
  610. this.setData({
  611. showGender: true
  612. })
  613. },
  614. onCloseGender() {
  615. this.setData({
  616. showGender: false,
  617. })
  618. },
  619. onSelectGender(e: any) {
  620. const genderList = this.data.genderList
  621. const gender = e.detail.value
  622. genderList.forEach((item: any, index: number) => {
  623. if (item.value === gender) {
  624. item.className = index === 0 ? 'one-gender select-gender' : 'two-gender select-gender'
  625. } else {
  626. item.className = index === 0 ? 'one-gender cancel-gender' : 'two-gender cancel-gender'
  627. }
  628. })
  629. console.log(e, genderList, gender, '1212')
  630. this.setData({
  631. gender,
  632. genderList
  633. })
  634. },
  635. /** 获取学校详情 */
  636. async getSchoolAreaDetail() {
  637. if (!this.data.schoolAreaId) return
  638. const { data } = await api_schoolAreaDetail({ id: this.data.schoolAreaId })
  639. const result = data.data || {}
  640. let tempGradeGradeList: any = []
  641. let tempClassList: any = []
  642. let schoolInstrumentSetType = ''
  643. if (result.school) {
  644. const schoolInfo = result.school || {};
  645. const schoolInstrumentList = schoolInfo.schoolInstrumentList || [];
  646. // forms.schoolInstrumentSetType = schoolInfo.instrumentSetType;
  647. if (schoolInfo.instrumentSetType === 'SCHOOL') {
  648. tempGradeGradeList = getGradeList(schoolInfo.gradeYear)
  649. tempClassList = classList
  650. schoolInstrumentSetType = schoolInfo.instrumentSetType
  651. } else if (schoolInfo.instrumentSetType === 'GRADE') {
  652. const gradeList: any = []
  653. schoolInstrumentList.forEach((item: any) => {
  654. gradeList.push({
  655. text: GRADE_ENUM[item.gradeNum],
  656. value: item.gradeNum,
  657. instrumentId: item.instrumentId
  658. })
  659. });
  660. gradeList.sort((a: any, b: any) => a.value - b.value);
  661. tempGradeGradeList = gradeList
  662. tempClassList = classList
  663. schoolInstrumentSetType = schoolInfo.instrumentSetType
  664. } else if (schoolInfo.instrumentSetType === 'CLASS') {
  665. // // 班级
  666. const tempGradeList: any[] = [];
  667. schoolInstrumentList.forEach((item: any) => {
  668. if (!tempGradeList.includes(item.gradeNum)) {
  669. tempGradeList.push(item.gradeNum);
  670. }
  671. });
  672. const lastGradeList: any[] = [];
  673. tempGradeList.forEach((temp: any) => {
  674. const list = {
  675. text: GRADE_ENUM[temp],
  676. value: temp,
  677. instrumentId: '',
  678. instrumentCode: '',
  679. instrumentName: '',
  680. classList: [] as any
  681. };
  682. schoolInstrumentList.forEach((item: any) => {
  683. if (temp === item.gradeNum) {
  684. list.instrumentId = item.instrumentId;
  685. list.instrumentCode = item.instrumentCode;
  686. list.instrumentName = item.instrumentName;
  687. list.classList.push({
  688. text: item.classNum + '班',
  689. value: item.classNum,
  690. instrumentCode: item.instrumentCode
  691. });
  692. }
  693. });
  694. // 排序班级
  695. list.classList.sort((a: any, b: any) => a.value - b.value);
  696. lastGradeList.push(list);
  697. });
  698. lastGradeList.sort((a: any, b: any) => a.value - b.value);
  699. tempGradeGradeList = lastGradeList
  700. tempClassList = lastGradeList[this.data.gradeGradeIndex]?.classList || []
  701. schoolInstrumentSetType = schoolInfo.instrumentSetType
  702. } else {
  703. tempGradeGradeList = getGradeList()
  704. tempClassList = classList
  705. schoolInstrumentSetType = ''
  706. }
  707. } else {
  708. tempGradeGradeList = getGradeList()
  709. tempClassList = classList
  710. schoolInstrumentSetType = ''
  711. }
  712. // 格式化年级班级 - 如果后台改了学校配置,本地保存了缓存,判断年级、班级是否存在
  713. const gradeIndex = this.data.gradeGradeIndex
  714. const classIndex = this.data.classIndex
  715. if ((tempGradeGradeList.length || 0) - 1 < gradeIndex) {
  716. tempClassList = tempGradeGradeList[0]?.classList || []
  717. this.setData({
  718. gradeGradeList: tempGradeGradeList,
  719. classList: tempClassList,
  720. schoolInstrumentSetType: schoolInstrumentSetType,
  721. gradeGradeIndex: 0,
  722. classIndex: 0,
  723. // currentClass: null,
  724. // currentClassTxt: '',
  725. // currentGradeNum: null,
  726. // currentGradeTxt: ''
  727. })
  728. } else if ((tempClassList.length || 0) - 1 < classIndex) {
  729. this.setData({
  730. gradeGradeList: tempGradeGradeList,
  731. classList: tempClassList,
  732. schoolInstrumentSetType: schoolInstrumentSetType,
  733. classIndex: 0,
  734. // currentClass: null,
  735. // currentClassTxt: '',
  736. })
  737. } else {
  738. this.setData({
  739. gradeGradeList: tempGradeGradeList,
  740. classList: tempClassList,
  741. schoolInstrumentSetType: schoolInstrumentSetType
  742. })
  743. }
  744. },
  745. /** 选择男女 */
  746. onCheckGender(e: any) {
  747. const { dataset } = e.target
  748. this.setData({
  749. gender: dataset.gender
  750. })
  751. },
  752. /** 显示选择地区 */
  753. onShowAreaList() {
  754. this.setData({
  755. showArea: true
  756. })
  757. },
  758. /** 关闭选择地区 */
  759. onCloseAreaList() {
  760. this.setData({
  761. showArea: false
  762. })
  763. },
  764. /** 选择地区关闭后 */
  765. onAreaAfterLeave() {
  766. this.setData({
  767. showAreaAfterLeave: true
  768. })
  769. },
  770. /** 选择地区打开前 */
  771. onAreaBeforeEnter() {
  772. this.setData({
  773. showAreaAfterLeave: false
  774. })
  775. },
  776. /** 确定选择地区 */
  777. submitArea(e: any) {
  778. const selectedOptions: any = e.detail.values
  779. this.setData({
  780. provinceCode: selectedOptions[0].code,
  781. cityCode: selectedOptions[1].code,
  782. regionCode: selectedOptions[2]?.code || null,
  783. provinceName: selectedOptions[0].name || '',
  784. cityName: selectedOptions[1].name || '',
  785. regionName: selectedOptions[2]?.name || '',
  786. showArea: false,
  787. searchName: '',
  788. schoolAreaId: '',
  789. schoolAreaName: '',
  790. schoolAreaIndex: 0,
  791. currentGradeNum: null,
  792. currentGradeTxt: '',
  793. gradeGradeIndex: 0,
  794. currentClass: null,
  795. currentClassTxt: '',
  796. classIndex: 0
  797. }, () => {
  798. this.getSchools()
  799. })
  800. },
  801. /** 关闭选择学校 */
  802. onCloseSchool() {
  803. this.setData({
  804. showSchool: false
  805. })
  806. },
  807. /** 选择学校关闭后 */
  808. onSchoolAfterLeave() {
  809. this.setData({
  810. showSchoolAfterLeave: true
  811. })
  812. },
  813. /** 选择学校打开前 */
  814. onSchoolBeforeEnter() {
  815. this.setData({
  816. showSchoolAfterLeave: false
  817. })
  818. },
  819. /** 选择学校 */
  820. onSelectSchool() {
  821. if (!this.data.provinceName) {
  822. wx.showToast({
  823. title: '请选择地区',
  824. icon: 'none'
  825. })
  826. return
  827. }
  828. this.setData({
  829. showSchool: true
  830. })
  831. },
  832. /** 确定选择学校 */
  833. onSubmitSchool() {
  834. if(this.data.tempChangeSchoolAreaId === this.data.schoolAreaId) {
  835. this.setData({
  836. showSchool: false
  837. })
  838. return
  839. }
  840. const detail = this.data.schoolAreaList.find((item: any) => item.value === this.data.tempChangeSchoolAreaId)
  841. const detailIndex = this.data.schoolAreaList.findIndex((item: any) => item.value === this.data.tempChangeSchoolAreaId)
  842. // console.log(detail, detailIndex, this.data.tempChangeSchoolAreaId)
  843. if (detailIndex === -1) return
  844. this.setData({
  845. schoolAreaName: detail.text,
  846. schoolAreaId: detail.value,
  847. schoolAreaIndex: detailIndex,
  848. showSchool: false,
  849. currentGradeNum: null,
  850. currentGradeTxt: '',
  851. gradeGradeIndex: 0,
  852. currentClass: null,
  853. currentClassTxt: '',
  854. classIndex: 0
  855. }, () => {
  856. this.getSchoolAreaDetail()
  857. })
  858. },
  859. onChangeSchool(e: any) {
  860. const { value } = e.detail.value
  861. this.setData({
  862. tempChangeSchoolAreaId: value
  863. })
  864. },
  865. onSearch() {
  866. this.setData({
  867. schoolAreaIndex: 0
  868. }, () => {
  869. this.getSchools(this.data.searchName);
  870. })
  871. },
  872. onSearchChange(e: any) {
  873. this.setData({
  874. searchName: e.detail
  875. })
  876. },
  877. /** 选择年级班级 */
  878. onSelectGradeClass() {
  879. if (!this.data.schoolAreaId) {
  880. wx.showToast({
  881. title: '请选择学校',
  882. icon: 'none'
  883. })
  884. return
  885. }
  886. this.setData({
  887. showGradeClass: true
  888. })
  889. },
  890. /** 年级班级 */
  891. onCloseGradeClass() {
  892. this.setData({
  893. showGradeClass: false
  894. })
  895. },
  896. onGradeClassBeforeEnter() {
  897. this.setData({
  898. showGradeClassAfterLeave: false
  899. })
  900. },
  901. onGradeClassAfterLeave() {
  902. this.setData({
  903. showGradeClassAfterLeave: true
  904. })
  905. },
  906. /** 确认选择年级班级 */
  907. onSubmitGradeClass(e: any) {
  908. const selectedOptions: any = e.detail.value
  909. const selectedIndexs: any = e.detail.index
  910. if (this.data.schoolInstrumentSetType === "CLASS") {
  911. const gradeDetail = this.data.gradeGradeList;
  912. const classList = gradeDetail?.find((item: any) => item.value === selectedOptions.value)
  913. console.log(classList, "classList")
  914. if (classList) {
  915. this.setData({
  916. classIndex: 0,
  917. classList: classList.classList
  918. })
  919. }
  920. }
  921. this.setData({
  922. currentGradeTxt: selectedOptions.text,
  923. currentGradeNum: selectedOptions.value,
  924. gradeGradeIndex: selectedIndexs,
  925. showGradeClass: false,
  926. currentClass: null,
  927. currentClassTxt: ''
  928. })
  929. },
  930. /** 选择班级 */
  931. onSelectClass() {
  932. if (!this.data.schoolAreaId) {
  933. wx.showToast({
  934. title: '请选择所在年级',
  935. icon: 'none'
  936. })
  937. return
  938. }
  939. this.setData({
  940. showClass: true
  941. })
  942. },
  943. /** 班级 */
  944. onCloseClass() {
  945. this.setData({
  946. showClass: false
  947. })
  948. },
  949. onClassBeforeEnter() {
  950. this.setData({
  951. showClassAfterLeave: false
  952. })
  953. },
  954. onClassAfterLeave() {
  955. this.setData({
  956. showClassAfterLeave: true
  957. })
  958. },
  959. /** 确认选择班级 */
  960. onSubmitClass(e: any) {
  961. const selectedOptions: any = e.detail.value
  962. const selectedIndexs: any = e.detail.index
  963. this.setData({
  964. currentClassTxt: selectedOptions.text,
  965. currentClass: selectedOptions.value,
  966. classIndex: selectedIndexs,
  967. showClass: false
  968. })
  969. },
  970. messageName(value: string) {
  971. const nameReg = /^[\u4E00-\u9FA5]+$/
  972. if (!value) {
  973. return '请填写享用者姓名';
  974. } else if (!nameReg.test(value)) {
  975. return '享用者姓名必须为中文';
  976. } else if (value.length < 2 || value.length > 14) {
  977. return '享用者姓名必须为2~14个字';
  978. } else {
  979. return ''
  980. }
  981. },
  982. /** 最终提交 */
  983. async onSubmitBuyer() {
  984. try {
  985. const params = this.data
  986. if (this.messageName(params.name)) {
  987. wx.showToast({
  988. title: this.messageName(params.name),
  989. icon: "none"
  990. })
  991. return
  992. }
  993. if (!params.phone || !/^1[3456789]\d{9}$/.test(params.phone)) {
  994. wx.showToast({
  995. title: '请填写正确的电话号码',
  996. icon: "none"
  997. })
  998. return
  999. }
  1000. if (!params.gender) {
  1001. wx.showToast({
  1002. title: '请选择性别',
  1003. icon: 'none'
  1004. })
  1005. return
  1006. }
  1007. if (!params.provinceCode || !params.cityCode) {
  1008. wx.showToast({
  1009. title: '请选择学校地区',
  1010. icon: "none"
  1011. })
  1012. return
  1013. }
  1014. if (!params.schoolAreaId) {
  1015. wx.showToast({
  1016. title: '请选择所在学校',
  1017. icon: "none"
  1018. })
  1019. return
  1020. }
  1021. if (!params.currentGradeNum) {
  1022. wx.showToast({
  1023. title: '请选择所在年级',
  1024. icon: "none"
  1025. })
  1026. return
  1027. }
  1028. if (!params.currentClass) {
  1029. wx.showToast({
  1030. title: '请选择所在班级',
  1031. icon: "none"
  1032. })
  1033. return
  1034. }
  1035. const objs = {
  1036. phone: params.phone,
  1037. name: params.name,
  1038. gender: params.gender,
  1039. currentGradeNum: params.currentGradeNum,
  1040. currentClass: params.currentClass,
  1041. schoolAreaId: params.schoolAreaId,
  1042. defaultStatus: false
  1043. }
  1044. const userBeneficiary = {
  1045. name: params.name,
  1046. phoneNumber: params.phone,
  1047. schoolInfo: (params.provinceName || '') + (params.cityName || '') + (params.regionName || '') + params.schoolAreaName + params.currentGradeTxt + params.currentClassTxt
  1048. }
  1049. let userBeneficiaryId = ''
  1050. this.setData({
  1051. buyerLoading: true
  1052. })
  1053. if (params.userBeneficiaryId) {
  1054. const { data } = await api_userBeneficiaryUpdate({
  1055. id: params.userBeneficiaryId,
  1056. ...objs
  1057. })
  1058. wx.showToast({
  1059. title: '保存成功',
  1060. icon: 'none'
  1061. })
  1062. userBeneficiaryId = data.data.id
  1063. } else {
  1064. const { data } = await api_userBeneficiarySave({
  1065. ...objs
  1066. })
  1067. wx.showToast({
  1068. title: '保存成功',
  1069. icon: 'none'
  1070. })
  1071. userBeneficiaryId = data.data.id
  1072. }
  1073. this.setData({
  1074. userBeneficiaryId,
  1075. userBeneficiaryInfo: userBeneficiary
  1076. })
  1077. this.onSetCatch(params)
  1078. this.onCloseBuyer()
  1079. } catch {
  1080. //
  1081. }
  1082. this.setData({
  1083. buyerLoading: false
  1084. })
  1085. },
  1086. /** 设置缓存 */
  1087. onSetCatch(params: any) {
  1088. wx.setStorageSync('buyerInfomation', JSON.stringify({
  1089. phone: params.phone,
  1090. name: params.name,
  1091. gender: params.gender,
  1092. schoolAreaId: params.schoolAreaId,
  1093. schoolAreaName: params.schoolAreaName,
  1094. currentGradeTxt: params.currentGradeTxt,
  1095. currentClass: params.currentClass,
  1096. currentClassTxt: params.currentClassTxt,
  1097. currentGradeNum: params.currentGradeNum,
  1098. cityCode: params.cityCode,
  1099. cityName: params.cityName,
  1100. provinceCode: params.provinceCode,
  1101. provinceName: params.provinceName,
  1102. regionCode: params.regionCode,
  1103. regionName: params.regionName,
  1104. schoolAreaIndex: params.schoolAreaIndex,
  1105. gradeGradeIndex: params.gradeGradeIndex,
  1106. classIndex: params.classIndex
  1107. }))
  1108. }
  1109. })