package.js 5.02 KB
import USERAPI from "~/api/usersApi/index";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    packageInfo: {},
    showAuthBtn: false,
    shareStaff: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!options.id) {
      return
    }
    this.login()
    this.getPackageDetail(options.id)
    // 推荐人
    if (options.shareStaffUuid) {
      this.data.shareStaff = {
        shareStaffUuid: options.shareStaffUuid,
        shareStaffName: options.shareStaffName
      }
      options.item_type = 4
      options.action_type = 1
      options.item_id = options.id
      this.data.recommandOptions = options
      // wx.$util.reportData(options, false, false, true)
    }
    if (options.activity_id) {
      this.data.options = options
      // wx.$util.reportData(options, true, false)
    }
  },
  // 登录
  login() {
    USERAPI.login().then(res => {
      if (res.data) {
        wx.$authStatus = res.data.phone ? false : true
        this.setNaviTitle(res.data.merchantName)
        this.setData({
          sessionId: res.data.sessionId,
          showAuthBtn: res.data.phone ? false : true
        })
        if (res.data.isLogin) {
          wx.p.cache.userInfo.setUserInfo(res.data)
          wx.p.cache.storageApi.setSessionId(res.data.sessionId)
          this.handlerCustomerId()
        }
      }
    })
  },
  // 设置标题名称
  setNaviTitle(title) {
    wx.setNavigationBarTitle({
      title
    })
  },
  getPhoneNumber({ detail }) {
    const { sessionId } = this.data
    if (!sessionId) return
    USERAPI.authUserPhone(detail, sessionId).then(({ data, code, message }) => {
      wx.hideLoading()
      if (code == '0') {
        wx.p.authStatus = false
       wx.p.cache.userInfo.setUserInfo(data)
       wx.p.cache.storageApi.setSessionId(data.sessionId)
        this.toCreatePackageNum()
        this.handlerCustomerId()
      } else wx.p.wxApi.showToast(message)
    })
  },
  // 获取套餐详情
  getPackageDetail(id) {
    wx.p.api.couponsApi
      .packageInfo({
        id
      })
      .then(res => {
        if (res.couponsDetailList.length) {
          res.couponsDetailList.map(item => {
            item.effectiveEndTime = new Date(item.effectiveEndTime).format('yyyy-MM-dd hh:mm:ss')
            // item.shopTotalPrice = this.priceFormat(item.shopTotalPrice)
            return item
          })
        }
        this.setData({
          packageInfo: res,
          packageId: id
        })
      })
  },
  // 获取门店单号
  toCreatePackageNum() {
    wx.p.wxApi.showLoading()
    wx.p.api.couponsApi.createOrderNum().then(res => {
      wx.hideLoading()
      if (res) {
        // this.setData({
        //   orderCode:res
        // })
        this.createStoreOrder(res)
      }
    })
  },
  // 创建订单
  createStoreOrder(orderCode) {
    let { packageId, sessionId, packageInfo, shareStaff } = this.data
    let para = {
      packageId,
      paySource: 3,
      sessionId,
      orderCode,
      ...shareStaff
    }
    let jumpParam = {
      orderCode,
      name: packageInfo.packageName,
      price: packageInfo.packagePrice
    }
    wx.p.wxApi.showLoading()
    let _this = this
    wx.p.api.couponsApi.createOrder(para).then(res => {
      wx.hideLoading()
      if (res) {
        const { timeStamp, nonceStr, packages, signType, paySign } = res
        wx.requestPayment({
          timeStamp: timeStamp,
          nonceStr: nonceStr,
          package: packages,
          signType: signType,
          paySign: paySign,
          success() {
            let temp = JSON.stringify(jumpParam)
            wx.navigateTo({
              url: '/packageEquity/pages/success/success?order=' + temp
            })
          },
          fail(res) {
            if (res.errMsg == 'requestPayment:fail cancel') {
              wx.p.wxApi.showToast('取消支付')
            }
          }
        })
      }
    })
  },
  // 客户id
  handlerCustomerId() {
    // let customerId = wx.p.storage.get('srcm_shopmall_customer_id')
    // if (customerId) USERAPI.updataCustomerPhone()
  },
  // 价格格式化
  // priceFormat(num, point = 2) {
  //   let numStr = num.toString().split('.')[0]
  //   if (numStr.length < 6) { // 判断数字有多长,如果小于6,,表示10万以内的数字,让其直接显示
  //     return numStr;
  //   } else if (numStr.length >= 6 && numStr.length <= 8) { // 如果数字大于6位,小于8位,让其数字后面加单位万
  //     point = 0
  //     let decimal = numStr.substring(numStr.length - 4, numStr.length - 4 + point)
  //     console.log(decimal);
  //     // 由千位,百位组成的一个数字
  //     return parseFloat(parseInt(num / 10000) + '.' + decimal) + '万'
  //   } else if (numStr.length > 8) { // 如果数字大于8位,让其数字后面加单位亿
  //     let decimal = numStr.substring(numStr.length - 8, numStr.length - 8 + point);
  //     console.log(decimal);
  //     return parseFloat(parseInt(num / 100000000) + '.' + decimal) + '亿'
  //   }
  // },
  onUnload: function () {
    // wx.$util.customerbehavior(2)
  }
})