refactor: 代码规范、Bug修复
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
import JSEncrypt from 'jsencrypt/bin/jsencrypt.min'
|
||||
|
||||
// 密钥对生成 http://web.chacuo.net/netrsakeypair
|
||||
|
||||
const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANL378k3RiZHWx5AfJqdH9xRNBmD9wGD\n' +
|
||||
'2iRe41HdTNF8RUhNnHit5NpMNtGL0NPTSSpPjjI1kJfVorRvaQerUgkCAwEAAQ=='
|
||||
|
||||
// 加密
|
||||
export function encrypt(txt) {
|
||||
const encryptor = new JSEncrypt()
|
||||
encryptor.setPublicKey(publicKey) // 设置公钥
|
||||
return encryptor.encrypt(txt) // 对需要加密的数据进行加密
|
||||
}
|
||||
|
||||
import JSEncrypt from 'jsencrypt/bin/jsencrypt.min'
|
||||
|
||||
// 密钥对生成 http://web.chacuo.net/netrsakeypair
|
||||
|
||||
const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANL378k3RiZHWx5AfJqdH9xRNBmD9wGD\n' +
|
||||
'2iRe41HdTNF8RUhNnHit5NpMNtGL0NPTSSpPjjI1kJfVorRvaQerUgkCAwEAAQ=='
|
||||
|
||||
// 加密
|
||||
export function encrypt(txt) {
|
||||
const encryptor = new JSEncrypt()
|
||||
encryptor.setPublicKey(publicKey) // 设置公钥
|
||||
return encryptor.encrypt(txt) // 对需要加密的数据进行加密
|
||||
}
|
||||
|
||||
|
||||
+387
-330
@@ -1,330 +1,387 @@
|
||||
import store from '@/store'
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
/**
|
||||
* 权限校验
|
||||
* @param value
|
||||
* @returns {boolean|*}
|
||||
*/
|
||||
export function CheckPermissions(value) {
|
||||
if (value && value instanceof Array && value.length > 0) {
|
||||
// 拥有的权限
|
||||
const roles = store.getters && store.getters.roles
|
||||
// 需要的权限
|
||||
const permissionRoles = value
|
||||
return roles.some(role => {
|
||||
return permissionRoles.includes(role)
|
||||
})
|
||||
} else {
|
||||
console.error(`need roles! Like v-permission="['admin','editor']"`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转数字
|
||||
* @param value
|
||||
* @returns {Number|*}
|
||||
*/
|
||||
export function StringToNumber(value) {
|
||||
try {
|
||||
return value ? Number(value) : value
|
||||
} catch (e) {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保留几位小数
|
||||
* @param num Number
|
||||
* @param accuracy 精度
|
||||
* @returns {string|number}
|
||||
*/
|
||||
export function NumberToFixed(num, accuracy) {
|
||||
return num instanceof Number ? num.toFixed(accuracy) : num
|
||||
}
|
||||
|
||||
/**
|
||||
* 浅拷贝
|
||||
* @param source 源对象
|
||||
* @param target 目标对象
|
||||
* @returns {*}
|
||||
*/
|
||||
export function CopyObject(source, target) {
|
||||
return Object.assign(target, source)
|
||||
}
|
||||
|
||||
/**
|
||||
* 深拷贝对象
|
||||
* @param obj
|
||||
*/
|
||||
export function DeepCopyObject(obj) {
|
||||
return JSON.parse(JSON.stringify(obj))
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计对象属性等于targetValue的数量
|
||||
* @param arr 对象数组
|
||||
* @param propKey 属性名称
|
||||
* @param targetValue 目标值
|
||||
* @returns {*|number}
|
||||
*/
|
||||
export function CountArraysObjectByPropKey(arr, propKey, targetValue) {
|
||||
if (!Array.isArray(arr)) return 0
|
||||
return arr.filter(item => item[propKey] === targetValue).length
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据value获取label
|
||||
* @param options /
|
||||
* @param value /
|
||||
* @returns {*}
|
||||
*/
|
||||
export function GetOptionLabelByValue(options, value) {
|
||||
if (options instanceof Array) {
|
||||
const find = options.find(f => f.value === value)
|
||||
if (find) {
|
||||
return find.label
|
||||
}
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化字节数字符串
|
||||
* @param value
|
||||
* @returns {string}
|
||||
* @constructor
|
||||
*/
|
||||
export function FormatBytesStr(value) {
|
||||
if (typeof value !== 'number' || isNaN(value) || value <= 0) {
|
||||
return '0 Bytes'
|
||||
}
|
||||
const units = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
||||
const index = Math.floor(Math.log(value) / Math.log(1024))
|
||||
const unit = units[Math.min(index, units.length - 1)]
|
||||
const result = (value / Math.pow(1024, index)).toFixed(2)
|
||||
return `${result} ${unit}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期字符串
|
||||
* @param originVal
|
||||
* @returns {string}
|
||||
* @constructor
|
||||
*/
|
||||
export function FormatDateStr(originVal) {
|
||||
if (originVal === undefined) {
|
||||
return ''
|
||||
}
|
||||
const dt = new Date(originVal)
|
||||
const y = dt.getFullYear()
|
||||
const m = (dt.getMonth() + 1 + '').padStart(2, '0')
|
||||
const d = (dt.getDate() + '').padStart(2, '0')
|
||||
return `${y}-${m}-${d}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期时间字符串
|
||||
* @param originVal
|
||||
* @returns {string}
|
||||
* @constructor
|
||||
*/
|
||||
export function FormatDateTimeStr(originVal) {
|
||||
if (originVal) {
|
||||
return dayjs(originVal).format('YYYY-MM-DD HH:mm:ss')
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期时间字符串,不带分割符号
|
||||
* @param originVal
|
||||
* @returns {string}
|
||||
* @constructor
|
||||
*/
|
||||
export function FormatDateTimeShortStr(originVal) {
|
||||
if (originVal) {
|
||||
return dayjs(originVal).format('YYYYMMDDHHmmss')
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化表格行列中的日期时间字符串
|
||||
* @param row
|
||||
* @param column
|
||||
* @param cellValue
|
||||
* @param index
|
||||
* @returns {string}
|
||||
* @constructor
|
||||
*/
|
||||
export function FormatRowDateTimeStr(row, column, cellValue, index) {
|
||||
if (cellValue === undefined) {
|
||||
return ''
|
||||
}
|
||||
const dt = new Date(cellValue)
|
||||
const y = dt.getFullYear()
|
||||
const m = (dt.getMonth() + 1 + '').padStart(2, '0')
|
||||
const d = (dt.getDate() + '').padStart(2, '0')
|
||||
const hh = (dt.getHours() + '').padStart(2, '0')
|
||||
const mm = (dt.getMinutes() + '').padStart(2, '0')
|
||||
const ss = (dt.getSeconds() + '').padStart(2, '0')
|
||||
return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取变量类型
|
||||
* @param value
|
||||
* @returns {string}
|
||||
* @constructor
|
||||
*/
|
||||
export function GetValueType(value) {
|
||||
return Object.prototype.toString.call(value).slice(8, -1).toLowerCase()
|
||||
}
|
||||
|
||||
/**
|
||||
* 数组去重
|
||||
* @param arr
|
||||
* @returns {any[]}
|
||||
* @constructor
|
||||
*/
|
||||
export function UniqueArrays(arr) {
|
||||
return [...new Set(arr)]
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机号掩码
|
||||
* @param mobile
|
||||
* @returns {*|string}
|
||||
* @constructor
|
||||
*/
|
||||
export function MaskMobile(mobile) {
|
||||
if (mobile.length > 7) {
|
||||
return mobile.substr(0, 3) + '****' + mobile.substr(7)
|
||||
}
|
||||
return mobile
|
||||
}
|
||||
|
||||
/**
|
||||
* 邮箱掩码
|
||||
* @param email
|
||||
* @returns {string}
|
||||
* @constructor
|
||||
*/
|
||||
export function MaskEmail(email) {
|
||||
if (String(email).indexOf('@') > 0) {
|
||||
const str = email.split('@')
|
||||
let _s = ''
|
||||
if (str[0].length > 3) {
|
||||
for (let i = 0; i < str[0].length - 3; i++) {
|
||||
_s += '*'
|
||||
}
|
||||
}
|
||||
return str[0].substr(0, 3) + _s + '@' + str[1]
|
||||
}
|
||||
return email
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Url中的查询参数
|
||||
* @param {string} url
|
||||
* @returns {Object}
|
||||
*/
|
||||
export function GetUrlQueryArgs(url) {
|
||||
url = url == null ? window.location.href : url
|
||||
const search = url.substring(url.lastIndexOf('?') + 1)
|
||||
const obj = {}
|
||||
const reg = /([^?&=]+)=([^?&=]*)/g
|
||||
search.replace(reg, (rs, $1, $2) => {
|
||||
const name = decodeURIComponent($1)
|
||||
let val = decodeURIComponent($2)
|
||||
val = String(val)
|
||||
obj[name] = val
|
||||
return rs
|
||||
})
|
||||
return obj
|
||||
}
|
||||
|
||||
/**
|
||||
* 将Url中的查询参数转成对象
|
||||
* @param {string} url
|
||||
* @returns {Object}
|
||||
*/
|
||||
export function ParseUrlQueryArgsToObject(url) {
|
||||
const search = url.split('?')[1]
|
||||
if (!search) {
|
||||
return {}
|
||||
}
|
||||
return JSON.parse(
|
||||
'{"' +
|
||||
decodeURIComponent(search)
|
||||
.replace(/"/g, '\\"')
|
||||
.replace(/&/g, '","')
|
||||
.replace(/=/g, '":"')
|
||||
.replace(/\+/g, ' ') +
|
||||
'"}'
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并两个属性不一致的对象
|
||||
* @param {Object} target
|
||||
* @param {(Object|Array)} source
|
||||
* @returns {Object}
|
||||
*/
|
||||
export function MergeObject(target, source) {
|
||||
if (typeof target !== 'object') {
|
||||
target = {}
|
||||
}
|
||||
if (Array.isArray(source)) {
|
||||
return source.slice()
|
||||
}
|
||||
Object.keys(source).forEach(property => {
|
||||
const sourceProperty = source[property]
|
||||
if (typeof sourceProperty === 'object') {
|
||||
target[property] = MergeObject(target[property], sourceProperty)
|
||||
} else {
|
||||
target[property] = sourceProperty
|
||||
}
|
||||
})
|
||||
return target
|
||||
}
|
||||
/**
|
||||
* 防抖动
|
||||
* @param {Function} func
|
||||
* @param {number} wait
|
||||
* @param {boolean} immediate
|
||||
* @return {*}
|
||||
*/
|
||||
export function DeBounce(func, wait, immediate = false) {
|
||||
let timeout, args, context, timestamp, result
|
||||
const later = function() {
|
||||
// 据上一次触发时间间隔
|
||||
const last = +new Date() - timestamp
|
||||
// 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
|
||||
if (last < wait && last > 0) {
|
||||
timeout = setTimeout(later, wait - last)
|
||||
} else {
|
||||
timeout = null
|
||||
// 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
|
||||
if (!immediate) {
|
||||
result = func.apply(context, args)
|
||||
context = args = null
|
||||
}
|
||||
}
|
||||
}
|
||||
return function(...args) {
|
||||
context = this
|
||||
timestamp = +new Date()
|
||||
const callNow = immediate && !timeout
|
||||
// 如果延时不存在,重新设定延时
|
||||
if (!timeout) timeout = setTimeout(later, wait)
|
||||
if (callNow) {
|
||||
result = func.apply(context, args)
|
||||
context = args = null
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
import store from '@/store'
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
/**
|
||||
* 权限校验
|
||||
* @param value
|
||||
* @returns {boolean|*}
|
||||
*/
|
||||
export function CheckPermissions(value) {
|
||||
if (value && value instanceof Array && value.length > 0) {
|
||||
// 拥有的权限
|
||||
const roles = store.getters && store.getters.roles
|
||||
// 需要的权限
|
||||
const permissionRoles = value
|
||||
return roles.some(role => {
|
||||
return permissionRoles.includes(role)
|
||||
})
|
||||
} else {
|
||||
console.error(`need roles! Like v-permission="['admin','editor']"`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转数字
|
||||
* @param value
|
||||
* @returns {Number|*}
|
||||
*/
|
||||
export function StringToNumber(value) {
|
||||
try {
|
||||
return value ? Number(value) : value
|
||||
} catch (e) {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保留几位小数
|
||||
* @param num Number
|
||||
* @param accuracy 精度
|
||||
* @returns {string|number}
|
||||
*/
|
||||
export function NumberToFixed(num, accuracy) {
|
||||
return num instanceof Number ? num.toFixed(accuracy) : num
|
||||
}
|
||||
|
||||
/**
|
||||
* 浅拷贝
|
||||
* @param source 源对象
|
||||
* @param target 目标对象
|
||||
* @returns {*}
|
||||
*/
|
||||
export function CopyObject(source, target) {
|
||||
return Object.assign(target, source)
|
||||
}
|
||||
|
||||
/**
|
||||
* 深拷贝对象
|
||||
* @param obj
|
||||
*/
|
||||
export function DeepCopyObject(obj) {
|
||||
return JSON.parse(JSON.stringify(obj))
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计对象属性等于targetValue的数量
|
||||
* @param arr 对象数组
|
||||
* @param propKey 属性名称
|
||||
* @param targetValue 目标值
|
||||
* @returns {*|number}
|
||||
*/
|
||||
export function CountArraysObjectByPropKey(arr, propKey, targetValue) {
|
||||
if (!Array.isArray(arr)) {
|
||||
return 0
|
||||
}
|
||||
let cnt = 0
|
||||
for (const element of arr) {
|
||||
if (element.hasOwnProperty(propKey)) {
|
||||
// console.error('that.pipelineInstance.propKey', propKey)
|
||||
// console.error('that.pipelineInstance.element[propKey]', element[propKey])
|
||||
// console.error('that.pipelineInstance.targetValue', targetValue)
|
||||
if (element[propKey] === targetValue) {
|
||||
cnt = cnt + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
return cnt
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据value获取label
|
||||
* @param options /
|
||||
* @param value /
|
||||
* @returns {*}
|
||||
*/
|
||||
export function GetOptionLabelByValue(options, value) {
|
||||
if (options instanceof Array) {
|
||||
const find = options.find(f => f.value === value)
|
||||
if (find) {
|
||||
return find.label
|
||||
}
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化字节数字符串
|
||||
* @param value
|
||||
* @returns {string}
|
||||
* @constructor
|
||||
*/
|
||||
export function FormatBytesStr(value) {
|
||||
if (typeof value !== 'number' || isNaN(value) || value <= 0) {
|
||||
return '0 Bytes'
|
||||
}
|
||||
const units = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
||||
const index = Math.floor(Math.log(value) / Math.log(1024))
|
||||
const unit = units[Math.min(index, units.length - 1)]
|
||||
const result = (value / Math.pow(1024, index)).toFixed(2)
|
||||
return `${result} ${unit}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期字符串
|
||||
* @param originVal
|
||||
* @returns {string}
|
||||
* @constructor
|
||||
*/
|
||||
export function FormatDateStr(originVal) {
|
||||
if (originVal === undefined) {
|
||||
return ''
|
||||
}
|
||||
const dt = new Date(originVal)
|
||||
const y = dt.getFullYear()
|
||||
const m = (dt.getMonth() + 1 + '').padStart(2, '0')
|
||||
const d = (dt.getDate() + '').padStart(2, '0')
|
||||
return `${y}-${m}-${d}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期时间字符串
|
||||
* @param originVal
|
||||
* @returns {string}
|
||||
* @constructor
|
||||
*/
|
||||
export function FormatDateTimeStr(originVal) {
|
||||
if (originVal) {
|
||||
return dayjs(originVal).format('YYYY-MM-DD HH:mm:ss')
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期时间字符串,不带分割符号
|
||||
* @param originVal
|
||||
* @returns {string}
|
||||
* @constructor
|
||||
*/
|
||||
export function FormatDateTimeShortStr(originVal) {
|
||||
if (originVal) {
|
||||
return dayjs(originVal).format('YYYYMMDDHHmmss')
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化表格行列中的日期时间字符串
|
||||
* @param row
|
||||
* @param column
|
||||
* @param cellValue
|
||||
* @param index
|
||||
* @returns {string}
|
||||
* @constructor
|
||||
*/
|
||||
export function FormatRowDateTimeStr(row, column, cellValue, index) {
|
||||
if (cellValue === undefined) {
|
||||
return ''
|
||||
}
|
||||
const dt = new Date(cellValue)
|
||||
const y = dt.getFullYear()
|
||||
const m = (dt.getMonth() + 1 + '').padStart(2, '0')
|
||||
const d = (dt.getDate() + '').padStart(2, '0')
|
||||
const hh = (dt.getHours() + '').padStart(2, '0')
|
||||
const mm = (dt.getMinutes() + '').padStart(2, '0')
|
||||
const ss = (dt.getSeconds() + '').padStart(2, '0')
|
||||
return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取变量类型
|
||||
* @param value
|
||||
* @returns {string}
|
||||
* @constructor
|
||||
*/
|
||||
export function GetValueType(value) {
|
||||
return Object.prototype.toString.call(value).slice(8, -1).toLowerCase()
|
||||
}
|
||||
|
||||
/**
|
||||
* 数组去重
|
||||
* @param arr
|
||||
* @returns {any[]}
|
||||
* @constructor
|
||||
*/
|
||||
export function UniqueArrays(arr) {
|
||||
return [...new Set(arr)]
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机号掩码
|
||||
* @param mobile
|
||||
* @returns {*|string}
|
||||
* @constructor
|
||||
*/
|
||||
export function MaskMobile(mobile) {
|
||||
if (mobile.length > 7) {
|
||||
return mobile.substr(0, 3) + '****' + mobile.substr(7)
|
||||
}
|
||||
return mobile
|
||||
}
|
||||
|
||||
/**
|
||||
* 邮箱掩码
|
||||
* @param email
|
||||
* @returns {string}
|
||||
* @constructor
|
||||
*/
|
||||
export function MaskEmail(email) {
|
||||
if (String(email).indexOf('@') > 0) {
|
||||
const str = email.split('@')
|
||||
let _s = ''
|
||||
if (str[0].length > 3) {
|
||||
for (let i = 0; i < str[0].length - 3; i++) {
|
||||
_s += '*'
|
||||
}
|
||||
}
|
||||
return str[0].substr(0, 3) + _s + '@' + str[1]
|
||||
}
|
||||
return email
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Url中的查询参数
|
||||
* @param {string} url
|
||||
* @returns {Object}
|
||||
*/
|
||||
export function GetUrlQueryArgs(url) {
|
||||
url = url == null ? window.location.href : url
|
||||
const search = url.substring(url.lastIndexOf('?') + 1)
|
||||
const obj = {}
|
||||
const reg = /([^?&=]+)=([^?&=]*)/g
|
||||
search.replace(reg, (rs, $1, $2) => {
|
||||
const name = decodeURIComponent($1)
|
||||
let val = decodeURIComponent($2)
|
||||
val = String(val)
|
||||
obj[name] = val
|
||||
return rs
|
||||
})
|
||||
return obj
|
||||
}
|
||||
|
||||
/**
|
||||
* 将Url中的查询参数转成对象
|
||||
* @param {string} url
|
||||
* @returns {Object}
|
||||
*/
|
||||
export function ParseUrlQueryArgsToObject(url) {
|
||||
const search = url.split('?')[1]
|
||||
if (!search) {
|
||||
return {}
|
||||
}
|
||||
return JSON.parse(
|
||||
'{"' +
|
||||
decodeURIComponent(search)
|
||||
.replace(/"/g, '\\"')
|
||||
.replace(/&/g, '","')
|
||||
.replace(/=/g, '":"')
|
||||
.replace(/\+/g, ' ') +
|
||||
'"}'
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并两个属性不一致的对象
|
||||
* @param {Object} target
|
||||
* @param {(Object|Array)} source
|
||||
* @returns {Object}
|
||||
*/
|
||||
export function MergeObject(target, source) {
|
||||
if (typeof target !== 'object') {
|
||||
target = {}
|
||||
}
|
||||
if (Array.isArray(source)) {
|
||||
return source.slice()
|
||||
}
|
||||
Object.keys(source).forEach(property => {
|
||||
const sourceProperty = source[property]
|
||||
if (typeof sourceProperty === 'object') {
|
||||
target[property] = MergeObject(target[property], sourceProperty)
|
||||
} else {
|
||||
target[property] = sourceProperty
|
||||
}
|
||||
})
|
||||
return target
|
||||
}
|
||||
|
||||
/**
|
||||
* 防抖动
|
||||
* @param {Function} func
|
||||
* @param {number} wait
|
||||
* @param {boolean} immediate
|
||||
* @return {*}
|
||||
*/
|
||||
export function DeBounce(func, wait, immediate = false) {
|
||||
let timeout, args, context, timestamp, result
|
||||
const later = function() {
|
||||
// 据上一次触发时间间隔
|
||||
const last = +new Date() - timestamp
|
||||
// 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
|
||||
if (last < wait && last > 0) {
|
||||
timeout = setTimeout(later, wait - last)
|
||||
} else {
|
||||
timeout = null
|
||||
// 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
|
||||
if (!immediate) {
|
||||
result = func.apply(context, args)
|
||||
context = args = null
|
||||
}
|
||||
}
|
||||
}
|
||||
return function(...args) {
|
||||
context = this
|
||||
timestamp = +new Date()
|
||||
const callNow = immediate && !timeout
|
||||
// 如果延时不存在,重新设定延时
|
||||
if (!timeout) timeout = setTimeout(later, wait)
|
||||
if (callNow) {
|
||||
result = func.apply(context, args)
|
||||
context = args = null
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化表格行列中的环境字符串
|
||||
* @param row
|
||||
* @param column
|
||||
* @param cellValue
|
||||
* @param index
|
||||
* @returns {string}
|
||||
* @constructor
|
||||
*/
|
||||
export function FormatRowEnvStr(row, column, cellValue, index) {
|
||||
if (cellValue === undefined) {
|
||||
return ''
|
||||
}
|
||||
const data = {
|
||||
daily: '日常环境',
|
||||
stage: '预发环境',
|
||||
gray: '灰度环境',
|
||||
production: '生产环境'
|
||||
}
|
||||
const title = data[cellValue]
|
||||
if (title) {
|
||||
return title
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
export function IsEmpty(obj) {
|
||||
if (obj === undefined) {
|
||||
// console.error('is undefined')
|
||||
return true
|
||||
}
|
||||
if (obj === '') {
|
||||
// console.error('is ""')
|
||||
return true
|
||||
}
|
||||
if (obj === 'undefined') {
|
||||
// console.error('is "undefined"')
|
||||
return true
|
||||
}
|
||||
// console.error('is null')
|
||||
return obj === null
|
||||
}
|
||||
|
||||
@@ -1,175 +1,171 @@
|
||||
/**
|
||||
* 是否链接
|
||||
* @param {string} path
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function IsExternal(path) {
|
||||
return /^(https?:|mailto:|tel:)/.test(path)
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 是否有效用户名
|
||||
// * @param {string} str
|
||||
// * @returns {Boolean}
|
||||
// */
|
||||
// export function IsValidUsername(str) {
|
||||
// const validMap = ['admin', 'editor']
|
||||
// return validMap.indexOf(str.trim()) >= 0
|
||||
// }
|
||||
|
||||
/**
|
||||
* 是否有效链接
|
||||
* @param {string} url
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function IsValidURL(url) {
|
||||
const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
|
||||
return reg.test(url)
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否小写字母
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function IsValidLowerCaseLetter(str) {
|
||||
const reg = /^[a-z]+$/
|
||||
return reg.test(str)
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否大写字母
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function IsValidUpperCase(str) {
|
||||
const reg = /^[A-Z]+$/
|
||||
return reg.test(str)
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否包含大、小写字母
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function IsValidLetter(str) {
|
||||
const reg = /^[A-Za-z]+$/
|
||||
return reg.test(str)
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否邮箱
|
||||
* @param {string} email
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function IsValidEmail(email) {
|
||||
const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
||||
return reg.test(email)
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 是否手机号
|
||||
// * @param phone
|
||||
// * @returns {boolean}
|
||||
// * @constructor
|
||||
// */
|
||||
// export function IsValidPhone(phone) {
|
||||
// const reg = /^1([38][0-9]|4[014-9]|[59][0-35-9]|6[2567]|7[0-8])\d{8}$/
|
||||
// return reg.test(phone)
|
||||
// }
|
||||
|
||||
/**
|
||||
* 是否是字符串
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function IsString(str) {
|
||||
return typeof str === 'string' || str instanceof String
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否数组
|
||||
* @param {Array} arg
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function IsArray(arg) {
|
||||
if (typeof Array.isArray === 'undefined') {
|
||||
return Object.prototype.toString.call(arg) === '[object Array]'
|
||||
}
|
||||
return Array.isArray(arg)
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否合法IP地址
|
||||
* @param rule
|
||||
* @param value
|
||||
* @param callback
|
||||
*/
|
||||
export function IsValidIP(rule, value, callback) {
|
||||
if (value === '' || value === undefined || value == null) {
|
||||
callback()
|
||||
} else {
|
||||
const reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
|
||||
if ((!reg.test(value)) && value !== '') {
|
||||
callback(new Error('请输入正确的IP地址'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 是否手机号码或者固话*/
|
||||
export function IsValidPhoneTwo(rule, value, callback) {
|
||||
const reg = /^((0\d{2,3}-\d{7,8})|(1([38][0-9]|4[014-9]|[59][0-35-9]|6[2567]|7[0-8])\d{8}))$/
|
||||
if (value === '' || value === undefined || value == null) {
|
||||
callback()
|
||||
} else {
|
||||
if ((!reg.test(value)) && value !== '') {
|
||||
callback(new Error('请输入正确的电话号码或者固话号码'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 是否固话*/
|
||||
export function IsValidTelephone(rule, value, callback) {
|
||||
const reg = /0\d{2}-\d{7,8}/
|
||||
if (value === '' || value === undefined || value == null) {
|
||||
callback()
|
||||
} else {
|
||||
if ((!reg.test(value)) && value !== '') {
|
||||
callback(new Error('请输入正确的固话(格式:区号+号码,如010-1234567)'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 是否手机号码*/
|
||||
export function IsValidPhone(rule, value, callback) {
|
||||
const reg = /^1([38][0-9]|4[014-9]|[59][0-35-9]|6[2567]|7[0-8])\d{8}$/
|
||||
if (value === '' || value === undefined || value == null) {
|
||||
callback()
|
||||
} else {
|
||||
if ((!reg.test(value)) && value !== '') {
|
||||
callback(new Error('请输入正确的电话号码'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 是否身份证号码*/
|
||||
export function IsValidIdNumber(rule, value, callback) {
|
||||
const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
|
||||
if (value === '' || value === undefined || value == null) {
|
||||
callback()
|
||||
} else {
|
||||
if ((!reg.test(value)) && value !== '') {
|
||||
callback(new Error('请输入正确的身份证号码'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 是否链接
|
||||
* @param {string} path
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function IsExternal(path) {
|
||||
return /^(https?:|mailto:|tel:)/.test(path)
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 是否有效用户名
|
||||
// * @param {string} str
|
||||
// * @returns {Boolean}
|
||||
// */
|
||||
// export function IsValidUsername(str) {
|
||||
// const validMap = ['admin', 'editor']
|
||||
// return validMap.indexOf(str.trim()) >= 0
|
||||
// }
|
||||
|
||||
/**
|
||||
* 是否有效链接
|
||||
* @param {string} url
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function IsValidURL(url) {
|
||||
const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
|
||||
return reg.test(url)
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否小写字母
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function IsValidLowerCaseLetter(str) {
|
||||
const reg = /^[a-z]+$/
|
||||
return reg.test(str)
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否大写字母
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function IsValidUpperCase(str) {
|
||||
const reg = /^[A-Z]+$/
|
||||
return reg.test(str)
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否包含大、小写字母
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function IsValidLetter(str) {
|
||||
const reg = /^[A-Za-z]+$/
|
||||
return reg.test(str)
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否邮箱
|
||||
* @param {string} email
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function IsValidEmail(email) {
|
||||
const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
||||
return reg.test(email)
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 是否手机号
|
||||
// * @param phone
|
||||
// * @returns {boolean}
|
||||
// * @constructor
|
||||
// */
|
||||
// export function IsValidPhone(phone) {
|
||||
// const reg = /^1([38][0-9]|4[014-9]|[59][0-35-9]|6[2567]|7[0-8])\d{8}$/
|
||||
// return reg.test(phone)
|
||||
// }
|
||||
|
||||
/**
|
||||
* 是否是字符串
|
||||
* @param {string} str
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function IsString(str) {
|
||||
return typeof str === 'string' || str instanceof String
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否数组
|
||||
* @param {Array} arg
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
export function IsArray(arg) {
|
||||
if (typeof Array.isArray === 'undefined') {
|
||||
return Object.prototype.toString.call(arg) === '[object Array]'
|
||||
}
|
||||
return Array.isArray(arg)
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否合法IP地址
|
||||
* @param rule
|
||||
* @param value
|
||||
* @param callback
|
||||
*/
|
||||
export function IsValidIP(rule, value, callback) {
|
||||
if (value === '' || value === undefined || value == null) {
|
||||
callback()
|
||||
} else {
|
||||
const reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
|
||||
if ((!reg.test(value)) && value !== '') {
|
||||
callback(new Error('请输入正确的IP地址'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 是否手机号码或者固话*/
|
||||
export function IsValidPhoneTwo(rule, value, callback) {
|
||||
const reg = /^((0\d{2,3}-\d{7,8})|(1([38][0-9]|4[014-9]|[59][0-35-9]|6[2567]|7[0-8])\d{8}))$/
|
||||
if (value === '' || value === undefined || value == null) {
|
||||
callback()
|
||||
} else {
|
||||
if ((!reg.test(value)) && value !== '') {
|
||||
callback(new Error('请输入正确的电话号码或者固话号码'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 是否固话*/
|
||||
export function IsValidTelephone(rule, value, callback) {
|
||||
const reg = /0\d{2}-\d{7,8}/
|
||||
if (value === '' || value === undefined || value == null) {
|
||||
callback()
|
||||
} else {
|
||||
if ((!reg.test(value)) && value !== '') {
|
||||
callback(new Error('请输入正确的固话(格式:区号+号码,如010-1234567)'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 是否手机号码*/
|
||||
export function IsValidPhone(value) {
|
||||
const reg = /^1([38][0-9]|4[014-9]|[59][0-35-9]|6[2567]|7[0-8])\d{8}$/
|
||||
if (value === '' || value === undefined || value == null) {
|
||||
return false
|
||||
} else {
|
||||
return !((!reg.test(value)) && value !== '')
|
||||
}
|
||||
}
|
||||
|
||||
/* 是否身份证号码*/
|
||||
export function IsValidIdNumber(rule, value, callback) {
|
||||
const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
|
||||
if (value === '' || value === undefined || value == null) {
|
||||
callback()
|
||||
} else {
|
||||
if ((!reg.test(value)) && value !== '') {
|
||||
callback(new Error('请输入正确的身份证号码'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +1,41 @@
|
||||
import store from '@/store'
|
||||
|
||||
/**
|
||||
* WebSocket客户端
|
||||
*/
|
||||
function CsWsClient(sid) {
|
||||
this.wsUri = store.getters && store.getters.websocketApi.replace('{sid}', sid)
|
||||
this.client = null
|
||||
/**
|
||||
* 初始化WebSocket连接
|
||||
* @param handleError -> function (e) { e是异常本身 }
|
||||
* @param handleMessage -> function (e) { e.data是message }
|
||||
*/
|
||||
this.connect = function(handleError, handleMessage) {
|
||||
this.client = new WebSocket(this.wsUri)
|
||||
// 连接发生错误
|
||||
this.client.onerror = handleError
|
||||
// 收到消息
|
||||
this.client.onmessage = handleMessage
|
||||
return this
|
||||
}
|
||||
/**
|
||||
* 发送客户端数据
|
||||
* @param data 客户端数据
|
||||
*/
|
||||
this.sendData = function(data) {
|
||||
this.client.send(JSON.stringify(data))
|
||||
}
|
||||
/**
|
||||
* 用于关闭ws连接
|
||||
*/
|
||||
this.close = function() {
|
||||
console.info('关闭连接')
|
||||
if (this.client.readyState === 1) {
|
||||
this.sendData([{}])
|
||||
this.client.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default CsWsClient
|
||||
import store from '@/store'
|
||||
|
||||
/**
|
||||
* WebSocket客户端
|
||||
*/
|
||||
function CsWsClient(sid) {
|
||||
this.wsUri = store.getters && store.getters.websocketApi.replace('{sid}', sid)
|
||||
this.client = null
|
||||
/**
|
||||
* 初始化WebSocket连接
|
||||
* @param handleError -> function (e) { e是异常本身 }
|
||||
* @param handleMessage -> function (e) { e.data是message }
|
||||
*/
|
||||
this.connect = function(handleError, handleMessage) {
|
||||
this.client = new WebSocket(this.wsUri)
|
||||
// 连接发生错误
|
||||
this.client.onerror = handleError
|
||||
// 收到消息
|
||||
this.client.onmessage = handleMessage
|
||||
return this
|
||||
}
|
||||
/**
|
||||
* 发送客户端数据
|
||||
* @param data 客户端数据
|
||||
*/
|
||||
this.sendData = function(data) {
|
||||
this.client.send(JSON.stringify(data))
|
||||
}
|
||||
/**
|
||||
* 用于关闭ws连接
|
||||
*/
|
||||
this.close = function() {
|
||||
console.info('关闭连接')
|
||||
if (this.client.readyState === 1) {
|
||||
this.sendData([{}])
|
||||
this.client.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default CsWsClient
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import Cookies from 'js-cookie'
|
||||
import Config from '@/settings'
|
||||
|
||||
const TokenKey = Config.TokenKey
|
||||
|
||||
export function getToken() {
|
||||
return Cookies.get(TokenKey)
|
||||
}
|
||||
|
||||
export function setToken(token, rememberMe) {
|
||||
if (rememberMe) {
|
||||
return Cookies.set(TokenKey, token, { expires: Config.tokenCookieExpires })
|
||||
} else return Cookies.set(TokenKey, token)
|
||||
}
|
||||
|
||||
export function removeToken() {
|
||||
return Cookies.remove(TokenKey)
|
||||
}
|
||||
import Cookies from 'js-cookie'
|
||||
import Config from '@/settings'
|
||||
|
||||
const TokenKey = Config.TokenKey
|
||||
|
||||
export function getToken() {
|
||||
return Cookies.get(TokenKey)
|
||||
}
|
||||
|
||||
export function setToken(token, rememberMe) {
|
||||
if (rememberMe) {
|
||||
return Cookies.set(TokenKey, token, { expires: Config.tokenCookieExpires })
|
||||
} else return Cookies.set(TokenKey, token)
|
||||
}
|
||||
|
||||
export function removeToken() {
|
||||
return Cookies.remove(TokenKey)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import Vue from 'vue'
|
||||
|
||||
/**
|
||||
* 全屏锁定加载动画
|
||||
*/
|
||||
// export default function CsLoading() {
|
||||
// this.instance = null
|
||||
// this.show = function() {
|
||||
// this.instance = Vue.prototype.$loading({
|
||||
// lock: true,
|
||||
// text: 'Loading',
|
||||
// spinner: 'el-icon-loading',
|
||||
// background: 'rgba(0, 0, 0, 0.7)'
|
||||
// })
|
||||
// }
|
||||
// this.close = function() {
|
||||
// if (this.instance) {
|
||||
// this.instance.close()
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
export default class CsLoading {
|
||||
constructor() {
|
||||
this.instance = null
|
||||
}
|
||||
|
||||
show() {
|
||||
this.instance = Vue.prototype.$loading({
|
||||
lock: true,
|
||||
text: 'Loading',
|
||||
spinner: 'el-icon-loading',
|
||||
background: 'rgba(0, 0, 0, 0.7)'
|
||||
})
|
||||
}
|
||||
|
||||
close() {
|
||||
if (this.instance) {
|
||||
this.instance.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,91 +1,91 @@
|
||||
import axios from 'axios'
|
||||
import router from '@/router/routers'
|
||||
import { Notification } from 'element-ui'
|
||||
import store from '../store'
|
||||
import { getToken } from '@/utils/auth'
|
||||
import Config from '@/settings'
|
||||
import Cookies from 'js-cookie'
|
||||
|
||||
// 创建axios实例
|
||||
const service = axios.create({
|
||||
baseURL: process.env.NODE_ENV === 'production' ? process.env.VUE_APP_BASE_API : '/', // api 的 base_url
|
||||
timeout: Config.timeout // 请求超时时间
|
||||
})
|
||||
|
||||
// request拦截器
|
||||
service.interceptors.request.use(
|
||||
config => {
|
||||
if (getToken()) {
|
||||
// 让每个请求携带自定义token 请根据实际情况自行修改
|
||||
config.headers['Authorization'] = getToken()
|
||||
}
|
||||
config.headers['Content-Type'] = 'application/json'
|
||||
return config
|
||||
},
|
||||
error => {
|
||||
Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// response 拦截器
|
||||
service.interceptors.response.use(
|
||||
response => {
|
||||
const data = response.data
|
||||
// console.error('data', data)
|
||||
return data
|
||||
},
|
||||
error => {
|
||||
// 兼容blob下载出错json提示
|
||||
if (error.response.data instanceof Blob && error.response.data.type.toLowerCase().indexOf('json') !== -1) {
|
||||
const reader = new FileReader()
|
||||
reader.readAsText(error.response.data, 'utf-8')
|
||||
reader.onload = function(e) {
|
||||
const errorMsg = JSON.parse(reader.result).message
|
||||
Notification.error({
|
||||
title: errorMsg,
|
||||
duration: 5000
|
||||
})
|
||||
}
|
||||
} else {
|
||||
let code = 0
|
||||
try {
|
||||
code = error.response.data.status
|
||||
} catch (e) {
|
||||
if (error.toString().indexOf('Error: timeout') !== -1) {
|
||||
Notification.error({
|
||||
title: '网络请求超时',
|
||||
duration: 5000
|
||||
})
|
||||
return Promise.reject(error)
|
||||
}
|
||||
}
|
||||
// console.log(code)
|
||||
if (code) {
|
||||
if (code === 401) {
|
||||
store.dispatch('LogOut').then(() => {
|
||||
// 用户登录界面提示
|
||||
Cookies.set('point', 401)
|
||||
location.reload()
|
||||
})
|
||||
} else if (code === 403) {
|
||||
router.push({ path: '/401' })
|
||||
} else {
|
||||
const errorMsg = error.response.data.message
|
||||
if (errorMsg !== undefined) {
|
||||
Notification.error({
|
||||
title: errorMsg,
|
||||
duration: 5000
|
||||
})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Notification.error({
|
||||
title: '接口请求失败',
|
||||
duration: 5000
|
||||
})
|
||||
}
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
export default service
|
||||
import axios from 'axios'
|
||||
import router from '@/router/routers'
|
||||
import { Notification } from 'element-ui'
|
||||
import store from '../store'
|
||||
import { getToken } from '@/utils/auth'
|
||||
import Config from '@/settings'
|
||||
import Cookies from 'js-cookie'
|
||||
|
||||
// 创建axios实例
|
||||
const service = axios.create({
|
||||
baseURL: process.env.NODE_ENV === 'production' ? process.env.VUE_APP_BASE_API : '/', // api 的 base_url
|
||||
timeout: Config.timeout // 请求超时时间
|
||||
})
|
||||
|
||||
// request拦截器
|
||||
service.interceptors.request.use(
|
||||
config => {
|
||||
if (getToken()) {
|
||||
// 让每个请求携带自定义token 请根据实际情况自行修改
|
||||
config.headers['Authorization'] = getToken()
|
||||
}
|
||||
config.headers['Content-Type'] = 'application/json'
|
||||
return config
|
||||
},
|
||||
error => {
|
||||
Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// response 拦截器
|
||||
service.interceptors.response.use(
|
||||
response => {
|
||||
const data = response.data
|
||||
// console.error('data', data)
|
||||
return data
|
||||
},
|
||||
error => {
|
||||
// 兼容blob下载出错json提示
|
||||
if (error.response.data instanceof Blob && error.response.data.type.toLowerCase().indexOf('json') !== -1) {
|
||||
const reader = new FileReader()
|
||||
reader.readAsText(error.response.data, 'utf-8')
|
||||
reader.onload = function(e) {
|
||||
const errorMsg = JSON.parse(reader.result).message
|
||||
Notification.error({
|
||||
title: errorMsg,
|
||||
duration: 5000
|
||||
})
|
||||
}
|
||||
} else {
|
||||
let code = 0
|
||||
try {
|
||||
code = error.response.data.status
|
||||
} catch (e) {
|
||||
if (error.toString().indexOf('Error: timeout') !== -1) {
|
||||
Notification.error({
|
||||
title: '网络请求超时',
|
||||
duration: 5000
|
||||
})
|
||||
return Promise.reject(error)
|
||||
}
|
||||
}
|
||||
// console.log(code)
|
||||
if (code) {
|
||||
if (code === 401) {
|
||||
store.dispatch('LogOut').then(() => {
|
||||
// 用户登录界面提示
|
||||
Cookies.set('point', 401)
|
||||
location.reload()
|
||||
})
|
||||
} else if (code === 403) {
|
||||
router.push({ path: '/401' })
|
||||
} else {
|
||||
const errorMsg = error.response.data.message
|
||||
if (errorMsg !== undefined) {
|
||||
Notification.error({
|
||||
title: errorMsg,
|
||||
duration: 5000
|
||||
})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Notification.error({
|
||||
title: '接口请求失败',
|
||||
duration: 5000
|
||||
})
|
||||
}
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
export default service
|
||||
|
||||
@@ -1,76 +1,76 @@
|
||||
import Date from '../plugins/DateExtendPlugin.js'
|
||||
|
||||
export const calendarBaseShortcuts = [{
|
||||
text: '今天',
|
||||
onClick(picker) {
|
||||
const startTime = new Date(new Date().setHours(0, 0, 0))
|
||||
const endTime = new Date(new Date().setHours(23, 59, 59))
|
||||
picker.$emit('pick', [startTime, endTime])
|
||||
}
|
||||
}, {
|
||||
text: '昨天',
|
||||
onClick(picker) {
|
||||
const startTime = new Date(new Date().daysAgo(1).setHours(0, 0, 0))
|
||||
const endTime = new Date(new Date().daysAgo(1).setHours(23, 59, 59))
|
||||
picker.$emit('pick', [startTime, endTime])
|
||||
}
|
||||
}, {
|
||||
text: '本周',
|
||||
onClick(picker) {
|
||||
const startTime = new Date(new Date().daysAgo(new Date().getDay() - 1).setHours(0, 0, 0))
|
||||
const endTime = new Date(new Date().setHours(23, 59, 59))
|
||||
picker.$emit('pick', [startTime, endTime])
|
||||
}
|
||||
}, {
|
||||
text: '这个月',
|
||||
onClick(picker) {
|
||||
const startTime = new Date(new Date().monthBegin().setHours(0, 0, 0))
|
||||
const endTime = new Date(new Date().setHours(23, 59, 59))
|
||||
picker.$emit('pick', [startTime, endTime])
|
||||
}
|
||||
}, {
|
||||
text: '当前季度',
|
||||
onClick(picker) {
|
||||
const startTime = new Date(new Date().quarterBegin().setHours(0, 0, 0))
|
||||
const endTime = new Date(new Date().setHours(23, 59, 59))
|
||||
picker.$emit('pick', [startTime, endTime])
|
||||
}
|
||||
}, {
|
||||
text: '最近30天',
|
||||
onClick(picker) {
|
||||
const startTime = new Date(new Date().daysAgo(30).setHours(0, 0, 0))
|
||||
const endTime = new Date(new Date().setHours(23, 59, 59))
|
||||
picker.$emit('pick', [startTime, endTime])
|
||||
}
|
||||
}]
|
||||
|
||||
export const calendarMoveShortcuts = [{
|
||||
text: '‹ 往前一天 ',
|
||||
onClick(picker) {
|
||||
let startTime = new Date(new Date().setHours(0, 0, 0))
|
||||
let endTime = new Date(new Date().setHours(23, 59, 59))
|
||||
if (!picker.value) {
|
||||
picker.value = [startTime, endTime]
|
||||
}
|
||||
startTime = picker.value[0].daysAgo(1)
|
||||
endTime = picker.value[1].daysAgo(1)
|
||||
picker.$emit('pick', [startTime, endTime])
|
||||
}
|
||||
}, {
|
||||
text: ' 往后一天 ›',
|
||||
onClick(picker) {
|
||||
let startTime = new Date(new Date().setHours(0, 0, 0))
|
||||
let endTime = new Date(new Date().setHours(23, 59, 59))
|
||||
if (!picker.value) {
|
||||
picker.value = [startTime, endTime]
|
||||
}
|
||||
startTime = picker.value[0].daysAgo(-1)
|
||||
endTime = picker.value[1].daysAgo(-1)
|
||||
picker.$emit('pick', [startTime, endTime])
|
||||
}
|
||||
}]
|
||||
|
||||
export const calendarShortcuts = [
|
||||
...calendarBaseShortcuts,
|
||||
...calendarMoveShortcuts
|
||||
]
|
||||
import Date from '../plugins/DateExtendPlugin.js'
|
||||
|
||||
export const calendarBaseShortcuts = [{
|
||||
text: '今天',
|
||||
onClick(picker) {
|
||||
const startTime = new Date(new Date().setHours(0, 0, 0))
|
||||
const endTime = new Date(new Date().setHours(23, 59, 59))
|
||||
picker.$emit('pick', [startTime, endTime])
|
||||
}
|
||||
}, {
|
||||
text: '昨天',
|
||||
onClick(picker) {
|
||||
const startTime = new Date(new Date().daysAgo(1).setHours(0, 0, 0))
|
||||
const endTime = new Date(new Date().daysAgo(1).setHours(23, 59, 59))
|
||||
picker.$emit('pick', [startTime, endTime])
|
||||
}
|
||||
}, {
|
||||
text: '本周',
|
||||
onClick(picker) {
|
||||
const startTime = new Date(new Date().daysAgo(new Date().getDay() - 1).setHours(0, 0, 0))
|
||||
const endTime = new Date(new Date().setHours(23, 59, 59))
|
||||
picker.$emit('pick', [startTime, endTime])
|
||||
}
|
||||
}, {
|
||||
text: '这个月',
|
||||
onClick(picker) {
|
||||
const startTime = new Date(new Date().monthBegin().setHours(0, 0, 0))
|
||||
const endTime = new Date(new Date().setHours(23, 59, 59))
|
||||
picker.$emit('pick', [startTime, endTime])
|
||||
}
|
||||
}, {
|
||||
text: '当前季度',
|
||||
onClick(picker) {
|
||||
const startTime = new Date(new Date().quarterBegin().setHours(0, 0, 0))
|
||||
const endTime = new Date(new Date().setHours(23, 59, 59))
|
||||
picker.$emit('pick', [startTime, endTime])
|
||||
}
|
||||
}, {
|
||||
text: '最近30天',
|
||||
onClick(picker) {
|
||||
const startTime = new Date(new Date().daysAgo(30).setHours(0, 0, 0))
|
||||
const endTime = new Date(new Date().setHours(23, 59, 59))
|
||||
picker.$emit('pick', [startTime, endTime])
|
||||
}
|
||||
}]
|
||||
|
||||
export const calendarMoveShortcuts = [{
|
||||
text: '‹ 往前一天 ',
|
||||
onClick(picker) {
|
||||
let startTime = new Date(new Date().setHours(0, 0, 0))
|
||||
let endTime = new Date(new Date().setHours(23, 59, 59))
|
||||
if (!picker.value) {
|
||||
picker.value = [startTime, endTime]
|
||||
}
|
||||
startTime = picker.value[0].daysAgo(1)
|
||||
endTime = picker.value[1].daysAgo(1)
|
||||
picker.$emit('pick', [startTime, endTime])
|
||||
}
|
||||
}, {
|
||||
text: ' 往后一天 ›',
|
||||
onClick(picker) {
|
||||
let startTime = new Date(new Date().setHours(0, 0, 0))
|
||||
let endTime = new Date(new Date().setHours(23, 59, 59))
|
||||
if (!picker.value) {
|
||||
picker.value = [startTime, endTime]
|
||||
}
|
||||
startTime = picker.value[0].daysAgo(-1)
|
||||
endTime = picker.value[1].daysAgo(-1)
|
||||
picker.$emit('pick', [startTime, endTime])
|
||||
}
|
||||
}]
|
||||
|
||||
export const calendarShortcuts = [
|
||||
...calendarBaseShortcuts,
|
||||
...calendarMoveShortcuts
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user