module.exports = { post, get, upload } // LOG控制 const isLog = false // 网络请求超时 const timeOut = 30 * 1000 const prefix = 'http://127.0.0.1:8888/api' // MARK 网络请求 function get(url, data) { let token = wx.getStorageSync("token") let header = { 'content-type': 'application/json', } if (token){ header.token = token } let schoolID = wx.getStorageSync("schoolID") if(schoolID){ header.schoolID = schoolID } return new Promise((resolve, reject) => { wx.request({ url: prefix+url, data: data, method: 'GET', dataType: 'json', timeout: timeOut, responseType: 'text', header: header, success: function (res) { let token = res.header.token if(token){ wx.setStorageSync("token", token) } if(res.statusCode!==200){ wx.showToast({ title: '内部错误', icon: 'none', duration: 3000, mask:true }) reject(res.statusCode) } resolve(res.data) }, fail: function (error) { wx.showToast({ title: '内部错误', icon: 'none', duration: 3000, mask:true }) reject(error) }, complete: (res) => { wx.hideLoading() wx.stopPullDownRefresh() if (isLog) { console.log('请求回调:', res) } }, }) }) } // MARK 网络请求 function post(url, data) { let token = wx.getStorageSync("token") let header = { 'content-type': 'application/json', } if (token){ header.token = token } let schoolID = wx.getStorageSync("schoolID") if(schoolID){ header.schoolID = schoolID } return new Promise((resolve, reject) => { wx.request({ url: prefix+url, data: data, method: 'POST', dataType: 'json', timeout: timeOut, responseType: 'text', header: header, success: function (res) { let token = res.header.token if(token){ wx.setStorageSync("token", token) } if(res.statusCode!==200){ wx.showToast({ title: '内部错误', icon: 'none', duration: 3000, mask:true }) reject(res.statusCode) } resolve(res.data) }, fail: function (error) { wx.showToast({ title: '内部错误', icon: 'none', duration: 3000, mask:true }) reject(error) }, complete: (res) => { wx.hideLoading() wx.stopPullDownRefresh() if (isLog) { console.log('请求回调:', res) } }, }) }) } // MARK 上传文件 function upload(event) { var upTask = wx.uploadFile({ url: event.url, filePath: event.filePath, name: Date.parse(new Date()), formData: event.formData, complete: (res) => { if (isLog) { console.log('文件上传:', res) } }, }) // UploadTask.abort() // 中断上传任务 // UploadTask.onProgressUpdate(function callback) // 监听上传进度变化事件 // UploadTask.offProgressUpdate(function callback) // 取消监听上传进度变化事件 return upTask }