mine.js 7.17 KB
// pages/tab/mine/mine.js
const navStyle = 'background-color: transparent;'
import {
  ssoApi,
  mineApi,
  couponsApi,
  indexApi,
  boutiquecouponApi
} from '../../../api/api'
import mapApi from '../../../utils/mapapi'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    utilList: [{
        text: '我的订单',
        url: 'order',
        title: '我的订单'
      },
      {
        text: '我的地址',
        url: 'address',
        title: '我的地址'
      },
      {
        text: '我的预约',
        url: 'reservation',
        title: '我的预约'
      },
      // {
      // 	text: '精品交付单',
      // 	url: 'boutiquePages'
      // },
      {
        text: '协议内容',
        icon: 'agreement',
        url: 'mineAgree',
        notIntercept: true
      },
      {
        text: '兑换专区',
        icon: 'exchange',
        disable: true,
        title: '兑换专区'
      },
      {
        text: '设置',
        url: 'setting',
        title: '设置'
      }
    ],
    navStyle,
    carList: [],
    showFillModal: false
  },
  /**v3.7.2 */
  showExchangeArea(item) {
    const USERINFO = wx.$util.getUserInfo()
    if (!USERINFO) return wx.$router.jumpVisitorPage(item)
    this.setData({
      showFillModal: true
    })
  },
  /**授权用户头像昵称 */
  showAuthModal() {
    this.setData({
      showAuth: true
    })
  },
  authSuccess(e) {
    let {
      data
    } = e.detail
    /**调取后端接口储存用户头像昵称 */
    if (data) {
      ssoApi.authUserInfo({
        ...data
      }).then(({
        code
      }) => {
        if (code == '0') {
          getApp().login().then(({
            data
          }) => {
            this.setData({
              userInfo: data
            })
          })
        }
      })
    }
  },
  onPageScroll({
    scrollTop
  }) {
    if (scrollTop >= 38 && this.data.navStyle) this.setData({
      navStyle: ''
    })
    if (scrollTop < 38 && !this.data.navStyle) this.setData({
      navStyle
    })
  },
  navbarReady(e) {
    let {
      height
    } = e.detail
    this.setData({
      user_box_style: `padding-top:${height + 20}px`
    })
  },
  goto(e) {
    let {
      url,
      item,
      name
    } = e.currentTarget.dataset
    let path = 'index'
    if (name) path = name
    if (url == 'coupon' || url == 'boutique') {
      item = {
        title: {
          coupon: '我的优惠券',
          boutique: '精品礼券'
        } [url]
      }
    }
    wx.$router.go(`${this.rootName}.${url}.${path}`, item)
  },
  jump(e) {
    const {
      item
    } = e.currentTarget.dataset
    if (item.disable) {
      this.showExchangeArea(item)
      return
    }
    if (item.h5) {
      wx.$router.jumpH5(item.h5)
    } else {
      wx.$router.go(`${this.rootName}.${item.url}.index`, {
        notIntercept: item.notIntercept || false,
        ...item
      })
    }
  },
  init() {
    this.getGHUserCarList()
  },
  phoneCall(e) {
    let {
      phone
    } = e.currentTarget.dataset
    wx.$util.phoneCall(phone)
  },
  getUserCouponList() {
    couponsApi.getCouponNum().then(res => {
      this.setData({
        total: res.total
      })
    })
  },
  getBoutiquecouponList() {
    boutiquecouponApi.getBoutiquecouponList({
      pageNum: 1,
      pageSize: 999999,
      usedFlag: 'N'
    }).then(data => {
      this.setData({
        gitTotal: data.total
      })
    })
  },
  getUserCarList() {
    mineApi.getUserCarList().then(data => {
      let carInfo = null
      data.map(e => {
        if (e.carTypeName) carInfo = e
      })
      this.setData({
        carInfo,
        carFlag: true
      })
    }).catch(() => {
      this.setData({
        carFlag: true
      })
    })

  },
  //获取当前用户顾问配置
  getConsultantInfo() {
    let merchantId = wx.getStorageSync('srcm_shopmall_merchant_id')
    if (merchantId) {
      merchantId = Number(JSON.parse(merchantId))
    }
    let data = {
      merchantId: merchantId
    }
    mineApi.getConsultantInfo(data).then(({
      code,
      data
    }) => {
      if (code == '0') {
        this.setData({
          consultant: data,
        })
      }
    })
  },
  //服务顾问
  goCounselor() {
    wx.$router.go('packageMine.counselor.index')
  },
  //打开服务顾问
  openConsultant() {
    wx.$router.go('packageMine.counselor.index')
  },
  getGHUserCarList() {
    mineApi.getGHUserCarList().then(({
      code,
      data
    }) => {
      this.setData({
        carFlag: true,
      })
      if (code == '0') {
        let {
          vehs
        } = data
        if (vehs.length) {
          let platPrefix = vehs[0].platNo.split('')[0]
          vehs[0].platNumber = vehs[0].platNo.replace(platPrefix, '')
          vehs[0].platPrefix = platPrefix
          this.setData({
            carList: vehs
          })
        }
      }
    })
  },

  login() {
    // this.setData({
    //   showLogin: true
    // })
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let {
      sapCode
    } = this.data
    let storeCode = wx.$util.getStorageSync('srcm_shopmall_store_code')
    if (sapCode != storeCode) {
      this.setData({
        merchantDetail: null,
        sapCode: storeCode
      })
    }

    if (!wx.$authStatus) {
      if (!wx.$isGroup) this.getMerchantDetail(storeCode)
      this.getUserCouponList()
      this.getConsultantInfo()
      this.getBoutiquecouponList()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (wx.$authStatus) return
    this.getGHUserCarList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const merchantId = wx.$util.getStorageSync('srcm_shopmall_merchant_id')
    let storeCode = wx.$util.getStorageSync('srcm_shopmall_store_code')
    let path = this.route
    if (merchantId) path = `${path}?merchantId=${merchantId}&storeCode=${storeCode || ''}`
    return {
      path
    }
  },
  getMerchantDetail(storeCode) {
    indexApi.getGHStoreList({
      storeCode
    }).then(data => {
      if (data.length) this.setData({
        merchantDetail: data[0]
      })
    })
  },
  openLocation(e) {
    let {
      item
    } = e.currentTarget.dataset
    if (item.location.latitude == '0.0') {
      mapApi.geocoder(item.address).then(({
        location
      }) => {
        wx.openLocation({
          latitude: Number(location.lat),
          longitude: Number(location.lng),
          name: item.storeName,
          address: item.address
        })
      })
      return
    }
    wx.openLocation({
      latitude: Number(item.location.latitude),
      longitude: Number(item.location.longitude),
      name: item.storeName,
      address: item.address
    })
  },

})