init
This commit is contained in:
59
src/utils/accessToken.js
Normal file
59
src/utils/accessToken.js
Normal file
@@ -0,0 +1,59 @@
|
||||
import { storage, tokenTableName } from '@/config'
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 获取accessToken
|
||||
* @returns {string|ActiveX.IXMLDOMNode|Promise<any>|any|IDBRequest<any>|MediaKeyStatus|FormDataEntryValue|Function|Promise<Credential | null>}
|
||||
*/
|
||||
export function getAccessToken() {
|
||||
if (storage) {
|
||||
if ('localStorage' === storage) {
|
||||
return localStorage.getItem(tokenTableName)
|
||||
} else if ('sessionStorage' === storage) {
|
||||
return sessionStorage.getItem(tokenTableName)
|
||||
} else {
|
||||
return localStorage.getItem(tokenTableName)
|
||||
}
|
||||
} else {
|
||||
return localStorage.getItem(tokenTableName)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 存储accessToken
|
||||
* @param accessToken
|
||||
* @returns {void|*}
|
||||
*/
|
||||
export function setAccessToken(accessToken) {
|
||||
if (storage) {
|
||||
if ('localStorage' === storage) {
|
||||
return localStorage.setItem(tokenTableName, accessToken)
|
||||
} else if ('sessionStorage' === storage) {
|
||||
return sessionStorage.setItem(tokenTableName, accessToken)
|
||||
} else {
|
||||
return localStorage.setItem(tokenTableName, accessToken)
|
||||
}
|
||||
} else {
|
||||
return localStorage.setItem(tokenTableName, accessToken)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 移除accessToken
|
||||
* @returns {void|Promise<void>}
|
||||
*/
|
||||
export function removeAccessToken() {
|
||||
if (storage) {
|
||||
if ('localStorage' === storage) {
|
||||
return localStorage.removeItem(tokenTableName)
|
||||
} else if ('sessionStorage' === storage) {
|
||||
return sessionStorage.clear()
|
||||
} else {
|
||||
return localStorage.removeItem(tokenTableName)
|
||||
}
|
||||
} else {
|
||||
return localStorage.removeItem(tokenTableName)
|
||||
}
|
||||
}
|
||||
31
src/utils/clipboard.js
Normal file
31
src/utils/clipboard.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import Vue from 'vue'
|
||||
import Clipboard from 'clipboard'
|
||||
|
||||
function clipboardSuccess() {
|
||||
Vue.prototype.$baseMessage('复制成功', 'success')
|
||||
}
|
||||
|
||||
function clipboardError() {
|
||||
Vue.prototype.$baseMessage('复制失败', 'error')
|
||||
}
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 复制数据
|
||||
* @param text
|
||||
* @param event
|
||||
*/
|
||||
export default function handleClipboard(text, event) {
|
||||
const clipboard = new Clipboard(event.target, {
|
||||
text: () => text,
|
||||
})
|
||||
clipboard.on('success', () => {
|
||||
clipboardSuccess()
|
||||
clipboard.destroy()
|
||||
})
|
||||
clipboard.on('error', () => {
|
||||
clipboardError()
|
||||
clipboard.destroy()
|
||||
})
|
||||
clipboard.onClick(event)
|
||||
}
|
||||
42
src/utils/encrypt.js
Normal file
42
src/utils/encrypt.js
Normal file
@@ -0,0 +1,42 @@
|
||||
import JSEncrypt from 'jsencrypt'
|
||||
import { getPublicKey } from '@/api/publicKey'
|
||||
|
||||
const privateKey =
|
||||
'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMFPa+v52FkSUXvcUnrGI/XzW3EpZRI0s9BCWJ3oNQmEYA5luWW5p8h0uadTIoTyYweFPdH4hveyxlwmS7oefvbIdiP+o+QIYW/R4Wjsb4Yl8MhR4PJqUE3RCy6IT9fM8ckG4kN9ECs6Ja8fQFc6/mSl5dJczzJO3k1rWMBhKJD/AgMBAAECgYEAucMakH9dWeryhrYoRHcXo4giPVJsH9ypVt4KzmOQY/7jV7KFQK3x//27UoHfUCak51sxFw9ek7UmTPM4HjikA9LkYeE7S381b4QRvFuf3L6IbMP3ywJnJ8pPr2l5SqQ00W+oKv+w/VmEsyUHr+k4Z+4ik+FheTkVWp566WbqFsECQQDjYaMcaKw3j2Zecl8T6eUe7fdaRMIzp/gcpPMfT/9rDzIQk+7ORvm1NI9AUmFv/FAlfpuAMrdL2n7p9uznWb7RAkEA2aP934kbXg5bdV0R313MrL+7WTK/qdcYxATUbMsMuWWQBoS5irrt80WCZbG48hpocJavLNjbtrjmUX3CuJBmzwJAOJg8uP10n/+ZQzjEYXh+BszEHDuw+pp8LuT/fnOy5zrJA0dO0RjpXijO3vuiNPVgHXT9z1LQPJkNrb5ACPVVgQJBALPeb4uV0bNrJDUb5RB4ghZnIxv18CcaqNIft7vuGCcFBAIPIRTBprR+RuVq+xHDt3sNXdsvom4h49+Hky1b0ksCQBBwUtVaqH6ztCtwUF1j2c/Zcrt5P/uN7IHAd44K0gIJc1+Csr3qPG+G2yoqRM8KVqLI8Z2ZYn9c+AvEE+L9OQY='
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description RSA加密
|
||||
* @param data
|
||||
* @returns {Promise<{param: PromiseLike<ArrayBuffer>}|*>}
|
||||
*/
|
||||
export async function encryptedData(data) {
|
||||
let publicKey = ''
|
||||
const res = await getPublicKey()
|
||||
publicKey = res.data.publicKey
|
||||
if (res.data.mockServer) {
|
||||
publicKey = ''
|
||||
}
|
||||
if (publicKey == '') {
|
||||
return data
|
||||
}
|
||||
const encrypt = new JSEncrypt()
|
||||
encrypt.setPublicKey(`-----BEGIN PUBLIC KEY-----${publicKey}-----END PUBLIC KEY-----`)
|
||||
data = encrypt.encrypt(JSON.stringify(data))
|
||||
return {
|
||||
param: data,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description RSA解密
|
||||
* @param data
|
||||
* @returns {PromiseLike<ArrayBuffer>}
|
||||
*/
|
||||
export function decryptedData(data) {
|
||||
const decrypt = new JSEncrypt()
|
||||
decrypt.setPrivateKey(`-----BEGIN RSA PRIVATE KEY-----${privateKey}-----END RSA PRIVATE KEY-----`)
|
||||
data = decrypt.decrypt(JSON.stringify(data))
|
||||
return data
|
||||
}
|
||||
25
src/utils/errorLog.js
Normal file
25
src/utils/errorLog.js
Normal file
@@ -0,0 +1,25 @@
|
||||
import Vue from 'vue'
|
||||
import store from '@/store'
|
||||
import { isArray, isString } from '@/utils/validate'
|
||||
import { errorLog } from '@/config'
|
||||
|
||||
const needErrorLog = errorLog
|
||||
const checkNeed = () => {
|
||||
const env = process.env.NODE_ENV
|
||||
if (isString(needErrorLog)) {
|
||||
return env === needErrorLog
|
||||
}
|
||||
if (isArray(needErrorLog)) {
|
||||
return needErrorLog.includes(env)
|
||||
}
|
||||
return false
|
||||
}
|
||||
if (checkNeed()) {
|
||||
Vue.config.errorHandler = (err, vm, info) => {
|
||||
console.error('vue-admin-beautiful错误拦截:', err, vm, info)
|
||||
const url = window.location.href
|
||||
Vue.nextTick(() => {
|
||||
store.dispatch('errorLog/addErrorLog', { err, vm, info, url })
|
||||
})
|
||||
}
|
||||
}
|
||||
60
src/utils/handleRoutes.js
Normal file
60
src/utils/handleRoutes.js
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description all模式渲染后端返回路由
|
||||
* @param constantRoutes
|
||||
* @returns {*}
|
||||
*/
|
||||
export function convertRouter(asyncRoutes) {
|
||||
return asyncRoutes.map((route) => {
|
||||
if (route.component) {
|
||||
if (route.component === 'Layout') {
|
||||
route.component = (resolve) => require(['@/layouts'], resolve)
|
||||
} else if (route.component === 'EmptyLayout') {
|
||||
route.component = (resolve) => require(['@/layouts/EmptyLayout'], resolve)
|
||||
} else {
|
||||
const index = route.component.indexOf('views')
|
||||
const path = index > 0 ? route.component.slice(index) : `views/${route.component}`
|
||||
route.component = (resolve) => require([`@/${path}`], resolve)
|
||||
}
|
||||
}
|
||||
if (route.children && route.children.length) route.children = convertRouter(route.children)
|
||||
if (route.children && route.children.length === 0) delete route.children
|
||||
return route
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 判断当前路由是否包含权限
|
||||
* @param permissions
|
||||
* @param route
|
||||
* @returns {boolean|*}
|
||||
*/
|
||||
function hasPermission(permissions, route) {
|
||||
if (route.meta && route.meta.permissions) {
|
||||
return permissions.some((role) => route.meta.permissions.includes(role))
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description intelligence模式根据permissions数组拦截路由
|
||||
* @param routes
|
||||
* @param permissions
|
||||
* @returns {[]}
|
||||
*/
|
||||
export function filterAsyncRoutes(routes, permissions) {
|
||||
const finallyRoutes = []
|
||||
routes.forEach((route) => {
|
||||
const item = { ...route }
|
||||
if (hasPermission(permissions, item)) {
|
||||
if (item.children) {
|
||||
item.children = filterAsyncRoutes(item.children, permissions)
|
||||
}
|
||||
finallyRoutes.push(item)
|
||||
}
|
||||
})
|
||||
return finallyRoutes
|
||||
}
|
||||
250
src/utils/index.js
Normal file
250
src/utils/index.js
Normal file
@@ -0,0 +1,250 @@
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 格式化时间
|
||||
* @param time
|
||||
* @param cFormat
|
||||
* @returns {string|null}
|
||||
*/
|
||||
export function parseTime(time, cFormat) {
|
||||
if (arguments.length === 0) {
|
||||
return null
|
||||
}
|
||||
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
|
||||
let date
|
||||
if (typeof time === 'object') {
|
||||
date = time
|
||||
} else {
|
||||
if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
|
||||
time = parseInt(time)
|
||||
}
|
||||
if (typeof time === 'number' && time.toString().length === 10) {
|
||||
time = time * 1000
|
||||
}
|
||||
date = new Date(time)
|
||||
}
|
||||
const formatObj = {
|
||||
y: date.getFullYear(),
|
||||
m: date.getMonth() + 1,
|
||||
d: date.getDate(),
|
||||
h: date.getHours(),
|
||||
i: date.getMinutes(),
|
||||
s: date.getSeconds(),
|
||||
a: date.getDay(),
|
||||
}
|
||||
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
||||
let value = formatObj[key]
|
||||
if (key === 'a') {
|
||||
return ['日', '一', '二', '三', '四', '五', '六'][value]
|
||||
}
|
||||
if (result.length > 0 && value < 10) {
|
||||
value = `0${value}`
|
||||
}
|
||||
return value || 0
|
||||
})
|
||||
return time_str
|
||||
}
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 格式化时间
|
||||
* @param time
|
||||
* @param option
|
||||
* @returns {string}
|
||||
*/
|
||||
export function formatTime(time, option) {
|
||||
if (typeof time === 'string' && !isNaN(Date.parse(time))) {
|
||||
// 处理 ISO 8601 格式的时间
|
||||
time = new Date(time)
|
||||
} else if (`${time}`.length === 10) {
|
||||
// 处理秒级时间戳
|
||||
time = new Date(parseInt(time) * 1000)
|
||||
} else {
|
||||
// 处理毫秒级时间戳
|
||||
time = new Date(+time)
|
||||
}
|
||||
|
||||
if (isNaN(time.getTime())) {
|
||||
return 'Invalid Date'
|
||||
}
|
||||
|
||||
const now = new Date()
|
||||
const diff = (now - time) / 1000
|
||||
|
||||
if (diff < 30) {
|
||||
return '刚刚'
|
||||
} else if (diff < 3600) {
|
||||
return `${Math.ceil(diff / 60)}分钟前`
|
||||
} else if (diff < 3600 * 24) {
|
||||
return `${Math.ceil(diff / 3600)}小时前`
|
||||
} else if (diff < 3600 * 24 * 2) {
|
||||
return '1天前'
|
||||
}
|
||||
|
||||
if (option) {
|
||||
return parseTime(time, option)
|
||||
} else {
|
||||
return `${time.getMonth() + 1}月${time.getDate()}日${time.getHours()}时${time.getMinutes()}分`
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 将url请求参数转为json格式
|
||||
* @param url
|
||||
* @returns {{}|any}
|
||||
*/
|
||||
export function paramObj(url) {
|
||||
const search = url.split('?')[1]
|
||||
if (!search) {
|
||||
return {}
|
||||
}
|
||||
return JSON.parse(`{"${decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"').replace(/\+/g, ' ')}"}`)
|
||||
}
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 父子关系的数组转换成树形结构数据
|
||||
* @param data
|
||||
* @returns {*}
|
||||
*/
|
||||
export function translateDataToTree(data) {
|
||||
const parent = data.filter((value) => value.parentId === 'undefined' || value.parentId == null)
|
||||
const children = data.filter((value) => value.parentId !== 'undefined' && value.parentId != null)
|
||||
const translator = (parent, children) => {
|
||||
parent.forEach((parent) => {
|
||||
children.forEach((current, index) => {
|
||||
if (current.parentId === parent.id) {
|
||||
const temp = JSON.parse(JSON.stringify(children))
|
||||
temp.splice(index, 1)
|
||||
translator([current], temp)
|
||||
typeof parent.children !== 'undefined' ? parent.children.push(current) : (parent.children = [current])
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
translator(parent, children)
|
||||
return parent
|
||||
}
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 树形结构数据转换成父子关系的数组
|
||||
* @param data
|
||||
* @returns {[]}
|
||||
*/
|
||||
export function translateTreeToData(data) {
|
||||
const result = []
|
||||
data.forEach((item) => {
|
||||
const loop = (data) => {
|
||||
result.push({
|
||||
id: data.id,
|
||||
name: data.name,
|
||||
parentId: data.parentId,
|
||||
})
|
||||
const child = data.children
|
||||
if (child) {
|
||||
for (let i = 0; i < child.length; i++) {
|
||||
loop(child[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
loop(item)
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 10位时间戳转换
|
||||
* @param time
|
||||
* @returns {string}
|
||||
*/
|
||||
export function tenBitTimestamp(time) {
|
||||
const date = new Date(time * 1000)
|
||||
const y = date.getFullYear()
|
||||
let m = date.getMonth() + 1
|
||||
m = m < 10 ? `${m}` : m
|
||||
let d = date.getDate()
|
||||
d = d < 10 ? `${d}` : d
|
||||
let h = date.getHours()
|
||||
h = h < 10 ? `0${h}` : h
|
||||
let minute = date.getMinutes()
|
||||
let second = date.getSeconds()
|
||||
minute = minute < 10 ? `0${minute}` : minute
|
||||
second = second < 10 ? `0${second}` : second
|
||||
return `${y}年${m}月${d}日 ${h}:${minute}:${second}` //组合
|
||||
}
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 13位时间戳转换
|
||||
* @param time
|
||||
* @returns {string}
|
||||
*/
|
||||
export function thirteenBitTimestamp(time) {
|
||||
const date = new Date(time / 1)
|
||||
const y = date.getFullYear()
|
||||
let m = date.getMonth() + 1
|
||||
m = m < 10 ? `${m}` : m
|
||||
let d = date.getDate()
|
||||
d = d < 10 ? `${d}` : d
|
||||
let h = date.getHours()
|
||||
h = h < 10 ? `0${h}` : h
|
||||
let minute = date.getMinutes()
|
||||
let second = date.getSeconds()
|
||||
minute = minute < 10 ? `0${minute}` : minute
|
||||
second = second < 10 ? `0${second}` : second
|
||||
return `${y}年${m}月${d}日 ${h}:${minute}:${second}` //组合
|
||||
}
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 获取随机id
|
||||
* @param length
|
||||
* @returns {string}
|
||||
*/
|
||||
export function uuid(length = 32) {
|
||||
const num = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
|
||||
let str = ''
|
||||
for (let i = 0; i < length; i++) {
|
||||
str += num.charAt(Math.floor(Math.random() * num.length))
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description m到n的随机数
|
||||
* @param m
|
||||
* @param n
|
||||
* @returns {number}
|
||||
*/
|
||||
export function random(m, n) {
|
||||
return Math.floor(Math.random() * (m - n) + n)
|
||||
}
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description addEventListener
|
||||
* @type {function(...[*]=)}
|
||||
*/
|
||||
export const on = (function () {
|
||||
return function (element, event, handler, useCapture = false) {
|
||||
if (element && event && handler) {
|
||||
element.addEventListener(event, handler, useCapture)
|
||||
}
|
||||
}
|
||||
})()
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description removeEventListener
|
||||
* @type {function(...[*]=)}
|
||||
*/
|
||||
export const off = (function () {
|
||||
return function (element, event, handler, useCapture = false) {
|
||||
if (element && event) {
|
||||
element.removeEventListener(event, handler, useCapture)
|
||||
}
|
||||
}
|
||||
})()
|
||||
12
src/utils/pageTitle.js
Normal file
12
src/utils/pageTitle.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import { title } from '@/config'
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 设置标题
|
||||
* @param pageTitle
|
||||
* @returns {string}
|
||||
*/
|
||||
export default function getPageTitle(pageTitle) {
|
||||
if (pageTitle) return `${pageTitle}-${title}`
|
||||
return `${title}`
|
||||
}
|
||||
20
src/utils/permission.js
Normal file
20
src/utils/permission.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import store from '@/store'
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 检查权限
|
||||
* @param value
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export default function checkPermission(value) {
|
||||
if (value && value instanceof Array && value.length > 0) {
|
||||
const permissions = store.getters['user/permissions']
|
||||
const permissionPermissions = value
|
||||
|
||||
return permissions.some((role) => {
|
||||
return permissionPermissions.includes(role)
|
||||
})
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
126
src/utils/request.js
Normal file
126
src/utils/request.js
Normal file
@@ -0,0 +1,126 @@
|
||||
import Vue from 'vue'
|
||||
import axios from 'axios'
|
||||
import {
|
||||
baseURL,
|
||||
contentType,
|
||||
debounce,
|
||||
invalidCode,
|
||||
loginInterception,
|
||||
noPermissionCode,
|
||||
requestTimeout,
|
||||
successCode,
|
||||
tokenName,
|
||||
} from '@/config'
|
||||
import store from '@/store'
|
||||
import qs from 'qs'
|
||||
import router from '@/router'
|
||||
import { isArray } from '@/utils/validate'
|
||||
|
||||
let loadingInstance
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 处理code异常
|
||||
* @param {*} code
|
||||
* @param {*} msg
|
||||
*/
|
||||
const handleCode = (code, msg) => {
|
||||
switch (code) {
|
||||
case invalidCode:
|
||||
Vue.prototype.$baseMessage(msg || `后端接口${code}异常`, 'error')
|
||||
store.dispatch('user/resetAccessToken').catch(() => {})
|
||||
if (loginInterception) {
|
||||
location.reload()
|
||||
}
|
||||
break
|
||||
case noPermissionCode:
|
||||
router.push({ path: '/401' }).catch(() => {})
|
||||
break
|
||||
default:
|
||||
Vue.prototype.$baseMessage(msg || `后端接口${code}异常`, 'error')
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
const instance = axios.create({
|
||||
baseURL,
|
||||
timeout: requestTimeout,
|
||||
headers: {
|
||||
'Content-Type': contentType,
|
||||
},
|
||||
})
|
||||
|
||||
instance.interceptors.request.use(
|
||||
(config) => {
|
||||
if (store.getters['user/accessToken']) {
|
||||
config.headers['authorization'] = store.getters['user/accessToken']
|
||||
}
|
||||
// 处理 GET 请求参数
|
||||
if (config.method === 'get' && config.params) {
|
||||
// 过滤掉空值参数
|
||||
config.params = Vue.prototype.$baseLodash.pickBy(config.params, Vue.prototype.$baseLodash.identity)
|
||||
}
|
||||
//这里会过滤所有为空、0、false的key,如果不需要请自行注释
|
||||
if (config.data) config.data = Vue.prototype.$baseLodash.pickBy(config.data, Vue.prototype.$baseLodash.identity)
|
||||
if (config.data && config.headers['Content-Type'] === 'application/x-www-form-urlencoded;charset=UTF-8')
|
||||
config.data = qs.stringify(config.data)
|
||||
if (debounce.some((item) => config.url.includes(item))) loadingInstance = Vue.prototype.$baseLoading()
|
||||
return config
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
instance.interceptors.response.use(
|
||||
(response) => {
|
||||
if (loadingInstance) loadingInstance.close()
|
||||
|
||||
const { data, config } = response
|
||||
const { code, msg } = data
|
||||
// 操作正常Code数组
|
||||
const codeVerificationArray = isArray(successCode) ? [...successCode] : [...[successCode]]
|
||||
// 是否操作正常
|
||||
if (codeVerificationArray.includes(code)) {
|
||||
if (data?.data?.code >= 400) {
|
||||
Vue.prototype.$baseMessage(data?.data?.msg, 'error')
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
} else {
|
||||
handleCode(code, msg)
|
||||
return Promise.reject(
|
||||
`vue-admin-beautiful请求异常拦截:${JSON.stringify({
|
||||
url: config.url,
|
||||
code,
|
||||
msg,
|
||||
})}` || 'Error'
|
||||
)
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
if (loadingInstance) loadingInstance.close()
|
||||
const { response, message } = error
|
||||
if (error.response && error.response.data) {
|
||||
const { status, data } = response
|
||||
handleCode(status, data.msg || message)
|
||||
return Promise.reject(error)
|
||||
} else {
|
||||
let { message } = error
|
||||
if (message === 'Network Error') {
|
||||
message = '后端接口连接异常'
|
||||
}
|
||||
if (message.includes('timeout')) {
|
||||
message = '后端接口请求超时'
|
||||
}
|
||||
if (message.includes('Request failed with status code')) {
|
||||
const code = message.substr(message.length - 3)
|
||||
message = `后端接口${code}异常`
|
||||
}
|
||||
Vue.prototype.$baseMessage(message || `后端接口未知异常`, 'error')
|
||||
return Promise.reject(error)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export default instance
|
||||
48
src/utils/static.js
Normal file
48
src/utils/static.js
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* @author https://vuejs-core.cn
|
||||
* @description 导入所有 controller 模块,浏览器环境中自动输出controller文件夹下Mock接口,请勿修改。
|
||||
*/
|
||||
import Mock from 'mockjs'
|
||||
import { paramObj } from '@/utils'
|
||||
|
||||
const mocks = []
|
||||
const files = require.context('../../mock/controller', false, /\.js$/)
|
||||
|
||||
files.keys().forEach((key) => {
|
||||
mocks.push(...files(key))
|
||||
})
|
||||
|
||||
export function mockXHR() {
|
||||
Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send
|
||||
Mock.XHR.prototype.send = function () {
|
||||
if (this.custom.xhr) {
|
||||
this.custom.xhr.withCredentials = this.withCredentials || false
|
||||
|
||||
if (this.responseType) {
|
||||
this.custom.xhr.responseType = this.responseType
|
||||
}
|
||||
}
|
||||
this.proxy_send(...arguments)
|
||||
}
|
||||
|
||||
function XHRHttpRequst(respond) {
|
||||
return function (options) {
|
||||
let result
|
||||
if (respond instanceof Function) {
|
||||
const { body, type, url } = options
|
||||
result = respond({
|
||||
method: type,
|
||||
body: JSON.parse(body),
|
||||
query: paramObj(url),
|
||||
})
|
||||
} else {
|
||||
result = respond
|
||||
}
|
||||
return Mock.mock(result)
|
||||
}
|
||||
}
|
||||
|
||||
mocks.forEach((item) => {
|
||||
Mock.mock(new RegExp(item.url), item.type || 'get', XHRHttpRequst(item.response))
|
||||
})
|
||||
}
|
||||
156
src/utils/vab.js
Normal file
156
src/utils/vab.js
Normal file
@@ -0,0 +1,156 @@
|
||||
import { loadingText, messageDuration, title } from '@/config'
|
||||
import * as lodash from 'lodash'
|
||||
import { Loading, Message, MessageBox, Notification } from 'element-ui'
|
||||
import store from '@/store'
|
||||
import { getAccessToken } from '@/utils/accessToken'
|
||||
|
||||
const accessToken = store.getters['user/accessToken']
|
||||
const layout = store.getters['settings/layout']
|
||||
|
||||
const install = (Vue) => {
|
||||
/* 全局accessToken */
|
||||
Vue.prototype.$baseAccessToken = () => {
|
||||
return accessToken || getAccessToken()
|
||||
}
|
||||
/* 全局标题 */
|
||||
Vue.prototype.$baseTitle = (() => {
|
||||
return title
|
||||
})()
|
||||
/* 全局加载层 */
|
||||
Vue.prototype.$baseLoading = (index, text) => {
|
||||
let loading
|
||||
if (!index) {
|
||||
loading = Loading.service({
|
||||
lock: true,
|
||||
text: text || loadingText,
|
||||
background: 'hsla(0,0%,100%,.8)',
|
||||
})
|
||||
} else {
|
||||
loading = Loading.service({
|
||||
lock: true,
|
||||
text: text || loadingText,
|
||||
spinner: `vab-loading-type${index}`,
|
||||
background: 'hsla(0,0%,100%,.8)',
|
||||
})
|
||||
}
|
||||
return loading
|
||||
}
|
||||
/* 全局多彩加载层 */
|
||||
Vue.prototype.$baseColorfullLoading = (index, text) => {
|
||||
let loading
|
||||
if (!index) {
|
||||
loading = Loading.service({
|
||||
lock: true,
|
||||
text: text || loadingText,
|
||||
spinner: 'dots-loader',
|
||||
background: 'hsla(0,0%,100%,.8)',
|
||||
})
|
||||
} else {
|
||||
switch (index) {
|
||||
case 1:
|
||||
index = 'dots'
|
||||
break
|
||||
case 2:
|
||||
index = 'gauge'
|
||||
break
|
||||
case 3:
|
||||
index = 'inner-circles'
|
||||
break
|
||||
case 4:
|
||||
index = 'plus'
|
||||
break
|
||||
}
|
||||
loading = Loading.service({
|
||||
lock: true,
|
||||
text: text || loadingText,
|
||||
spinner: `${index}-loader`,
|
||||
background: 'hsla(0,0%,100%,.8)',
|
||||
})
|
||||
}
|
||||
return loading
|
||||
}
|
||||
/* 全局Message */
|
||||
Vue.prototype.$baseMessage = (message, type) => {
|
||||
Message({
|
||||
offset: 60,
|
||||
showClose: true,
|
||||
message: message,
|
||||
type: type,
|
||||
dangerouslyUseHTMLString: true,
|
||||
duration: messageDuration,
|
||||
})
|
||||
}
|
||||
|
||||
/* 全局Alert */
|
||||
Vue.prototype.$baseAlert = (content, title, callback) => {
|
||||
MessageBox.alert(content, title || '温馨提示', {
|
||||
confirmButtonText: '确定',
|
||||
dangerouslyUseHTMLString: true,
|
||||
callback: () => {
|
||||
if (callback) {
|
||||
callback()
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/* 全局Confirm */
|
||||
Vue.prototype.$baseConfirm = (content, title, callback1, callback2) => {
|
||||
MessageBox.confirm(content, title || '温馨提示', {
|
||||
confirmButtonText: '确定',
|
||||
cancelButtonText: '取消',
|
||||
closeOnClickModal: false,
|
||||
type: 'warning',
|
||||
})
|
||||
.then(() => {
|
||||
if (callback1) {
|
||||
callback1()
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
if (callback2) {
|
||||
callback2()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/* 全局Notification */
|
||||
Vue.prototype.$baseNotify = (message, title, type, position) => {
|
||||
Notification({
|
||||
title: title,
|
||||
message: message,
|
||||
position: position || 'top-right',
|
||||
type: type || 'success',
|
||||
duration: messageDuration,
|
||||
})
|
||||
}
|
||||
|
||||
/* 全局TableHeight */
|
||||
Vue.prototype.$baseTableHeight = (formType) => {
|
||||
let height = window.innerHeight
|
||||
let paddingHeight = 400
|
||||
const formHeight = 50
|
||||
|
||||
if (layout === 'vertical') {
|
||||
paddingHeight = 365
|
||||
}
|
||||
|
||||
if ('number' == typeof formType) {
|
||||
height = height - paddingHeight - formHeight * formType
|
||||
} else {
|
||||
height = height - paddingHeight
|
||||
}
|
||||
return height
|
||||
}
|
||||
|
||||
/* 全局lodash */
|
||||
Vue.prototype.$baseLodash = lodash
|
||||
/* 全局事件总线 */
|
||||
Vue.prototype.$baseEventBus = new Vue()
|
||||
}
|
||||
|
||||
if (typeof window !== 'undefined' && window.Vue) {
|
||||
install(window.Vue)
|
||||
}
|
||||
|
||||
export default install
|
||||
53
src/utils/validate.js
Normal file
53
src/utils/validate.js
Normal file
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* @author zxwk1998 (不想保留author可删除)
|
||||
* @description 判读是否为外链
|
||||
* @param path
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isExternal(path) {
|
||||
return /^(https?:|mailto:|tel:)/.test(path)
|
||||
}
|
||||
|
||||
/**
|
||||
* @author https://github.com/zxwk1998/vue-admin-better (不想保留author可删除)
|
||||
* @description 校验密码是否小于6位
|
||||
* @param str
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isPassword(str) {
|
||||
return str.length >= 6
|
||||
}
|
||||
|
||||
/**
|
||||
* @author zxwk1998 (不想保留author可删除)
|
||||
* @description 判断是否是字符串
|
||||
* @param str
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isString(str) {
|
||||
return typeof str === 'string' || str instanceof String
|
||||
}
|
||||
|
||||
/**
|
||||
* @author zxwk1998 (不想保留author可删除)
|
||||
* @description 判断是否是数组
|
||||
* @param arg
|
||||
* @returns {arg is any[]|boolean}
|
||||
*/
|
||||
export function isArray(arg) {
|
||||
if (typeof Array.isArray === 'undefined') {
|
||||
return Object.prototype.toString.call(arg) === '[object Array]'
|
||||
}
|
||||
return Array.isArray(arg)
|
||||
}
|
||||
|
||||
/**
|
||||
* @author zxwk1998 (不想保留author可删除)
|
||||
* @description 判断是否是手机号
|
||||
* @param str
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isPhone(str) {
|
||||
const reg = /^1\d{10}$/
|
||||
return reg.test(str)
|
||||
}
|
||||
Reference in New Issue
Block a user