index.ts 31 KB

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