index.js 4.08 KB
// index.js
import wx from '../../utils/wx';
import accountModel from '../../models/account/index';
import indexModel from '../../models/home/coupon/index';
import Yas from '../../common/yas';
import goMiniApp from '../../router/jump-to-miniapp';

let app = getApp();
let yas;

Page({
    data: {
        notLogin: true, // 默认未登录
        discountCoupon: 0,
        experienceCoupon: 0,
        coinNum: 0,
        userInfo: {
            phoneNum: '',
            avatarUrl: '',
            defaultAvatar: false,
            nickName: '',
            vipImg: '',
            growth: 0
        },
        
        
      myQrCodeUrl: 'https://m.yohobuy.com/home/user/qrcode'
    },
    onLoad: function() {
        yas = new Yas(app); // 实例化埋点
        yas.pageOpenReport();
    },
    onShow: function() {
        app = app || getApp();
        this.showBindUserInfo();
    },

  
    showBindUserInfo: function() {
        app = app || getApp();

        if (!(app && app.getUid())) {
            setTimeout(() => {
                this.setData({notLogin: true});
            }, 10);

            indexModel.indexData({uid: '1000000000'}).then(res => {
                this.setData({
                  discountCoupon: 0,
                  experienceCoupon: 0
                });
            });
            return Promise.resolve({code: 401, data: [], message: '未登录'});
        }
      accountModel.getProfile().then((res) => {
        let defaultAvatar = '../../static/images/icons/default-avatar.png';
        let resUserInfo = res.data || {};
        let vip_level = resUserInfo.vip_info.cur_level
        let vipImg = this.setVipImg(vip_level)

        this.setData({
          notLogin: false,
          userInfo: Object.assign({}, this.data.userInfo, app.globalData.userInfo, {
            nickName: resUserInfo.nickname,
            avatarUrl: resUserInfo.head_ico || defaultAvatar,
            phoneNum: resUserInfo.mobile,
            defaultAvatar: (resUserInfo.head_ico || defaultAvatar) === defaultAvatar,
            growth: resUserInfo.vip_info.cur_growth,
            vipImg
          })
        });
      })
      accountModel.getCoinTotal().then((res) => {
        if (res.code !== 200) {
          return Promise.reject();
        }
        this.setData({
          coinNum: res.data && res.data.total || 0
        });
      }).catch(() => {})

      indexModel.experienceCount().then((res) => {
        if (res.code !== 200) {
          return Promise.reject();
        }
        let experienceCoupon = res && res.data && res.data.couponQty;
        this.setData({
          experienceCoupon
        });
      }).catch(() => { })

      indexModel.discountCount().then((res) => {
        if (res.code !== 200) {
          return Promise.reject();
        }
        let discountCoupon = res && res.data && res.data.couponQty;
        this.setData({
          discountCoupon
        });
      }).catch(() => { })
    },
  setVipImg: function (e) {
    let img = '';
    if (e == 1) {
      img = '../../static/images/my/silver@3x.png';
    } else if (e == 2) {
      img = '../../static/images/my/gold@3x.png';
    } else if (e == 3) {
      img = '../../static/images/my/platinum@3x.png';
    }
    return img
  },
  //点击我的二维码
  tapMyQRCode() {
    let url = '../webview/webview?url=' + this.data.myQrCodeUrl;
    wx.navigateTo({
      url,
    })
  },
    tapNavItem: function({target}) {
      let that = this
        let type = target.dataset.type;

        if (!type) {
            return;
        }

        const funcs = {
            order: () => {
                goMiniApp({
                    app: 'yohobuy',
                    page: 'orders'
                });
            },
          couponList:() => {
            global.router.go('couponList')
          },
          yohoCoin: () => {
            global.router.go('yohoCoin', { coinTotal: that.data.coinNum});
            },
          experience: () => {
            global.router.go('experience');
            },
          setting: () => {
            global.router.go('setting');
            }
        };

        funcs[type] && funcs[type]();
    }
});