123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- import {
- validatenull
- } from './validate'
- /**
- * 动态插入css
- */
- export const loadStyle = (url, callback) => {
- if (!isInclude(url)) {
- const link = document.createElement('link');
- link.type = 'text/css';
- link.rel = 'stylesheet';
- link.href = url;
- const head = document.getElementsByTagName('head')[0];
- addCallback(link, callback)
- head.appendChild(link);
- } else {
- callback && callback()
- }
- };
- /**
- * 动态插入js
- */
- export const loadScript = (url, callback) => {
- if (!isInclude(url)) {
- const link = document.createElement('script');
- link.src = url;
- link.type = 'text/javascript'
- const head = document.getElementsByTagName('head')[0];
- addCallback(link, callback)
- head.appendChild(link);
- } else {
- callback && callback()
- }
- };
- /**
- * 判断是否已经存在某个css 或者js
- * @param {*} name
- */
- const isInclude = (name) => {
- var js = /js$/i.test(name);
- var es = document.getElementsByTagName(js ? 'script' : 'link');
- for (var i = 0; i < es.length; i++)
- if (es[i][js ? 'src' : 'href'].indexOf(name) != -1) return true;
- return false;
- }
- const addCallback = (obj, callback) => {
- if (obj.addEventListener) {
- obj.addEventListener('load', function() {
- callback && callback();
- }, false);
- } else if (obj.attachEvent) {
- obj.attachEvent('onreadystatechange', function() {
- var target = window.event.srcElement;
- if (target.readyState == 'loaded') {
- callback && callback();
- }
- });
- }
- }
- //表单序列化
- export const serialize = data => {
- let list = [];
- Object.keys(data).forEach(ele => {
- list.push(`${ele}=${data[ele]}`)
- })
- return list.join('&');
- };
- export const getObjType = obj => {
- var toString = Object.prototype.toString;
- var map = {
- '[object Boolean]': 'boolean',
- '[object Number]': 'number',
- '[object String]': 'string',
- '[object Function]': 'function',
- '[object Array]': 'array',
- '[object Date]': 'date',
- '[object RegExp]': 'regExp',
- '[object Undefined]': 'undefined',
- '[object Null]': 'null',
- '[object Object]': 'object'
- };
- if (obj instanceof Element) {
- return 'element';
- }
- return map[toString.call(obj)];
- };
- /**
- * 对象深拷贝
- */
- export const deepClone = data => {
- var type = getObjType(data);
- var obj;
- if (type === 'array') {
- obj = [];
- } else if (type === 'object') {
- obj = {};
- } else {
- //不再具有下一层次
- return data;
- }
- if (type === 'array') {
- for (var i = 0, len = data.length; i < len; i++) {
- obj.push(deepClone(data[i]));
- }
- } else if (type === 'object') {
- for (var key in data) {
- obj[key] = deepClone(data[key]);
- }
- }
- return obj;
- };
- /**
- * 设置灰度模式
- */
- export const toggleGrayMode = (status) => {
- if (status) {
- document.body.className = document.body.className + ' grayMode';
- } else {
- document.body.className = document.body.className.replace(' grayMode', '');
- }
- };
- /**
- * 设置主题
- */
- export const setTheme = (name) => {
- document.body.className = name;
- }
- /**
- * 加密处理
- */
- export const encryption = (params) => {
- let {
- data,
- type,
- param,
- key
- } = params;
- let result = JSON.parse(JSON.stringify(data));
- if (type == 'Base64') {
- param.forEach(ele => {
- result[ele] = btoa(result[ele]);
- })
- } else if (type == 'Aes') {
- param.forEach(ele => {
- result[ele] = window.CryptoJS.AES.encrypt(result[ele], key).toString();
- })
- }
- return result;
- };
- /**
- * 浏览器判断是否全屏
- */
- export const fullscreenToggel = () => {
- if (fullscreenEnable()) {
- exitFullScreen();
- } else {
- reqFullScreen();
- }
- };
- /**
- * esc监听全屏
- */
- export const listenfullscreen = (callback) => {
- function listen() {
- callback()
- }
- document.addEventListener('fullscreenchange', function() {
- listen();
- });
- document.addEventListener('mozfullscreenchange', function() {
- listen();
- });
- document.addEventListener('webkitfullscreenchange', function() {
- listen();
- });
- document.addEventListener('msfullscreenchange', function() {
- listen();
- });
- };
- /**
- * 浏览器判断是否全屏
- */
- export const fullscreenEnable = () => {
- var isFullscreen = document.isFullScreen || document.mozIsFullScreen || document.webkitIsFullScreen
- return isFullscreen;
- }
- /**
- * 浏览器全屏
- */
- export const reqFullScreen = () => {
- let _documentDom = document.getElementById('index')
- if (_documentDom.requestFullScreen) {
- _documentDom.requestFullScreen();
- } else if (_documentDom.webkitRequestFullScreen) {
- _documentDom.webkitRequestFullScreen();
- } else if (_documentDom.mozRequestFullScreen) {
- _documentDom.mozRequestFullScreen();
- }
- // if (document.documentElement.requestFullScreen) {
- // document.documentElement.requestFullScreen();
- // } else if (document.documentElement.webkitRequestFullScreen) {
- // document.documentElement.webkitRequestFullScreen();
- // } else if (document.documentElement.mozRequestFullScreen) {
- // document.documentElement.mozRequestFullScreen();
- // }
- };
- /**
- * 浏览器退出全屏
- */
- export const exitFullScreen = () => {
- let _documentDom = document.getElementById('contentView')
- if (_documentDom.requestFullScreen) {
- document.exitFullScreen();
- } else if (_documentDom.webkitRequestFullScreen) {
- document.webkitCancelFullScreen();
- } else if (_documentDom.mozRequestFullScreen) {
- document.mozCancelFullScreen();
- }
- // if (document.documentElement.requestFullScreen) {
- // document.exitFullScreen();
- // } else if (document.documentElement.webkitRequestFullScreen) {
- // document.webkitCancelFullScreen();
- // } else if (document.documentElement.mozRequestFullScreen) {
- // document.mozCancelFullScreen();
- // }
- };
- /**
- * 递归寻找子类的父类
- */
- export const findParent = (menu, id) => {
- for (let i = 0; i < menu.length; i++) {
- if (menu[i].children.length != 0) {
- for (let j = 0; j < menu[i].children.length; j++) {
- if (menu[i].children[j].id == id) {
- return menu[i];
- } else {
- if (menu[i].children[j].children.length != 0) {
- return findParent(menu[i].children[j].children, id);
- }
- }
- }
- }
- }
- };
- /**
- * 判断路由是否相等
- */
- export const diff = (obj1, obj2) => {
- delete obj1.close;
- var o1 = obj1 instanceof Object;
- var o2 = obj2 instanceof Object;
- if (!o1 || !o2) {
- /* 判断不是对象 */
- return obj1 === obj2;
- }
- if (Object.keys(obj1).length !== Object.keys(obj2).length) {
- return false;
- //Object.keys() 返回一个由对象的自身可枚举属性(key值)组成的数组,例如:数组返回下表:let arr = ["a", "b", "c"];console.log(Object.keys(arr))->0,1,2;
- }
- for (var attr in obj1) {
- var t1 = obj1[attr] instanceof Object;
- var t2 = obj2[attr] instanceof Object;
- if (t1 && t2) {
- return diff(obj1[attr], obj2[attr]);
- } else if (obj1[attr] !== obj2[attr]) {
- return false;
- }
- }
- return true;
- }
- /**
- * 根据字典的value显示label
- */
- export const findByvalue = (dic, value) => {
- let result = '';
- if (validatenull(dic)) return value;
- if (typeof(value) == 'string' || typeof(value) == 'number' || typeof(value) == 'boolean') {
- let index = 0;
- index = findArray(dic, value);
- if (index != -1) {
- result = dic[index].label;
- } else {
- result = value;
- }
- } else if (value instanceof Array) {
- result = [];
- let index = 0;
- value.forEach(ele => {
- index = findArray(dic, ele);
- if (index != -1) {
- result.push(dic[index].label);
- } else {
- result.push(value);
- }
- });
- result = result.toString();
- }
- return result;
- };
- /**
- * 根据字典的value查找对应的index
- */
- export const findArray = (dic, value) => {
- for (let i = 0; i < dic.length; i++) {
- if (dic[i].value == value) {
- return i;
- }
- }
- return -1;
- };
- /**
- * 生成随机len位数字
- */
- export const randomLenNum = (len, date) => {
- let random = '';
- random = Math.ceil(Math.random() * 100000000000000).toString().substr(0, len ? len : 4);
- if (date) random = random + Date.now();
- return random;
- };
- /**
- * 打开小窗口
- */
- export const openWindow = (url, title, w, h) => {
- // Fixes dual-screen position Most browsers Firefox
- const dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : screen.left
- const dualScreenTop = window.screenTop !== undefined ? window.screenTop : screen.top
- const width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document
- .documentElement.clientWidth : screen.width
- const height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document
- .documentElement.clientHeight : screen.height
- const left = ((width / 2) - (w / 2)) + dualScreenLeft
- const top = ((height / 2) - (h / 2)) + dualScreenTop
- const newWindow = window.open(url, title,
- 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=' +
- w + ', height=' + h + ', top=' + top + ', left=' + left)
- // Puts focus on the newWindow
- if (window.focus) {
- newWindow.focus()
- }
- }
- // 路由处理 开始
- export const isURL = (s) => {
- return /^http[s]?:\/\/.*/.test(s);
- }
- export const objToform = (obj) => {
- let result = [];
- Object.keys(obj).forEach(ele => {
- result.push(`${ele}=${obj[ele]}`);
- });
- return result.join('&');
- }
- // 设置标题
- export const setTitle = title => {
- const defaultTitle = this.$t('title');
- title = title ? `${title}-${defaultTitle}` : defaultTitle;
- document.title = title;
- }
- export const closeTag = value => {
- let tag = value || this.$store.getters.tag;
- if (typeof value === 'string') {
- tag = this.$store.getters.tagList.filter(ele => ele.value === value)[0];
- }
- this.$store.commit('DEL_TAG', tag);
- }
- export const generateTitle = (title, key, _this) => {
- const hasKey = _this.$te('route.' + (key || title));
- if (hasKey) {
- const translatedTitle = _this.$t('route.' + (key || title))
- return translatedTitle;
- }
- console.log(title)
- return title;
- }
- //处理路由
- export const getPath = (params) => {
- let {
- src
- } = params;
- let result = src || '/';
- if (src.includes('http') || src.includes('https')) {
- result = `/myiframe/urlPath?${objToform(params)}`;
- }
- return result;
- }
- //设置路由值
- export const getValue = (route) => {
- let value = '';
- if (route.query.src) {
- value = route.query.src;
- } else {
- value = route.path;
- }
- return value;
- }
- // 路由处理 结束
- // null赋值空字符串
- export const nullToString = (obj) => {
- Object.keys(obj).forEach(function(key) {
- console.log(key, obj[key])
- if (obj[key] === null) {
- obj[key] = ''
- }
- })
- }
|