1.0.1 • Published 2 years ago

apican v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

apican

apican 是一个用于 uniapp 的 http 请求拦截器。

request.js

import apican from './apican/apican.js';

apican.config.baseURL = process.env.NODE_ENV == 'development' ? 'http://test.com' : 'http://test2.com';
apican.config.withCredentials = true;
apican.config.header['Authorization'] = localStorage.getItem('token') || 'Bearer eyKhGcyOiJk...';
apican.config.header['Content-Type'] = 'application/json';
apican.config.header['ACCEPT'] = 'application/json';

//请求前拦截器
http.interceptor.request = (config) => {
    //添加通用参数
    config.header = {
        "Authorization": "Bearer eyKhGcyOiJk..."
    }
}

//响应拦截器
apican.interceptors.response= (response) => {
  if (typeof response.data !== 'object') {
    return response;
  }

  return response;
}
 
export default apican;

main.js 全局引用

import request from './common/request.js';
const app = createSSRApp(App);
app.config.globalProperties.$request = request;