index.ts 29 KB

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