index.ts 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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. const detail = this.data.schoolAreaList.find((item: any) => item.value === this.data.tempChangeSchoolAreaId)
  883. const detailIndex = this.data.schoolAreaList.findIndex((item: any) => item.value === this.data.tempChangeSchoolAreaId)
  884. // console.log(detail, detailIndex, this.data.tempChangeSchoolAreaId)
  885. if (detailIndex === -1) return
  886. this.setData({
  887. schoolAreaName: detail.text,
  888. schoolAreaId: detail.value,
  889. schoolAreaIndex: detailIndex,
  890. showSchool: false,
  891. currentGradeNum: null,
  892. currentGradeTxt: '',
  893. gradeGradeIndex: 0,
  894. currentClass: null,
  895. currentClassTxt: '',
  896. classIndex: 0
  897. }, () => {
  898. this.getSchoolAreaDetail()
  899. })
  900. },
  901. onChangeSchool(e: any) {
  902. const { value } = e.detail.value
  903. this.setData({
  904. tempChangeSchoolAreaId: value
  905. })
  906. },
  907. onSearch() {
  908. this.setData({
  909. schoolAreaIndex: 0
  910. }, () => {
  911. this.getSchools(this.data.searchName);
  912. })
  913. },
  914. onSearchChange(e: any) {
  915. this.setData({
  916. searchName: e.detail
  917. })
  918. },
  919. /** 选择年级班级 */
  920. onSelectGradeClass() {
  921. if (!this.data.schoolAreaId) {
  922. wx.showToast({
  923. title: '请选择学校',
  924. icon: 'none'
  925. })
  926. return
  927. }
  928. this.setData({
  929. showGradeClass: true
  930. })
  931. },
  932. /** 年级班级 */
  933. onCloseGradeClass() {
  934. this.setData({
  935. showGradeClass: false
  936. })
  937. },
  938. onGradeClassBeforeEnter() {
  939. this.setData({
  940. showGradeClassAfterLeave: false
  941. })
  942. },
  943. onGradeClassAfterLeave() {
  944. this.setData({
  945. showGradeClassAfterLeave: true
  946. })
  947. },
  948. /** 确认选择年级班级 */
  949. onSubmitGradeClass(e: any) {
  950. const selectedOptions: any = e.detail.value
  951. const selectedIndexs: any = e.detail.index
  952. if (this.data.schoolInstrumentSetType === "CLASS") {
  953. const gradeDetail = this.data.gradeGradeList;
  954. const classList = gradeDetail?.find((item: any) => item.value === selectedOptions.value)
  955. console.log(classList, "classList")
  956. if (classList) {
  957. this.setData({
  958. classIndex: 0,
  959. classList: classList.classList
  960. })
  961. }
  962. }
  963. this.setData({
  964. currentGradeTxt: selectedOptions.text,
  965. currentGradeNum: selectedOptions.value,
  966. gradeGradeIndex: selectedIndexs,
  967. showGradeClass: false,
  968. currentClass: null,
  969. currentClassTxt: ''
  970. })
  971. },
  972. /** 选择班级 */
  973. onSelectClass() {
  974. if (!this.data.schoolAreaId) {
  975. wx.showToast({
  976. title: '请选择所在年级',
  977. icon: 'none'
  978. })
  979. return
  980. }
  981. this.setData({
  982. showClass: true
  983. })
  984. },
  985. /** 班级 */
  986. onCloseClass() {
  987. this.setData({
  988. showClass: false
  989. })
  990. },
  991. onClassBeforeEnter() {
  992. this.setData({
  993. showClassAfterLeave: false
  994. })
  995. },
  996. onClassAfterLeave() {
  997. this.setData({
  998. showClassAfterLeave: true
  999. })
  1000. },
  1001. /** 确认选择班级 */
  1002. onSubmitClass(e: any) {
  1003. const selectedOptions: any = e.detail.value
  1004. const selectedIndexs: any = e.detail.index
  1005. this.setData({
  1006. currentClassTxt: selectedOptions.text,
  1007. currentClass: selectedOptions.value,
  1008. classIndex: selectedIndexs,
  1009. showClass: false
  1010. })
  1011. },
  1012. messageName(value: string) {
  1013. const nameReg = /^[\u4E00-\u9FA5]+$/
  1014. if (!value) {
  1015. return '请填写享用者姓名';
  1016. } else if (!nameReg.test(value)) {
  1017. return '享用者姓名必须为中文';
  1018. } else if (value.length < 2 || value.length > 14) {
  1019. return '享用者姓名必须为2~14个字';
  1020. } else {
  1021. return ''
  1022. }
  1023. },
  1024. /** 最终提交 */
  1025. async onSubmitBuyer() {
  1026. try {
  1027. const params = this.data
  1028. if (this.messageName(params.name)) {
  1029. wx.showToast({
  1030. title: this.messageName(params.name),
  1031. icon: "none"
  1032. })
  1033. return
  1034. }
  1035. if (!params.phone || !/^1[3456789]\d{9}$/.test(params.phone)) {
  1036. wx.showToast({
  1037. title: '请填写正确的电话号码',
  1038. icon: "none"
  1039. })
  1040. return
  1041. }
  1042. if (!params.gender) {
  1043. wx.showToast({
  1044. title: '请选择性别',
  1045. icon: 'none'
  1046. })
  1047. return
  1048. }
  1049. if (!params.provinceCode || !params.cityCode) {
  1050. wx.showToast({
  1051. title: '请选择学校地区',
  1052. icon: "none"
  1053. })
  1054. return
  1055. }
  1056. if (!params.schoolAreaId) {
  1057. wx.showToast({
  1058. title: '请选择所在学校',
  1059. icon: "none"
  1060. })
  1061. return
  1062. }
  1063. if (!params.currentGradeNum) {
  1064. wx.showToast({
  1065. title: '请选择所在年级',
  1066. icon: "none"
  1067. })
  1068. return
  1069. }
  1070. if (!params.currentClass) {
  1071. wx.showToast({
  1072. title: '请选择所在班级',
  1073. icon: "none"
  1074. })
  1075. return
  1076. }
  1077. const objs = {
  1078. phone: params.phone,
  1079. name: params.name,
  1080. gender: params.gender,
  1081. currentGradeNum: params.currentGradeNum,
  1082. currentClass: params.currentClass,
  1083. schoolAreaId: params.schoolAreaId,
  1084. defaultStatus: false
  1085. }
  1086. const userBeneficiary = {
  1087. name: params.name,
  1088. phoneNumber: params.phone,
  1089. schoolInfo: (params.provinceName || '') + (params.cityName || '') + (params.regionName || '') + params.schoolAreaName + params.currentGradeTxt + params.currentClassTxt
  1090. }
  1091. let userBeneficiaryId = ''
  1092. this.setData({
  1093. buyerLoading: true
  1094. })
  1095. if (params.userBeneficiaryId) {
  1096. const { data } = await api_userBeneficiaryUpdate({
  1097. id: params.userBeneficiaryId,
  1098. ...objs
  1099. })
  1100. wx.showToast({
  1101. title: '保存成功',
  1102. icon: 'none'
  1103. })
  1104. userBeneficiaryId = data.data.id
  1105. } else {
  1106. const { data } = await api_userBeneficiarySave({
  1107. ...objs
  1108. })
  1109. wx.showToast({
  1110. title: '保存成功',
  1111. icon: 'none'
  1112. })
  1113. userBeneficiaryId = data.data.id
  1114. }
  1115. this.setData({
  1116. userBeneficiaryId,
  1117. userBeneficiaryInfo: userBeneficiary
  1118. })
  1119. this.onSetCatch(params)
  1120. this.onCloseBuyer()
  1121. } catch {
  1122. //
  1123. }
  1124. this.setData({
  1125. buyerLoading: false
  1126. })
  1127. },
  1128. /** 设置缓存 */
  1129. onSetCatch(params: any) {
  1130. wx.setStorageSync('buyerInfomation', JSON.stringify({
  1131. phone: params.phone,
  1132. name: params.name,
  1133. gender: params.gender,
  1134. schoolAreaId: params.schoolAreaId,
  1135. schoolAreaName: params.schoolAreaName,
  1136. currentGradeTxt: params.currentGradeTxt,
  1137. currentClass: params.currentClass,
  1138. currentClassTxt: params.currentClassTxt,
  1139. currentGradeNum: params.currentGradeNum,
  1140. cityCode: params.cityCode,
  1141. cityName: params.cityName,
  1142. provinceCode: params.provinceCode,
  1143. provinceName: params.provinceName,
  1144. regionCode: params.regionCode,
  1145. regionName: params.regionName,
  1146. schoolAreaIndex: params.schoolAreaIndex,
  1147. gradeGradeIndex: params.gradeGradeIndex,
  1148. classIndex: params.classIndex
  1149. }))
  1150. }
  1151. })