//app.js
App({
  onLaunch: function() {
    let that = this;
   
    // 展示本地存储能力
    wx.request({
      url: 'http://39.97.184.199:8080/CommonInterface/GetToken',

      method: 'POST',

      success: function(res) { //返回取得的数据
        // console.log(res.data.ResultObj)
        wx.setStorageSync('authToken', res.data.ResultObj)
      },
      fail: function(e) {
        reject('网络出错');
        // wx.hideLoading()
        wx.hideNavigationBarLoading()
      },
      complate() {
        wx.hideLoading({
          title: '加载中',
        })
      }
    })

    wx.login({
      success: function(res) {
        wx.login({
          success: (res) => {
            console.log(res)
            let url = '/Staff/GetOpenID';
            let params = {
              code: res.code,
            }
            that.post(url, params).then(res => {
              wx.setStorageSync('openid', res.openid);
            }).catch((errMsg) => {})

          }
        });
      },
      fail: function(res) {},
      complete: function(res) {},
    })

    setTimeout(function(){
      that.getuserinfo()
    },1500)
  
    

   

  },
  getuserinfo() {
    let that = this;
    var url = '/Staff/SelectRoleMenuByUser';
    var params = {
      openid: wx.getStorageSync('openid'),

    }
    that.post(url, params).then((res) => {
      console.log(res);
      wx.setStorageSync('current', 0)
      wx.setStorageSync('Suid', res.Suid);
      wx.setStorageSync('UserType', res.UserType)
      //usertyppe  1生活家  2散户  3 经销商

      // that.setData({
      //   usertype: res.UserType,
      //   usercode: res.UserCode
      // })


    }).catch((err) => {

    })
  },

  //封装post
  post: function(url, data, headerParams) {
    /**
     * 自定义post函数,返回Promise
     * +-------------------
     * @param {String}      url 接口网址
     * @param {arrayObject} data 要传的数组对象 like: {name: 'name', age: 32}
     * +-------------------
     * @return {Promise}    promise 返回promise供后续操作
     */
    // wx.showLoading({
    //   title: '加载中',
    // })
    let that = this;

    wx.showNavigationBarLoading()
    wx.showLoading({
      title: '加载中',
    })
    var promise = new Promise((resolve, reject) => {
      //init
      let postData = data;

      //网络请求
      let header = {
        'content-type': 'application/json;charset=utf-8',
        //'content-type':'application/x-www-form-urlencoded',
        'authToken': wx.getStorageSync('authToken') || ''
      }
      header = Object.assign(header, headerParams)
      wx.request({
        url: that.globalData.baseUrl + url,
        data: postData,
        method: 'POST',
        header: header,
        success: function(res) { //返回取得的数据
          console.log(res)
          if (res.data.ResultCode == '1') {
            resolve(res.data.ResultObj);
          } else {
            reject(res.data)
          }
          setTimeout(function() {
            wx.hideLoading()
            wx.hideNavigationBarLoading()
          }, 600)
        },
        fail: function(e) {
          reject('网络出错');
          // wx.hideLoading()
          wx.hideNavigationBarLoading()
        },
        complate() {
          wx.hideLoading({
            title: '加载中',
          })
        },
         complete: function () {
          wx.hideNavigationBarLoading()
        },
      })
    });
    return promise;
  },







  globalData: {
    userInfo: null,
    baseUrl: 'http://39.97.184.199:8080'

  },


  //底部导航跳转

  //  jump(e) {
  //   console.log(e)
  //   var that = this;
  //   that.setData({
  //     num: e.currentTarget.dataset.num
  //   })
  //   let number = e.currentTarget.dataset.num
  //   console.log(number)
  //   if (number == 1) {
  //    wx.navigateTo({
  //      url: '/pages/homeindex/homeindex',
  //    })

  //   } else if (number == 2) {
  //     wx.reLaunch({
  //       url: '../publicindex/publicindex',
  //     })
  //   } else if (number == 3) {

  //     that.setData({
  //       publishshow: true,
  //       num: 1
  //     })
  //   } else if (number == 4) {
  //     wx.reLaunch({
  //       url: '../mudipageindex/mudipageindex',
  //     })
  //   } else if (number == 5) {
  //     wx.reLaunch({
  //       url: '../myindex/myindex',
  //     })
  //   }
  // },
})