util.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. import {
  2. validatenull
  3. } from './validate'
  4. /**
  5. * 动态插入css
  6. */
  7. export const loadStyle = (url, callback) => {
  8. if (!isInclude(url)) {
  9. const link = document.createElement('link');
  10. link.type = 'text/css';
  11. link.rel = 'stylesheet';
  12. link.href = url;
  13. const head = document.getElementsByTagName('head')[0];
  14. addCallback(link, callback)
  15. head.appendChild(link);
  16. } else {
  17. callback && callback()
  18. }
  19. };
  20. /**
  21. * 动态插入js
  22. */
  23. export const loadScript = (url, callback) => {
  24. if (!isInclude(url)) {
  25. const link = document.createElement('script');
  26. link.src = url;
  27. link.type = 'text/javascript'
  28. const head = document.getElementsByTagName('head')[0];
  29. addCallback(link, callback)
  30. head.appendChild(link);
  31. } else {
  32. callback && callback()
  33. }
  34. };
  35. /**
  36. * 判断是否已经存在某个css 或者js
  37. * @param {*} name
  38. */
  39. const isInclude = (name) => {
  40. var js = /js$/i.test(name);
  41. var es = document.getElementsByTagName(js ? 'script' : 'link');
  42. for (var i = 0; i < es.length; i++)
  43. if (es[i][js ? 'src' : 'href'].indexOf(name) != -1) return true;
  44. return false;
  45. }
  46. const addCallback = (obj, callback) => {
  47. if (obj.addEventListener) {
  48. obj.addEventListener('load', function() {
  49. callback && callback();
  50. }, false);
  51. } else if (obj.attachEvent) {
  52. obj.attachEvent('onreadystatechange', function() {
  53. var target = window.event.srcElement;
  54. if (target.readyState == 'loaded') {
  55. callback && callback();
  56. }
  57. });
  58. }
  59. }
  60. //表单序列化
  61. export const serialize = data => {
  62. let list = [];
  63. Object.keys(data).forEach(ele => {
  64. list.push(`${ele}=${data[ele]}`)
  65. })
  66. return list.join('&');
  67. };
  68. export const getObjType = obj => {
  69. var toString = Object.prototype.toString;
  70. var map = {
  71. '[object Boolean]': 'boolean',
  72. '[object Number]': 'number',
  73. '[object String]': 'string',
  74. '[object Function]': 'function',
  75. '[object Array]': 'array',
  76. '[object Date]': 'date',
  77. '[object RegExp]': 'regExp',
  78. '[object Undefined]': 'undefined',
  79. '[object Null]': 'null',
  80. '[object Object]': 'object'
  81. };
  82. if (obj instanceof Element) {
  83. return 'element';
  84. }
  85. return map[toString.call(obj)];
  86. };
  87. /**
  88. * 对象深拷贝
  89. */
  90. export const deepClone = data => {
  91. var type = getObjType(data);
  92. var obj;
  93. if (type === 'array') {
  94. obj = [];
  95. } else if (type === 'object') {
  96. obj = {};
  97. } else {
  98. //不再具有下一层次
  99. return data;
  100. }
  101. if (type === 'array') {
  102. for (var i = 0, len = data.length; i < len; i++) {
  103. obj.push(deepClone(data[i]));
  104. }
  105. } else if (type === 'object') {
  106. for (var key in data) {
  107. obj[key] = deepClone(data[key]);
  108. }
  109. }
  110. return obj;
  111. };
  112. /**
  113. * 设置灰度模式
  114. */
  115. export const toggleGrayMode = (status) => {
  116. if (status) {
  117. document.body.className = document.body.className + ' grayMode';
  118. } else {
  119. document.body.className = document.body.className.replace(' grayMode', '');
  120. }
  121. };
  122. /**
  123. * 设置主题
  124. */
  125. export const setTheme = (name) => {
  126. document.body.className = name;
  127. }
  128. /**
  129. * 加密处理
  130. */
  131. export const encryption = (params) => {
  132. let {
  133. data,
  134. type,
  135. param,
  136. key
  137. } = params;
  138. let result = JSON.parse(JSON.stringify(data));
  139. if (type == 'Base64') {
  140. param.forEach(ele => {
  141. result[ele] = btoa(result[ele]);
  142. })
  143. } else if (type == 'Aes') {
  144. param.forEach(ele => {
  145. result[ele] = window.CryptoJS.AES.encrypt(result[ele], key).toString();
  146. })
  147. }
  148. return result;
  149. };
  150. /**
  151. * 浏览器判断是否全屏
  152. */
  153. export const fullscreenToggel = () => {
  154. if (fullscreenEnable()) {
  155. exitFullScreen();
  156. } else {
  157. reqFullScreen();
  158. }
  159. };
  160. /**
  161. * esc监听全屏
  162. */
  163. export const listenfullscreen = (callback) => {
  164. function listen() {
  165. callback()
  166. }
  167. document.addEventListener('fullscreenchange', function() {
  168. listen();
  169. });
  170. document.addEventListener('mozfullscreenchange', function() {
  171. listen();
  172. });
  173. document.addEventListener('webkitfullscreenchange', function() {
  174. listen();
  175. });
  176. document.addEventListener('msfullscreenchange', function() {
  177. listen();
  178. });
  179. };
  180. /**
  181. * 浏览器判断是否全屏
  182. */
  183. export const fullscreenEnable = () => {
  184. var isFullscreen = document.isFullScreen || document.mozIsFullScreen || document.webkitIsFullScreen
  185. return isFullscreen;
  186. }
  187. /**
  188. * 浏览器全屏
  189. */
  190. export const reqFullScreen = () => {
  191. let _documentDom = document.getElementById('index')
  192. if (_documentDom.requestFullScreen) {
  193. _documentDom.requestFullScreen();
  194. } else if (_documentDom.webkitRequestFullScreen) {
  195. _documentDom.webkitRequestFullScreen();
  196. } else if (_documentDom.mozRequestFullScreen) {
  197. _documentDom.mozRequestFullScreen();
  198. }
  199. // if (document.documentElement.requestFullScreen) {
  200. // document.documentElement.requestFullScreen();
  201. // } else if (document.documentElement.webkitRequestFullScreen) {
  202. // document.documentElement.webkitRequestFullScreen();
  203. // } else if (document.documentElement.mozRequestFullScreen) {
  204. // document.documentElement.mozRequestFullScreen();
  205. // }
  206. };
  207. /**
  208. * 浏览器退出全屏
  209. */
  210. export const exitFullScreen = () => {
  211. let _documentDom = document.getElementById('contentView')
  212. if (_documentDom.requestFullScreen) {
  213. document.exitFullScreen();
  214. } else if (_documentDom.webkitRequestFullScreen) {
  215. document.webkitCancelFullScreen();
  216. } else if (_documentDom.mozRequestFullScreen) {
  217. document.mozCancelFullScreen();
  218. }
  219. // if (document.documentElement.requestFullScreen) {
  220. // document.exitFullScreen();
  221. // } else if (document.documentElement.webkitRequestFullScreen) {
  222. // document.webkitCancelFullScreen();
  223. // } else if (document.documentElement.mozRequestFullScreen) {
  224. // document.mozCancelFullScreen();
  225. // }
  226. };
  227. /**
  228. * 递归寻找子类的父类
  229. */
  230. export const findParent = (menu, id) => {
  231. for (let i = 0; i < menu.length; i++) {
  232. if (menu[i].children.length != 0) {
  233. for (let j = 0; j < menu[i].children.length; j++) {
  234. if (menu[i].children[j].id == id) {
  235. return menu[i];
  236. } else {
  237. if (menu[i].children[j].children.length != 0) {
  238. return findParent(menu[i].children[j].children, id);
  239. }
  240. }
  241. }
  242. }
  243. }
  244. };
  245. /**
  246. * 判断路由是否相等
  247. */
  248. export const diff = (obj1, obj2) => {
  249. delete obj1.close;
  250. var o1 = obj1 instanceof Object;
  251. var o2 = obj2 instanceof Object;
  252. if (!o1 || !o2) {
  253. /* 判断不是对象 */
  254. return obj1 === obj2;
  255. }
  256. if (Object.keys(obj1).length !== Object.keys(obj2).length) {
  257. return false;
  258. //Object.keys() 返回一个由对象的自身可枚举属性(key值)组成的数组,例如:数组返回下表:let arr = ["a", "b", "c"];console.log(Object.keys(arr))->0,1,2;
  259. }
  260. for (var attr in obj1) {
  261. var t1 = obj1[attr] instanceof Object;
  262. var t2 = obj2[attr] instanceof Object;
  263. if (t1 && t2) {
  264. return diff(obj1[attr], obj2[attr]);
  265. } else if (obj1[attr] !== obj2[attr]) {
  266. return false;
  267. }
  268. }
  269. return true;
  270. }
  271. /**
  272. * 根据字典的value显示label
  273. */
  274. export const findByvalue = (dic, value) => {
  275. let result = '';
  276. if (validatenull(dic)) return value;
  277. if (typeof(value) == 'string' || typeof(value) == 'number' || typeof(value) == 'boolean') {
  278. let index = 0;
  279. index = findArray(dic, value);
  280. if (index != -1) {
  281. result = dic[index].label;
  282. } else {
  283. result = value;
  284. }
  285. } else if (value instanceof Array) {
  286. result = [];
  287. let index = 0;
  288. value.forEach(ele => {
  289. index = findArray(dic, ele);
  290. if (index != -1) {
  291. result.push(dic[index].label);
  292. } else {
  293. result.push(value);
  294. }
  295. });
  296. result = result.toString();
  297. }
  298. return result;
  299. };
  300. /**
  301. * 根据字典的value查找对应的index
  302. */
  303. export const findArray = (dic, value) => {
  304. for (let i = 0; i < dic.length; i++) {
  305. if (dic[i].value == value) {
  306. return i;
  307. }
  308. }
  309. return -1;
  310. };
  311. /**
  312. * 生成随机len位数字
  313. */
  314. export const randomLenNum = (len, date) => {
  315. let random = '';
  316. random = Math.ceil(Math.random() * 100000000000000).toString().substr(0, len ? len : 4);
  317. if (date) random = random + Date.now();
  318. return random;
  319. };
  320. /**
  321. * 打开小窗口
  322. */
  323. export const openWindow = (url, title, w, h) => {
  324. // Fixes dual-screen position Most browsers Firefox
  325. const dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : screen.left
  326. const dualScreenTop = window.screenTop !== undefined ? window.screenTop : screen.top
  327. const width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document
  328. .documentElement.clientWidth : screen.width
  329. const height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document
  330. .documentElement.clientHeight : screen.height
  331. const left = ((width / 2) - (w / 2)) + dualScreenLeft
  332. const top = ((height / 2) - (h / 2)) + dualScreenTop
  333. const newWindow = window.open(url, title,
  334. 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=' +
  335. w + ', height=' + h + ', top=' + top + ', left=' + left)
  336. // Puts focus on the newWindow
  337. if (window.focus) {
  338. newWindow.focus()
  339. }
  340. }
  341. // 路由处理 开始
  342. export const isURL = (s) => {
  343. return /^http[s]?:\/\/.*/.test(s);
  344. }
  345. export const objToform = (obj) => {
  346. let result = [];
  347. Object.keys(obj).forEach(ele => {
  348. result.push(`${ele}=${obj[ele]}`);
  349. });
  350. return result.join('&');
  351. }
  352. // 设置标题
  353. export const setTitle = title => {
  354. const defaultTitle = this.$t('title');
  355. title = title ? `${title}-${defaultTitle}` : defaultTitle;
  356. document.title = title;
  357. }
  358. export const closeTag = value => {
  359. let tag = value || this.$store.getters.tag;
  360. if (typeof value === 'string') {
  361. tag = this.$store.getters.tagList.filter(ele => ele.value === value)[0];
  362. }
  363. this.$store.commit('DEL_TAG', tag);
  364. }
  365. export const generateTitle = (title, key, _this) => {
  366. const hasKey = _this.$te('route.' + (key || title));
  367. if (hasKey) {
  368. const translatedTitle = _this.$t('route.' + (key || title))
  369. return translatedTitle;
  370. }
  371. console.log(title)
  372. return title;
  373. }
  374. //处理路由
  375. export const getPath = (params) => {
  376. let {
  377. src
  378. } = params;
  379. let result = src || '/';
  380. if (src.includes('http') || src.includes('https')) {
  381. result = `/myiframe/urlPath?${objToform(params)}`;
  382. }
  383. return result;
  384. }
  385. //设置路由值
  386. export const getValue = (route) => {
  387. let value = '';
  388. if (route.query.src) {
  389. value = route.query.src;
  390. } else {
  391. value = route.path;
  392. }
  393. return value;
  394. }
  395. // 路由处理 结束
  396. // null赋值空字符串
  397. export const nullToString = (obj) => {
  398. Object.keys(obj).forEach(function(key) {
  399. console.log(key, obj[key])
  400. if (obj[key] === null) {
  401. obj[key] = ''
  402. }
  403. })
  404. }