import { api_shopInstruments, api_shopProduct } from "../../api/login" import { debounce, formatPrice } from "../../utils/util" // 获取应用实例 const app = getApp() // pages/select-goods/index.ts Page({ /** * 页面的初始数据 */ data: { instrumentList: [] as any, list: [] as any, isOverSaled: false, // 是否所有商品都没有库存 smallGoods: {}, // 最小金额商品 selected: {} as any, selectInstrumentId: '', // 选中的乐器 selectedInstrument: {} as any, formatSelectGood: { typeName: '', showSalePrice: '', // 显示的现价 originalPrice: 0, // 原价 salePrice: 0, // 现价 discountPrice: '' // 已省 } as any, // 格式化所有选中的数据 userBeneficiaryId: '', // 选中用户的编号 userBeneficiaryInfo: { name: '', phoneNumber: '', schoolInfo: '' }, }, /** * 生命周期函数--监听页面加载 */ onLoad() { this.onInit() }, /** * 获取基础信息 */ async onInit() { try { const result = await api_shopInstruments({ appId: app.globalData.appId }) const instrumentList = result.data.data || [] instrumentList?.forEach((item: any) => { item.showSalePrice = formatPrice(item.salePrice || 0, 'ALL') }) const { data } = await api_shopProduct({ appId: app.globalData.appId }); const list = data.data || [] let selected: any = {} let isOverSaled = true // 是否销售完 // 最少金额商品 let smallGoods: any = {} list.forEach((item: any) => { item.originalPrice = formatPrice(item.originalPrice, "ALL"); item.showSalePrice = formatPrice(item.salePrice, "ALL"); item.typeName = this.formatPeriod(item.num, item.period); item.discountPrice = formatPrice( item.originalPrice - item.salePrice, "ALL" ); const prices: any = formatPrice(item.salePrice) item.integerPart = prices.integerPart item.decimalPart = prices.decimalPart if (item.stockNum > 0) { isOverSaled = false if (!selected.id) { selected = item } } // 获取最小金额 if (smallGoods?.salePrice) { smallGoods = smallGoods.salePrice <= item.salePrice ? smallGoods : item } else { smallGoods = item } }); if (isOverSaled) { // 没有可购买商品则默认选中第一个商品 selected = list[0] } this.setData({ list, instrumentList, // 乐器列表 isOverSaled, selected, smallGoods, selectInstrumentId: '', selectedInstrument: {}, userBeneficiaryId: '', userBeneficiaryInfo: { name: '', phoneNumber: '', schoolInfo: '' } }, () => { this.onFormatGoods() }) } catch (e) { console.log(e, 'e') } }, // 格式化类型 formatPeriod(num: number, type: string) { const template: any = { DAY: "天卡", MONTH: "月卡", YEAR: "年卡" } if (type === "YEAR" && num >= 99) { return '永久卡' } return num + (template[type] || '') }, // 选择 onSelectGoods(e: any) { const { dataset } = e.currentTarget const item = this.data.list.find((item: any) => item.id === dataset.id) // 判断是否有库存 if (item.stockNum <= 0) { return } this.setData({ selected: item || {} }, () => { this.onFormatGoods() }) }, /** 选中乐器 */ onSelectInstrument(e: any) { const { dataset } = e.currentTarget; if (dataset.id === this.data.selectInstrumentId) { this.setData({ selectInstrumentId: '', selectedInstrument: {} }, () => { this.onFormatGoods() }) } else { const item = this.data.instrumentList.find((item: any) => item.id === dataset.id); this.setData({ selectInstrumentId: dataset.id, selectedInstrument: item || {} }, () => { this.onFormatGoods() }) } }, isLogin() { // 判断是否登录 if (!app.globalData.isLogin) { wx.navigateTo({ url: '../login/login', }) return false } return true }, /** 格式化选中的商品 */ onFormatGoods() { const selected = this.data.selected; const selectedInstrument = this.data.selectedInstrument const params = { typeName: '', showSalePrice: '' as any, // 显示的现价 originalPrice: 0, // 原价 salePrice: 0, // 现价 discountPrice: '' as any, // 已省 integerPart: '', decimalPart: '', } // 选中期限 if (selected.id) { params.typeName = selected.typeName params.showSalePrice = selected.showSalePrice params.originalPrice = selected.originalPrice params.salePrice = selected.salePrice params.discountPrice = selected.discountPrice const prices: any = formatPrice(params.salePrice); params.integerPart = prices.integerPart params.decimalPart = prices.decimalPart } // 选中乐器 if (selectedInstrument.id) { params.typeName = selected.typeName ? selected.typeName + '+' + selectedInstrument.name : selectedInstrument.name params.originalPrice = Number(selected.originalPrice) + Number(selectedInstrument.originalPrice) params.salePrice = Number(selected.salePrice) + Number(selectedInstrument.salePrice) params.showSalePrice = formatPrice(params.salePrice, "ALL"); params.discountPrice = formatPrice( params.originalPrice - params.salePrice, "ALL" ); const prices: any = formatPrice(params.salePrice); params.integerPart = prices.integerPart params.decimalPart = prices.decimalPart } this.setData({ formatSelectGood: params }) }, onSubmit() { // 判断是否登录 const that = this if (!this.data.userBeneficiaryId) { wx.showToast({ title: '请填写享用者的个人信息', icon: 'none' }) return } debounce(function () { if (!that.isLogin()) { return } const params = [] as any const selected = that.data.selected if (selected.id) { params.push({ pic: selected.pic, name: selected.name, period: selected.period, num: selected.num, originalPrice: selected.originalPrice, salePrice: selected.salePrice, shopId: selected.shopId, id: selected.id, goodsType: 'ACTIVATION_CODE', // INSTRUMENTS }) } const selectedInstrument = that.data.selectedInstrument if (selectedInstrument.id) { params.push({ pic: selectedInstrument.pic, name: selectedInstrument.name, period: selectedInstrument?.period, num: selectedInstrument?.num || 0, originalPrice: selectedInstrument.originalPrice, salePrice: selectedInstrument.salePrice, shopId: selectedInstrument.shopId, id: selectedInstrument.id, goodsType: 'INSTRUMENTS', // INSTRUMENTS }) } let info = JSON.stringify({ ...params }); info = encodeURIComponent(info); console.log(params, "params") wx.navigateTo({ url: `../orders/order-detail?orderInfo=${info}&userBeneficiaryId=${that.data.userBeneficiaryId}`, success: () => { that.setData({ popupShow: false, currentIndex: 1, }) } }) }, 500)() }, })