|  | var app = getApp();
const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px'
const extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
const Http = require("../../utils/HttpBasics");
const config = require("../../config/config.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navigationBarHeight,
    num: 1,
    showShade: false,
    coverImg: "",
    title: "",
    salePriceStr: "", //单价
    freightPriceStr: "", //运费
    remainInventory: "", //库存
    seti: [],
    area: "",
    address: "",
    consignee: "",
    mobile: "",
    couponChannelId: "",
    thenArr: {}, //选中的地址对象
    contentType:0,
    shippingtype:1,//1是自提 2配送
    total:0,//合计
  },
  goAddStie() {
    wx.navigateTo({
      url: '/pages/siteAdd/siteAdd?url=/pages/fillIndent/fillIndent',
    })
  },
  setSite(e) {
    let id = e.currentTarget.dataset.id
    let arr = []
    arr = this.data.seti.filter(item => {
      return item.id == id
    })[0]
    this.setData({
      thenArr: arr,
      area: arr.regionStr,
      address: arr.address,
      consignee: arr.consignee,
      mobile: arr.mobile,
    })
  },
  getSetiLsit() {
    Http.get({
      url: config.api.siteList,
      data: {
        pageNum: 1,
        pageSize: 1000
      }
    }).then(res => {
      let temp = res.data.list
      if (temp.length > 0) {
        temp.map(item => {
          // let regionStr = ""
          let tempstr = ""
          JSON.parse(item.region).map(item2 => {
            tempstr += item2
          })
          item.regionStr = tempstr
        })
        console.log(temp, "temp")
        this.setData({
          seti: temp,
        })
        let tempRegion = JSON.parse(temp[0].region)
        let str = ""
        tempRegion.map(item => {
          str += item
        })
        this.setData({
          thenArr: temp[0],
          area: str,
          address: temp[0].address,
          consignee: temp[0].consignee,
          mobile: temp[0].mobile
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.message ? err.message : err.data,
        icon: 'none',
        duration: 2000,
      });
    })
  },
  setTotal(){
    //计算总和
    let str = 0
    str = (Number(this.data.salePriceStr) * (this.data.num)) + Number(this.data.freightPriceStr)
    this.setData({
      total: str.toFixed(2)
    })
  },
  addNum() {
    let num = this.data.num
    if (this.data.num >= this.data.remainInventory) {
      wx.showToast({
        title: '购买总数超库存',
        icon: 'none',
        duration: 2000,
      })
    } else {
      num = num + 1
      this.setData({
        num: num
      })
      this.setTotal()
    }
  },
  dleNum() {
    let num = this.data.num
    if (this.data.num <= 1) {
      wx.showToast({
        title: '购买总数不能为0',
        icon: 'none',
        duration: 2000,
      })
    } else {
      num = num - 1
      this.setData({
        num: num
      })
      this.setTotal()
    }
  },
  hieShade() {
    this.setData({
      showShade: false
    })
  },
  showTShade() {
    this.setData({
      showShade: true
    })
  },
  /**
   * 支付订单更新
   */
  payOrderUpdate: (orderId, payOrderId, status, reason, type, _this, composeOrderType) => {
    // 支付成功
    Http.post({
      url: config.api.payOrderUpdate,
      data: {
        payOrderId: payOrderId,
        composeOrderId: orderId,
        status: status,
        reason: reason
      }
    }).then(res => {
        //订单详情
        wx.hideLoading()
        wx.navigateTo({
          url: `/pages/order/detail/index?orderId=${orderId}&contentType=${_this.data.contentType}&composeOrderType=${composeOrderType}&dingdan=order`
        });
      })
      .catch(err => {
        console.log(err);
        if (!type) {
          setTimeout(function () {
            _this.payOrderUpdate(orderId, payOrderId, status, reason, type, _this, composeOrderType);
          }, 2000)
        }
      })
  },
  pay() {
    console.log(this.data.shippingtype)
    if (this.data.area == "" && this.data.shippingtype==2) {
      wx.showToast({
        title: "请选择地址",
        icon: 'none',
        duration: 2000,
      });
      return
    }
    let data = []
    let tempObj = {
      signleOrder: {
        couponChannelId: this.data.couponChannelId,
        couponId: this.data.couponId,
        formId: ""
      },
      count: this.data.num,
      shippingType: this.data.shippingtype, //1自提 2配送
    }
    if (this.data.shippingtype==2){
      tempObj.address = this.data.thenArr
    }
    data.push(tempObj)
    Http.post({
      url: config.api.goodsShopCartSave,
      data: JSON.stringify(data)
    }).then(res => {
      let {mainOrderId,composeOrderType} = { ...res.data}
      console.log(mainOrderId, composeOrderType)
      Http.post({
        url: config.api.payOrderCreate,
        data: {
          orderId: mainOrderId,
          composeOrderType: composeOrderType
        }
      }).then(res => {
        var payOrderId = "" + res.data.payOrderId;
        let that = this
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: (res.data.signType) ? res.data.signType : "MD5",
          paySign: res.data.paySign,
          success: res => {
            wx.showLoading({
              title: '订单正在处理中...',
            })
            that.payOrderUpdate(mainOrderId, payOrderId, 1, "", "", that, composeOrderType)
            
          },
          fail: res => {
            wx.showToast({
              title: '支付已取消',
              icon: 'none',
              duration: 2000,
            })
          }
        })
      }).catch(err => {
        wx.showToast({
          title: err.message ? err.message : err.data,
          icon: 'none',
          duration: 2000,
        });
      })
    }).catch(err => {
      wx.showToast({
        title: err.message ? err.message : err.data,
        icon: 'none',
        duration: 2000,
      });
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      couponChannelId: options.couponChannelId,
      couponId: options.couponId,
      shippingtype: options.shippingtype
    })
    Http.get({
      url: config.api.checkUserStatus,
      data: {
        token: app.globalData.token
      }
    }).then(res => {
      Http.get({
        url: config.api.checkPhoneStatus,
        data: {}
      }).then(res => {
        //授权完成 开始支付
      }).catch(err => {
        wx.redirectTo({
          url: `/pages/getphoneInfo/index?path=fi&fillIndentId=${this.data.couponChannelId}&couponId${this.data.couponId}`
        });
      })
    }).catch(err => {
      wx.navigateTo({
        url: `/pages/getuserinfo/index?fromflag=fillIndent&fillIndentId=${this.data.couponChannelId}&couponId${this.data.couponId}`,
      })
    })
  },
  getCouponDetail() {
    Http.get({
      url: config.api.couponDetail,
      data: {
        couponChannelId: this.data.couponChannelId
      }
    }).then(res => {
      let temp = res.data
      this.setData({
        coverImg: temp.coverImg,
        title: temp.title,
        salePriceStr: temp.salePriceStr,
        freightPriceStr: temp.freightPriceStr,
        remainInventory: temp.remainInventory,
        contentType: res.data.contentType
      })
      this.setTotal()
    }).catch(err => {
      wx.showToast({
        title: err.message ? err.message : err.data,
        icon: 'none',
        duration: 2000,
      });
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getCouponDetail()
    this.getSetiLsit()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
  },
  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function() {
  // }
})
 |