orderConfirm.js 4.62 KB
/**
 * orderConfirm.js
 *@author dennis
 *@createtime 2018/10/26
 *@description 订单支付确认界面
 */

import orderService from '../orderService'
import {getImgUrl} from '../../../utils';
import router from '../../../router/index.js'
import { prePay } from '../../order/wxpay.js'
import Yas from '../../../utils/yas';

let yas;

const api = new orderService();
Page({

  data: {
    payLabel: '待支付:',
    payButton: '去支付',
    protocolLabel: '我已阅读并同意',
    protocolText: '买家协议',
    agreeProtocol: false,
    checkImage: '../../../assets/images/select@2x.png',
    unCheckImage: '../../../assets/images/unselect@2x.png',
    isStore: 0,
    storeId: 0
  },
  onLoad: async function (option) {
    yas = new Yas(this);
    yas.pageOpenReport();
    this.setData({
      isStore: Number(option.is_store) || 0,
      storeId: option.store_id || 0,
      skup: option.skup || '',
      product_id: option.product_id || '',
      hasAddress: false
    })
    await this.fetchData(option.is_store);
    if (!option.is_store) {
      await this.fetchAddress()
    }
  },

  async fetchData(isStore) {
    wx.showLoading({
      title: '',
    });
    let params = {
      skup: this.data.skup,
      store_id: this.data.storeId
    }
    let data;
    if (isStore) {
      data = await api.createOfflinePayment(params, () => wx.hideLoading())
      let index;
      data.promotionFormulaList.forEach((value, i) => {
        if (value.promotion === '运费') {
          index = i;
        }
      })
      data.promotionFormulaList.splice(index,1)
    } else {
      data = await api.createPaymentInfo(params, () => wx.hideLoading())
    }
    if (data.good && data.good.goodImg) {
      data.good.goodImg = getImgUrl(data.good.goodImg, 270, 270)
    }
    this.setData(data)
  },

  async fetchAddress() {
    wx.showLoading({
      title: '',
    })

    let params = {}

    await api.yohoApi().buyerGetAddress(params, () => wx.hideLoading())
      .then(data => {
        if (Array.isArray(data)) {
          for (let item of data) {
            let index = data.indexOf(item)
            if (item.is_default === 'Y') {
              this.setData({
                address: item,
                hasAddress: true,
              })
              break
            }
            if (index == data.length - 1) {
              this.setData({
                address: item,
                hasAddress: true,
              })
            }
          }
        }

      })
      .catch(error => {
        console.log(error)
      })
    api.defaultApi();
  },

  async submit() {

    if (!this.data.agreeProtocol) return;
    let data;
    try {
      if (this.data.isStore) {
        this.check = this.selectComponent("#check");
        let checkRes = await this.check.init();
        if (!checkRes.result) return;
        let param = checkRes.data;
        param.skup = this.data.skup
        wx.showLoading({
          title: '',
        })
        data = await api.storeBuyerSubmit(param, () => {
          wx.hideLoading()
        })

      } else {
        if (!this.data.hasAddress) {
          wx.showToast({
            title: '请选择地址'
          })
          return
        }

        let addressId = this.data.hasAddress && this.data.address && this.data.address.address_id
        let skup = this.data.skup
        let channelNo = '';

        wx.showLoading({
          title: '',
        })
        data = await api.buyerSubmit(skup, channelNo, addressId, () => wx.hideLoading());
      }
      if (data && data.orderCode) {
        let orderCode = data.orderCode;
        let productId = data.productId ? data.productId : '';
        let params = {
          RPD_SKUP: skup,
          ORD_NUM: orderCode + ''
        };
        yas.report('YB_SC_ORD', params);
        prePay(productId, orderCode, 0);
      }
    } catch (error) {
      wx.showToast({
        title: error.message,
        icon: 'none'
      })
    }
  },


  chooseAddress: function () {
    let addressId = this.data.address ? this.data.address.address_id : ''
    let params = {
      currentMode: 'modeSelect',
      address_id: addressId
    }
    router.go('addressManager', params);
  },

  selectComplete: function (data) {

    if (data) {
      this.setData({
        address: data,
        hasAddress: true
      })
    }

  },

  checkProtocol: function () {
    this.setData({
      agreeProtocol: !this.data.agreeProtocol
    })
  },

  goProtocol: function () {
    let url = `http://m.yohobuy.com/activity/student/detail/renzhen?openby:yohobuy={\"action\":\"go.h5\",\"params\":{\"url\":\"https://activity.yoho.cn/feature/3189.html?title=买家协议\"}}`;
    router.goUrl(url);
  },
})