// let BASE_Server = "http://127.0.0.1:8329"







let BASE_Server = "http://192.168.1.100:8080"
let BASE_Server_Login = "http://192.168.1.100:9200"
// let BASE_Server = "http://192.168.54.126:8080"
// let BASE_Server_Login = "http://192.168.54.126:9200"
let app = getApp()



function send_post_login(url, data, successfun) {

  wx.request({
    url: BASE_Server_Login + url, //仅为示例，并非真实的接口地址 
    method: "POST",
    header: {
      'content-type': 'application/json'
    },
    data: data,
    success(res) {
      successfun(res)
      // var d = decodeURIComponent(decodeURIComponent(res.data.data))
      // console.log("*****", d) 
      // var json = JSON.parse(d)
      // successfun(json)
    }
  })
}



//-----------------------------登录，获取token-----------------------------------
function init_userInfo() {
  if (app.globalToken == null) {
    console.log(1111111)
    wx.getUserProfile({
      desc: 'desc',
      success: (res) => {
        getSysUserInfo(res.userInfo)
      },
      fail: res => {
        console.log(res)
      }
    })
  }
}

function getSysUserInfo(info) {
  console.log(2222)
  let that = this
  wx.login({
    success(res) {
      var code = res.code
      var data = {
        wxCode: code,
        wxNickName: info.nickName,
        wxAvatarUrl: info.avatarUrl
      }
      // http.send_post_login("/minapp/AppLoginController/appLogin",data)
      send_post_login("/applogin", data, loginSuccess)
    },
    fail(res) {
      console.log("ffff", res)
    }
  })
}

function loginSuccess(res) {
  if (res.data.code != 200) {
    wx.showToast({
      title: res.data.msg,
      icon: "none"
    })
  } else {
    app.globalToken = res.data.data.access_token
    //登录成功，获取首页数据
    this.initIndexPage()
  }
  console.log("!!!!!", app.globalToken)
}





//------------------------------------------------------------------






function send_post(url, data, successfun) {
  console.log("令牌:", app.globalToken)
  //如果令牌为空或过期，需要重新请求

  wx.request({
    url: BASE_Server + url, //仅为示例，并非真实的接口地址 
    method: "POST",
    header: {
      'content-type': 'application/json',
      'Authorization': 'Bearer ' + app.globalToken
    },
    data: data,
    success(res) {
      // var d = decodeURIComponent(decodeURIComponent(res.data.data))
      // var json = JSON.parse(d)
      successfun(res)
    }
  })
}

function post(url, data, successfun) {
  wx.request({
    url: BASE_Server + url, //仅为示例，并非真实的接口地址
    method: "POST",
    header: {
      'content-type': 'application/x-www-form-urlencoded',
      'Authorization': 'Bearer ' + app.globalToken
    },
    data: data,
    success(res) {
      successfun(res.data)
    }
  })
}

function wxpost(url, data, successfun) {
  wx.request({
    url: BASE_Server + url, //仅为示例，并非真实的接口地址
    method: "POST",
    header: {
      'content-type': 'application/json',
      'Authorization': 'Bearer ' + app.globalToken
    },
    data: data,
    success(res) {
      successfun(res.data)
    }
  })
}

function get(url, data, successfun) {
  wx.request({
    url: BASE_Server + url, //仅为示例，并非真实的接口地址
    method: "Get",
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    params: data,
    success(res) {
      successfun(res.data)
    }
  })
}

function send_postdecode(url, data, successfun) {
  wx.request({
    url: BASE_Server + url, //仅为示例，并非真实的接口地址
    method: "POST",
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    data: data,
    success(res) {
      var json = JSON.parse(res.data)
      successfun(json)
    }
  })
}

function send_photo(data, successfun) {
  var that = this
  for (let i = 0; i < data.length; i++) {
    console.log("data长度=" + data.length)
    console.log(data)
    wx.uploadFile({
      url: BASE_Server + "/FileUpLoadController/upload.action", //仅为示例，非真实的接口地址
      filePath: data[i] + "",
      name: 'file',
      success: function (res) {
        var json = JSON.parse(res.data)
        var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
        wx.setStorageSync('filename' + (i + 1), sss.data.filename);
        successfun(sss)
      }
    })
  }
}

function send(data, successfun) {
  var that = this
  wx.uploadFile({
    url: BASE_Server + "/CarIDScanController/carIduploadGetNumber.action", //仅为示例，非真实的接口地址
    filePath: data[0] + "",
    name: 'file',
    success: function (res) {
      var json = JSON.parse(res.data)
      var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
      // wx.setStorageSync("carnumber", sss.data.carNumber)
      successfun(sss)
    },
    fail: function (res) {}
  })
}
module.exports = {
  BASE_Server: BASE_Server,
  send_post: send_post,
  send_photo: send_photo,
  send: send,
  post: post,
  wxpost: wxpost,
  get: get,
  send_postdecode: send_postdecode,


  send_post_login,
  showLoading,
  hideLoading,
}


function showLoading() {
  wx.showLoading({
    title: '加载中...',
    mask: true,
  })
}

function hideLoading() {
  wx.hideLoading()
}