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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.setNaviTitle()
    // decodeUrlLink(options)
    if (!options.id) {
      return
    }
    this.packageId = options.id
    // 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.p.cache.storageApi.reportData(options, false, false, true)
    // }
    // if (options.activity_id) {
    //   this.data.options = options
    //   wx.p.cache.storageApi.reportData(options, true, false)
    // }
  },
  async onShow() {
    this.getPackageDetail(this.packageId)
    this.setData({
      showAuthBtn: false
    })
    const loginStatus = !wx.p.isLogin
    if (loginStatus) return this.setData({
      showAuthBtn: true
    })
  },
  // 登录
  // login() {
  //   wx.p.api.userApi.login().then(res => {
  //     if (res.data) {
  //       wx.p.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.storageApi.setUserInfo(res.data)
  //         wx.p.cache.storageApi.setSessionId(res.data.sessionId)
  //         this.handlerCustomerId()
  //       }
  //     }
  //   })
  // },
  // 设置标题名称
  setNaviTitle(tile) {
    const {
      merchantName
    } = wx.p.storage.get('merchant_info')
    wx.setNavigationBarTitle({
      title: merchantName
    })
  },
  // getPhoneNumber({ detail }) {
  //   const { sessionId } = this.data
  //   if (!sessionId) return
  //   wx.p.api.userApi.authUserPhone(detail, sessionId).then(({ data, code, message }) => {
  //     wx.hideLoading()
  //     if (code == '0') {
  //       // wx.$Track.setTrack({}, 3)
  //       wx.p.authStatus = false
  //       wx.p.cache.storageApi.setUserInfo(data)
  //       wx.p.cache.storageApi.setSessionId(data.sessionId)
  //       this.toCreatePackageNum()
  //       // this.handlerCustomerId()
  //     } else wx.p.wxApi.showToast(message)
  //   })
  // },
  // 获取套餐详情
  getPackageDetail(id) {
    const {
      merchantId
    } = wx.p.storage.get('merchant_info')
    wx.p.api.couponsApi
      .packageInfo({
        id,
        merchantId
      })
      .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
        })
      })
  },
  // 获取门店单号
  async toCreatePackageNum() {
    const loginStatus = !wx.p.isLogin
    if (loginStatus) return
    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() {
            wx.p.router.go('/packageEquity/pages/success/success',{
              order: jumpParam
            })
            // if (_this.data.options) {
            //   wx.p.cache.storageApi.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.p.cache.storageApi.reportData(options, false, false, true)
            // }
          },
          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) wx.p.api.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.p.cache.storageApi.customerbehavior(2)
  }
})