// index.ts import { api_executeOrder, api_queryByParamName, 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, isOverSaled: false, // 是否所有商品都没有库存 selected: {} as any, isH5Pay: true, // 是否公众号支付 paymentType: null as any, // 支付类型 paymentChannel: null as any, }, /** * 生命周期函数--监听页面加载 */ onLoad() { this.queryPayType() }, /** * 获取基础信息 */ 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 } if(that.data.isH5Pay) { that.onH5Play(that.data.selected) } else { let info = JSON.stringify({ ...that.data.selected }); info = encodeURIComponent(info); wx.navigateTo({ url: `../orders/order-detail?orderInfo=${info}`, }) that.setData({ popupShow: false }) } }, 500)() }, async onH5Play(goodsInfo: any) { wx.showLoading({ mask: true, title: "加载中...", }); try { const { salePrice, shopId, name, id, orderNo } = goodsInfo const { data } = await api_executeOrder({ "orderType": "WECHAT_MINI", "paymentType": this.data.paymentType, "paymentCashAmount": salePrice, "paymentCouponAmount": 0, "shopId": shopId, "openId": app.globalData.userInfo?.liteOpenid, "goodsInfos": [{ "goodsId": id, "goodsNum": 1, "goodsType": "ACTIVATION_CODE", "paymentCashAmount": salePrice, "paymentCouponAmount": 0 }], "orderName": name, "orderDesc": name }) if (data.code === 200) { const { paymentConfig, paymentType, orderNo } = data.data // 测试h5页面支付流程 const params = { paymentType: paymentType, //'adapay-cooleshow-6560', pay_channel: 'wx_pub', wxAppId: paymentConfig.wxAppId, //'wxbde13f59d40cb4f2', body: paymentConfig.body, //'aaa', price: paymentConfig.price, // '0.01', orderNo: paymentConfig.merOrderNo, userId: paymentConfig.userId } wx.navigateTo({ url: '../order-webview/order-webview?orderInfo=' + encodeURIComponent(JSON.stringify(params)) }) wx.hideLoading() } else { wx.hideLoading() wx.showToast({ title: '购买失败', icon: 'none' }) } } catch { wx.hideLoading() } }, // 获取后台配置的支付方式 async queryPayType() { try { // wxlite_payment_service_provider const { data } = await api_queryByParamName({ paramName: 'wxlite_payment_service_provider' }); if (data.code == 200) { const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {} this.setData({ paymentType: paramValue.vendor, paymentChannel: paramValue.channel }); } } catch (error) { console.log(error, "error"); } }, /** * 生命周期函数--监听页面显示 */ onShow() { this.onInit() }, })