美文网首页
封装axios 多个token的情况 是否携带token的情

封装axios 多个token的情况 是否携带token的情

作者: Null丶sleep | 来源:发表于2020-09-11 16:52 被阅读0次

    点个赞可好~

    转载请标明出处~

    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')
    }
    

    相关文章

      网友评论

          本文标题:封装axios 多个token的情况 是否携带token的情

          本文链接:https://www.haomeiwen.com/subject/zqvfrktx.html