index.ts 32 KB

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