text: const app = getApp()
  const CONFIG = require('../../config.js')
  const WXAPI = require('apifm-wxapi')
  const AUTH = require('../../utils/auth')
  const TOOLS = require('../../utils/tools.js')
  
  Page({
  	data: {
      wxlogin: true,
  
      balance:0.00,
      freeze:0,
      score:0,
      growth:0,
      score_sign_continuous:0,
      rechargeOpen: false // 是否开启充值[预存]功能
    },
  	onLoad() {
  	},	
    onShow() {
      const _this = this
      const order_hx_uids = wx.getStorageSync('order_hx_uids')
      this.setData({
        version: CONFIG.version,
        vipLevel: app.globalData.vipLevel,
        order_hx_uids
      })
      AUTH.checkHasLogined().then(isLogined => {
        this.setData({
          wxlogin: isLogined
        })
        if (isLogined) {        
          _this.getUserApiInfo();
          _this.getUserAmount();
        }
      })
      // 获取购物车数据，显示TabBarBadge
      TOOLS.showTabBarBadge();
    },
    aboutUs : function () {
      wx.showModal({
        title: '关于我们',
        content: '蜀地优鲜诞生于疫情，是资深旅游从业人，行业自救经营的综合性店铺，主要推广健康时尚的生活理念，商品以生鲜果蔬类为主，还涉足健康、亲子、美妆、特供、美食等多元化品类。在线联系客服和添加蜀幺妹儿微信号：shuyou5588，都会为您送去意想不到的惊喜哟！',
        showCancel:false
      })
    },
    loginOut(){
      AUTH.loginOut()
      wx.reLaunch({
        url: '/pages/my/index'
      })
    },
    getPhoneNumber: function(e) {
      if (!e.detail.errMsg || e.detail.errMsg != "getPhoneNumber:ok") {
        wx.showModal({
          title: '提示',
          content: e.detail.errMsg,
          showCancel: false
        })
        return;
      }
      WXAPI.bindMobileWxa(wx.getStorageSync('token'), e.detail.encryptedData, e.detail.iv).then(res => {
        if (res.code === 10002) {
          this.setData({
            wxlogin: false
          })
          return
        }
        if (res.code == 0) {
          wx.showToast({
            title: '绑定成功',
            icon: 'success',
            duration: 2000
          })
          this.getUserApiInfo();
        } else {
          wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel: false
          })
        }
      })
    },
    getUserApiInfo: function () {
      var that = this;
      WXAPI.userDetail(wx.getStorageSync('token')).then(function (res) {
        if (res.code == 0) {
          let _data = {}
          _data.apiUserInfoMap = res.data
          if (res.data.base.mobile) {
            _data.userMobile = res.data.base.mobile
          }
          if (that.data.order_hx_uids && that.data.order_hx_uids.indexOf(res.data.base.id) != -1) {
            _data.canHX = true // 具有扫码核销的权限
          }
          that.setData(_data);
        }
      })
    },
    getUserAmount: function () {
      var that = this;
      WXAPI.userAmount(wx.getStorageSync('token')).then(function (res) {
        if (res.code == 0) {
          that.setData({
            balance: res.data.balance.toFixed(2),
            freeze: res.data.freeze.toFixed(2),
            score: res.data.score,
            growth: res.data.growth
          });
        }
      })
    },
    goAsset: function () {
      wx.navigateTo({
        url: "/pages/asset/index"
      })
    },
    goScore: function () {
      wx.navigateTo({
        url: "/pages/score/index"
      })
    },
    goOrder: function (e) {
      wx.navigateTo({
        url: "/pages/order-list/index?type=" + e.currentTarget.dataset.type
      })
    },
    cancelLogin() {
      this.setData({
        wxlogin: true
      })
    },
    goLogin() {
      this.setData({
        wxlogin: false
      })
    },
    processLogin(e) {
      if (!e.detail.userInfo) {
        wx.showToast({
          title: '已取消',
          icon: 'none',
        })
        return;
      }
      AUTH.register(this);
    },
    scanOrderCode(){
      wx.scanCode({
        onlyFromCamera: true,
        success(res) {
          wx.navigateTo({
            url: '/pages/order-details/scan-result?hxNumber=' + res.result,
          })
        },
        fail(err) {
          console.error(err)
          wx.showToast({
            title: err.errMsg,
            icon: 'none'
          })
        }
      })
    },
  })