index.ts 34 KB

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