Vue+axios請(qǐng)求封裝

1、請(qǐng)求源地址配置config.base.js

export default {
    "baseURL": "http://abc.com",
    "loginURL": "http://abc.com",
    "uploadImg": "http://abc.com/image/upload",
    "batchImageUpload": "http://abc.com/product/batchImageUpload/",
    "sentry": {
        "DSN": "",
        "version": "v1.0.0"
    }
}

2、請(qǐng)求封裝request.js

import axios from "axios"
import Cookies from "js-cookie"
import config  from "../../config/config.base"

let instance = axios.create({
    headers: {'content-type': 'application/json'},
    headers: {'Authorization': Cookies.get("Authorization")}
})

instance.defaults.baseURL = config.baseURL
instance.defaults.withCredentials = true

// 添加請(qǐng)求攔截器
instance.interceptors.request.use(function (config) {
    // 在發(fā)送請(qǐng)求之前做些什么
  config.headers.channelToken = Cookies.get('channelToken')
    return config
}, function (error) {
    // 對(duì)請(qǐng)求錯(cuò)誤做些什么
    return Promise.reject(error)
})

// 添加響應(yīng)攔截器
instance.interceptors.response.use(function (response) {
    // 對(duì)響應(yīng)數(shù)據(jù)做點(diǎn)什么  
    if (response.status == "200") {
        if (response.data.code == "1001") {
            const loginUrl = Cookies.get("sysUrl")
            Cookies.remove("userName", { domain: "abc.com" })
            Cookies.remove("token", { domain: "abc.com" })
            localStorage.clear()
            window.location.href = loginUrl
        }    
    }
    return response.data
}, function (error) {
    // 對(duì)響應(yīng)錯(cuò)誤做點(diǎn)什么
    console.log('--- 401 res error response ---')
    console.log(error.response)
    if(error && error.response && error.response.status == '401') {
        const loginUrl = Cookies.get("sysUrl")
        Cookies.remove("userName", { domain: "abc.com" })
        Cookies.remove("token", { domain: "abc.com" })
        localStorage.clear()
        window.location.href = loginUrl
    }     
    return Promise.reject(error)
})

export default instance

3、axios請(qǐng)求統(tǒng)一歸類(lèi) api.js

import axios from "./request"

<1>不需要傳參

// 獲取渠道列表
export const getChannelList = () => {
    return axios({
        url: "/classification/treeList/",
        method: "post",
        data: {}
    })
}

<2>需要傳參

// 根據(jù)昵稱(chēng)和平臺(tái)搜索評(píng)論內(nèi)容
export const getSearchComment = (params) => {
    return axios({
        url: "/searchComment/",
        method: "post",
        data: params
    })
}

4、在相應(yīng)的vue文件中進(jìn)行方法調(diào)用channel.vue

<1>不需要傳參

import {getChannelList} from './api'
// 獲取渠道列表
        getChannelList().then((res) => {
            if(res.code == "201") {
                // console.log(res.body);
                this.query.channel = res.body
            }
        })

<2>需要傳參

getSearchComment() {  //自定義的方法
          let params = {
            fromPlatform: this.searchPlatform,// 平臺(tái)淶源
            nickName: this.name,// 網(wǎng)名
            pageSize: this.pagination.pageSize,// 每頁(yè)幾條數(shù)據(jù)
            pageNum: this.pagination.pageNum,// 當(dāng)前頁(yè)數(shù)
          }
          // 根據(jù)昵稱(chēng)和平臺(tái)搜索評(píng)論內(nèi)容
          getSearchComment(params).then((res) => {
              if(res.code == "201"){
                  // console.log(res.body);
                  this.tableData = res.body;
                  this.pagination.totalCount = res.totalCount
              }
          }) 
          // console.log(this.searchImgListDate)
        },
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容