index.ts 35 KB

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