index.ts 34 KB

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