// index.ts import { api_shopProduct } from "../../api/login"; import { debounce } from '../../utils/util' // 获取应用实例 const app = getApp() // pages/orders/orders.ts Page({ /** * 页面的初始数据 */ data: { imgList: [ 'https://oss.dayaedu.com/ktyq/1732672861312.png', 'https://oss.dayaedu.com/ktyq/1732613781003.png', 'https://oss.dayaedu.com/ktyq/1733118132694.png', 'https://oss.dayaedu.com/ktyq/1732613807112.png', // 'https://oss.dayaedu.com/ktyq/1732101102921.png', ], goodsImgList: [ "https://oss.dayaedu.com/ktyq/1733130441242.png", "https://oss.dayaedu.com/ktyq/1733118166964.png", "https://oss.dayaedu.com/ktyq/1732706055542.png", "https://oss.dayaedu.com/ktyq/1732706066094.png" ], serviceShow: true, current: 0, // autoplay: false, // interval: 5000, // duration: 500, popupShow: false, list: [] as any, isOverSaled: false, // 是否所有商品都没有库存 selected: {} as any, isFromPreviewImage: false, isShowOperation: false, // 是否显示操作按钮 }, /** * 生命周期函数--监听页面加载 */ onLoad() { // this.onInit() }, /** * 获取基础信息 */ async onInit() { try { const { data } = await api_shopProduct({ appId: app.globalData.appId }); const list = data.data || [] let selected: any = {} let isOverSaled = true // 是否销售完 list.forEach((item: any) => { item.originalPrice = this.formatPrice(item.originalPrice, 'ALL'); item.typeName = this.formatPeriod(item.num, item.period); const prices: any = this.formatPrice(item.salePrice) item.integerPart = prices.integerPart item.decimalPart = prices.decimalPart if(item.stockNum > 0) { isOverSaled = false if( !selected.id) { selected = item } } }); if(isOverSaled) { // 没有可购买商品则默认选中第一个商品 selected = list[0] } this.setData({ list, isOverSaled, selected }) } catch(e) { console.log(e, 'e') } }, // 格式化价格 formatPrice(price: number, type?: string) { const amountStr = price.toFixed(2) const [integerPart, decimalPart] = amountStr.split('.'); if(type === 'ALL') { return amountStr } return { integerPart, decimalPart } }, // 格式化类型 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 || {} }) }, // 事件处理函数 changeSwiper(e: any) { const detail = e.detail; if(detail.source === 'touch' || detail.source == 'autoplay') { this.setData({ current: detail.current }) } }, isLogin() { // 判断是否登录 if(!app.globalData.isLogin) { wx.navigateTo({ url: '../login/login', }) return false } return true }, /** 我的订单 */ onOrder() { // 判断是否登录 if(!this.isLogin()) { return } wx.navigateTo({ url: '../orders/orders', }) }, onBuyShop() { // 判断是否登录 if(!this.isLogin()) { return } this.setData({ popupShow: true }) }, onClose() { this.setData({ popupShow: false }) }, onSubmit() { // 判断是否登录 const that = this debounce(function () { if(!that.isLogin()) { return } let info = JSON.stringify({ ...that.data.selected }); info = encodeURIComponent(info); wx.navigateTo({ url: `../orders/order-detail?orderInfo=${info}`, success: () => { that.setData({ popupShow: false }) } }) }, 300)() }, onPreivewBannerImg(e: { currentTarget: { dataset: any } }) { wx.previewImage({ current: e.currentTarget.dataset.src, urls: this.data.imgList, success: () => { this.setData({ isFromPreviewImage: true }) } }) }, onPreivewGoodsImg(e: { currentTarget: { dataset: any } }) { wx.previewImage({ current: e.currentTarget.dataset.src, urls: this.data.goodsImgList, success: () => { this.setData({ isFromPreviewImage: true }) } }) }, onPreivewGoods(e: { currentTarget: { dataset: any } }) { wx.previewImage({ current: e.currentTarget.dataset.src, urls: [e.currentTarget.dataset.src], success: () => { this.setData({ isFromPreviewImage: true }) } }) }, /** * 生命周期函数--监听页面显示 */ onShow() { if(!this.data.isFromPreviewImage) { this.onInit() } else { this.setData({ isFromPreviewImage: false }) } this.setData({ serviceShow: true }) }, onHide() { this.setData({ serviceShow: false }) }, onScroll(e: { detail: any }) { // console.log(e, 'any') const scrollTop = e.detail.scrollTop || 0 if(scrollTop > 40) { this.setData({ isShowOperation: true }) } else { this.setData({ isShowOperation: false }) } }, onShareAppMessage() { return { title: '音乐数字AI器乐工具', path: '/pages/index/index', imageUrl: 'https://oss.dayaedu.com/ktyq/1733311074676.png' } }, onShareTimeline() { return { title: '音乐数字AI器乐工具', path: '/pages/index/index', imageUrl: 'https://oss.dayaedu.com/ktyq/1733311074676.png' } } })