index.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. }, () => {
  207. this.onFormatGoods()
  208. })
  209. } catch (e) {
  210. console.log(e, 'e')
  211. }
  212. },
  213. // 格式化类型
  214. formatPeriod(num: number, type: string) {
  215. const template: any = {
  216. DAY: "天卡",
  217. MONTH: "月卡",
  218. YEAR: "年卡"
  219. }
  220. if (type === "YEAR" && num >= 99) {
  221. return '永久卡'
  222. }
  223. return num + (template[type] || '')
  224. },
  225. // 选择
  226. onSelectGoods(e: any) {
  227. const { dataset } = e.currentTarget
  228. const item = this.data.list.find((item: any) => item.id === dataset.id)
  229. // 判断是否有库存
  230. if (item.stockNum <= 0) {
  231. return
  232. }
  233. this.setData({
  234. selected: item || {}
  235. }, () => {
  236. this.onFormatGoods()
  237. })
  238. },
  239. /** 选中乐器 */
  240. onSelectInstrument(e: any) {
  241. const { dataset } = e.currentTarget;
  242. if (dataset.id === this.data.selectInstrumentId) {
  243. this.setData({
  244. selectInstrumentId: '',
  245. selectedInstrument: {}
  246. }, () => {
  247. this.onFormatGoods()
  248. })
  249. } else {
  250. const item = this.data.instrumentList.find((item: any) => item.id === dataset.id);
  251. this.setData({
  252. selectInstrumentId: dataset.id,
  253. selectedInstrument: item || {}
  254. }, () => {
  255. this.onFormatGoods()
  256. })
  257. }
  258. },
  259. onFirstChange(e: any) {
  260. const detail = e.detail;
  261. if (detail.source === 'touch' || detail.source == 'autoplay') {
  262. this.setData({
  263. firstCurrent: detail.current
  264. })
  265. }
  266. },
  267. // 事件处理函数
  268. changeSwiper(e: any) {
  269. const detail = e.detail;
  270. if (detail.source === 'touch' || detail.source == 'autoplay') {
  271. this.setData({
  272. current: detail.current
  273. })
  274. }
  275. },
  276. /** 格式化选中的商品 */
  277. onFormatGoods() {
  278. const selected = this.data.selected;
  279. const selectedInstrument = this.data.selectedInstrument
  280. const params = {
  281. typeName: '',
  282. showSalePrice: '' as any, // 显示的现价
  283. originalPrice: 0, // 原价
  284. salePrice: 0, // 现价
  285. discountPrice: '' as any, // 已省
  286. integerPart: '',
  287. decimalPart: '',
  288. }
  289. // 选中期限
  290. if (selected.id) {
  291. params.typeName = selected.typeName
  292. params.showSalePrice = selected.showSalePrice
  293. params.originalPrice = selected.originalPrice
  294. params.salePrice = selected.salePrice
  295. params.discountPrice = selected.discountPrice
  296. const prices: any = formatPrice(params.salePrice);
  297. params.integerPart = prices.integerPart
  298. params.decimalPart = prices.decimalPart
  299. }
  300. // 选中乐器
  301. if (selectedInstrument.id) {
  302. params.typeName = selected.typeName ? selected.typeName + '+' + selectedInstrument.name : selectedInstrument.name
  303. params.originalPrice = Number(selected.originalPrice) + Number(selectedInstrument.originalPrice)
  304. params.salePrice = Number(selected.salePrice) + Number(selectedInstrument.salePrice)
  305. params.showSalePrice = formatPrice(params.salePrice, "ALL");
  306. params.discountPrice = formatPrice(
  307. params.originalPrice - params.salePrice,
  308. "ALL"
  309. );
  310. const prices: any = formatPrice(params.salePrice);
  311. params.integerPart = prices.integerPart
  312. params.decimalPart = prices.decimalPart
  313. }
  314. this.setData({
  315. formatSelectGood: params
  316. })
  317. },
  318. isLogin() {
  319. // 判断是否登录
  320. if (!app.globalData.isLogin) {
  321. wx.navigateTo({
  322. url: '../login/login',
  323. })
  324. return false
  325. }
  326. return true
  327. },
  328. /** 我的订单 */
  329. onOrder() {
  330. // 判断是否登录
  331. if (!this.isLogin()) {
  332. return
  333. }
  334. wx.navigateTo({
  335. url: '../orders/orders',
  336. })
  337. },
  338. /** 客服 */
  339. onService() {
  340. this.setData({
  341. showService: true
  342. })
  343. },
  344. changePop(event: { detail: any }) {
  345. this.setData({
  346. showService: event.detail
  347. })
  348. },
  349. onBuyShop() {
  350. // 判断是否登录
  351. if (!this.isLogin()) {
  352. return
  353. }
  354. this.setData({
  355. popupShow: true
  356. })
  357. },
  358. onClose() {
  359. this.setData({
  360. popupShow: false
  361. })
  362. },
  363. onSubmit() {
  364. // 判断是否登录
  365. const that = this
  366. if (!this.data.userBeneficiaryId) {
  367. wx.showToast({
  368. title: '请填写享用者信息',
  369. icon: 'none'
  370. })
  371. return
  372. }
  373. debounce(function () {
  374. if (!that.isLogin()) {
  375. return
  376. }
  377. const params = [] as any
  378. const selected = that.data.selected
  379. if (selected.id) {
  380. params.push({
  381. pic: selected.pic,
  382. name: selected.name,
  383. originalPrice: selected.originalPrice,
  384. salePrice: selected.salePrice,
  385. shopId: selected.shopId,
  386. id: selected.id,
  387. goodsType: 'ACTIVATION_CODE', // INSTRUMENTS
  388. })
  389. }
  390. const selectedInstrument = that.data.selectedInstrument
  391. if (selectedInstrument.id) {
  392. params.push({
  393. pic: selectedInstrument.pic,
  394. name: selectedInstrument.name,
  395. originalPrice: selectedInstrument.originalPrice,
  396. salePrice: selectedInstrument.salePrice,
  397. shopId: selectedInstrument.shopId,
  398. id: selectedInstrument.id,
  399. goodsType: 'INSTRUMENTS', // INSTRUMENTS
  400. })
  401. }
  402. let info = JSON.stringify({
  403. ...params
  404. });
  405. info = encodeURIComponent(info);
  406. console.log(params, "params")
  407. wx.navigateTo({
  408. url: `../orders/order-detail?orderInfo=${info}`,
  409. success: () => {
  410. that.setData({
  411. popupShow: false
  412. })
  413. }
  414. })
  415. }, 500)()
  416. },
  417. onPreivewBannerImg(e: { currentTarget: { dataset: any } }) {
  418. wx.previewImage({
  419. current: e.currentTarget.dataset.src,
  420. urls: this.data.imgList,
  421. success: () => {
  422. this.setData({
  423. isFromPreviewImage: true
  424. })
  425. }
  426. })
  427. },
  428. onPreivewGoodsImg(e: { currentTarget: { dataset: any } }) {
  429. wx.previewImage({
  430. current: e.currentTarget.dataset.src,
  431. urls: this.data.goodsImgList,
  432. success: () => {
  433. this.setData({
  434. isFromPreviewImage: true
  435. })
  436. }
  437. })
  438. },
  439. onPreivewGoods(e: { currentTarget: { dataset: any } }) {
  440. wx.previewImage({
  441. current: e.currentTarget.dataset.src,
  442. urls: [e.currentTarget.dataset.src],
  443. success: () => {
  444. this.setData({
  445. isFromPreviewImage: true
  446. })
  447. }
  448. })
  449. },
  450. /**
  451. * 生命周期函数--监听页面显示
  452. */
  453. onShow() {
  454. if (!this.data.isFromPreviewImage) {
  455. this.onInit()
  456. } else {
  457. this.setData({
  458. isFromPreviewImage: false
  459. })
  460. }
  461. },
  462. onShareAppMessage() {
  463. return {
  464. title: '器乐数字AI工具',
  465. path: '/pages/index/index',
  466. imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
  467. }
  468. },
  469. onShareTimeline() {
  470. return {
  471. title: '器乐数字AI工具',
  472. path: '/pages/index/index',
  473. imageUrl: 'https://oss.dayaedu.com/ktyq/1733312164991.png'
  474. }
  475. },
  476. onSelectBuyer() {
  477. // 从缓存里面获取用户信息
  478. this.getUserDetail()
  479. if (this.data.areaList.length <= 0) {
  480. this.getAreas()
  481. }
  482. this.getSchools()
  483. this.setData({
  484. showBuyer: true
  485. })
  486. },
  487. onCloseBuyer() {
  488. this.setData({
  489. showBuyer: false
  490. }, () => {
  491. if(!this.data.userBeneficiaryId) {
  492. this.onSetCatch(this.data)
  493. }
  494. })
  495. },
  496. async getUserDetail() {
  497. try {
  498. // const { data } = await api_userBeneficiaryDetail({
  499. // id: this.data.userBeneficiaryId
  500. // })
  501. // console.log(data, 'data')
  502. const information = wx.getStorageSync('buyerInfomation')
  503. const users = information ? JSON.parse(information) : null
  504. if (users) {
  505. this.setData({
  506. phone: users.phone,
  507. name: users.name,
  508. gender: users.gender,
  509. schoolAreaId: users.schoolAreaId,
  510. schoolAreaName: users.schoolAreaName,
  511. currentGradeTxt: users.currentGradeTxt,
  512. currentClass: users.currentClass,
  513. currentClassTxt: users.currentClassTxt,
  514. currentGradeNum: users.currentGradeNum,
  515. cityCode: users.cityCode,
  516. cityName: users.cityName,
  517. provinceCode: users.provinceCode,
  518. provinceName: users.provinceName,
  519. regionCode: users.regionCode,
  520. regionName: users.regionName,
  521. schoolAreaIndex: users.schoolAreaIndex,
  522. gradeGradeIndex: users.gradeGradeIndex,
  523. classIndex: users.classIndex
  524. }, () => {
  525. this.getSchoolAreaDetail()
  526. })
  527. }
  528. } catch {
  529. //
  530. }
  531. },
  532. /** 获取省市区 */
  533. async getAreas() {
  534. try {
  535. const { data } = await api_sysAreaQueryAllProvince({})
  536. this.setData({
  537. areaList: this.formateArea(data.data)
  538. })
  539. } catch {
  540. //
  541. }
  542. },
  543. formateArea(area: any[]) {
  544. const province_list: { [_: string]: string } = {};
  545. const city_list: { [_: string]: string } = {};
  546. const county_list: { [_: string]: string } = {};
  547. area.forEach((item: any) => {
  548. province_list[item.code] = item.name;
  549. });
  550. area.forEach((item: any) => {
  551. item.areas && item.areas.forEach((city: any) => {
  552. city_list[city.code] = city.name;
  553. });
  554. });
  555. area.forEach((item: any) => {
  556. item.areas && item.areas.forEach((city: any) => {
  557. city.areas && city.areas.forEach((county: any) => {
  558. county_list[county.code] = county.name;
  559. });
  560. });
  561. });
  562. return {
  563. province_list,
  564. city_list,
  565. county_list
  566. };
  567. },
  568. /** 获取学校列表 */
  569. async getSchools(name?: string) {
  570. this.setData({
  571. schoolLoading: true
  572. })
  573. try {
  574. // 判断是否有地区信息
  575. if (!this.data.provinceCode || !this.data.cityCode || !this.data.regionCode) {
  576. return
  577. }
  578. const { data } = await api_schoolAreaList({
  579. name,
  580. testFlag: true,
  581. provinceCode: this.data.provinceCode,
  582. cityCode: this.data.cityCode,
  583. regionCode: this.data.regionCode
  584. })
  585. const result = data.data || []
  586. const tempList: any[] = []
  587. result.forEach((item: any) => {
  588. tempList.push({
  589. text: item.name,
  590. value: item.id
  591. })
  592. })
  593. let tempSchoolId = ''
  594. if (tempList.length > 0) {
  595. const first = tempList[0]
  596. tempSchoolId = first.value || ''
  597. }
  598. this.setData({
  599. schoolAreaList: tempList,
  600. tempChangeSchoolAreaId: tempSchoolId
  601. })
  602. } catch {
  603. //
  604. }
  605. this.setData({
  606. schoolLoading: false
  607. })
  608. },
  609. onCheckedGender() {
  610. this.setData({
  611. showGender: true
  612. })
  613. },
  614. onCloseGender() {
  615. this.setData({
  616. showGender: false,
  617. })
  618. },
  619. onSelectGender(e: any) {
  620. const genderList = this.data.genderList
  621. const gender = e.detail.value
  622. genderList.forEach((item: any, index: number) => {
  623. if (item.value === gender) {
  624. item.className = index === 0 ? 'one-gender select-gender' : 'two-gender select-gender'
  625. } else {
  626. item.className = index === 0 ? 'one-gender cancel-gender' : 'two-gender cancel-gender'
  627. }
  628. })
  629. console.log(e, genderList, gender, '1212')
  630. this.setData({
  631. gender,
  632. genderList
  633. })
  634. },
  635. /** 获取学校详情 */
  636. async getSchoolAreaDetail() {
  637. if(!this.data.schoolAreaId) return
  638. const { data } = await api_schoolAreaDetail({ id: this.data.schoolAreaId })
  639. const result = data.data || {}
  640. if (result.school) {
  641. const schoolInfo = result.school || {};
  642. const schoolInstrumentList = schoolInfo.schoolInstrumentList || [];
  643. // forms.schoolInstrumentSetType = schoolInfo.instrumentSetType;
  644. if (schoolInfo.instrumentSetType === 'SCHOOL') {
  645. this.setData({
  646. gradeGradeList: getGradeList(schoolInfo.gradeYear),
  647. classList: classList,
  648. schoolInstrumentSetType: schoolInfo.instrumentSetType
  649. })
  650. } else if (schoolInfo.instrumentSetType === 'GRADE') {
  651. const gradeList: any = []
  652. schoolInstrumentList.forEach((item: any) => {
  653. gradeList.push({
  654. text: GRADE_ENUM[item.gradeNum],
  655. value: item.gradeNum,
  656. instrumentId: item.instrumentId
  657. })
  658. });
  659. gradeList.sort((a: any, b: any) => a.value - b.value);
  660. this.setData({
  661. gradeGradeList: gradeList,
  662. classList: classList,
  663. schoolInstrumentSetType: schoolInfo.instrumentSetType
  664. })
  665. } else if (schoolInfo.instrumentSetType === 'CLASS') {
  666. // // 班级
  667. const tempGradeList: any[] = [];
  668. schoolInstrumentList.forEach((item: any) => {
  669. if (!tempGradeList.includes(item.gradeNum)) {
  670. tempGradeList.push(item.gradeNum);
  671. }
  672. });
  673. const lastGradeList: any[] = [];
  674. tempGradeList.forEach((temp: any) => {
  675. const list = {
  676. text: GRADE_ENUM[temp],
  677. value: temp,
  678. instrumentId: '',
  679. instrumentCode: '',
  680. instrumentName: '',
  681. classList: [] as any
  682. };
  683. schoolInstrumentList.forEach((item: any) => {
  684. if (temp === item.gradeNum) {
  685. list.instrumentId = item.instrumentId;
  686. list.instrumentCode = item.instrumentCode;
  687. list.instrumentName = item.instrumentName;
  688. list.classList.push({
  689. text: item.classNum + '班',
  690. value: item.classNum,
  691. instrumentCode: item.instrumentCode
  692. });
  693. }
  694. });
  695. // 排序班级
  696. list.classList.sort((a: any, b: any) => a.value - b.value);
  697. lastGradeList.push(list);
  698. });
  699. lastGradeList.sort((a: any, b: any) => a.value - b.value);
  700. this.setData({
  701. gradeGradeList: lastGradeList,
  702. classList: lastGradeList[this.data.gradeGradeIndex]?.classList || [],
  703. schoolInstrumentSetType: schoolInfo.instrumentSetType
  704. })
  705. } else {
  706. this.setData({
  707. gradeGradeList: getGradeList(),
  708. classList: classList,
  709. schoolInstrumentSetType: ''
  710. })
  711. }
  712. } else {
  713. this.setData({
  714. gradeGradeList: getGradeList(),
  715. classList: classList,
  716. schoolInstrumentSetType: ''
  717. })
  718. }
  719. },
  720. /** 选择男女 */
  721. onCheckGender(e: any) {
  722. const { dataset } = e.target
  723. this.setData({
  724. gender: dataset.gender
  725. })
  726. },
  727. /** 显示选择地区 */
  728. onShowAreaList() {
  729. this.setData({
  730. showArea: true
  731. })
  732. },
  733. /** 关闭选择地区 */
  734. onCloseAreaList() {
  735. this.setData({
  736. showArea: false
  737. })
  738. },
  739. /** 确定选择地区 */
  740. submitArea(e: any) {
  741. const selectedOptions: any = e.detail.values
  742. this.setData({
  743. provinceCode: selectedOptions[0].code,
  744. cityCode: selectedOptions[1].code,
  745. regionCode: selectedOptions[2].code,
  746. provinceName: selectedOptions[0].name,
  747. cityName: selectedOptions[1].name,
  748. regionName: selectedOptions[2].name,
  749. showArea: false,
  750. searchName: '',
  751. schoolAreaId: '',
  752. schoolAreaName: '',
  753. schoolAreaIndex: 0,
  754. currentGradeNum: null,
  755. currentGradeTxt: '',
  756. gradeGradeIndex: 0,
  757. currentClass: null,
  758. currentClassTxt: '',
  759. classIndex: 0
  760. }, () => {
  761. this.getSchools()
  762. })
  763. },
  764. /** 关闭选择学校 */
  765. onCloseSchool() {
  766. this.setData({
  767. showSchool: false
  768. })
  769. },
  770. /** 选择学校关闭后 */
  771. onSchoolAfterLeave() {
  772. this.setData({
  773. showSchoolAfterLeave: true
  774. })
  775. },
  776. /** 选择学校打开前 */
  777. onSchoolBeforeEnter() {
  778. this.setData({
  779. showSchoolAfterLeave: false
  780. })
  781. },
  782. /** 选择学校 */
  783. onSelectSchool() {
  784. if (!this.data.provinceName) {
  785. wx.showToast({
  786. title: '请选择地区',
  787. icon: 'none'
  788. })
  789. return
  790. }
  791. this.setData({
  792. showSchool: true
  793. })
  794. },
  795. /** 确定选择学校 */
  796. onSubmitSchool() {
  797. const detail = this.data.schoolAreaList.find((item: any) => item.value === this.data.tempChangeSchoolAreaId)
  798. const detailIndex = this.data.schoolAreaList.findIndex((item: any) => item.value === this.data.tempChangeSchoolAreaId)
  799. // console.log(detail, detailIndex, this.data.tempChangeSchoolAreaId)
  800. if (detailIndex === -1) return
  801. this.setData({
  802. schoolAreaName: detail.text,
  803. schoolAreaId: detail.value,
  804. schoolAreaIndex: detailIndex,
  805. showSchool: false,
  806. currentGradeNum: null,
  807. currentGradeTxt: '',
  808. gradeGradeIndex: 0,
  809. currentClass: null,
  810. currentClassTxt: '',
  811. classIndex: 0
  812. }, () => {
  813. this.getSchoolAreaDetail()
  814. })
  815. },
  816. onChangeSchool(e: any) {
  817. const { value } = e.detail.value
  818. this.setData({
  819. tempChangeSchoolAreaId: value
  820. })
  821. },
  822. onSearch() {
  823. this.setData({
  824. schoolAreaIndex: 0
  825. }, () => {
  826. this.getSchools(this.data.searchName);
  827. })
  828. },
  829. onSearchChange(e: any) {
  830. this.setData({
  831. searchName: e.detail
  832. })
  833. },
  834. /** 选择年级班级 */
  835. onSelectGradeClass() {
  836. if (!this.data.schoolAreaId) {
  837. wx.showToast({
  838. title: '请选择学校',
  839. icon: 'none'
  840. })
  841. return
  842. }
  843. this.setData({
  844. showGradeClass: true
  845. })
  846. },
  847. /** 年级班级 */
  848. onCloseGradeClass() {
  849. this.setData({
  850. showGradeClass: false
  851. })
  852. },
  853. onGradeClassBeforeEnter() {
  854. this.setData({
  855. showGradeClassAfterLeave: false
  856. })
  857. },
  858. onGradeClassAfterLeave() {
  859. this.setData({
  860. showGradeClassAfterLeave: true
  861. })
  862. },
  863. /** 确认选择年级班级 */
  864. onSubmitGradeClass(e: any) {
  865. const selectedOptions: any = e.detail.value
  866. const selectedIndexs: any = e.detail.index
  867. if (this.data.schoolInstrumentSetType === "CLASS") {
  868. const gradeDetail = this.data.gradeGradeList;
  869. const classList = gradeDetail?.find((item: any) => item.value === selectedOptions.value)
  870. console.log(classList, "classList")
  871. if (classList) {
  872. this.setData({
  873. classIndex: 0,
  874. classList: classList.classList
  875. })
  876. }
  877. }
  878. this.setData({
  879. currentGradeTxt: selectedOptions.text,
  880. currentGradeNum: selectedOptions.value,
  881. gradeGradeIndex: selectedIndexs,
  882. showGradeClass: false,
  883. currentClass: null,
  884. currentClassTxt: ''
  885. })
  886. },
  887. /** 选择班级 */
  888. onSelectClass() {
  889. if (!this.data.schoolAreaId) {
  890. wx.showToast({
  891. title: '请选择所在年级',
  892. icon: 'none'
  893. })
  894. return
  895. }
  896. this.setData({
  897. showClass: true
  898. })
  899. },
  900. /** 班级 */
  901. onCloseClass() {
  902. this.setData({
  903. showClass: false
  904. })
  905. },
  906. onClassBeforeEnter() {
  907. this.setData({
  908. showClassAfterLeave: false
  909. })
  910. },
  911. onClassAfterLeave() {
  912. this.setData({
  913. showClassAfterLeave: true
  914. })
  915. },
  916. /** 确认选择班级 */
  917. onSubmitClass(e: any) {
  918. const selectedOptions: any = e.detail.value
  919. const selectedIndexs: any = e.detail.index
  920. this.setData({
  921. currentClassTxt: selectedOptions.text,
  922. currentClass: selectedOptions.value,
  923. classIndex: selectedIndexs,
  924. showClass: false
  925. })
  926. },
  927. /** 最终提交 */
  928. async onSubmitBuyer() {
  929. try {
  930. const params = this.data
  931. if (!params.name) {
  932. wx.showToast({
  933. title: '请填写享用者姓名',
  934. icon: "none"
  935. })
  936. return
  937. }
  938. if (!params.phone || !/^1[3456789]\d{9}$/.test(params.phone)) {
  939. wx.showToast({
  940. title: '请填写正确的电话号码',
  941. icon: "none"
  942. })
  943. return
  944. }
  945. if(!params.gender) {
  946. wx.showToast({
  947. title: '请选择性别',
  948. icon: 'none'
  949. })
  950. return
  951. }
  952. if (!params.provinceCode || !params.cityCode || !params.regionCode) {
  953. wx.showToast({
  954. title: '请选择学校地区',
  955. icon: "none"
  956. })
  957. return
  958. }
  959. if (!params.schoolAreaId) {
  960. wx.showToast({
  961. title: '请选择所在学校',
  962. icon: "none"
  963. })
  964. return
  965. }
  966. if (!params.currentGradeNum) {
  967. wx.showToast({
  968. title: '请选择所在年级',
  969. icon: "none"
  970. })
  971. return
  972. }
  973. if (!params.currentClass) {
  974. wx.showToast({
  975. title: '请选择所在班级',
  976. icon: "none"
  977. })
  978. return
  979. }
  980. const objs = {
  981. phone: params.phone,
  982. name: params.name,
  983. gender: params.gender,
  984. currentGradeNum: params.currentGradeNum,
  985. currentClass: params.currentClass,
  986. schoolAreaId: params.schoolAreaId,
  987. defaultStatus: false
  988. }
  989. const userBeneficiary = {
  990. name: params.name,
  991. phoneNumber: params.phone,
  992. schoolInfo: params.schoolAreaName + params.currentGradeTxt + params.currentClassTxt
  993. }
  994. let userBeneficiaryId = ''
  995. this.setData({
  996. buyerLoading: true
  997. })
  998. if (params.userBeneficiaryId) {
  999. const { data } = await api_userBeneficiaryUpdate({
  1000. id: params.userBeneficiaryId,
  1001. ...objs
  1002. })
  1003. wx.showToast({
  1004. title: '保存成功',
  1005. icon: 'none'
  1006. })
  1007. userBeneficiaryId = data.data.id
  1008. } else {
  1009. const { data } = await api_userBeneficiarySave({
  1010. ...objs
  1011. })
  1012. wx.showToast({
  1013. title: '保存成功',
  1014. icon: 'none'
  1015. })
  1016. userBeneficiaryId = data.data.id
  1017. }
  1018. this.setData({
  1019. userBeneficiaryId,
  1020. userBeneficiaryInfo: userBeneficiary
  1021. })
  1022. this.onSetCatch(params)
  1023. this.onCloseBuyer()
  1024. } catch {
  1025. //
  1026. }
  1027. this.setData({
  1028. buyerLoading: false
  1029. })
  1030. },
  1031. /** 设置缓存 */
  1032. onSetCatch(params: any) {
  1033. wx.setStorageSync('buyerInfomation', JSON.stringify({
  1034. phone: params.phone,
  1035. name: params.name,
  1036. gender: params.gender,
  1037. schoolAreaId: params.schoolAreaId,
  1038. schoolAreaName: params.schoolAreaName,
  1039. currentGradeTxt: params.currentGradeTxt,
  1040. currentClass: params.currentClass,
  1041. currentClassTxt: params.currentClassTxt,
  1042. currentGradeNum: params.currentGradeNum,
  1043. cityCode: params.cityCode,
  1044. cityName: params.cityName,
  1045. provinceCode: params.provinceCode,
  1046. provinceName: params.provinceName,
  1047. regionCode: params.regionCode,
  1048. regionName: params.regionName,
  1049. schoolAreaIndex: params.schoolAreaIndex,
  1050. gradeGradeIndex: params.gradeGradeIndex,
  1051. classIndex: params.classIndex
  1052. }))
  1053. }
  1054. })