index.ts 30 KB

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