init commit & 混料程序模块
This commit is contained in:
24
src/utils/filters.js
Normal file
24
src/utils/filters.js
Normal file
@@ -0,0 +1,24 @@
|
||||
/** filters */
|
||||
import moment from 'moment'
|
||||
|
||||
export const dictFilter = dictTypeId => {
|
||||
return val => {
|
||||
return JSON.parse(localStorage.getItem('dictList'))[dictTypeId].find(item => item.dictValue === val)?.dictLabel || '-'
|
||||
}
|
||||
}
|
||||
|
||||
export const timeFilter = (val) => {
|
||||
return moment(val).format('YYYY-MM-DD HH:mm:ss')
|
||||
}
|
||||
|
||||
export const pick = (obj, paths) => {
|
||||
let result = {}
|
||||
paths.forEach(key => {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
result[key] = obj[key];
|
||||
} else {
|
||||
result[key] = null
|
||||
}
|
||||
})
|
||||
return result
|
||||
}
|
||||
99
src/utils/index.js
Normal file
99
src/utils/index.js
Normal file
@@ -0,0 +1,99 @@
|
||||
import Cookies from 'js-cookie'
|
||||
import store from '@/store'
|
||||
|
||||
/**
|
||||
* 权限
|
||||
* @param {*} key
|
||||
*/
|
||||
export function hasPermission (key) {
|
||||
return window.SITE_CONFIG['permissions'].indexOf(key) !== -1 || false
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字典数据列表
|
||||
* @param dictType 字典类型
|
||||
*/
|
||||
export function getDictDataList (dictType) {
|
||||
const type = window.SITE_CONFIG['dictList'].find((element) => (element.dictType === dictType))
|
||||
if (type) {
|
||||
return type.dataList
|
||||
} else {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字典名称
|
||||
* @param dictType 字典类型
|
||||
* @param dictValue 字典值
|
||||
*/
|
||||
export function getDictLabel (dictType, dictValue) {
|
||||
const type = window.SITE_CONFIG['dictList'].find((element) => (element.dictType === dictType))
|
||||
if (type) {
|
||||
const val = type.dataList.find((element) => (element.dictValue === dictValue + ''))
|
||||
if (val) {
|
||||
return val.dictLabel
|
||||
} else {
|
||||
return dictValue
|
||||
}
|
||||
} else {
|
||||
return dictValue
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除登录信息
|
||||
*/
|
||||
export function clearLoginInfo () {
|
||||
store.commit('resetStore')
|
||||
Cookies.remove('token')
|
||||
window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] = false
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取uuid
|
||||
*/
|
||||
export function getUUID () {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
|
||||
return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取svg图标(id)列表
|
||||
*/
|
||||
export function getIconList () {
|
||||
var res = []
|
||||
var list = document.querySelectorAll('svg symbol')
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
res.push(list[i].id)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
/**
|
||||
* 树形数据转换
|
||||
* @param {*} data
|
||||
* @param {*} id
|
||||
* @param {*} pid
|
||||
*/
|
||||
export function treeDataTranslate (data, id = 'id', pid = 'pid') {
|
||||
var res = []
|
||||
var temp = {}
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
temp[data[i][id]] = data[i]
|
||||
}
|
||||
for (var k = 0; k < data.length; k++) {
|
||||
if (!temp[data[k][pid]] || data[k][id] === data[k][pid]) {
|
||||
res.push(data[k])
|
||||
continue
|
||||
}
|
||||
if (!temp[data[k][pid]]['children']) {
|
||||
temp[data[k][pid]]['children'] = []
|
||||
}
|
||||
temp[data[k][pid]]['children'].push(data[k])
|
||||
data[k]['_level'] = (temp[data[k][pid]]._level || 0) + 1
|
||||
}
|
||||
return res
|
||||
}
|
||||
65
src/utils/request.js
Normal file
65
src/utils/request.js
Normal file
@@ -0,0 +1,65 @@
|
||||
import axios from 'axios'
|
||||
import Cookies from 'js-cookie'
|
||||
import router from '@/router'
|
||||
import qs from 'qs'
|
||||
import { clearLoginInfo } from '@/utils'
|
||||
import isPlainObject from 'lodash/isPlainObject'
|
||||
|
||||
const http = axios.create({
|
||||
baseURL: window.SITE_CONFIG['apiURL'],
|
||||
timeout: 1000 * 180,
|
||||
withCredentials: true
|
||||
})
|
||||
|
||||
/**
|
||||
* 请求拦截
|
||||
*/
|
||||
http.interceptors.request.use(config => {
|
||||
config.headers['Accept-Language'] = Cookies.get('language') || 'zh-CN'
|
||||
config.headers['token'] = Cookies.get('token') || ''
|
||||
console.log('[request interceptor] token is:', config.headers['token'])
|
||||
// 默认参数
|
||||
var defaults = {}
|
||||
// 防止缓存,GET请求默认带_t参数
|
||||
if (config.method === 'get') {
|
||||
config.params = {
|
||||
...config.params,
|
||||
...{ '_t': new Date().getTime() }
|
||||
}
|
||||
}
|
||||
if (isPlainObject(config.params)) {
|
||||
config.params = {
|
||||
...defaults,
|
||||
...config.params
|
||||
}
|
||||
}
|
||||
if (isPlainObject(config.data)) {
|
||||
config.data = {
|
||||
...defaults,
|
||||
...config.data
|
||||
}
|
||||
if (/^application\/x-www-form-urlencoded/.test(config.headers['content-type'])) {
|
||||
config.data = qs.stringify(config.data)
|
||||
}
|
||||
}
|
||||
return config
|
||||
}, error => {
|
||||
return Promise.reject(error)
|
||||
})
|
||||
|
||||
/**
|
||||
* 响应拦截
|
||||
*/
|
||||
http.interceptors.response.use(response => {
|
||||
if (response.data.code === 401 || response.data.code === 10001) {
|
||||
clearLoginInfo()
|
||||
router.replace({ name: 'login' })
|
||||
return Promise.reject(response.data.msg)
|
||||
}
|
||||
return response
|
||||
}, error => {
|
||||
console.error(error)
|
||||
return Promise.reject(error)
|
||||
})
|
||||
|
||||
export default http
|
||||
31
src/utils/validate.js
Normal file
31
src/utils/validate.js
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* 邮箱
|
||||
* @param {*} s
|
||||
*/
|
||||
export function isEmail (s) {
|
||||
return /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s)
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机号码
|
||||
* @param {*} s
|
||||
*/
|
||||
export function isMobile (s) {
|
||||
return /^1[0-9]{10}$/.test(s)
|
||||
}
|
||||
|
||||
/**
|
||||
* 电话号码
|
||||
* @param {*} s
|
||||
*/
|
||||
export function isPhone (s) {
|
||||
return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s)
|
||||
}
|
||||
|
||||
/**
|
||||
* URL地址
|
||||
* @param {*} s
|
||||
*/
|
||||
export function isURL (s) {
|
||||
return /^http[s]?:\/\/.*/.test(s)
|
||||
}
|
||||
Reference in New Issue
Block a user