import * as $CONST from './constant' import { Message } from 'element-ui' // import { notify } from '@nutui/nutui' export const _ = require('lodash') /** * 集合转换为JSON * @param obj collection数据 * @author TJ 2019/03/11 * @example 略 */ export function collectionToJson (collection) { if (!_.isArray(collection)) return [] const arr = [] // 数据体 _.forEach(collection, function (item) { const tempObj = {} if (Object.prototype.hasOwnProperty.call(item, 'data') && _.isArray(item.data)) { _.forEach(item.data, function (obj) { tempObj[_.trim(obj.name)] = _.trim(obj.value) }) } arr.push(tempObj) }) return arr } /** * collection数据转换为标准JSON * @param obj collection数据 * @author TJ 2019/03/11 * @example 略 */ export function transformStandardJson (obj) { let body = {} let data = null let pages = null const collection = obj.collection if (_.isObject(collection)) { // 错误的场景 if (Object.prototype.hasOwnProperty.call(collection, 'items')) { return { code: 300, body: null, msg: collection.error } } // 数据体 if (Object.prototype.hasOwnProperty.call(collection, 'items') && _.isArray(collection.items)) { data = collectionToJson(collection.items) } // 分页信息 if (Object.prototype.hasOwnProperty.call(collection, 'page') && _.isArray(collection.data)) { pages = collectionToJson([collection.page]) } // 组装body数据 body = { data: data } if (pages) { _.extend(body, { total: pages[0].totalElements, pageIndex: 0, pageSize: pages[0].size, pages: pages[0].totalPages }) } return { code: 200, body: body, msg: '操作成功' } } } /** * POST PUT等请求参数转换成集合 * @param json object * @returns collection * @author TJ 2019/03/11 * @example 略 */ export function transformParams (obj) { const collection = { version: '1.0', href: '', items: [], templates: [] } if (_.isObject(obj)) { const arr = [] _.each(obj, function (val, key) { let newVal = val if (_.isArray(val)) { newVal = val.join(',') } arr.push({ name: key, value: newVal }) }) const o = { data: arr } collection.templates.push(o) } return { collection: collection } } /** * 将内容存储到sessionStorage * @param key {string} key * @param content {Object} 存储json对象 * @author TJ 2018/05/28 * @example 略 */ export function setSessionStorage (key, content) { if (!key) return false const jsonContent = JSON.stringify(content) jsonContent ? sessionStorage.setItem(key, jsonContent) : sessionStorage.setItem(key, content) } /** * 获取存储到sessionStorage的内容 * @param key {string} key * @return {object} 返回json对象 * @author TJ 2018/05/28 * @example 略 */ export function getSessionStorage (key) { const item = sessionStorage.getItem(key) if (!item) return false const result = JSON.parse(sessionStorage.getItem(key)) return result || item } /** * 删除存储到sessionStorage的内容 * @param key {string} key * @author TJ 2018/05/28 * @example 略 */ export function removeSessionStorage (key) { sessionStorage.removeItem(key) } /** * 将内容存储到localStorage * @param key {string} key * @param content {Object} 存储json对象 * @author TJ 2018/05/28 * @example 略 */ export function setLocalStorage (key, content) { if (!key) return false const jsonContent = JSON.stringify(content) jsonContent ? localStorage.setItem(key, jsonContent) : localStorage.setItem(key, content) } /** * 获取存储到localStorage的内容 * @param key {string} key * @return {object} 返回json对象 * @author TJ 2018/05/28 * @example 略 */ export function getLocalStorage (key) { const item = localStorage.getItem(key) if (!item) return false const result = JSON.parse(localStorage.getItem(key)) return result || item } /** * localStorage * @param key {string} key * @author TJ 2018/05/28 * @example 略 */ export function removeLocalStorage (key) { localStorage.removeItem(key) } /** * 判断json对象是否为空对象 * @param obj {object} json对象 * @return {boolean} 空对象返回 true 否则返回 false * @author TJ 2018/05/28 * @example 略 */ export function isEmptyObject (obj) { if (obj === null) return true return Object.keys(obj).length === 0 } /** * 过滤参数 * @param params {object} 需要格式化的时间 * @return {object} 格式化后的时间 * @author TJ 2017/05/28 * @example 略 */ export function filterParams (params) { if (!_.isObject(params)) { return params } const newParams = {} _.each(params, function (v, k) { // 过滤掉条件是空的项 if (typeof v === 'string' && (v.length === 0 || v === '*全*部*')) { } else { newParams[k] = v } }) return newParams } /** * 将null转换为空对象 * @param params {obj} * @author TJ 2018/05/31 */ export function emptyObjectWrapper (obj) { return obj === null ? {} : obj } /** * 格式化时间 * @param time {string} 需要格式化的时间 * @param cFormat {string} 时间格式 * @return {string} 格式化后的时间 * @author TJ 2017/07/21 * @example 略 */ export function parseTime (time, cFormat) { if (!time) return false if (arguments.length === 0) { return false } const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}' let date if (typeof time === 'object') { date = time } else { if (('' + time).length === 10) time = parseInt(time) * 1000 if (('' + time).length === 8 && ('' + time).indexOf('-') === -1 && ('' + time).indexOf('/') === -1) { time = time.substring(0, 4) + '-' + time.substring(4, 6) + '-' + time.substring(6, 8) } 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 timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => { let value = formatObj[key] if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1] if (result.length > 0 && value < 10) { value = '0' + value } return value || 0 }) return timeStr } /** * 将时间数组分割成开始时间和结束时间 * @param time {array} * @return {object} * @author TJ 2017/08/01 * @example 略 */ export function formatTime (time) { if (Array.isArray(time)) { if (!time[0] || !time[1]) return false var start = parseTime(time[0], '{y}-{m}-{d}') var end = parseTime(time[1], '{y}-{m}-{d}') return { start: start, end: end } } return false } /** * 权限API拼接用户名 * @param api {String} * @return {boolean} * @author TJ 2017/08/01 * @example 略 */ export function getJoinOauthApi (url) { const name = window._loginName ? window._loginName : '' if (url.includes('?')) { url += '&createId=' + name } else { url += 'createId=' + name } return url } /** * 拼接查询URL * @param url {String} * @param obj {obj} * @return {String} * @author TJ 2017/08/01 * @example 略 */ export function joinQueryUrl (url, obj) { let str = '' let fullUrl = '' for (const key in obj) { if (!Object.prototype.hasOwnProperty.call(obj, key)) return if (str) { str += '&' } str += key + '=' + obj[key] } if (url.includes('?')) { fullUrl = url + '&' + str } else { fullUrl = url + '?' + str } return fullUrl } /** * 判断参数是否嵌入在url中 * @param url {String} * @return {Boolean} * @author TJ 2018/08/27 */ export function isInlineParams (url) { const splitChar = '{$' if (url && url.indexOf(splitChar) > -1) { return true } else { return false } } /** * 替换url里的参数 * @param url {String} * @param params {obj} * @author TJ 2018/05/31 */ export function replaceUrlParams (url, params) { if (url) { if (!isInlineParams(url)) { return url } // 正则匹配{},生成数组 const patt = /\{.*?\}/g const arr = url.match(patt) ? url.match(patt) : [] arr.forEach(function (item) { const key = item.replace('{', '').replace('}', '').replace('$', '') url = url.replace(item, params[key]) }) } return url } /** * 配置Echart主题颜色 * @param {obj} echart 实例 * @author TJ 2017/10/17 * @return 无返回结果 */ export function resgisterTheme (echart) { /* eslint-disable */ let theme = { 'color': [ '#29d0b0', '#2d99ed', '#fd8667', '#72ccff', '#f7c5a0', '#d4a4eb', '#fdc547', '#76f2f2', '#da4d00', '#b0419e' ], 'backgroundColor': 'transparents', 'textStyle': { 'itemStyle': { 'normal': { 'color': '#fff' } } }, 'title': { 'textStyle': { 'color': '#ffffff' }, 'subtextStyle': { 'color': '#dddddd' } }, 'line': { 'itemStyle': { 'normal': { 'borderWidth': '1' } }, 'lineStyle': { 'normal': { 'width': '1' } }, 'symbolSize': '4', 'symbol': 'circle', 'smooth': false }, 'radar': { 'itemStyle': { 'normal': { 'borderWidth': '4' } }, 'lineStyle': { 'normal': { 'width': '3' } }, 'symbolSize': '1', 'symbol': 'circle', 'smooth': true }, 'bar': { 'itemStyle': { 'normal': { 'barBorderWidth': 0, 'barBorderColor': '#ccc' }, 'emphasis': { 'barBorderWidth': 0, 'barBorderColor': '#ccc' } } }, 'pie': { 'itemStyle': { 'normal': { 'borderWidth': 0, 'borderColor': '#ccc' }, 'emphasis': { 'borderWidth': 0, 'borderColor': '#ccc' } } }, 'scatter': { 'itemStyle': { 'normal': { 'borderWidth': 0, 'borderColor': '#ccc' }, 'emphasis': { 'borderWidth': 0, 'borderColor': '#ccc' } } }, 'boxplot': { 'itemStyle': { 'normal': { 'borderWidth': 0, 'borderColor': '#ccc' }, 'emphasis': { 'borderWidth': 0, 'borderColor': '#ccc' } } }, 'parallel': { 'itemStyle': { 'normal': { 'borderWidth': 0, 'borderColor': '#ccc' }, 'emphasis': { 'borderWidth': 0, 'borderColor': '#ccc' } } }, 'sankey': { 'itemStyle': { 'normal': { 'borderWidth': 0, 'borderColor': '#ccc' }, 'emphasis': { 'borderWidth': 0, 'borderColor': '#ccc' } } }, 'funnel': { 'itemStyle': { 'normal': { 'borderWidth': 0, 'borderColor': '#ccc' }, 'emphasis': { 'borderWidth': 0, 'borderColor': '#ccc' } } }, 'gauge': { 'itemStyle': { 'normal': { 'borderWidth': 1, 'borderColor': '#fff' }, 'emphasis': { 'borderWidth': 1, 'borderColor': '#fff' } } }, 'candlestick': { 'itemStyle': { 'normal': { 'color': '#fc97af', 'color0': 'transparent', 'borderColor': '#fc97af', 'borderColor0': '#87f7cf', 'borderWidth': '2' } } }, 'graph': { 'itemStyle': { 'normal': { 'borderWidth': 0, 'borderColor': '#ccc' } }, 'lineStyle': { 'normal': { 'width': '1', 'color': '#ffffff' } }, 'symbolSize': '5', 'symbol': 'circle', 'smooth': true, 'color': [ '#29d0b0', '#2d99ed', '#fd8667', '#72ccff', '#f7c5a0', '#d4a4eb', '#fdc547', '#76f2f2', '#da4d00', '#b0419e' ], 'label': { 'normal': { 'textStyle': { 'color': '#293441' } } } }, 'map': { 'itemStyle': { 'normal': { 'areaColor': '#f3f3f3', 'borderColor': '#999999', 'borderWidth': 0.5 }, 'emphasis': { 'areaColor': 'rgba(255,178,72,1)', 'borderColor': '#eb8146', 'borderWidth': 1 } }, 'label': { 'normal': { 'textStyle': { 'color': '#893448' } }, 'emphasis': { 'textStyle': { 'color': 'rgb(137,52,72)' } } } }, 'geo': { 'itemStyle': { 'normal': { 'areaColor': '#f3f3f3', 'borderColor': '#999999', 'borderWidth': 0.5 }, 'emphasis': { 'areaColor': 'rgba(255,178,72,1)', 'borderColor': '#eb8146', 'borderWidth': 1 } }, 'label': { 'normal': { 'textStyle': { 'color': '#893448' } }, 'emphasis': { 'textStyle': { 'color': 'rgb(137,52,72)' } } } }, 'categoryAxis': { 'axisLine': { 'show': true, 'lineStyle': { 'color': '#fff' } }, 'axisTick': { 'show': false, 'lineStyle': { 'color': '#fff' } }, 'axisLabel': { 'show': true, 'textStyle': { 'color': '#fff' } }, 'splitLine': { 'show': false, 'lineStyle': { 'color': [ '#e6e6e6' ] } }, 'splitArea': { 'show': false, 'areaStyle': { 'color': [ 'rgba(250,250,250,0.05)', 'rgba(200,200,200,0.02)' ] } } }, 'valueAxis': { 'axisLine': { 'show': true, 'lineStyle': { 'color': '#fff' } }, 'axisTick': { 'show': false, 'lineStyle': { 'color': '#fff' } }, 'axisLabel': { 'show': true, 'textStyle': { 'color': '#fff' } }, 'splitLine': { 'show': false, 'lineStyle': { 'color': [ '#e6e6e6' ] } }, 'splitArea': { 'show': false, 'areaStyle': { 'color': [ 'rgba(250,250,250,0.05)', 'rgba(200,200,200,0.02)' ] } } }, 'logAxis': { 'axisLine': { 'show': true, 'lineStyle': { 'color': '#fff' } }, 'axisTick': { 'show': false, 'lineStyle': { 'color': '#333' } }, 'axisLabel': { 'show': true, 'textStyle': { 'color': '#fff' } }, 'splitLine': { 'show': false, 'lineStyle': { 'color': [ '#e6e6e6' ] } }, 'splitArea': { 'show': false, 'areaStyle': { 'color': [ 'rgba(250,250,250,0.05)', 'rgba(200,200,200,0.02)' ] } } }, 'timeAxis': { 'axisLine': { 'show': true, 'lineStyle': { 'color': '#fff' } }, 'axisTick': { 'show': false, 'lineStyle': { 'color': '#fff' } }, 'axisLabel': { 'show': true, 'textStyle': { 'color': '#fff' } }, 'splitLine': { 'show': false, 'lineStyle': { 'color': [ '#fff' ] } }, 'splitArea': { 'show': false, 'areaStyle': { 'color': [ 'rgba(250,250,250,0.05)', 'rgba(200,200,200,0.02)' ] } } }, 'toolbox': { 'iconStyle': { 'normal': { 'borderColor': '#999999' }, 'emphasis': { 'borderColor': '#666666' } } }, 'legend': { 'textStyle': { 'color': '#e0e0e0' } }, 'tooltip': { 'axisPointer': { 'lineStyle': { 'color': '#cccccc', 'width': 1 }, 'crossStyle': { 'color': '#cccccc', 'width': 1 } } }, 'timeline': { 'lineStyle': { 'color': '#87f7cf', 'width': 1 }, 'itemStyle': { 'normal': { 'color': '#87f7cf', 'borderWidth': 1 }, 'emphasis': { 'color': '#f7f494' } }, 'controlStyle': { 'normal': { 'color': '#87f7cf', 'borderColor': '#87f7cf', 'borderWidth': 0.5 }, 'emphasis': { 'color': '#87f7cf', 'borderColor': '#87f7cf', 'borderWidth': 0.5 } }, 'checkpointStyle': { 'color': '#fc97af', 'borderColor': 'rgba(252,151,175,0.3)' }, 'label': { 'normal': { 'textStyle': { 'color': '#87f7cf' } }, 'emphasis': { 'textStyle': { 'color': '#87f7cf' } } } }, 'visualMap': { 'color': [ '#fc97af', '#87f7cf' ] }, 'dataZoom': { 'backgroundColor': 'rgba(255,255,255,0)', 'dataBackgroundColor': 'rgba(114,204,255,1)', 'fillerColor': 'rgba(114,204,255,0.2)', 'handleColor': '#72ccff', 'handleSize': '100%', 'textStyle': { 'color': '#fff' } }, 'markPoint': { 'label': { 'normal': { 'textStyle': { 'color': '#293441' } }, 'emphasis': { 'textStyle': { 'color': '#293441' } } } } } echart.registerTheme('dark', theme) /* eslint-enable */ } /** * 根据路由切换皮肤标识,默认皮肤未蓝色 * @author TJ 2019/05/29 */ export function changeThemeClass (toRouterName) { const darkSkinRouterName = [] if (darkSkinRouterName.includes(toRouterName)) { document.body.className = 'dark' } else { document.body.className = 'blue' } } /** * 判断要查询的数组是否至少有一个元素包含在目标数组中 * @param {Array} target 目标数组 * @param {Array} arr 需要查询的数组 */ export const hasOneOf = (targetarr, arr) => { return targetarr.some(_ => arr.indexOf(_) > -1) } /** * 判断两个对象是否相等,这两个对象的值只能是数字或字符串 * @param {Object} obj1 对象 * @param {Object} obj2 对象 */ export const objEqual = (obj1, obj2) => { const keysArr1 = Object.keys(obj1) const keysArr2 = Object.keys(obj2) if (keysArr1.length !== keysArr2.length) return false else if (keysArr1.length === 0 && keysArr2.length === 0) return true /* eslint-disable-next-line */ else return !keysArr1.some(key => obj1[key] != obj2[key]) } /** * 判断值是否为真,不包括对0的判断 * @param {String} val 字符 */ export function isTrue (val) { return !_.isNull(val) && !_.isUndefined(val) && val !== '' } /** * 解析URL参数 * * @param {String} url * @return {object} */ export function getQueryObject (url = window.location.href) { if (!url) return const search = url.substring(url.lastIndexOf('?') + 1) const obj = {} const reg = /([^?&=]+)=([^?&=]*)/g search.replace(reg, function (rs, $1, $2) { const name = decodeURIComponent($1) let val = decodeURIComponent($2) val = String(val) obj[name] = val // return rs }) return obj } /** * 递归查询树节点 * * @param {object} treeData 树结构数据集合 * @param {string} prop 属性字段 * @param {string} propValue 属性对应的值 * @return {object} 节点对象 */ export function recursion (tree, prop, propValue) { if (!tree || !Array.isArray(tree)) return false let result = {} function handelTree (tree) { for (const o of tree) { if (o.children && Array.isArray(o.children) && o.children.length) { if (o[prop] !== propValue) { // 递归 handelTree(o.children) } else { result = o break } } else { if (o[prop] === propValue) { result = o } } } return result } return handelTree(tree) } export function success (msg = $CONST.MSG_SYS_SUCCESS) { Message({ message: msg, type: 'success' }) } export function fail (msg = $CONST.MSG_SYS_FAIL) { Message({ message: msg, type: 'error' }) } export function error (msg = $CONST.MSG_SYS_ERR) { Message({ message: msg, type: 'error' }) } export function warning (msg = $CONST.MSG_SYS_WARNING) { Message({ message: msg, type: 'warning' }) } export function info (msg = $CONST.MSG_SYS_CANCELED) { Message({ message: msg, type: 'info' }) } /** * 异步加载组件 * @author TJ 2019/05/30 */ /* eslint-disable */ export function lazyLoadView(AsyncView) { const AsyncComponent = () => ({ // 需要加载的组件 (应该是一个 `Promise` 对象) component: AsyncView, // 异步组件加载时使用的组件 loading: '', // 加载失败时使用的组件 error: '', // 展示加载时组件的延时时间。默认值是 200 (毫秒) delay: 200, // 如果提供了超时时间且组件加载也超时了, // 则使用加载失败时使用的组件。默认值是:`Infinity` timeout: 3000 }) return Promise.resolve({ functional: true, render(h, { data, children }) { // Transparently pass any props or children // to the view component. return h(AsyncComponent, data, children) } }) }