//app.js
import api from './utils/Api.js'

App({
  onLaunch: function(options) {
    this.options = options
    // 用户登录
    // this.checkUserLogin()
  },

  onShow: function(options) {
    wx.showShareMenu({
      withShareTicket: true,
      success: function(ret) {
        console.log('shareTickets:', ret)
      }
    })
  },

  /**
   * 用户资料授权
   */
  checkUserLogin: function(callback) {
    let _app = this
    if (!wx.getStorageSync('token')) {
      wx.navigateTo({
        url: '/pages/auth/index',
      })
    } else {
      api.me.info().then(res => {
        _app.globalData.userInfo = res.data
        callback && callback(res)
      })
    }
  },

  /**
   * 显示成功提示框
   */
  showSuccess(msg, callback) {
    wx.showToast({
      title: msg,
      icon: 'success',
      mask: true,
      duration: 1500,
      success() {
        callback && (setTimeout(function() {
          callback();
        }, 1500));
      }
    });
  },

  /**
   * 显示失败提示框
   */
  showError(msg, callback) {
    wx.showModal({
      title: '友情提示',
      content: msg,
      showCancel: false,
      success(res) {
        callback && callback();
      }
    });
  },

  globalData: {
    userInfo: null
  }
})