//app.js
App({
  onLaunch: function () {
    let that = this
    that.init();
    wx.login({
      success(res){
        that.login(res.code)
      },
    })
  },
  post(url, data, showLoad){
    wx.showNavigationBarLoading()
    var promise = new Promise((resolve, reject) => {
      let that = this;
      //网络请求  
      wx.request({
        url: this.globalData.baseUrl + url,
        data: data,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'token': wx.getStorageSync('token') || ''
        },
        success: function (res) { //返回取得的数据
        wx.hideNavigationBarLoading()
          if (res.statusCode == '200') {
            if (showLoad && res.data.code == 0) {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 300
              })
            }
            resolve(res.data);
          } else if (res.statusCode == '401') {
            wx.showToast({
              title: '您还没登录,请先登录~',
              icon: 'none',
              duration: 500,
              success() {
                setTimeout(function () {
                  wx.navigateTo({
                    url: '/pages/index/start/start',
                  })
                }, 500)
              }
            })
          }else {
            reject(res)
          }
        },
        fail: function (e) {
          wx.hideNavigationBarLoading()
        }
      })
    })
    return promise
  },
  post1(url, data, showLoad) {
    wx.showNavigationBarLoading()
    var promise = new Promise((resolve, reject) => {
      let that = this;
      //网络请求  
      wx.request({
        url: this.globalData.baseUrl + url,
        data: data,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'token': wx.getStorageSync('token') || ''
        },
        success: function (res) { //返回取得的数据
          console.log(res)
          wx.hideNavigationBarLoading()
          if (res.statusCode == '200') {
            if (showLoad && res.data.code == 0) {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 300
              })
            }
            resolve(res.data);
          } else if (res.statusCode == '401') {
            wx.showToast({
              title: '您还没登录,请先登录~',
              icon: 'none',
              duration: 500,
              success() {
                setTimeout(function () {
                  wx.navigateTo({
                    url: '/pages/index/start/start',
                  })
                }, 500)
              }
            })
          } else {
            reject(res)
          }
        },
        fail: function (e) {
          wx.hideNavigationBarLoading()
        }
      })
    })
    return promise
  },
  //button登录
  buttonLogin(userInfo,callback){
    let that = this
    wx.login({
      success(res){
        let url = 'common/login'
        let params = {
          userInfo:userInfo,
          code: res.code,
          store_id: wx.getStorageSync('store_id') ? wx.getStorageSync('store_id') : 0
        }
        that.post(url,params,true).then(r1=>{
          if(r1.code==1){
            wx.setStorageSync('token', r1.data.userInfo.token)
            callback()
          }
        })
      },
    })
    
  },
  //登录
  login(code){
    let url = 'common/codeToToken'
    let params = {
      code: code
    }
    this.post(url,params,false).then(r=>{
      if (r.code == 1) {
        console.log('尝试登录成功')
        wx.setStorageSync('token', r.data.userInfo.token)
        wx.setStorageSync('session_key', r.data.sessionKey.session_key)
        wx.setStorageSync('openid', r.data.sessionKey.openid)
      }else{
        wx.setStorageSync('session_key', r.data.sessionKey.session_key)
        wx.setStorageSync('openid', r.data.sessionKey.openid)
      }
    })
  },
  //初始化
  init(){
    let that=this;
    this.post('common/init',{},false).then(r=>{
      if (r.code == 1) {
        wx.setStorageSync('initData',r.data);
        //that.globalData.site_name=r.data.site_name;
        that.globalData.site_name='';
        wx.setNavigationBarTitle({
          title: ''
        })
      }
    })
  },
  globalData: {
    userInfo: null,
    site_name: '',
    imgUrl: '/img/',
    baseUrl: 'https://www.meinavioce.com/api/'
  }
})