From 32d761ec5ef854fa02b9fa0546cda5e148e4626a Mon Sep 17 00:00:00 2001 From: lampo1024 Date: Sat, 16 Feb 2019 21:15:29 +0800 Subject: [PATCH] Add request prefix options --- DncZeus.App/src/api/user.js | 6 +++++- DncZeus.App/src/config/index.js | 5 +++-- DncZeus.App/src/libs/api.request.js | 4 ++-- DncZeus.App/src/libs/axios.js | 21 +++++++++++++++++---- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/DncZeus.App/src/api/user.js b/DncZeus.App/src/api/user.js index 9620ffc..d3420ed 100644 --- a/DncZeus.App/src/api/user.js +++ b/DncZeus.App/src/api/user.js @@ -14,7 +14,11 @@ export const login = ({ export const getUserInfo = (token) => { return axios.request({ url: 'account/profile', - method: 'get' + method: 'get', + //是否在请求资源中添加资源的前缀 + withPrefix: false, //设置为true或者不设置此属性,将默认添加配置文件config.baseUrl.prefix的前缀,如果设置下面这个属性[prefix],默认配置文件中的默认前缀将不可用 + //请求资源的前缀重写 + prefix:"api/v1/" //设此属性权重最高,将覆盖配置文件[baseUrl.prefix]中的前缀,withPrefix对此属性不起作用(也就是说只要设置了此属性,都将在请求中添加设置的前缀) }) } diff --git a/DncZeus.App/src/config/index.js b/DncZeus.App/src/config/index.js index 04a0e37..8de1875 100644 --- a/DncZeus.App/src/config/index.js +++ b/DncZeus.App/src/config/index.js @@ -17,8 +17,9 @@ export default { * @description api请求基础路径 */ baseUrl: { - dev: 'http://localhost:54321/api/v1/', - pro: 'http://localhost:54321/api/v1/' + dev: 'http://localhost:54321/', + pro: 'http://localhost:54321/', + prefix:"api/v1/" }, authUrl: { dev: 'http://localhost:54321/api/oauth/auth', diff --git a/DncZeus.App/src/libs/api.request.js b/DncZeus.App/src/libs/api.request.js index 01cb87b..4949b30 100644 --- a/DncZeus.App/src/libs/api.request.js +++ b/DncZeus.App/src/libs/api.request.js @@ -1,6 +1,6 @@ import HttpRequest from '@/libs/axios' import config from '@/config' const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro - -const axios = new HttpRequest(baseUrl) +const prefix = config.baseUrl.prefix; +const axios = new HttpRequest(baseUrl,prefix) export default axios diff --git a/DncZeus.App/src/libs/axios.js b/DncZeus.App/src/libs/axios.js index 8ed210e..792cfe5 100644 --- a/DncZeus.App/src/libs/axios.js +++ b/DncZeus.App/src/libs/axios.js @@ -32,8 +32,9 @@ const addErrorLog = errorInfo => { } class HttpRequest { - constructor(baseUrl = baseURL) { + constructor(baseUrl = baseURL, prefix) { this.baseUrl = baseUrl + this.prefix = prefix this.queue = {} } getInsideConfig() { @@ -55,15 +56,15 @@ class HttpRequest { showError(error, errorInfo) { let message = "接口服务错误,请稍候再试."; - let statusCode = -1; - if(error.response&& error.response.status){ + let statusCode = -1; + if (error.response && error.response.status) { statusCode = error.response.status; } switch (statusCode) { case 401: message = "接口服务错误,原因:未授权的访问(没有权限或者登录已超时)"; break; - case 500: + case 500: message = "接口服务错误,原因:[" + error.response.statusText + "]"; break; case -1: @@ -136,6 +137,18 @@ class HttpRequest { } request(options) { const instance = axios.create() + let withPrefix = true + if (options.withPrefix !== undefined && options.withPrefix == false) { + withPrefix = false + } + let url = options.url + if (options.prefix !== undefined && options.prefix.length > 0) { + url = options.prefix + options.url + } + else if (withPrefix) { + url = this.prefix + options.url + } + options.url = url options = Object.assign(this.getInsideConfig(), options) this.interceptors(instance, options.url) return instance(options)