Created
November 30, 2017 19:08
-
-
Save WunGCQ/126260372a1243bed7cb2d9ef8dbf495 to your computer and use it in GitHub Desktop.
my axios
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Axios from 'axios'; | |
import { | |
isArray | |
} from 'lodash'; | |
import qs from 'qs'; | |
const CGI_SERVER = process.env.CGI_SERVER; | |
export const cgiServerPath = () => CGI_SERVER; | |
const postConfig = { | |
method: 'POST', | |
headers: { | |
Accept: 'application/json, application/xml, text/plain, text/html, *.*', | |
'Accept-Language': 'zh-CN', | |
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', | |
}, | |
paramsSerializer(params) { | |
return qs.stringify(params, { | |
arrayFormat: 'brackets' | |
}); | |
}, | |
}; | |
const getConfig = { | |
method: 'GET', | |
headers: { | |
Accept: 'application/json, application/xml, text/plain, text/html, *.*', | |
'Accept-Language': 'zh-CN', | |
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', | |
}, | |
}; | |
Axios.defaults.withCredentials = true; | |
function cpy(data) { | |
const d = {}; | |
if (!data) { | |
return d; | |
} | |
for (const i in data) { | |
const temp = data[i]; | |
if (temp === null || temp === void(0) || temp === '') {} else { | |
d[i] = data[i]; | |
} | |
} | |
return d; | |
} | |
export async function post(url, postData) { | |
try { | |
const response = await Axios(url, { | |
...postConfig, | |
data: qs.stringify(cpy(postData), { | |
arrayFormat: 'brackets' | |
}), | |
}); | |
return { ...response.data, | |
ok: (response.data.code == 0) | |
}; | |
} catch (e) { | |
showErrorMessage(e); | |
return e && e.response && (e.response.data || e.response) || { | |
error: e | |
}; | |
} | |
} | |
export async function uploadPost(url, postData, onProgress) { | |
try { | |
const data = new FormData(); | |
for (let i in postData) { | |
data.append(i, postData[i]); | |
} | |
const response = await Axios(CGI_SERVER + url, { | |
method: 'POST', | |
headers: { | |
Accept: 'application/json, application/xml, text/plain, text/html, *.*', | |
'Accept-Language': 'zh-CN', | |
'Content-Type': 'multipart/form-data' | |
}, | |
data, | |
onUploadProgress: function (progressEvent) { | |
const progress = progressEvent.loaded / progressEvent.total; | |
onProgress && onProgress(progress); | |
}, | |
}); | |
return { ...response.data, | |
ok: (response.data.status >= 200 && response.data.status < 300) | |
}; | |
} catch (e) { | |
showErrorMessage(e); | |
return e && e.response && (e.response.data || e.response) || { | |
error: e | |
}; | |
} | |
} | |
export function uploadProgressingPost(url, postData, onProgress) { | |
const data = new FormData(); | |
for (let i in postData) { | |
data.append(i, postData[i]); | |
} | |
return Axios(CGI_SERVER + url, { | |
method: 'POST', | |
headers: { | |
Accept: 'application/json, application/xml, text/plain, text/html, *.*', | |
'Accept-Language': 'zh-CN', | |
'Content-Type': 'multipart/form-data' | |
}, | |
data, | |
onUploadProgress: function (progressEvent) { | |
var progress = progressEvent.loaded / progressEvent.total; | |
onProgress(progress); | |
}, | |
}).catch((e) => { | |
showErrorMessage(e); | |
}); | |
} | |
export async function uploadPut(url, postData, onProgress) { | |
try { | |
const data = new FormData(); | |
for (let i in postData) { | |
data.append(i, postData[i]); | |
} | |
const response = await Axios(CGI_SERVER + url, { | |
method: 'PUT', | |
headers: { | |
Accept: 'application/json, application/xml, text/plain, text/html, *.*', | |
'Accept-Language': 'zh-CN', | |
'Content-Type': 'multipart/form-data' | |
}, | |
data, | |
onUploadProgress: function (progressEvent) { | |
var progress = progressEvent.loaded / progressEvent.total; | |
onProgress && onProgress(progress); | |
}, | |
}); | |
return { ...response.data, | |
ok: (response.data.status >= 200 && response.data.status < 300) | |
}; | |
} catch (e) { | |
showErrorMessage(e); | |
return e && e.response && (e.response.data || e.response) || { | |
error: e | |
}; | |
} | |
} | |
export async function post_json(url, postData) { | |
try { | |
const response = await Axios(CGI_SERVER + url, { | |
method: 'POST', | |
headers: { | |
Accept: 'application/json, application/xml, text/plain, text/html, *.*', | |
'Accept-Language': 'zh-CN', | |
'Content-Type': 'application/json; charset=utf-8', | |
}, | |
paramsSerializer(params) { | |
return JSON.stringify(params); | |
}, | |
data: JSON.stringify(cpy(postData)), | |
}); | |
return { ...response.data, | |
ok: (response.data.status >= 200 && response.data.status < 400) | |
}; | |
} catch (e) { | |
showErrorMessage(e); | |
return e && e.response && (e.response.data || e.response) || { | |
error: e | |
}; | |
} | |
} | |
export async function put_json(url, postData) { | |
try { | |
const response = await Axios(CGI_SERVER + url, { | |
method: 'PUT', | |
headers: { | |
Accept: 'application/json, application/xml, text/plain, text/html, *.*', | |
'Accept-Language': 'zh-CN', | |
'Content-Type': 'application/json; charset=utf-8', | |
}, | |
paramsSerializer(params) { | |
return JSON.stringify(params); | |
}, | |
data: JSON.stringify(postData), | |
}); | |
return { ...response.data, | |
ok: (response.data.status >= 200 && response.data.status < 400) | |
}; | |
} catch (e) { | |
showErrorMessage(e); | |
return e && e.response && (e.response.data || e.response) || { | |
error: e | |
}; | |
} | |
} | |
export async function del(url, postData) { | |
try { | |
const response = await Axios(CGI_SERVER + url, { | |
...postConfig, | |
method: 'DELETE', | |
data: qs.stringify(cpy(postData), { | |
arrayFormat: 'brackets' | |
}), | |
}); | |
return { ...response.data, | |
ok: (response.data.status >= 200 && response.data.status < 400) | |
}; | |
} catch (e) { | |
showErrorMessage(e); | |
return e && e.response && (e.response.data || e.response) || { | |
error: e | |
}; | |
} | |
} | |
export async function get(url, params) { | |
try { | |
const response = await Axios(url, { ...getConfig, | |
params: cpy({...params,timestamp:Date.now()}) | |
}); | |
return { ...response.data, | |
ok: (response.data.code == 0) | |
}; | |
} catch (e) { | |
showErrorMessage(e); | |
return e && e.response && (e.response.data || e.response) || e; | |
} | |
} | |
export async function put(url, postData) { | |
try { | |
const response = await Axios(CGI_SERVER + url, { | |
...postConfig, | |
method: 'PUT', | |
data: qs.stringify(cpy(postData), { | |
arrayFormat: 'brackets' | |
}), | |
}); | |
return { ...response.data, | |
ok: (response.data.status >= 200 && response.data.status < 300) | |
}; | |
} catch (e) { | |
showErrorMessage(e); | |
return e && e.response && (e.response.data || e.response) || { | |
error: e | |
}; | |
} | |
} | |
export function showErrorMessage(err) { | |
if (err.response) { | |
try { | |
const { | |
data: { | |
message: msg, | |
reason, | |
status | |
}, | |
status: requestStatus | |
} = err.response; | |
if (requestStatus === 404) { | |
} else if (requestStatus === 401) { | |
window.location.href = '/'; | |
} else if (requestStatus === 403) { | |
Message.error('没有权限'); | |
} else { | |
Message.error(reason, 5); | |
} | |
} catch (e) { | |
Message.error(err.message); | |
} | |
} else { | |
Message.error(err && err.message || err); | |
} | |
} | |
const jsonRes = (res) => { | |
res.json = function () { | |
return res.data; | |
}; | |
return res; | |
}; | |
export function pGet(url, params) { | |
return Axios(CGI_SERVER + url, { ...getConfig, | |
params: cpy(params) | |
}).then(jsonRes); | |
} | |
export function pPost(url, params) { | |
return Axios(CGI_SERVER + url, { ...postConfig, | |
params: cpy(params) | |
}).then(jsonRes); | |
} | |
export function pPut(url, params) { | |
return Axios(CGI_SERVER + url, { ...postConfig, | |
method: 'PUT', | |
params: cpy(params) | |
}) | |
.then(jsonRes); | |
} | |
export function pDel(url, params) { | |
return Axios(CGI_SERVER + url, { ...postConfig, | |
method: 'DELETE', | |
params: cpy(params) | |
}) | |
.then(jsonRes); | |
} | |
export function jsonp(url, params) { | |
// thefair | |
// response :{code:0,message: { text: '', action:''},result} | |
return new Promise((resolve, reject) => { | |
const script = document.createElement('script'); | |
const name = `temp${Math.floor(Math.random() * 10000)}${Date.now()}`; | |
const temp = function (data) { | |
if (typeof data == 'string' || typeof data == 'number') { | |
resolve({ | |
ok: false, | |
result: data | |
}); | |
} else { | |
if (data.code != 0) { | |
weui.toast(data.message.text,1000); | |
} | |
resolve({ | |
ok: data.code == 0, | |
message: data.message, | |
result: data.result, | |
}); | |
} | |
document.body.removeChild(script); | |
}; | |
window[name] = temp; | |
const str = qs.stringify(params, { | |
arrayFormat: 'brackets' | |
}); | |
script.src = `${url}?__callback=${name}&${str}`; | |
document.body.appendChild(script); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment