// let BASE_Server = "http://127.0.0.1:8329" // let BASE_Server = "https://192.168.1.109:8080" let BASE_Server = "https://sookajs.top:18080" let app = getApp() //-----------------------------登录,获取token----------------------------------- function init_userInfo() { if (app.globalToken == null) { wx.getUserProfile({ desc: 'desc', success: (res) => { getSysUserInfo(res.userInfo) }, fail: res => { } }) } } function getSysUserInfo(info) { 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("/auth/applogin", data, loginSuccess) }, fail(res) { } }) } function loginSuccess(res) { if (res.data.code != 200) { wx.showToast({ title: res.data.msg != null ? res.data.msg : '', icon: "none" }) } else { app.globalToken = res.data.access_token //登录成功,获取首页数据 this.initIndexPage() } } //------------------------------------------------------------------ /** * 携带token json数据 POST请求 * @param {*} url * @param {*} data * @param {*} successfun */ function send_post(url, data, successfun) { if (app.globalToken == null) { wx.showToast({ title: '尚未登录,登录后即可使用', icon: 'none' }) return } 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) authToken(res.data) successfun(res.data) }, fail(res){ // console.log("TAG","返回数据:"+res.errno+'_'+res.errMsg) wx.showToast({ title: "网络请求失败", }) } }) } /** * 携带token表单数据POST请求 * @param {*} url * @param {*} data * @param {*} successfun */ function post(url, data, successfun) { if (app.globalToken == null) { wx.showToast({ title: '尚未登录,登录后即可使用', icon: 'none' }) return } 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) }, fail(res){ wx.showToast({ title: "网络请求失败", }) } }) } /** * 无token json数据使用的POST请求 * @param {*} url * @param {*} data * @param {*} successfun */ function post_token(url, data, successfun) { wx.request({ url: BASE_Server + url, //仅为示例,并非真实的接口地址 method: "POST", header: { 'content-type': 'application/json', 'Authorization': 'Bearer ' + app.globalToken }, data: data, success(res) { authToken(res.data) successfun(res.data) }, fail(res){ wx.showToast({ title: "网络请求失败", icon:'none', }) } }) } /** * 备用的GET请求 * @param {*} url * @param {*} data * @param {*} successfun */ function get(url, data, successfun) { if (app.globalToken == null) { wx.showToast({ title: '尚未登录,登录后即可使用', icon: 'none' }) return } wx.request({ url: BASE_Server + url, //仅为示例,并非真实的接口地址 method: "GET", header: { 'content-type': 'application/x-www-form-urlencoded', 'Authorization': 'Bearer ' + app.globalToken }, params: data, success(res) { successfun(res.data) }, fail(res){ wx.showToast({ title: "网络请求失败", }) } }) } /** * 携带token使用的GET请求 * @param {*} url * @param {*} data * @param {*} successfun */ function send_get(url, data, successfun) { if (app.globalToken == null) { wx.showToast({ title: '尚未登录,登录后即可使用', icon: 'none' }) return } wx.request({ url: BASE_Server + url, //仅为示例,并非真实的接口地址 method: "GET", 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) authToken(res.data) successfun(res.data) }, fail(res){ wx.showToast({ title: "网络请求失败", icon:'none', }) } }) } /** * 无token 可使用的 GET 请求 * @param {*} url * @param {*} data * @param {*} successfun */ function get_token(url, data, successfun) { wx.request({ url: BASE_Server + url, //仅为示例,并非真实的接口地址 method: "GET", 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) authToken(res.data) successfun(res.data) }, fail(res){ wx.showToast({ title: "网络请求失败", icon:'none', }) } }) } 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++) { 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_photo: send_photo, send: send, post: post, send_post: send_post, post_token: post_token, // wxpost: wxpost, get: get, send_get:send_get, get_token:get_token, send_postdecode: send_postdecode, // send_post_login, showLoading, hideLoading, } function showLoading() { wx.showLoading({ title: '加载中...', mask: true, }) } function hideLoading() { wx.hideLoading() } function authToken(res){ if(res.code==401){//登录token过期状态码 wx.clearStorage({ success:(res)=>{ wx.reLaunch({ url: '../denglu/denglu', }) } }) } } // 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) // } // }) // }