const md5 = require('./md5.js'); const ENV = require('./env.js'); var api = { /** * post 请求数据 * * @param string urlName url地址 * @param {object} data 数据对象 * @param function thenFun then回调 * @param function catchFun catch回调 */ post:function(urlName, data, thenFun,catchFun) { //数据组合 var newData = Object.assign(api.setSign(data), ENV.appData()); uni.$u.http.post(urlName, newData).then(res => { if(thenFun) thenFun(res); }).catch((rs) =>{ if(catchFun) catchFun(rs); }); }, /** * get 请求数据 * * @param string urlName url地址 * @param {object} data 数据对象 * @param function thenFun then回调 * @param function catchFun catch回调 */ get:function(urlName, data, thenFun,catchFun) { //数据组合 var newData = Object.assign(api.setSign(data), ENV.appData()); uni.$u.http.get(urlName,{params:newData}).then(res => { if(thenFun) thenFun(res); }).catch((rs) =>{ if(catchFun) catchFun(rs); }); }, /** * put 请求数据 * * @param string urlName url地址 * @param {object} data 数据对象 * @param function thenFun then回调 * @param function catchFun catch回调 */ put:function(urlName, data, thenFun,catchFun) { //数据组合 var newData = Object.assign(api.setSign(data), ENV.appData()); uni.$u.http.put(urlName, newData).then(res => { if(thenFun) thenFun(res); }).catch((rs) =>{ if(catchFun) catchFun(rs); }); }, /** * delete 请求数据 * * @param string urlName url地址 * @param {object} data 数据对象 * @param function thenFun then回调 * @param function catchFun catch回调 */ delete:function(urlName, data, thenFun,catchFun) { //数据组合 var newData = Object.assign(api.setSign(data), ENV.appData()); uni.$u.http.delete(urlName, newData).then(res => { if(thenFun) thenFun(res); }).catch((rs) =>{ if(catchFun) catchFun(rs); }); }, /** * upload 请求数据 * * @param string urlName url地址 * @param {object} data 数据对象 {filePath:'',fileType:'image'} * @param function thenFun then回调 * @param function catchFun catch回调 */ upload:function(urlName, data, thenFun,catchFun){ var newData = data; uni.$u.http.upload(urlName, { params: {}, /* 会加在url上 */ // #ifdef MP-ALIPAY fileType: newData.fileType, // 仅支付宝小程序,且必填。image/video/audio // #endif filePath: newData.filePath, // 要上传文件资源的路径。 // 注:如果局部custom与全局custom有同名属性,则后面的属性会覆盖前面的属性,相当于Object.assign(全局,局部) custom: {auth:true,toast:true,}, // 可以加一些自定义参数,在拦截器等地方使用。比如这里我加了一个auth,可在拦截器里拿到,如果true就传token name: 'files', // 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容 // #ifdef H5 || APP-PLUS timeout: 60000, // H5(HBuilderX 2.9.9+)、APP(HBuilderX 2.9.9+) // #endif header: { "Content-Type": "multipart/form-data" }, /* 会与全局header合并,如有同名属性,局部覆盖全局 */ formData: {}, // HTTP 请求中其他额外的 form data // 返回当前请求的task, options。请勿在此处修改options。非必填 getTask: (task, options) => { task.onProgressUpdate((res) => { console.log('上传进度' + res.progress); console.log('已经上传的数据长度' + res.totalBytesSent); console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend); }); }, }).then(res => { if(thenFun) thenFun(res); }).catch((rs) =>{ if(catchFun) catchFun(rs); }); }, setSign:function(data) { const date = parseInt(new Date().getTime() / 1000); const str = date + 'xingzhi&&2021'; data.i_app_timestamp = date; data.i_app_key = md5(str); //判断登录 openid 和 unionid 的来源 // #ifdef APP-PLUS data.i_app_platform = 'APP-PLUS'; // #endif // #ifdef MP //微信小程序 // #ifdef MP-WEIXIN data.i_app_platform = 'MP-WEIXIN'; // #endif //支付宝小程序 // #ifdef MP-ALIPAY data.i_app_platform = 'MP-ALIPAY'; // #endif //百度小程序 // #ifdef MP-BAIDU data.i_app_platform = 'MP-BAIDU'; // #endif //头条小程序 // #ifdef MP-TOUTIAO data.i_app_platform = 'MP-TOUTIAO'; // #endif //QQ小程序 // #ifdef MP-QQ data.i_app_platform = 'MP-QQ'; // #endif //360小程序 // #ifdef MP-360 data.i_app_platform = 'MP-360'; // #endif // #endif // #ifdef H5 data.i_app_platform = 'H5'; // #endif //语言包 data.i_app_lang ='zh'; //系统 data.i_app_system = uni.getStorageSync('app_system'); //这里是token 如果登录每个接口都需要这个 const token = uni.getStorageSync('token'); if(token.length > 0) { data.token = token; } return data; }, /** * 返回域名 * */ domain:function() { return ENV.CdnUrl; }, /** * 返回接口地址 * */ api:function() { return ENV.ApiUrl; }, /** * 返回接口环境 * */ env:function(){ return ENV.EnvShow; }, } module.exports = api;