const WxParse = require('../../wxParse/wxParse.js'); const $ = global; new $.Page({ data: { amount: 1 }, onLoad(options) { const id = options.id; if (id) { $.wx.showToast({ title: '加载中' }, 'loading'); $.request(`product/${id}`, 'GET', {}).then((res) => { if (res.data) { $.wx.hideToast('loading'); const article = res.data.info; this.setData({ detail: res.data, unitPrice: res.data.price }); WxParse.wxParse('article', 'html', article, this, 5) } }); } }, minusAmount() { if (this.data.amount > 1) { this.setData({ amount: this.data.amount - 1 }); } }, addAmount() { this.setData({ amount: this.data.amount + 1 }); }, toPay() { const { name, phone, address, amount } = this.data; if (!name || !phone || !address || !amount) { return $.wx.showToast({ title: '请把信息填写完整' }); } if (!this._isPhone(phone)) { return $.wx.showToast({ title: '请输入正确的手机号码' }); } $.wx.showToast({ title: '购买中' }, 'loading'); $.request('product_order/store', 'POST', { address, contact: name, phone, products: [{ id: this.data.detail.id, amount }] }).then((res) => { if (res.data) { this.callWXPay(res.data.id, res.data.order_type || 'product_order'); } }); }, callWXPay(order_id, order_type) { $.request('payments/pay', 'POST', { order_id, order_type }).then((res) => { if (res.msg) { const { timestamp, paySign, nonceStr } = res.msg; const pck = res.msg.package; wx.requestPayment({ timeStamp: timestamp + '', nonceStr, package: pck, signType: 'MD5', paySign, success(res) { console.log('支付成功', res); $.wx.showToast({ title: '支付成功' }); }, fail() { $.wx.showToast({ title: '支付失败' }); }, complete() { setTimeout(function () { $.router.goto({ path: '/pages/order/index', type: $.ROUTER_TYPE.SWITCH_TAB }); }, 2000); } }); } }); }, toCart() { $.request('shopcar/add', 'POST', { product_id: this.data.detail.id, product_amount: this.data.amount || 1 }).then((res) => { $.wx.showToast({ title: '添加成功' }); // setTimeout(function () { // $.router.goto({ // path: '/pages/cart/index', // type: $.ROUTER_TYPE.SWITCH_TAB // }); // }, 2000); }); } });