// index.ts import { api_shopProduct } from "../../api/login"; import { debounce } from '../../utils/util' // 获取应用实例 const app = getApp() // pages/orders/orders.ts Page({ /** * 页面的初始数据 */ data: { current: 0, autoplay: false, interval: 5000, duration: 500, popupShow: false, list: [] as any, selected: {} as any, }, /** * 生命周期函数--监听页面加载 */ onLoad() { this.onInit() }, /** * 获取基础信息 */ async onInit() { try { const { data } = await api_shopProduct({ appId: app.globalData.appId }); const list = data.data || [] let selected: any = {} list.forEach((item: any) => { item.originalPrice = this.formatPrice(item.originalPrice, 'ALL'); item.typeName = this.formatPeriod(item.num, item.period); if(item.stockNum > 0 && !selected.id) { const prices: any = this.formatPrice(item.salePrice) selected = { ...item, ...prices } } }); this.setData({ list, 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) 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, status: 'ing' }); info = encodeURIComponent(info); wx.navigateTo({ url: `../orders/order-detail?orderInfo=${info}`, }) that.setData({ popupShow: false }) }, 500)() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })