const APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8"; const APPLICATION_JSON_UTF8_FORM = 'application/x-www-form-urlencoded;charset=UTF-8'; const RESPONSE_OK = 20000; const TOKEN_BEOVERDUE = 40001; const TOKEN = wx.getStorageSync('accessToken') == null ? '' : wx.getStorageSync('accessToken'); //测试环境 const HOST = 'https://wordpad-api-test.efunbox.cn'; //正式环境 // const HOST = 'https://wordpad-api-test.efunbox.cn'; const httpData = { userId: 'test' } //API接口 const interfaces = { //这里边放入请求的接口 //#region 教师端 //获取全国地址 getOrganizeAll: '/organize/all', //获取学校信息和班级信息(根据type判断) getSchoolOrClass: '/school', //手机号登录注册 phoneRegister: '/tch/user/register', //获取手机验证码 getVerifyCode: '/tch/user/verifyCode', //账号密码登录 pwdPhoneLogin: '/tch/user/login', //修改密码 updatePassWord: '/tch/user/updatePassword', //更新个人信息 updateUserInfo: '/tch/user', //根据userId获取用户信息 getUserById: '/tch/user/getUserById', //根据deviceId获取设备信息(后面拼接deviceId) getDeviceById: '/tch/device/', //根据地区,学校,班级获取设备ID getDevideIdByRSC: '/tch/device/site', //提交设备报修 postDeviceFault: '/tch/fault', //上传单个图片 uploadImageList: '/file/upload', //根据userId获取提交的报修记录 getDeviceFault: '/tch/fault', //根据故障ID获取故障详情(后面拼接ID) getFaultInfoByDevId: '/tch/fault/', //开始上课(创建文件库)(获取文件库get方法)(删除文件delete方法) createLess: '/tch/lesson/', //获取文件夹所有文件接口(拼接ID)(删除文件delete方法) getDocumentFile: '/tch/document', //#endregion } function fun(url: String, type: any, data: any, otherType: any) { if (!TOKEN) { //这里可以做一个请求的拦截 看有没有携带token console.log("Token:", TOKEN) } let promise = new Promise((resolve, reject) => { wx.request({ url: HOST + url, method: type, data: type == 'GET' ? data : JSON.stringify(data), header: { 'content-type': otherType ? APPLICATION_JSON_UTF8_FORM : APPLICATION_JSON_UTF8_VALUE, 'access-token': wx.getStorageSync('accessToken') == null ? '' : wx.getStorageSync('accessToken'), 'userId': httpData.userId }, success: function (res: any) { wx.stopPullDownRefresh(); if (res) { resolve(res) } else { wx.showToast({ title: res.data.subMsg ? res.data.subMsg : res.data.msg, icon: 'none', duration: 2000 }) } }, fail: function (res: any) { wx.stopPullDownRefresh(); reject(res) } }) }) return promise } function uploadFile(url: String, filePath: string, fileName: string, formData: any) { let promise = new Promise((resolve, reject) => { wx.uploadFile({ url: HOST + url, filePath: filePath, name: fileName, formData: formData, success(res: any) { resolve(res) // const data = res.data //do something }, fail(res: any) { reject(res) } }) }) return promise; } function downLoadFile(url: string, onProgress: any) { let promise = new Promise((resolve, reject) => { let download = wx.downloadFile({ url: url, success(res: any) { resolve(res) // const data = res.data //do something }, fail(res: any) { reject(res) } }) download.onProgressUpdate((res) => { onProgress(res) console.log('下载进度', res.progress) console.log('已经下载的数据长度', res.totalBytesWritten) console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite) }) }) return promise; } export const httpUtil = { httpData, interfaces, wxGet: function (url: String, data: any) { return fun(url, 'GET', data, null) }, wxPost: function (url: String, data: any) { return fun(url, 'POST', data, null) }, wxFormPost: function (url: String, data: any) { return fun(url, 'POST', data, 'formPost') }, wxPut: function (url: String, data: any) { return fun(url, 'PUT', data, null) }, wxDel: function (url: String, data: any) { return fun(url, 'DELETE', data, null) }, wxUploadFile: function (url: String, filePath: string, fileName: string, formData: any) { return uploadFile(url, filePath, fileName, formData) }, wxDownLoadFile: function (url: string, onProgress: any) { return downLoadFile(url, onProgress) } }