package.js 6.95 KB
// packageEquity/pages/package/package.js
import { couponsApi, ssoApi, equityApi } from '../../../api/api'
Page({

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.$util.decodeUrlLink(options)
    if(options.scene){
      options.id = wx.getStorageSync('packageId')
      if(!options.id){
        this.intval = setInterval(()=>{
          options.id = wx.getStorageSync('packageId')
          if(options.id){
            clearInterval(this.intval)
            this.getPackageDetail(options.id)
            wx.removeStorageSync('packageId');
          }
        },100)
      }else{
        wx.removeStorageSync('packageId');
      }
    }
    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() {
    getApp().login(true).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) {
    wx.$util.setNavigationBarTitleText(title)
    wx.setNavigationBarTitle({
      title
    })
  },
  getPhoneNumber({
    detail
  }) {
    delete detail.errMsg
    if (detail.iv && detail.encryptedData) {
      const {
        sessionId
      } = this.data
      if (!sessionId) return
      wx.$util.showLoading()
      detail.sessionId = sessionId
      ssoApi.authUserPhone(detail).then(({
        data,
        code
      }) => {
        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()
          // let { merchantId, phone } = data
          // this.toPostFreeCard(merchantId, phone, data)
        }
      })
    }
  },
  // 获取套餐详情
  getPackageDetail(id) {
    couponsApi.packageInfo({ id }).then(res => {
      if (res&&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 || 0)
          return item
        })
      }
      if(res.giveGoldFlag&&res.giveGoldFlag==2){
        this.getVIPCard()
      }
      this.setData({
        packageInfo: res,
        packageId: id
      })
      wx.$Track.setTrack({
        packageInfo:res,
        type: '查看套餐详情'
      }, 6)
    })
  },
  // 获取门店单号
  toCreatePackageNum() {
    wx.$util.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.$util.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: '/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.$util.showToast('取消支付')
            }
          }
        })
      }
    })
  },
  // 客户id
  handlerCustomerId() {
    let customerId = wx.$util.getStorageSync('srcm_shopmall_customer_id')
    let merchantId = wx.$util.getStorageSync('srcm_shopmall_merchant_id')
    if (customerId) {
      ssoApi.updataCustomerPhone({
        customerId,
        merchantId
      }).then(() => {
        wx.removeStorageSync('srcm_shopmall_customer_id')
      })
    }
  },
  getVIPCard(){
    const merchantId = wx.$util.getStorageSync('srcm_shopmall_merchant_id')
    let vipCardTitle = '广汇车主会员卡'
    equityApi.memberCard({merchantId}).then((res) => {
      vipCardTitle = res.equityGoldName || res.equityName
      this.setData({
        vipCardTitle,
        showVipCard:true
      })
    });
  },
  // 价格格式化
  // 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)
  },
})