123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- 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);
- });
- }
- });
|