diff --git a/config/url.js b/config/url.js deleted file mode 100644 index 581de634..00000000 --- a/config/url.js +++ /dev/null @@ -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 diff --git a/src/config/index.js b/src/config/index.js index b7996133..3b395357 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -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' + } } diff --git a/src/libs/api.request.js b/src/libs/api.request.js index 0c8e4c37..01cb87b4 100644 --- a/src/libs/api.request.js +++ b/src/libs/api.request.js @@ -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 diff --git a/src/libs/axios.js b/src/libs/axios.js index c518a398..103d5e57 100644 --- a/src/libs/axios.js +++ b/src/libs/axios.js @@ -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 diff --git a/src/libs/tools.js b/src/libs/tools.js index 3fcd79c2..0ca81d61 100644 --- a/src/libs/tools.js +++ b/src/libs/tools.js @@ -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) } /** diff --git a/src/mock/data.js b/src/mock/data.js index 462624d7..517daa02 100644 --- a/src/mock/data.js +++ b/src/mock/data.js @@ -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 } diff --git a/src/mock/login.js b/src/mock/login.js index 45713dec..9e84a551 100644 --- a/src/mock/login.js +++ b/src/mock/login.js @@ -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 }