// app.js
var restAjax = require('utils/restAjax.js');
App({
  restAjax: restAjax,
  apis: restAjax.apis,
  baseUrls: restAjax.baseUrl,
  onLaunch() {
    this.globalData.windowHeight = wx.getSystemInfoSync().windowHeight
    //设置状态栏
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        let capsule = wx.getMenuButtonBoundingClientRect();
        if (capsule) {
          this.globalData.Custom = capsule;
          this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
        } else {
          this.globalData.CustomBar = e.statusBarHeight + 50;
        }
      }
    })
    //获取是否绑定手机号
    try {
      var bindPhone = wx.getStorageSync('bindPhone')
      if (bindPhone != '' && bindPhone.length > 0) {
        this.globalData.bindPhone = bindPhone
      }
    } catch (error) {

    }
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },
  // 登录
  doLogin: function () {
    var self = this;
    wx.login({
      success(res) {
        console.log(res)
        app.restAjax.post(app.restAjax.path(apis.login, [baseUrls.loginUrl]), {
          jsCode: res.code
        }, null, function (code, data) {
          var token = data.data.split('_')[0]
          self.setData({
            token: token,
            bindPhone: data.data.split('_')[1]
          })
          wx.setStorageSync('token', token)
          wx.setStorageSync('bindPhone', data.data.split('_')[1])
          app.globalData.token = token
          self.getUserInfo()
        }, function (code, data) {
          app.dialog.msg(data.msg);
        });
      }
    })
  },
  // 获取用户信息
  getUserInfo: function () {
    var self = this
    app.restAjax.get(app.restAjax.path('{loginUrl}app/user/get-app-user', [app.personIntroUrl]), {}, {
      headers: {
        token: app.globalData.token
      }
    }, function (code, data) {
      app.globalData.userInfo = data
    }, function (code, data) {
      app.dialog.msg(data.msg);
    });
  },
  globalData: {
    userInfo: null,
    bindPhone: '',
    token: 'dEIwTEV0MEp5d1o2dlNBYUd4MVp0akV2MXVFbktCMWZ5cjFIbGpNMytQWkRLMWUvdjBUcDkrSk13ajBiYzJSS2JtY1p0cTlhYUxqRm10TUM4S1lSSFhabHVZdlRJRkwrUjA2Uk5pN2UyU0FYYXVQWVpxZHhEUTlCT1p3QURHcVNKVlVrakhOUlZJb2lyQ05wdVQvY1hCSHM5Wmk5VDBEM0ZOQlV1MWlwYm0yVjRGdmExNzRTS3Iyb1IyTThhSkczQk9uS3NOL25jVzJJSEJlbWpId2lKakREWm8yOHVhaUlRZVVKVmptbFdvbGZtUllBUG43M3pjKzhNcm11ZUY2ZHpsZmk1dU5kS04yTWM0Z0JiSmljV1E9PQ=='
  }
})