index.ts 33 KB

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