index.ts 34 KB

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