index.ts 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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. const genderList = this.data.genderList
  562. genderList.forEach((item: any, index: number) => {
  563. if (item.value === users.gender) {
  564. item.className = index === 0 ? 'one-gender select-gender' : 'two-gender select-gender'
  565. } else {
  566. item.className = index === 0 ? 'one-gender cancel-gender' : 'two-gender cancel-gender'
  567. }
  568. })
  569. this.setData({
  570. genderList
  571. })
  572. })
  573. }
  574. } catch {
  575. //
  576. }
  577. },
  578. /** 获取省市区 */
  579. async getAreas() {
  580. try {
  581. const { data } = await api_sysAreaQueryAllProvince({})
  582. this.setData({
  583. areaList: this.formateArea(data.data)
  584. })
  585. } catch {
  586. //
  587. }
  588. },
  589. formateArea(area: any[]) {
  590. const province_list: { [_: string]: string } = {};
  591. const city_list: { [_: string]: string } = {};
  592. const county_list: { [_: string]: string } = {};
  593. area.forEach((item: any) => {
  594. province_list[item.code] = item.name;
  595. });
  596. area.forEach((item: any) => {
  597. item.areas && item.areas.forEach((city: any, index: number) => {
  598. let code = city.code + ""
  599. // 某些数据不标准 这里需要转换一下
  600. if (code[4] !== "0" || code[5] !== "0") {
  601. // 现在把区域的数据改为市的
  602. const newCode = code.substring(0, 2) + (index < 10 ? `a${index}` : index < 20 ? `b${index - 10}` : index < 30 ? `c${index - 20}` : `d${index - 30}`) + "00";
  603. this.data.cacheArea.push({
  604. cityCode: code,
  605. shiftCityCode: newCode
  606. })
  607. code = newCode
  608. }
  609. city_list[code] = city.name;
  610. });
  611. });
  612. area.forEach((item: any) => {
  613. item.areas && item.areas.forEach((city: any) => {
  614. city.areas && city.areas.forEach((county: any) => {
  615. county_list[county.code] = county.name;
  616. });
  617. });
  618. });
  619. return {
  620. province_list,
  621. city_list,
  622. county_list
  623. };
  624. },
  625. // 转换
  626. formateCityCode(reverse?: boolean) {
  627. if (!this.data.regionCode && this.data.cityCode) {
  628. const cityCodeObj = this.data.cacheArea.find((item: any) => {
  629. return item[reverse ? "cityCode" : "shiftCityCode"] == this.data.cityCode
  630. })
  631. return cityCodeObj ? cityCodeObj[reverse ? "shiftCityCode" : "cityCode"] : ""
  632. }
  633. return this.data.cityCode
  634. },
  635. /** 获取学校列表 */
  636. async getSchools(name?: string) {
  637. this.setData({
  638. schoolLoading: true
  639. })
  640. try {
  641. // 判断是否有地区信息
  642. if (!this.data.provinceCode || !this.data.cityCode) {
  643. return
  644. }
  645. const citycode = this.formateCityCode()
  646. const { data } = await api_schoolAreaList({
  647. name,
  648. testFlag: true,
  649. provinceCode: this.data.provinceCode,
  650. cityCode: citycode,
  651. regionCode: this.data.regionCode
  652. })
  653. const result = data.data || []
  654. const tempList: any[] = []
  655. result.forEach((item: any) => {
  656. tempList.push({
  657. text: item.name,
  658. value: item.id
  659. })
  660. })
  661. let tempSchoolId = ''
  662. if (tempList.length > 0) {
  663. const first = tempList[0]
  664. tempSchoolId = first.value || ''
  665. }
  666. this.setData({
  667. schoolAreaList: tempList,
  668. tempChangeSchoolAreaId: tempSchoolId
  669. })
  670. } catch {
  671. //
  672. }
  673. this.setData({
  674. schoolLoading: false
  675. })
  676. },
  677. onCheckedGender() {
  678. this.setData({
  679. showGender: true
  680. })
  681. },
  682. onCloseGender() {
  683. this.setData({
  684. showGender: false,
  685. })
  686. },
  687. onSelectGender(e: any) {
  688. const genderList = this.data.genderList
  689. const gender = e.detail.value
  690. genderList.forEach((item: any, index: number) => {
  691. if (item.value === gender) {
  692. item.className = index === 0 ? 'one-gender select-gender' : 'two-gender select-gender'
  693. } else {
  694. item.className = index === 0 ? 'one-gender cancel-gender' : 'two-gender cancel-gender'
  695. }
  696. })
  697. console.log(e, genderList, gender, '1212')
  698. this.setData({
  699. gender,
  700. genderList
  701. })
  702. },
  703. /** 获取学校详情 */
  704. async getSchoolAreaDetail() {
  705. if (!this.data.schoolAreaId) return
  706. const { data } = await api_schoolAreaDetail({ id: this.data.schoolAreaId })
  707. const result = data.data || {}
  708. let tempGradeGradeList: any = []
  709. let tempClassList: any = []
  710. let schoolInstrumentSetType = ''
  711. if (result.school) {
  712. const schoolInfo = result.school || {};
  713. const schoolInstrumentList = schoolInfo.schoolInstrumentList || [];
  714. // forms.schoolInstrumentSetType = schoolInfo.instrumentSetType;
  715. if (schoolInfo.instrumentSetType === 'SCHOOL') {
  716. tempGradeGradeList = getGradeList(schoolInfo.gradeYear)
  717. tempClassList = classList
  718. schoolInstrumentSetType = schoolInfo.instrumentSetType
  719. } else if (schoolInfo.instrumentSetType === 'GRADE') {
  720. const gradeList: any = []
  721. schoolInstrumentList.forEach((item: any) => {
  722. gradeList.push({
  723. text: GRADE_ENUM[item.gradeNum],
  724. value: item.gradeNum,
  725. instrumentId: item.instrumentId
  726. })
  727. });
  728. gradeList.sort((a: any, b: any) => a.value - b.value);
  729. tempGradeGradeList = gradeList
  730. tempClassList = classList
  731. schoolInstrumentSetType = schoolInfo.instrumentSetType
  732. } else if (schoolInfo.instrumentSetType === 'CLASS') {
  733. // // 班级
  734. const tempGradeList: any[] = [];
  735. schoolInstrumentList.forEach((item: any) => {
  736. if (!tempGradeList.includes(item.gradeNum)) {
  737. tempGradeList.push(item.gradeNum);
  738. }
  739. });
  740. const lastGradeList: any[] = [];
  741. tempGradeList.forEach((temp: any) => {
  742. const list = {
  743. text: GRADE_ENUM[temp],
  744. value: temp,
  745. instrumentId: '',
  746. instrumentCode: '',
  747. instrumentName: '',
  748. classList: [] as any
  749. };
  750. schoolInstrumentList.forEach((item: any) => {
  751. if (temp === item.gradeNum) {
  752. list.instrumentId = item.instrumentId;
  753. list.instrumentCode = item.instrumentCode;
  754. list.instrumentName = item.instrumentName;
  755. list.classList.push({
  756. text: item.classNum + '班',
  757. value: item.classNum,
  758. instrumentCode: item.instrumentCode
  759. });
  760. }
  761. });
  762. // 排序班级
  763. list.classList.sort((a: any, b: any) => a.value - b.value);
  764. lastGradeList.push(list);
  765. });
  766. lastGradeList.sort((a: any, b: any) => a.value - b.value);
  767. tempGradeGradeList = lastGradeList
  768. tempClassList = lastGradeList[this.data.gradeGradeIndex]?.classList || []
  769. schoolInstrumentSetType = schoolInfo.instrumentSetType
  770. } else {
  771. tempGradeGradeList = getGradeList()
  772. tempClassList = classList
  773. schoolInstrumentSetType = ''
  774. }
  775. } else {
  776. tempGradeGradeList = getGradeList()
  777. tempClassList = classList
  778. schoolInstrumentSetType = ''
  779. }
  780. // 格式化年级班级 - 如果后台改了学校配置,本地保存了缓存,判断年级、班级是否存在
  781. const gradeIndex = this.data.gradeGradeIndex
  782. const classIndex = this.data.classIndex
  783. if ((tempGradeGradeList.length || 0) - 1 < gradeIndex) {
  784. tempClassList = tempGradeGradeList[0]?.classList || []
  785. this.setData({
  786. gradeGradeList: tempGradeGradeList,
  787. classList: tempClassList,
  788. schoolInstrumentSetType: schoolInstrumentSetType,
  789. gradeGradeIndex: 0,
  790. classIndex: 0,
  791. // currentClass: null,
  792. // currentClassTxt: '',
  793. // currentGradeNum: null,
  794. // currentGradeTxt: ''
  795. })
  796. } else if ((tempClassList.length || 0) - 1 < classIndex) {
  797. this.setData({
  798. gradeGradeList: tempGradeGradeList,
  799. classList: tempClassList,
  800. schoolInstrumentSetType: schoolInstrumentSetType,
  801. classIndex: 0,
  802. // currentClass: null,
  803. // currentClassTxt: '',
  804. })
  805. } else {
  806. this.setData({
  807. gradeGradeList: tempGradeGradeList,
  808. classList: tempClassList,
  809. schoolInstrumentSetType: schoolInstrumentSetType
  810. })
  811. }
  812. },
  813. /** 选择男女 */
  814. onCheckGender(e: any) {
  815. const { dataset } = e.target
  816. this.setData({
  817. gender: dataset.gender
  818. })
  819. },
  820. /** 显示选择地区 */
  821. onShowAreaList() {
  822. this.setData({
  823. showArea: true
  824. })
  825. },
  826. /** 关闭选择地区 */
  827. onCloseAreaList() {
  828. this.setData({
  829. showArea: false
  830. })
  831. },
  832. /** 选择地区关闭后 */
  833. onAreaAfterLeave() {
  834. this.setData({
  835. showAreaAfterLeave: true
  836. })
  837. },
  838. /** 选择地区打开前 */
  839. onAreaBeforeEnter() {
  840. this.setData({
  841. showAreaAfterLeave: false
  842. })
  843. },
  844. /** 确定选择地区 */
  845. submitArea(e: any) {
  846. const selectedOptions: any = e.detail.values
  847. this.setData({
  848. provinceCode: selectedOptions[0].code,
  849. cityCode: selectedOptions[1].code,
  850. regionCode: selectedOptions[2]?.code || null,
  851. provinceName: selectedOptions[0].name || '',
  852. cityName: selectedOptions[1].name || '',
  853. regionName: selectedOptions[2]?.name || '',
  854. showArea: false,
  855. searchName: '',
  856. schoolAreaId: '',
  857. schoolAreaName: '',
  858. schoolAreaIndex: 0,
  859. currentGradeNum: null,
  860. currentGradeTxt: '',
  861. gradeGradeIndex: 0,
  862. currentClass: null,
  863. currentClassTxt: '',
  864. classIndex: 0
  865. }, () => {
  866. this.getSchools()
  867. })
  868. },
  869. /** 关闭选择学校 */
  870. onCloseSchool() {
  871. this.setData({
  872. showSchool: false
  873. })
  874. },
  875. /** 选择学校关闭后 */
  876. onSchoolAfterLeave() {
  877. this.setData({
  878. showSchoolAfterLeave: true
  879. })
  880. },
  881. /** 选择学校打开前 */
  882. onSchoolBeforeEnter() {
  883. this.setData({
  884. showSchoolAfterLeave: false
  885. })
  886. },
  887. /** 选择学校 */
  888. onSelectSchool() {
  889. if (!this.data.provinceName) {
  890. wx.showToast({
  891. title: '请选择地区',
  892. icon: 'none'
  893. })
  894. return
  895. }
  896. this.setData({
  897. showSchool: true
  898. })
  899. },
  900. /** 确定选择学校 */
  901. onSubmitSchool() {
  902. if(this.data.tempChangeSchoolAreaId === this.data.schoolAreaId) {
  903. this.setData({
  904. showSchool: false
  905. })
  906. return
  907. }
  908. const detail = this.data.schoolAreaList.find((item: any) => item.value === this.data.tempChangeSchoolAreaId)
  909. const detailIndex = this.data.schoolAreaList.findIndex((item: any) => item.value === this.data.tempChangeSchoolAreaId)
  910. // console.log(detail, detailIndex, this.data.tempChangeSchoolAreaId)
  911. if (detailIndex === -1) return
  912. this.setData({
  913. schoolAreaName: detail.text,
  914. schoolAreaId: detail.value,
  915. schoolAreaIndex: detailIndex,
  916. showSchool: false,
  917. currentGradeNum: null,
  918. currentGradeTxt: '',
  919. gradeGradeIndex: 0,
  920. currentClass: null,
  921. currentClassTxt: '',
  922. classIndex: 0
  923. }, () => {
  924. this.getSchoolAreaDetail()
  925. })
  926. },
  927. onChangeSchool(e: any) {
  928. const { value } = e.detail.value
  929. this.setData({
  930. tempChangeSchoolAreaId: value
  931. })
  932. },
  933. onSearch() {
  934. this.setData({
  935. schoolAreaIndex: 0
  936. }, () => {
  937. this.getSchools(this.data.searchName);
  938. })
  939. },
  940. onSearchChange(e: any) {
  941. this.setData({
  942. searchName: e.detail
  943. })
  944. },
  945. /** 选择年级班级 */
  946. onSelectGradeClass() {
  947. if (!this.data.schoolAreaId) {
  948. wx.showToast({
  949. title: '请选择学校',
  950. icon: 'none'
  951. })
  952. return
  953. }
  954. this.setData({
  955. showGradeClass: true
  956. })
  957. },
  958. /** 年级班级 */
  959. onCloseGradeClass() {
  960. this.setData({
  961. showGradeClass: false
  962. })
  963. },
  964. onGradeClassBeforeEnter() {
  965. this.setData({
  966. showGradeClassAfterLeave: false
  967. })
  968. },
  969. onGradeClassAfterLeave() {
  970. this.setData({
  971. showGradeClassAfterLeave: true
  972. })
  973. },
  974. /** 确认选择年级班级 */
  975. onSubmitGradeClass(e: any) {
  976. const selectedOptions: any = e.detail.value
  977. const selectedIndexs: any = e.detail.index
  978. if (this.data.schoolInstrumentSetType === "CLASS") {
  979. const gradeDetail = this.data.gradeGradeList;
  980. const classList = gradeDetail?.find((item: any) => item.value === selectedOptions.value)
  981. console.log(classList, "classList")
  982. if (classList) {
  983. this.setData({
  984. classIndex: 0,
  985. classList: classList.classList
  986. })
  987. }
  988. }
  989. this.setData({
  990. currentGradeTxt: selectedOptions.text,
  991. currentGradeNum: selectedOptions.value,
  992. gradeGradeIndex: selectedIndexs,
  993. showGradeClass: false,
  994. currentClass: null,
  995. currentClassTxt: ''
  996. })
  997. },
  998. /** 选择班级 */
  999. onSelectClass() {
  1000. if (!this.data.schoolAreaId) {
  1001. wx.showToast({
  1002. title: '请选择所在年级',
  1003. icon: 'none'
  1004. })
  1005. return
  1006. }
  1007. this.setData({
  1008. showClass: true
  1009. })
  1010. },
  1011. /** 班级 */
  1012. onCloseClass() {
  1013. this.setData({
  1014. showClass: false
  1015. })
  1016. },
  1017. onClassBeforeEnter() {
  1018. this.setData({
  1019. showClassAfterLeave: false
  1020. })
  1021. },
  1022. onClassAfterLeave() {
  1023. this.setData({
  1024. showClassAfterLeave: true
  1025. })
  1026. },
  1027. /** 确认选择班级 */
  1028. onSubmitClass(e: any) {
  1029. const selectedOptions: any = e.detail.value
  1030. const selectedIndexs: any = e.detail.index
  1031. this.setData({
  1032. currentClassTxt: selectedOptions.text,
  1033. currentClass: selectedOptions.value,
  1034. classIndex: selectedIndexs,
  1035. showClass: false
  1036. })
  1037. },
  1038. messageName(value: string) {
  1039. const nameReg = /^[\u4E00-\u9FA5]+$/
  1040. if (!value) {
  1041. return '请填写享用者姓名';
  1042. } else if (!nameReg.test(value)) {
  1043. return '享用者姓名必须为中文';
  1044. } else if (value.length < 2 || value.length > 14) {
  1045. return '享用者姓名必须为2~14个字';
  1046. } else {
  1047. return ''
  1048. }
  1049. },
  1050. /** 最终提交 */
  1051. async onSubmitBuyer() {
  1052. try {
  1053. const params = this.data
  1054. if (this.messageName(params.name)) {
  1055. wx.showToast({
  1056. title: this.messageName(params.name),
  1057. icon: "none"
  1058. })
  1059. return
  1060. }
  1061. if (!params.phone || !/^1[3456789]\d{9}$/.test(params.phone)) {
  1062. wx.showToast({
  1063. title: '请填写正确的电话号码',
  1064. icon: "none"
  1065. })
  1066. return
  1067. }
  1068. if (!params.gender) {
  1069. wx.showToast({
  1070. title: '请选择性别',
  1071. icon: 'none'
  1072. })
  1073. return
  1074. }
  1075. if (!params.provinceCode || !params.cityCode) {
  1076. wx.showToast({
  1077. title: '请选择学校地区',
  1078. icon: "none"
  1079. })
  1080. return
  1081. }
  1082. if (!params.schoolAreaId) {
  1083. wx.showToast({
  1084. title: '请选择所在学校',
  1085. icon: "none"
  1086. })
  1087. return
  1088. }
  1089. if (!params.currentGradeNum) {
  1090. wx.showToast({
  1091. title: '请选择所在年级',
  1092. icon: "none"
  1093. })
  1094. return
  1095. }
  1096. if (!params.currentClass) {
  1097. wx.showToast({
  1098. title: '请选择所在班级',
  1099. icon: "none"
  1100. })
  1101. return
  1102. }
  1103. const objs = {
  1104. phone: params.phone,
  1105. name: params.name,
  1106. gender: params.gender,
  1107. currentGradeNum: params.currentGradeNum,
  1108. currentClass: params.currentClass,
  1109. schoolAreaId: params.schoolAreaId,
  1110. defaultStatus: false
  1111. }
  1112. const userBeneficiary = {
  1113. name: params.name,
  1114. phoneNumber: params.phone,
  1115. schoolInfo: (params.provinceName || '') + (params.cityName || '') + (params.regionName || '') + params.schoolAreaName + params.currentGradeTxt + params.currentClassTxt
  1116. }
  1117. let userBeneficiaryId = ''
  1118. this.setData({
  1119. buyerLoading: true
  1120. })
  1121. if (params.userBeneficiaryId) {
  1122. const { data } = await api_userBeneficiaryUpdate({
  1123. id: params.userBeneficiaryId,
  1124. ...objs
  1125. })
  1126. wx.showToast({
  1127. title: '保存成功',
  1128. icon: 'none'
  1129. })
  1130. userBeneficiaryId = data.data.id
  1131. } else {
  1132. const { data } = await api_userBeneficiarySave({
  1133. ...objs
  1134. })
  1135. wx.showToast({
  1136. title: '保存成功',
  1137. icon: 'none'
  1138. })
  1139. userBeneficiaryId = data.data.id
  1140. }
  1141. this.setData({
  1142. userBeneficiaryId,
  1143. userBeneficiaryInfo: userBeneficiary
  1144. })
  1145. this.onSetCatch(params)
  1146. this.onCloseBuyer()
  1147. } catch {
  1148. //
  1149. }
  1150. this.setData({
  1151. buyerLoading: false
  1152. })
  1153. },
  1154. /** 设置缓存 */
  1155. onSetCatch(params: any) {
  1156. wx.setStorageSync('buyerInfomation', JSON.stringify({
  1157. phone: params.phone,
  1158. name: params.name,
  1159. gender: params.gender,
  1160. schoolAreaId: params.schoolAreaId,
  1161. schoolAreaName: params.schoolAreaName,
  1162. currentGradeTxt: params.currentGradeTxt,
  1163. currentClass: params.currentClass,
  1164. currentClassTxt: params.currentClassTxt,
  1165. currentGradeNum: params.currentGradeNum,
  1166. cityCode: params.cityCode,
  1167. cityName: params.cityName,
  1168. provinceCode: params.provinceCode,
  1169. provinceName: params.provinceName,
  1170. regionCode: params.regionCode,
  1171. regionName: params.regionName,
  1172. schoolAreaIndex: params.schoolAreaIndex,
  1173. gradeGradeIndex: params.gradeGradeIndex,
  1174. classIndex: params.classIndex
  1175. }))
  1176. }
  1177. })