Skip to content

Commit

Permalink
Add request prefix options
Browse files Browse the repository at this point in the history
  • Loading branch information
lampo1024 committed Feb 16, 2019
1 parent a2536d8 commit 32d761e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
6 changes: 5 additions & 1 deletion DncZeus.App/src/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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对此属性不起作用(也就是说只要设置了此属性,都将在请求中添加设置的前缀)
})
}

Expand Down
5 changes: 3 additions & 2 deletions DncZeus.App/src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions DncZeus.App/src/libs/api.request.js
Original file line number Diff line number Diff line change
@@ -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
21 changes: 17 additions & 4 deletions DncZeus.App/src/libs/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 32d761e

Please sign in to comment.