index.js 3.83 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: '未登录'});
        }

        return Promise.all([
            accountModel.getProfile(),
            accountModel.getCoinTotal(),
            indexModel.experienceCount(),
            indexModel.discountCount()
        ]).then(res => {
            let defaultAvatar = '../../static/images/icons/default-avatar.png';
            let resUserInfo = res[0].data || {};
            let vip_level = resUserInfo.vip_info.cur_level
            let vipImg = this.setVipImg(vip_level)
            let discountCoupon = res[3].data.couponQty;
            let experienceCoupon = res[2].data.couponQty;

            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
                }),
                coinNum: res[1].data && res[1].data.total || 0,
                discountCoupon,
                experienceCoupon
            });
        }).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 + '&hideShareMenu=true';
    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]();
    }
});