util.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. if (document.documentElement.requestFullScreen) {
  192. document.documentElement.requestFullScreen();
  193. } else if (document.documentElement.webkitRequestFullScreen) {
  194. document.documentElement.webkitRequestFullScreen();
  195. } else if (document.documentElement.mozRequestFullScreen) {
  196. document.documentElement.mozRequestFullScreen();
  197. }
  198. };
  199. /**
  200. * 浏览器退出全屏
  201. */
  202. export const exitFullScreen = () => {
  203. if (document.documentElement.requestFullScreen) {
  204. document.exitFullScreen();
  205. } else if (document.documentElement.webkitRequestFullScreen) {
  206. document.webkitCancelFullScreen();
  207. } else if (document.documentElement.mozRequestFullScreen) {
  208. document.mozCancelFullScreen();
  209. }
  210. };
  211. /**
  212. * 递归寻找子类的父类
  213. */
  214. export const findParent = (menu, id) => {
  215. for (let i = 0; i < menu.length; i++) {
  216. if (menu[i].children.length != 0) {
  217. for (let j = 0; j < menu[i].children.length; j++) {
  218. if (menu[i].children[j].id == id) {
  219. return menu[i];
  220. } else {
  221. if (menu[i].children[j].children.length != 0) {
  222. return findParent(menu[i].children[j].children, id);
  223. }
  224. }
  225. }
  226. }
  227. }
  228. };
  229. /**
  230. * 判断路由是否相等
  231. */
  232. export const diff = (obj1, obj2) => {
  233. delete obj1.close;
  234. var o1 = obj1 instanceof Object;
  235. var o2 = obj2 instanceof Object;
  236. if (!o1 || !o2) {
  237. /* 判断不是对象 */
  238. return obj1 === obj2;
  239. }
  240. if (Object.keys(obj1).length !== Object.keys(obj2).length) {
  241. return false;
  242. //Object.keys() 返回一个由对象的自身可枚举属性(key值)组成的数组,例如:数组返回下表:let arr = ["a", "b", "c"];console.log(Object.keys(arr))->0,1,2;
  243. }
  244. for (var attr in obj1) {
  245. var t1 = obj1[attr] instanceof Object;
  246. var t2 = obj2[attr] instanceof Object;
  247. if (t1 && t2) {
  248. return diff(obj1[attr], obj2[attr]);
  249. } else if (obj1[attr] !== obj2[attr]) {
  250. return false;
  251. }
  252. }
  253. return true;
  254. }
  255. /**
  256. * 根据字典的value显示label
  257. */
  258. export const findByvalue = (dic, value) => {
  259. let result = '';
  260. if (validatenull(dic)) return value;
  261. if (typeof(value) == 'string' || typeof(value) == 'number' || typeof(value) == 'boolean') {
  262. let index = 0;
  263. index = findArray(dic, value);
  264. if (index != -1) {
  265. result = dic[index].label;
  266. } else {
  267. result = value;
  268. }
  269. } else if (value instanceof Array) {
  270. result = [];
  271. let index = 0;
  272. value.forEach(ele => {
  273. index = findArray(dic, ele);
  274. if (index != -1) {
  275. result.push(dic[index].label);
  276. } else {
  277. result.push(value);
  278. }
  279. });
  280. result = result.toString();
  281. }
  282. return result;
  283. };
  284. /**
  285. * 根据字典的value查找对应的index
  286. */
  287. export const findArray = (dic, value) => {
  288. for (let i = 0; i < dic.length; i++) {
  289. if (dic[i].value == value) {
  290. return i;
  291. }
  292. }
  293. return -1;
  294. };
  295. /**
  296. * 生成随机len位数字
  297. */
  298. export const randomLenNum = (len, date) => {
  299. let random = '';
  300. random = Math.ceil(Math.random() * 100000000000000).toString().substr(0, len ? len : 4);
  301. if (date) random = random + Date.now();
  302. return random;
  303. };
  304. /**
  305. * 打开小窗口
  306. */
  307. export const openWindow = (url, title, w, h) => {
  308. // Fixes dual-screen position Most browsers Firefox
  309. const dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : screen.left
  310. const dualScreenTop = window.screenTop !== undefined ? window.screenTop : screen.top
  311. const width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document
  312. .documentElement.clientWidth : screen.width
  313. const height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document
  314. .documentElement.clientHeight : screen.height
  315. const left = ((width / 2) - (w / 2)) + dualScreenLeft
  316. const top = ((height / 2) - (h / 2)) + dualScreenTop
  317. const newWindow = window.open(url, title,
  318. 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=' +
  319. w + ', height=' + h + ', top=' + top + ', left=' + left)
  320. // Puts focus on the newWindow
  321. if (window.focus) {
  322. newWindow.focus()
  323. }
  324. }
  325. // 路由处理 开始
  326. export const isURL = (s) => {
  327. return /^http[s]?:\/\/.*/.test(s);
  328. }
  329. export const objToform = (obj) => {
  330. let result = [];
  331. Object.keys(obj).forEach(ele => {
  332. result.push(`${ele}=${obj[ele]}`);
  333. });
  334. return result.join('&');
  335. }
  336. // 设置标题
  337. export const setTitle = title => {
  338. const defaultTitle = this.$t('title');
  339. title = title ? `${title}-${defaultTitle}` : defaultTitle;
  340. document.title = title;
  341. }
  342. export const closeTag = value => {
  343. let tag = value || this.$store.getters.tag;
  344. if (typeof value === 'string') {
  345. tag = this.$store.getters.tagList.filter(ele => ele.value === value)[0];
  346. }
  347. this.$store.commit('DEL_TAG', tag);
  348. }
  349. export const generateTitle = (title, key, _this) => {
  350. const hasKey = _this.$te('route.' + (key || title));
  351. if (hasKey) {
  352. const translatedTitle = _this.$t('route.' + (key || title))
  353. return translatedTitle;
  354. }
  355. console.log(title)
  356. return title;
  357. }
  358. //处理路由
  359. export const getPath = (params) => {
  360. let {
  361. src
  362. } = params;
  363. let result = src || '/';
  364. if (src.includes('http') || src.includes('https')) {
  365. result = `/myiframe/urlPath?${objToform(params)}`;
  366. }
  367. return result;
  368. }
  369. //设置路由值
  370. export const getValue = (route) => {
  371. let value = '';
  372. if (route.query.src) {
  373. value = route.query.src;
  374. } else {
  375. value = route.path;
  376. }
  377. return value;
  378. }
  379. // 路由处理 结束
  380. // null赋值空字符串
  381. export const nullToString = (obj) => {
  382. Object.keys(obj).forEach(function(key) {
  383. console.log(key, obj[key])
  384. if (obj[key] === null) {
  385. obj[key] = ''
  386. }
  387. })
  388. }