You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.1 KiB
88 lines
2.1 KiB
4 years ago
|
/*---------------------------------------------------------------
|
||
|
| 请求接口封装 |
|
||
|
---------------------------------------------------------------*/
|
||
|
import axios from 'axios'
|
||
|
import log from 'dai-js/modules/log'
|
||
|
import curry from 'dai-js/tools/curry'
|
||
|
|
||
|
const request = curry(
|
||
|
(method, url, data = {}, headers = {}, progress = () => {}) => {
|
||
|
return new Promise((reslove) => {
|
||
|
let returnIniData = {
|
||
|
httpCode: '',
|
||
|
data: {},
|
||
|
msg: '',
|
||
|
code: ''
|
||
|
}
|
||
|
|
||
|
// 添加服务器端URL
|
||
|
function processUrl(url) {
|
||
|
if (url.indexOf('http://') > -1 || url.indexOf('https://') > -1) {
|
||
|
return url
|
||
|
}
|
||
|
return process.env.VUE_APP_API_SERVER + url
|
||
|
}
|
||
|
|
||
|
url = processUrl(url)
|
||
|
|
||
|
const succFn = (res) => {
|
||
|
// log(`[request成功] ${url}`, data, res);
|
||
|
let retData = {
|
||
|
...returnIniData,
|
||
|
...res.data,
|
||
|
httpCode: res.statusCode
|
||
|
}
|
||
|
// if(typeof Vue.$afterRequestHook == 'function'){
|
||
|
// retData = Vue.$afterRequestHook(retData);
|
||
|
// }
|
||
|
|
||
|
reslove(retData)
|
||
|
}
|
||
|
|
||
|
const failFn = (err) => {
|
||
|
// log(`[request失败] ${url}`, data, err);
|
||
|
|
||
|
reslove(
|
||
|
Object.assign({}, returnIniData, {
|
||
|
httpCode: '9999', //访问出现意外
|
||
|
msg: '网络错误'
|
||
|
})
|
||
|
)
|
||
|
}
|
||
|
|
||
|
if (method.toUpperCase() == 'POST') {
|
||
|
axios
|
||
|
.post(url, data, {
|
||
|
headers,
|
||
|
responseType: 'json'
|
||
|
// progress,
|
||
|
// credentials: false,
|
||
|
})
|
||
|
.then(succFn)
|
||
|
.catch(failFn)
|
||
|
} else {
|
||
|
axios
|
||
|
.get(url, {
|
||
|
params: data,
|
||
|
headers,
|
||
|
responseType: 'json'
|
||
|
// credentials: true,
|
||
|
})
|
||
|
.then(succFn)
|
||
|
.catch(failFn)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
)
|
||
|
|
||
|
export const requestGet = request('get')
|
||
|
|
||
|
export const requestPost = request('post')
|
||
|
|
||
|
export default {
|
||
|
install(Vue) {
|
||
|
Vue.prototype.$requestGet = requestGet
|
||
|
Vue.prototype.$requestPost = requestPost
|
||
|
}
|
||
|
}
|