index.ts 34 KB

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