no message
Former-commit-id: 487e22a0c9bf5da4474f9449ce95070e9a4ed727
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
import store from '@/store'
|
||||
import axios from 'axios'
|
||||
import { Message } from 'element-ui'
|
||||
import util from '@/libs/util'
|
||||
|
||||
// 创建一个错误
|
||||
function errorCreat (msg) {
|
||||
const err = new Error(msg)
|
||||
errorLog(err)
|
||||
throw err
|
||||
}
|
||||
|
||||
// 记录和显示错误
|
||||
function errorLog (err) {
|
||||
// 添加到日志
|
||||
store.dispatch('d2admin/log/add', {
|
||||
type: 'error',
|
||||
err,
|
||||
info: '数据请求异常'
|
||||
})
|
||||
// 打印到控制台
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
util.log.danger('>>>>>> Error >>>>>>')
|
||||
console.log(err)
|
||||
}
|
||||
// 显示提示
|
||||
Message({
|
||||
message: err.message,
|
||||
type: 'error',
|
||||
duration: 5 * 1000
|
||||
})
|
||||
}
|
||||
|
||||
// 创建一个 axios 实例
|
||||
const service = axios.create({
|
||||
baseURL: process.env.VUE_APP_API,
|
||||
timeout: 5000 // 请求超时时间
|
||||
})
|
||||
|
||||
// 请求拦截器
|
||||
service.interceptors.request.use(
|
||||
config => {
|
||||
// 在请求发送之前做一些处理
|
||||
const token = util.cookies.get('token')
|
||||
// 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
|
||||
config.headers['X-Token'] = token
|
||||
return config
|
||||
},
|
||||
error => {
|
||||
// 发送失败
|
||||
console.log(error)
|
||||
Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// 响应拦截器
|
||||
service.interceptors.response.use(
|
||||
response => {
|
||||
// dataAxios 是 axios 返回数据中的 data
|
||||
const dataAxios = response.data
|
||||
// 这个状态码是和后端约定的
|
||||
const { code } = dataAxios
|
||||
// 根据 code 进行判断
|
||||
if (code === undefined) {
|
||||
// 如果没有 code 代表这不是项目后端开发的接口 比如可能是 D2Admin 请求最新版本
|
||||
return dataAxios
|
||||
} else {
|
||||
// 有 code 代表这是一个后端接口 可以进行进一步的判断
|
||||
switch (code) {
|
||||
case 0:
|
||||
// [ 示例 ] code === 0 代表没有错误
|
||||
return dataAxios.data
|
||||
case 'xxx':
|
||||
// [ 示例 ] 其它和后台约定的 code
|
||||
errorCreat(`[ code: xxx ] ${dataAxios.msg}: ${response.config.url}`)
|
||||
break
|
||||
default:
|
||||
// 不是正确的 code
|
||||
errorCreat(`${dataAxios.msg}: ${response.config.url}`)
|
||||
break
|
||||
}
|
||||
}
|
||||
},
|
||||
error => {
|
||||
if (error && error.response) {
|
||||
switch (error.response.status) {
|
||||
case 400: error.message = '请求错误'; break
|
||||
case 401: error.message = '未授权,请登录'; break
|
||||
case 403: error.message = '拒绝访问'; break
|
||||
case 404: error.message = `请求地址出错: ${error.response.config.url}`; break
|
||||
case 408: error.message = '请求超时'; break
|
||||
case 500: error.message = '服务器内部错误'; break
|
||||
case 501: error.message = '服务未实现'; break
|
||||
case 502: error.message = '网关错误'; break
|
||||
case 503: error.message = '服务不可用'; break
|
||||
case 504: error.message = '网关超时'; break
|
||||
case 505: error.message = 'HTTP版本不受支持'; break
|
||||
default: break
|
||||
}
|
||||
}
|
||||
errorLog(error)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
export default service
|
||||
@@ -0,0 +1,39 @@
|
||||
// Element
|
||||
import ElementUI from 'element-ui'
|
||||
import 'element-ui/lib/theme-chalk/index.css'
|
||||
// flex 布局库
|
||||
import 'flex.css'
|
||||
// 组件
|
||||
import '@/components'
|
||||
// svg 图标
|
||||
import '@/assets/svg-icons'
|
||||
// 过滤器
|
||||
import d2VueFiltersDate from '@d2-projects/vue-filters-date'
|
||||
// 功能插件
|
||||
import pluginError from '@/plugin/error'
|
||||
import pluginLog from '@/plugin/log'
|
||||
import pluginOpen from '@/plugin/open'
|
||||
|
||||
export default {
|
||||
async install (Vue, options) {
|
||||
// 设置为 false 以阻止 vue 在启动时生成生产提示
|
||||
// https://cn.vuejs.org/v2/api/#productionTip
|
||||
Vue.config.productionTip = false
|
||||
// 当前环境
|
||||
Vue.prototype.$env = process.env.NODE_ENV
|
||||
// 当前的 baseUrl
|
||||
Vue.prototype.$baseUrl = process.env.BASE_URL
|
||||
// 当前版本
|
||||
Vue.prototype.$version = process.env.VUE_APP_VERSION
|
||||
// 构建时间
|
||||
Vue.prototype.$buildTime = process.env.VUE_APP_BUILD_TIME
|
||||
// Element
|
||||
Vue.use(ElementUI)
|
||||
// 过滤器 日期模块
|
||||
Vue.use(d2VueFiltersDate)
|
||||
// 插件
|
||||
Vue.use(pluginError)
|
||||
Vue.use(pluginLog)
|
||||
Vue.use(pluginOpen)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import store from '@/store'
|
||||
import util from '@/libs/util'
|
||||
|
||||
export default {
|
||||
install (Vue, options) {
|
||||
Vue.config.errorHandler = function (err, vm, info) {
|
||||
Vue.nextTick(() => {
|
||||
// 添加 log
|
||||
store.dispatch('d2admin/log/add', {
|
||||
type: 'error',
|
||||
err,
|
||||
vm,
|
||||
info
|
||||
})
|
||||
// 只在开发模式下打印 log
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
util.log.capsule('D2Admin', 'ErrorHandler', 'danger')
|
||||
util.log.danger('>>>>>> 错误信息 >>>>>>')
|
||||
console.log(info)
|
||||
util.log.danger('>>>>>> Vue 实例 >>>>>>')
|
||||
console.log(vm)
|
||||
util.log.danger('>>>>>> Error >>>>>>')
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import store from '@/store'
|
||||
import util from '@/libs/util'
|
||||
|
||||
export default {
|
||||
install (Vue, options) {
|
||||
// 快速打印 log
|
||||
Vue.prototype.$log = util.log
|
||||
// 快速记录日志
|
||||
Vue.prototype.$logAdd = function (info, show = true) {
|
||||
// store 赋值
|
||||
store.dispatch('d2admin/log/add', {
|
||||
type: 'log',
|
||||
info
|
||||
})
|
||||
// 显示在控制台
|
||||
if (show && process.env.NODE_ENV === 'development') {
|
||||
util.log.default(info)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import util from '@/libs/util'
|
||||
|
||||
export default {
|
||||
install (Vue, options) {
|
||||
Vue.prototype.$open = util.open
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user