106 lines
2.6 KiB
JavaScript
106 lines
2.6 KiB
JavaScript
|
import config from '/config/config'
|
||
|
const request = (method, url, options) => {
|
||
|
let methods = '';
|
||
|
let headers = {};
|
||
|
// console.log(dd.$toolAll.getCache('token'),'token数据');
|
||
|
switch (method) {
|
||
|
case 'get':
|
||
|
methods = 'GET'
|
||
|
headers = {
|
||
|
'Content-Type': 'application/json; charset=UTF-8',
|
||
|
'Authorization': 'Bearer '+ dd.$toolAll.getCache('token') || ''
|
||
|
}
|
||
|
break;
|
||
|
case 'post':
|
||
|
methods = 'POST'
|
||
|
headers = {
|
||
|
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
||
|
'Authorization': 'Bearer '+ dd.$toolAll.getCache('token') || ''
|
||
|
}
|
||
|
break;
|
||
|
case 'postForm':
|
||
|
methods = 'POST'
|
||
|
headers = {
|
||
|
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
||
|
'Authorization': 'Bearer '+ dd.$toolAll.getCache('token') || ''
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
return new Promise((resolve, reject) => {
|
||
|
dd.httpRequest({
|
||
|
url: config.BASE_URL+url,
|
||
|
method: methods,
|
||
|
data: options,
|
||
|
headers:headers,
|
||
|
success: (res) => {
|
||
|
if (res.status == 200) {
|
||
|
// 返回成功结果
|
||
|
resolve(res.data);
|
||
|
} else {
|
||
|
// 结果失败返回
|
||
|
reject(res);
|
||
|
}
|
||
|
},
|
||
|
fail: (err) => {
|
||
|
// 接口调取失败返回
|
||
|
reject(err);
|
||
|
},
|
||
|
complete: rest => {
|
||
|
// 不管成功与否都要返回
|
||
|
resolve(rest);
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
// 上传文件 封装请求
|
||
|
const uploadFile = (url, options) => {
|
||
|
console.log(options)
|
||
|
return new Promise((resolve, reject) => {
|
||
|
dd.uploadFile({
|
||
|
url: config.BASE_URL+url,
|
||
|
filePath: options.image,
|
||
|
name: 'image',
|
||
|
fileType:'image',
|
||
|
formData: options,
|
||
|
header: {
|
||
|
'Content-Type': 'multipart/form-data;charset=UTF-8',
|
||
|
'Authorization': 'Bearer '+ dd.$toolAll.getCache('token') || ''
|
||
|
},
|
||
|
success: res => {
|
||
|
if (res.statusCode == 200) {
|
||
|
let temp = JSON.parse(res.data)
|
||
|
if (temp.code == 0) {
|
||
|
resolve(temp)
|
||
|
} else {
|
||
|
reject(temp)
|
||
|
}
|
||
|
} else {
|
||
|
reject(res)
|
||
|
}
|
||
|
},
|
||
|
fail(e) {
|
||
|
|
||
|
},
|
||
|
complete: () => {
|
||
|
|
||
|
}
|
||
|
});
|
||
|
})
|
||
|
}
|
||
|
export default {
|
||
|
get: (url, options) => {
|
||
|
return request('get', url, options)
|
||
|
},
|
||
|
// JOSN格式
|
||
|
post: (url, options) => {
|
||
|
return request('post', url, options)
|
||
|
},
|
||
|
// form-data格式
|
||
|
postForm: (url, options) => {
|
||
|
return request('postForm', url, options)
|
||
|
},
|
||
|
// 上传
|
||
|
upload: (url, options) => {
|
||
|
return uploadFile(url, options)
|
||
|
}
|
||
|
}
|