const webSocket = require('./utils/websocket.js');

App({
  //设置全局请求URL
  globalData: {
    url: 'https://xiaochengxuapi.jrjyjrjy.com/v1/'
  },

  judge_Land() {
    
    try {
      if (wx.getStorageSync('access_token')) {   //是否注册过

        wx.reLaunch({
          url: '/pages/index/index'        //注册过 进入首页
        })

      } else if (wx.getStorageSync('wx_id')) {  //是否授权
        // console.log(2);
        wx.reLaunch({
          url: '/pages/Landpage/Land/Land'    //授权过 进入登陆页
        })
      } else {
        wx.reLaunch({
          url: '/pages/Jurisdiction/click_Jurisdiction/click_Jurisdiction'   //未授权  进入授权页
        })
      }
      // const value = wx.getStorageSync('access_token')
      // if (value) {
      //   // console.log(1234)
      //   console.log(value);
      //   // Do something with return value
      // }else{
      //   wx.reLaunch({
      //     url: '/pages/Landpage/Land/Land'    //授权过 进入登陆页
      //   })  
      // }
    } catch (e) {
      // Do something when catch error
    }




  
  },

  onLoad: function (options) {

    webSocket.connectSocket();

  
  },

  onLaunch: function (options) {
    wx.showShareMenu();

    var _this = this;
    // 查看是否授权
 
  },

  // 提示框
  Tips(text) {

    wx.showToast({
      title: text,
      icon: 'none',  // 不显示图标
      duration: 2500
    })

  },


  // 加载
  showLoading: function () {

    wx.showToast({

      title: '加载中',

      icon: 'loading'

    });

  },
  // 关闭加载
  cancelLoading: function () {
    wx.hideToast();
  },

  /**
  * 封装wx.request请求
  * url: 请求地址
  * data： 要传递的参数
  * callback： 请求成功回调函数
  * errFun： 请求失败回调函数
  **/
  postRequest(p_url, p_data, callback, errFun) {
    p_data.key = wx.getStorageSync('access_key');
    p_data.token = wx.getStorageSync('access_token');
    wx.request({
      url: this.globalData.url + p_url,
      method: 'POST',
      data: p_data,
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      dataType: 'json',
      success: function (res) {
          if(res.data.code == "401"){
            wx.clearStorageSync();
            wx.reLaunch({
              url: '/pages/Landpage/Land/Land'
            });
          }
        return typeof callback == "function" && callback(res.data)
      },
      fail: function (err) {
        return typeof errFun == "function" && errFun(err.data)
      }
    })
  },

  getRequest(g_url, g_data, callback, errFun) {
    g_data.key = wx.getStorageSync('access_key');
    g_data.token = wx.getStorageSync('access_token');
    wx.request({
      url: this.globalData.url + g_url,
      method: 'GET',
      data: g_data,
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      dataType: 'json',
      success: function (res) {

        return typeof callback == "function" && callback(res.data)
      },
      fail: function (err) {
        return typeof errFun == "function" && errFun(err.data)
      }
    })
  },

  // 图片方法
  Multigraph(dt_url, dt_filePath, callback) {
    wx.uploadFile({
      url: this.globalData.url + dt_url,
      filePath: dt_filePath,
      name: 'img',
      formData: {
        key: wx.getStorageSync('access_key'),
        token: wx.getStorageSync('access_token'),
      },
      success: function (res) {
        var data = JSON.parse(res.data);

        return typeof callback == "function" && callback(data)

        wx.showModal({
          title: '提示',
          content: '上传失败，请从新上传',
          showCancel: false,

        })

      }, fail: function (d) {

        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false,
        })

      }
    })

  },


  getDistrict(latitude, longitude) {

    let _page = this;
    let keys = '3VTBZ-JEH6Q-M7D5I-GNPVS-3QWBH-QUFY3';
    wx.request({
      url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${keys}`,
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          console.log(res.data.result.address_component);
          _page.postRequest('trajectory/location/position', res.data.result.address_component)
          // // 省
          // let province = res.data.result.address_component.province;
          // // 市
          // let city = res.data.result.address_component.city;
          // // 区
          // let district = res.data.result.address_component.district;
          // console.log(province);

        }
      }
    })
  },
  Imsocket: function () {
    return webSocket;
  },
  UserId: function () {
    return wx.getStorageSync('access_token');
  },
  UserToken: function () {
      return wx.getStorageSync('access_user');
  }

})



getApp().judge_Land(); //判断登陆




