index.ts 32 KB

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