vue中的axios的二次封装(JS)

 axios.js

import Vue from 'vue'
import axios from 'axios'
import apiConfig from '@/config/api.config'
import { TOKEN_KEY } from '@/data/constant'
import FieldStyleUtils from '@/utils/field-style'

axios.defaults.baseURL = apiConfig.apiHost
axios.defaults.headers.common['Accept'] = 'application/json'

// request拦截器
axios.interceptors.request.use(
  config => {
    const token = Vue.ls.get(TOKEN_KEY)
    const role = Vue.ls.get('X-Role')
    if (token) {
      config.headers['X-Token'] = token // 让每个请求携带自定义 token
    }
    if (role) {
      config.headers['X-Role'] = role
    }
    config.data = FieldStyleUtils.toSnakecase(config.data)
    config.params = FieldStyleUtils.toSnakecase(config.params)
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
// response 拦截器
axios.interceptors.response.use(
  response => {
    return FieldStyleUtils.toCamelcase(response.data)
  },
  error => {
    let data
    if (error.response) {
      data = error.response.data
      if (error.response.status >= 500) {
        data = {
          code: '500',
          message: '服务器内部发生了错误'
        }
      } else if (error.response.status === 401) {
        Vue.ls.remove(TOKEN_KEY)
      }
    } else {
      data = {
        message: '网络错误'
      }
    }
    return Promise.reject(data || error)
  }
)

const setHeaderToken = (token) => {
  axios.defaults.headers.common['X-Token'] = token
}

const clearHeaderToken = () => {
  delete axios.defaults.headers.common['X-Token']
  Vue.ls.clear()
  window.location.reload()
}

export default axios

export {
  setHeaderToken,
  clearHeaderToken
}

api

import axios from '@/utils/axios'
import Base from '../base'

const path = '/technologies'

class Technology extends Base {
  constructor (attrs = {}) {
    super(attrs)
  }
  static async list (params) {
    const { technologies, count } = await axios.get(path, { params })
    const list = technologies.map(item => new Technology(item))
    return { technologies: list, count }
  }

  static async detail (id) {
    const { technology: attrs } = await axios.get(`${path}/${id}`)
    return new Technology(attrs)
  }
}

export default Technology

调用

    async fetchList () {
      this.loading = true
      try {
        this.pagination.pageNo++
        const params = {
          text: this.keyword,
          ...this.pagination
        }
        const { technologies } = await Technology.list(params)
        if (technologies.length > 0) {
          this.technologies.push(...technologies)
        } else {
          this.finished = true
          this.pagination.pageNo--
        }
      } catch ({ message = '获取成果列表失败' }) {
        this.error = true
        this.$toast.fail(message)
      } finally {
        this.loading = false
      }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值