Merge pull request #932 from iview/2.0

替换axios.js封装,修改api基础路径在src/config/index.js里配置
This commit is contained in:
Lison 2018-08-31 12:17:03 +08:00 committed by GitHub
commit 6e9068fb06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 59 additions and 109 deletions

View File

@ -1,4 +0,0 @@
const DEV_URL = 'https://www.easy-mock.com/mock/5add9213ce4d0e69998a6f51/iview-admin/'
const PRO_URL = 'https://produce.com'
export default process.env.NODE_ENV === 'development' ? DEV_URL : PRO_URL

View File

@ -8,5 +8,12 @@ export default {
* 如果不使用则需要在路由中给需要在菜单中展示的路由设置meta: {title: 'xxx'}
* 用来在菜单中显示文字
*/
useI18n: false
useI18n: false,
/**
* @description api请求基础路径
*/
baseUrl: {
dev: 'https://www.easy-mock.com/mock/5add9213ce4d0e69998a6f51/iview-admin/',
pro: 'https://produce.com'
}
}

View File

@ -1,3 +1,6 @@
import HttpRequest from '@/libs/axios'
const axios = new HttpRequest()
import config from '@/config'
const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro
const axios = new HttpRequest(baseUrl)
export default axios

View File

@ -1,88 +1,52 @@
import Axios from 'axios'
import baseURL from '_conf/url'
import { Message } from 'iview'
import Cookies from 'js-cookie'
import { TOKEN_KEY } from '@/libs/util'
class httpRequest {
constructor () {
this.options = {
method: '',
url: ''
}
// 存储请求队列
import axios from 'axios'
// import { Spin } from 'iview'
class HttpRequest {
constructor (baseUrl = baseURL) {
this.baseUrl = baseUrl
this.queue = {}
}
// 销毁请求实例
destroy (url) {
delete this.queue[url]
const queue = Object.keys(this.queue)
return queue.length
}
// 请求拦截
interceptors (instance, url) {
// 添加请求拦截器
instance.interceptors.request.use(config => {
if (!config.url.includes('/users')) {
config.headers['x-access-token'] = Cookies.get(TOKEN_KEY)
}
// Spin.show()
// 在发送请求之前做些什么
return config
}, error => {
// 对请求错误做些什么
return Promise.reject(error)
})
// 添加响应拦截器
instance.interceptors.response.use((res) => {
let { data } = res
const is = this.destroy(url)
if (!is) {
setTimeout(() => {
// Spin.hide()
}, 500)
}
if (data.code !== 200) {
// 后端服务在个别情况下回报201待确认
if (data.code === 401) {
Cookies.remove(TOKEN_KEY)
window.location.href = window.location.pathname + '#/login'
Message.error('未登录,或登录失效,请登录')
} else {
if (data.msg) Message.error(data.msg)
}
return false
}
return data
}, (error) => {
Message.error('服务内部错误')
// 对响应错误做点什么
return Promise.reject(error)
})
}
// 创建实例
create () {
let conf = {
baseURL: baseURL,
// timeout: 2000,
getInsideConfig () {
const config = {
baseURL: this.baseUrl,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'X-URL-PATH': location.pathname
//
}
}
return Axios.create(conf)
return config
}
// 合并请求实例
mergeReqest (instances = []) {
//
distroy (url) {
delete this.queue[url]
if (!Object.keys(this.queue).length) {
// Spin.hide()
}
}
interceptors (instance, url) {
// 请求拦截
instance.interceptors.request.use(config => {
// 添加全局的loading...
if (!Object.keys(this.queue).length) {
// Spin.show() // 不建议开启,因为界面不友好
}
this.queue[url] = true
return config
}, error => {
return Promise.reject(error)
})
// 响应拦截
instance.interceptors.response.use(res => {
this.distroy(url)
const { data, status } = res
return { data, status }
}, error => {
this.distroy(url)
return Promise.reject(error)
})
}
// 请求实例
request (options) {
var instance = this.create()
const instance = axios.create()
options = Object.assign(this.getInsideConfig(), options)
this.interceptors(instance, options.url)
options = Object.assign({}, options)
this.queue[options.url] = instance
return instance(options)
}
}
export default httpRequest
export default HttpRequest

View File

@ -38,8 +38,8 @@ export const getUnion = (arr1, arr2) => {
* @param {Array} arr 需要查询的数组
* @description 判断要查询的数组是否至少有一个元素包含在目标数组中
*/
export const hasOneOf = (target, arr) => {
return target.some(_ => arr.indexOf(_) > -1)
export const hasOneOf = (targetarr, arr) => {
return targetarr.some(_ => arr.indexOf(_) > -1)
}
/**

View File

@ -11,11 +11,7 @@ export const getTableData = req => {
createTime: '@date'
}))
})
return {
code: 200,
data: tableData,
msg: ''
}
return tableData
}
export const getDragList = req => {
@ -26,9 +22,5 @@ export const getDragList = req => {
id: Random.increment(10)
}))
})
return {
code: 200,
data: dragList,
msg: ''
}
return dragList
}

View File

@ -18,26 +18,14 @@ const USER_MAP = {
export const login = req => {
req = JSON.parse(req.body)
return {
code: 200,
data: {token: USER_MAP[req.userName].token},
msg: ''
}
return {token: USER_MAP[req.userName].token}
}
export const getUserInfo = req => {
const params = getParams(req.url)
return {
code: 200,
data: USER_MAP[params.token],
msg: ''
}
return USER_MAP[params.token]
}
export const logout = req => {
return {
code: 200,
data: null,
msg: ''
}
return null
}