home.js 2.76 KB
import wx from '../../utils/wx';
import event from '../../common/event';
import Yas from '../../common/yas';
import homeModel from '../../models/home/index';
import accountModel from '../../models/account/index';
import {getPhoneNumber, getUserInfoLogin} from '../../common/login';


// 获取应用实例
let app = getApp();
let router = global.router;
let yas;

Page({
  data: {
    userInfo: {},
    isLogin: false,
    hasUnionID: false,
    infoNum: {}
  },
  getUserInfoLogin,
  getPhoneNumber,
  onLoad: function() {
    event.on('user-login-success', this.loginSucess);
    event.on('bind-auto-register-type-report', params => {
      yas.report('YB_REGISTER_SUCCESS', params);
    });
    event.on('wx-union-id-update', () => {
      this.setData({
        hasUnionID: !!app.globalData.unionID
      });
    });

    yas = new Yas(app);
  },
  onShow: function() {
    this.setData({
      hasUnionID: !!app.getUnionID()
    });

    if (app.getUid()) {
      this.showUserInfo();
    }

    this.getInfoNum();
    yas.pageOpenReport();
    yas.report('YB_MAIN_TAB_C', {TAB_ID: 4});
  },
  loginSucess: function() {
    this.showUserInfo();
    this.getInfoNum();
  },
  getInfoNum: function() {
    if (app.getUid()) {
      homeModel.infoNum({app_type: app.getAppType()})
        .then(res => {
          if (res.code === 200) {
            this.data.infoNum = res.data;
            this.setData({
              infoNum: this.data.infoNum,
            });
          }
        });
    }
  },
  toOrdersTap: function(e) {
    if (!this.data.isLogin) {
      return wx.showToast({
        title: '请先完成登录/注册,再查看!',
        icon: 'none',
        duration: 2000
      });
    }
    let type = parseInt(e.currentTarget.dataset.type);

    const EVENTS = {
      1: 'YB_MY_ORD', // 我的订单
      2: 'YB_MY_TOPAY', // 待付款
      3: 'YB_MY_TOSEND', // 待发货
      4: 'YB_MY_TOREC', // 待收货
    };

    yas.report(EVENTS[type]);

    router.go('orderList', {type});
  },
  toAddress: function() {
    if (!this.data.isLogin) {
      return wx.showToast({
        title: '请先完成登录/注册,再查看!',
        icon: 'none',
        duration: 2000
      });
    }
    router.go('address');
  },
  toService: function() {
    router.go('service');
  },
  showUserInfo: function() {
    const defaultAvatar = 'https://img10.static.yhbimg.com/headimg/2013/11/28/09/01cae078abe5fe320c88cdf4c220212688.gif';

    accountModel.getProfile().then(res => {
      wx.hideLoading();
      this.setData({
        isLogin: true,
        userInfo: {
          phoneNum: res.data.mobile,
          nickName: res.data.nickname || res.data.profile_name,
          avatarUrl: res.data.head_ico || defaultAvatar
        }
      });
    }).catch(() => {
      wx.hideLoading();
    });
  }
});