index.js 9.27 KB
const navStyle = 'background-color: transparent;'
let navBarHeight = 38
// import { mineApi, shopmallApi } from '~/api/api'
// import MineApi from '~/api/mineApi/index'
// import NP from 'number-precision'
// import USERAPI from '~/api/usersApi/index'
Page({
  data: {
    navStyle,
    tabs: ['商品详情', '商品参数'],
    curTabIndex: 0,
    service: {
      1001: '不支持无理由退货',
      1002: '支持7天无理由退货',
      1003: '支持7天无理由退货(拆封后不支持)',
      1004: '支持7天无理由退货(激活后不支持)',
      1005: '支持7天无理由退货(安装后不支持)',
      1006: '支持7天无理由退货(使用后不支持)',
    },
    skuInfo: '',
    staffShop: false,
    isCarOwner: false,
    payWayActions: [{ name: '金额支付', type: 1, disabled: false }, { name: '积分兑换', type: 2, disabled: false }],
    detailInfo: {
      pointsPrice: 25,
      marketPrice: 30,
      imageResponses: [{
        imgUrl: 'https://img02.glsx.com.cn/weapp/resource/miniprogram/points-mall/index/index-bg.png'
      }],
      saleNum: 600,
      remaining: 56,
      goodsName: '九阳保温饭盒焖烧杯九阳保温饭盒焖烧杯九阳保温饭盒焖烧杯九阳',
      goodsDescribe: '<figure class="image"><img src="https://img.yunzhonghe.com/image/1592793907468wHVTWiUHIc.png"></figure>'
    }
  },
  appHide() {
    wx.offAppHide()
    wx.onAppHide(() => {
      if (this.timer) clearInterval(this.timer)
      this.trackQuery.view = 5
    })
  },
  /**添加埋点结束 */
  navbarReady(e) {
    navBarHeight = e.detail.height
  },
  onPageScroll(e) {
    const { curTabIndex } = this.data
    if (curTabIndex == 0) {
      this.scrollY = e.scrollTop
    }
    this.setData({
      navStyle: e.scrollTop >= navBarHeight || curTabIndex ? '' : navStyle,
      opacity: curTabIndex == 1 ? 1 : e.scrollTop / 200,
    })
  },
  goodsActionReady(e) {
    this.setData({
      padding_bottom: e.detail
    })
  },
  goodsActionClick(e) {
    if (e.detail == 'join') return this.toAddShopCar()
    if (this.data.isCarOwner) return this.judgePayWay()
    this.showSkuModal(e.detail)
  },
  onLoad(options) {
    wx.p.router.getRouterParams(options).then(params => {
      // this.getDetail(options)
    }).catch(err => {
      console.error('参数获取失败:', err)
    })
    // this.getPlatFormAddress(staff)
  },
  showSkuModal(type) {
    console.log('111')
    // if (this.data.isCarOwner) return
    this.setData({
      showSku: true,
      clickType: type
    })
    wx.setPageStyle({
      style: {
        overflow: 'hidden'
      }
    })
  },
  // 获取默认的地址
  getPlatFormAddress(staff) {
    wx.showLoading()
    let para = {}
    if (staff) {
      para.businessType = 1
    }else{
      const SID = wx.$util.getSessionId()
      if (!SID) return wx.hideLoading()
    }
    shopmallApi.platformAddressList(para).then(data => {
      wx.hideLoading()
      let contactAddress = '', defaultAddress = {}
      let _isSetDefault = false
      if (data && data.length) {
        data.forEach(el => {
          if (el.isDefault == 1) {
            contactAddress = el.provinceName + el.cityName + el.countyName + el.address,
              defaultAddress = el
            _isSetDefault = true
          }
        })
        if (!_isSetDefault) {
          let _first = data[0]
          contactAddress = _first.provinceName + _first.cityName + _first.countyName + _first.address,
            defaultAddress = _first
        }
      }
      this.setData({
        addressList: data,
        contactAddress,
        defaultAddress
      })
    })
  },
  jump(e) {
    let { url, isChooseAddress } = e.currentTarget.dataset
    wx.$router.go(url, {
      isChooseAddress,
      platform: true,
      staff: this.data.staffShop,
      notIntercept: this.data.staffShop
    })
  },
  back() {
    wx.$router.back()
  },
  changeTab(e) {
    if (this.data.opacity < 0.8) return
    this.setData({
      curTabIndex: e.currentTarget.dataset.index,
      opacity: e.currentTarget.dataset.index
    }, () => {
      setTimeout(() => {
        wx.pageScrollTo({
          scrollTop: this.scrollY
        })
      }, 200)
    })
  },
  // 获取详情
  getDetail(options) {
    if(!options || !options.id)return
    const { id, staff, score, isCarOwner, openId } = options
    const merchantId = wx.$storage.get("srcm_shopmall_merchant_id");
    this.merchantId = merchantId
    let para = { id }, canPay = true
    if (staff) para.businessType = 1
    if (isCarOwner) {
      if (openId) {
        this.shareOpenId = options.openId
      }
      this.data.enter_time = new Date().format('yyyy-MM-dd hh:mm:ss')
      this.firstEnter = true
      this.appHide()
      this.getMyIntegral()
      para.merchantId = merchantId
    }
    shopmallApi.platformGoodsDetail(para).then(res => {
      if (res && res.specificationResponses) {
        this.toGetSpecString(res, res.startQty)
      }
      if (staff) {
        canPay = score > res.redeemPoints
      }
      // if (isCarOwner) {
      //   res.retailPrice = NP.divide(res.retailPrice, 100)
      // }
      this.setData({
        detailInfo: res || {},
        canPay,
        isCarOwner
      })
    })
  },
  toGetSpecString(info, count) {
    let skuInfo = this.data, num = '', weight = ''
    num = `${count}${info.goodsUnit}`
    weight = `${info.grossWeight}${info.grossWeightUnit}`
    skuInfo = `${info.specificationResponses[0].specValue}` + `,${weight}` + `,${num}`
    this.setData({
      skuInfo
    })
  },
  getCount(e) {
    this.toGetSpecString(this.data.detailInfo, e.detail)
    this.count = e.detail
  },
  // 添加购物车
  toAddShopCar() {
    const { detailInfo, isCarOwner } = this.data
    const PARAMS = {
      goodsId: detailInfo.id,
      goodsNumber: this.count || detailInfo.startQty,
      cartType: 2,
      goodsSkuCode: detailInfo.goodsSkuCode
    }
    if (isCarOwner) {
      PARAMS.carOwnerPlatformFlag = 1
      PARAMS.merchantId = this.merchantId
      PARAMS.cartType = 4
    }
    if(isCarOwner&&!detailInfo.retailPrice&&detailInfo.exchangeIntegral)return
    shopmallApi.platformAddCar(PARAMS).then(res => {
      wx.$api.showToast('添加成功!在购物车等亲~')
    })
  },
  // 同步选择的地址
  resetAddress(e) {
    this.setData(e.detail)
  },
  toDuiHuang() {
    if (this.data.canPay) {
      this.showSkuModal('pay')
    }
  },
  getViewInfo() {
    wx.$api.getViewInfo(
      '.fixed_btn_wrap',
      res => {
        this.setData({
          padding_bottom: res.height
        })
      },
      this
    )
  },
  closeSku() {
    wx.setPageStyle({
      style: {
        overflow: 'unset'
      }
    })
  },
  //获取我的积分
  getMyIntegral() {
    shopmallApi.getMyIntegralApi({}).then(res => {
      if (res) {
        this.setData({
          availableIntegral: res.availableIntegral
        })
      }
    })
  },
  judgePayWay() {
    const { availableIntegral, detailInfo, payWayActions } = this.data
    if (detailInfo.exchangeIntegralFlag) {
      const canPay = +availableIntegral > +detailInfo.exchangeIntegral
      const canMoneyPay = !detailInfo.retailPrice
      if (!canPay) {
        payWayActions[1].disabled = true
        payWayActions[1].name = '积分不足'
      }
      if(canMoneyPay){
        payWayActions[0].disabled = true
      }
      this.setData({
        showPayWay: true,
        canPay,
        payWayActions
      })
    } else {
      this.jumpToPay()
    }
  },
  closePayWayModal() {
    this.setData({
      showPayWay: false
    })
  },
  //选择支付方式
  selectPayWay(e) {
    const val = e.detail
    if (val.type == 2) {
      this.useJFtoPay = true
    } else {
      this.useJFtoPay = false
    }
    this.jumpToPay()
    this.closePayWayModal()
  },
  // 跳转确认支付订单页面
  jumpToPay() {
    const { detailInfo } = this.data
    const obj = {
      useJFtoPay: this.useJFtoPay,
      merchantName: detailInfo.merchantName,
      merchantId: detailInfo.merchantId,
      goodsList: [{
        goodsImage: detailInfo.imageResponses[0].imgUrl,
        goodsSkuName: detailInfo.goodsSkuName,
        retailPrice: detailInfo.retailPrice,
        exchangeIntegral: detailInfo.exchangeIntegral,
        goodsNumber: 1,
        goodsStock: detailInfo.goodsStock,
        goodsId: detailInfo.id,
        isPay: false,
        goodsSkuCode: detailInfo.goodsSkuCode,
      }]
    }
    const query = `?goodsInfo=${JSON.stringify(obj)}`
    wx.navigateTo({
      url: '/packageShopmall/packageMall/pages/platformShop/pay/index' + query,
    });
  },
  onUnload: function () {
    const {
      isCarOwner
    } = this.data
    if (!isCarOwner) return
    if (this.timer) clearInterval(this.timer)
    this.trackQuery.view = 5
  },
  onShareAppMessage: function () {
    const merchantId = wx.$storage.get('srcm_shopmall_merchant_id')
    const userInfo = wx.$util.getUserInfo()
    let {
      detailInfo,
      isCarOwner
    } = this.data, open_id = ''
    if (!isCarOwner) return
    this.trackQuery.view = 4
    if (userInfo) open_id = userInfo.openId
    let title = detailInfo.goodsSkuName
    let path = `${this.route}?id=${detailInfo.id}&openId=${open_id}`
    let imageUrl = ''
    detailInfo.imageResponses.map(e => {
      imageUrl = e.imgUrl
    })

    if (merchantId) path = `${path}&merchantId=${merchantId}&isCarOwner=${isCarOwner || false}`
    return {
      imageUrl,
      title,
      path
    }
  },

})