点个赞可好~
转载请标明出处~
import Axios from 'axios'
import qs from 'qs'
import * as ramda from 'ramda'
const apiAuthDomain = 'http://www.xxxxxx.com'
const apiControlDomain = 'http://www.xxxxxxxxx.com'
/**
* 成功回调函数
* @param {*} res
* @param {*} resolve
* @param {*} reject
*/
function successCallBack(res, resolve, reject) {
if (res.data.success) {
resolve(res)
} else {
reject(res)
}
}
/**
* 异常回调函数
* @param {*} err 异常数据
* @param {*} reject
*/
function failCallBack(err, reject) {
reject(err)
}
/** Axios 基础配置 */
Axios.defaults.timeout = 5000
/**
* http request 拦截器
*/
Axios.interceptors.request.use(
config => {
let tokenName = ''
if (
!ramda.isNil(config.params['tokenName']) &&
!ramda.isEmpty(config.params['tokenName'])
) {
// console.log(config);
tokenName = config.params['tokenName']
}
if (config.params['authorization']) {
// 两个token 谁有就拿那个
let token = ''
if (localStorage.getItem(tokenName) == null) {
token = sessionStorage.getItem(tokenName)
} else {
token = localStorage.getItem(tokenName) // 读取数据;
}
config.headers['Authorization'] = token
}
delete config.params['authorization']
delete config.params['tokenName']
// config.headers["Content-Type"] = 'application/x-www-form-urlencoded';
return config
},
err => {
return Promise.reject(err)
}
)
/**
* http response 拦截器
*/
// Axios.interceptors.response.use(
// response => {
// if (response.data.errCode === "") {
// //
// }
// console.log({ "axios-res": response })
// return response;
// },
// err => {
// console.log({ "axios-err": err })
// return Promise.reject(err);
// }
// )
/**
* 处理请求链接,参数
* @param { Object } paramData 参数/数据
* @param { String } authorization Token
*/
function resolveAuthorization(paramData, authorization, tokenName) {
if (ramda.isNil(paramData)) {
paramData = {}
}
paramData.authorization = authorization
paramData.tokenName = tokenName
return paramData
}
/**
* Axios Get 请求
* @param { String } url Url
* @param { Object } paramData 参数/数据
* @param { String } authorization Token
*/
function Get(url, paramData, authorization = true, tokenName = 'token') {
return new Promise((resolve, reject) => {
Axios.get(url, {
params: resolveAuthorization(paramData, authorization, tokenName)
})
.then(res => {
successCallBack(res, resolve, reject)
})
.catch(err => {
failCallBack(err, reject)
})
})
}
/**
* Axios Post 请求
* @param { String } url Url
* @param { Object } paramData 参数/数据
* @param { String } authorization Token
*/
function Post(url, paramData, authorization = true, tokenName = 'token') {
return new Promise((resolve, reject) => {
Axios.post(url, paramData, {
params: resolveAuthorization({}, authorization, tokenName)
})
.then(res => {
successCallBack(res, resolve, reject)
})
.catch(err => {
failCallBack(err, reject)
})
})
}
/**
* Axios Put 方法
* @param { String } url Url
* @param { Object } paramData 参数/数据
* @param { String } authorization Token
*/
function Put(url, paramData, authorization = true, tokenName = 'token') {
return new Promise((resolve, reject) => {
Axios.put(url, paramData, {
params: resolveAuthorization(paramData, authorization, tokenName)
})
.then(res => {
successCallBack(res, resolve, reject)
})
.catch(err => {
failCallBack(err, reject)
})
})
}
/**
* Axios Del 方法
* @param { String } url Url
* @param { Object } paramData 参数/数据
* @param { String } authorization Token
*/
function Del(url, paramData, authorization = true, tokenName = 'token') {
return new Promise((resolve, reject) => {
Axios.delete(url, {
params: resolveAuthorization(paramData, authorization, tokenName)
})
.then(res => {
successCallBack(res, resolve, reject)
})
.catch(err => {
failCallBack(err, reject)
})
})
}
export { apiAuthDomain, apiControlDomain, Get, Put, Post, Del }
用法
/**
* @用法
* @默认展示Get用法
* @其他方法基本一致
*/
//默认用法 默认携带token
export function GteFirm(data) {
return http.Get(http.apiAuthDomain + '/api/user/orgs', data)
}
//不需要携带token
export function RsaRegister(data) {
return http.Get(http.apiAuthDomain + '/api/auth/secretkey', data, false)
}
//后台拼接的写法
export function GetEnterCode(data) {
return http.Get(http.apiAuthDomain + '/api/auth/reacquire/' + data, {})
}
//默认携带token
export function GetUserFrom(data) {
return http.Get(http.apiAuthDomain + '/api/usercenter/me', data, true)
}
//默认携带名字为EntrCodetoken的token
export function GetUserFrom(data) {
return http.Get(http.apiAuthDomain + '/api/usercenter/me', data, true, 'EntrCodetoken')
}
//请求服务地址为apiControlDomain的接口
export function GetUserFrom(data) {
return http.Get(http.apiControlDomain + '/api/usercenter/me', data, true, 'EntrCodetoken')
}
网友评论