package.js 5.8 KB
// packageEquity/pages/package/package.js
import { couponsApi } from '~/api/api'
import USER from '~/api/usersApi/index'
import { setNavigationBarTitleText } from '~/utils/cache/storageApi'
import { decodeUrlLink } from '~/utils/format'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    packageInfo: {},
    showAuthBtn: false,
    shareStaff: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    decodeUrlLink(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() {
    USER.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.$util.setUserInfo(res.data)
          wx.$util.setSessionId(res.data.sessionId)
          this.handlerCustomerId()
        }
      }
    })
  },
  // 设置标题名称
  setNaviTitle(title) {
    setNavigationBarTitleText(title)
    wx.setNavigationBarTitle({
      title
    })
  },
  getPhoneNumber({ detail }) {
    const { sessionId } = this.data
    if (!sessionId) return
    USER.authUserPhone(detail, sessionId).then(({ data, code, message }) => {
      wx.hideLoading()
      if (code == '0') {
        wx.$Track.setTrack({}, 3)
        wx.$authStatus = false
        wx.$util.setUserInfo(data)
        wx.$util.setSessionId(data.sessionId)
        this.toCreatePackageNum()
        this.handlerCustomerId()
      } else wx.$api.showToast(message)
    })
  },
  // 获取套餐详情
  getPackageDetail(id) {
    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
        })
        wx.$Track.setTrack(
          {
            packageInfo: res,
            type: '查看套餐详情'
          },
          6
        )
      })
  },
  // 获取门店单号
  toCreatePackageNum() {
    wx.$api.showLoading()
    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.$api.showLoading()
    let _this = this
    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: '/packageShopmall/packageEquity/pages/success/success?order=' + temp
            })
            if (_this.data.options) {
              wx.$util.reportData(_this.data.options, false, true)
            }
            if (_this.data.recommandOptions) {
              let options = _this.data.recommandOptions
              options.item_type = 4
              options.action_type = 3
              options.item_id = options.id
              options.order_id = orderCode
              wx.$util.reportData(options, false, false, true)
            }
          },
          fail(res) {
            if (res.errMsg == 'requestPayment:fail cancel') {
              wx.$api.showToast('取消支付')
            }
          }
        })
      }
    })
  },
  // 客户id
  handlerCustomerId() {
    let customerId = wx.$storage.get('srcm_shopmall_customer_id')
    if (customerId) USER.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)
  }
})