index.ts 34 KB

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