url拼接参数

将后台返回的数据对象拼接到url地址上

// 拆分数据
splitData: function (data) {
    let url = ''
    for (let k in data) {
        if (data.hasOwnProperty(k)) {
            let value = data[k] || ''
            url = url + '&' + k + '=' + encodeURIComponent(value)
        }
    }
    return url ? url.substring(1) : ''
},

// 拼接参数
contractUrl: function (url, data) {
    return url + (url.indexOf('?') < 0 ? '?' : '&') + this.splitData(data)
},

后台返回的数据

let url = "https://ds.capcn.com.cn/facevi"
let data = {
    institutionID: "100200",
    returnImage: "00",
    frontUrl: "http://22.202.250.78/api/face-formt/face-info",
    backUrl: "http://22.202.100.12/api/face-recognition/face-info",
    remark: "00125",
    frontMethod: "0",
}

拼接后的地址

https://ds.capcn.com.cn/facevi?institutionID=100200&returnImage=00&frontUrl=http%3A%2F%2F22.202.250.78%2Fapi%2Fface-formt%2Fface-info&backUrl=http%3A%2F%2F22.202.100.12%2Fapi%2Fface-recognition%2Fface-info&remark=00125&frontMethod=0

备注

post方法

/**
 *  导出excel文件
 * @param url 下载地址
 * @param data 参数
 * @param name 下载文件名
 */
export const $export2 = (url, data, name) => {
    let opt = {
        url: url,
        method: 'post',
        responseType: 'blob',
        data: data
    }
    axios.request(opt).then(res => {
        const BLOB = res.data
        const fileReader = new FileReader()
        fileReader.readAsDataURL(BLOB)
        fileReader.onload = (event) => {
            let a = document.createElement('a')
            a.download = `${name}.xls`
            a.href = event.target.result
            document.body.appendChild(a)
            a.click()
            document.body.removeChild(a)
        }
    })
}

get方法

/**
 *  导出excel文件 get方法拼接地址
 * @param url 下载地址
 * @param data 参数
 * @param name 下载文件名
 */
export const $export = (url, data, name) => {
    let exportIn = {
        splitData: function (data) {
            let url = ''
            for (let k in data) {
                if (data.hasOwnProperty(k)) {
                    let value = data[k] || ''
                    url = url + '&' + k + '=' + encodeURIComponent(value)
                }
            }
            return url ? url.substring(1) : ''
        },
        contractUrl: function (url, data) {
            return url + (url.indexOf('?') < 0 ? '?' : '&') + exportIn.splitData(data)
        },
    }
    let finalUrl = exportIn.contractUrl(url, data)
    window.open(finalUrl)
}
posted @ 2020-02-16 15:13  小方块的世界  阅读(3936)  评论(0编辑  收藏  举报