index.ts 33 KB

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