index.ts 29 KB

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