util.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. import {validatenull} from './validate'
  2. //表单序列化
  3. export const serialize = data => {
  4. let list = [];
  5. Object.keys(data).forEach(ele => {
  6. list.push(`${ele}=${data[ele]}`)
  7. })
  8. return list.join('&');
  9. };
  10. export const getObjType = obj => {
  11. var toString = Object.prototype.toString;
  12. var map = {
  13. '[object Boolean]': 'boolean',
  14. '[object Number]': 'number',
  15. '[object String]': 'string',
  16. '[object Function]': 'function',
  17. '[object Array]': 'array',
  18. '[object Date]': 'date',
  19. '[object RegExp]': 'regExp',
  20. '[object Undefined]': 'undefined',
  21. '[object Null]': 'null',
  22. '[object Object]': 'object'
  23. };
  24. if (obj instanceof Element) {
  25. return 'element';
  26. }
  27. return map[toString.call(obj)];
  28. };
  29. export const getViewDom = () => {
  30. return window.document.getElementById('avue-view').getElementsByClassName('el-scrollbar__wrap')[0]
  31. }
  32. /**
  33. * 对象深拷贝
  34. */
  35. export const deepClone = data => {
  36. var type = getObjType(data);
  37. var obj;
  38. if (type === 'array') {
  39. obj = [];
  40. } else if (type === 'object') {
  41. obj = {};
  42. } else {
  43. //不再具有下一层次
  44. return data;
  45. }
  46. if (type === 'array') {
  47. for (var i = 0, len = data.length; i < len; i++) {
  48. obj.push(deepClone(data[i]));
  49. }
  50. } else if (type === 'object') {
  51. for (var key in data) {
  52. obj[key] = deepClone(data[key]);
  53. }
  54. }
  55. return obj;
  56. };
  57. /**
  58. * 设置灰度模式
  59. */
  60. export const toggleGrayMode = (status) => {
  61. if (status) {
  62. document.body.className = document.body.className + ' grayMode';
  63. } else {
  64. document.body.className = document.body.className.replace(' grayMode', '');
  65. }
  66. };
  67. /**
  68. * 设置主题
  69. */
  70. export const setTheme = (name) => {
  71. document.body.className = name;
  72. }
  73. /**
  74. * 加密处理
  75. */
  76. export const encryption = (params) => {
  77. let {
  78. data,
  79. type,
  80. param,
  81. key
  82. } = params;
  83. let result = JSON.parse(JSON.stringify(data));
  84. if (type == 'Base64') {
  85. param.forEach(ele => {
  86. result[ele] = btoa(result[ele]);
  87. })
  88. } else if (type == 'Aes') {
  89. param.forEach(ele => {
  90. result[ele] = window.CryptoJS.AES.encrypt(result[ele], key).toString();
  91. })
  92. }
  93. return result;
  94. };
  95. /**
  96. * 浏览器判断是否全屏
  97. */
  98. export const fullscreenToggel = () => {
  99. if (fullscreenEnable()) {
  100. exitFullScreen();
  101. } else {
  102. reqFullScreen();
  103. }
  104. };
  105. /**
  106. * esc监听全屏
  107. */
  108. export const listenfullscreen = (callback) => {
  109. function listen() {
  110. callback()
  111. }
  112. document.addEventListener("fullscreenchange", function () {
  113. listen();
  114. });
  115. document.addEventListener("mozfullscreenchange", function () {
  116. listen();
  117. });
  118. document.addEventListener("webkitfullscreenchange", function () {
  119. listen();
  120. });
  121. document.addEventListener("msfullscreenchange", function () {
  122. listen();
  123. });
  124. };
  125. /**
  126. * 浏览器判断是否全屏
  127. */
  128. export const fullscreenEnable = () => {
  129. var isFullscreen = document.isFullScreen || document.mozIsFullScreen || document.webkitIsFullScreen
  130. return isFullscreen;
  131. }
  132. /**
  133. * 浏览器全屏
  134. */
  135. export const reqFullScreen = () => {
  136. if (document.documentElement.requestFullScreen) {
  137. document.documentElement.requestFullScreen();
  138. } else if (document.documentElement.webkitRequestFullScreen) {
  139. document.documentElement.webkitRequestFullScreen();
  140. } else if (document.documentElement.mozRequestFullScreen) {
  141. document.documentElement.mozRequestFullScreen();
  142. }
  143. };
  144. /**
  145. * 浏览器退出全屏
  146. */
  147. export const exitFullScreen = () => {
  148. if (document.documentElement.requestFullScreen) {
  149. document.exitFullScreen();
  150. } else if (document.documentElement.webkitRequestFullScreen) {
  151. document.webkitCancelFullScreen();
  152. } else if (document.documentElement.mozRequestFullScreen) {
  153. document.mozCancelFullScreen();
  154. }
  155. };
  156. /**
  157. * 递归寻找子类的父类
  158. */
  159. export const findParent = (menu, id) => {
  160. for (let i = 0; i < menu.length; i++) {
  161. if (menu[i].children.length != 0) {
  162. for (let j = 0; j < menu[i].children.length; j++) {
  163. if (menu[i].children[j].id == id) {
  164. return menu[i];
  165. } else {
  166. if (menu[i].children[j].children.length != 0) {
  167. return findParent(menu[i].children[j].children, id);
  168. }
  169. }
  170. }
  171. }
  172. }
  173. };
  174. /**
  175. * 判断2个对象属性和值是否相等
  176. */
  177. /**
  178. * 动态插入css
  179. */
  180. export const loadStyle = url => {
  181. const link = document.createElement('link');
  182. link.type = 'text/css';
  183. link.rel = 'stylesheet';
  184. link.href = url;
  185. const head = document.getElementsByTagName('head')[0];
  186. head.appendChild(link);
  187. };
  188. /**
  189. * 判断路由是否相等
  190. */
  191. export const diff = (obj1, obj2) => {
  192. delete obj1.close;
  193. var o1 = obj1 instanceof Object;
  194. var o2 = obj2 instanceof Object;
  195. if (!o1 || !o2) { /* 判断不是对象 */
  196. return obj1 === obj2;
  197. }
  198. if (Object.keys(obj1).length !== Object.keys(obj2).length) {
  199. return false;
  200. //Object.keys() 返回一个由对象的自身可枚举属性(key值)组成的数组,例如:数组返回下表:let arr = ["a", "b", "c"];console.log(Object.keys(arr))->0,1,2;
  201. }
  202. for (var attr in obj1) {
  203. var t1 = obj1[attr] instanceof Object;
  204. var t2 = obj2[attr] instanceof Object;
  205. if (t1 && t2) {
  206. return diff(obj1[attr], obj2[attr]);
  207. } else if (obj1[attr] !== obj2[attr]) {
  208. return false;
  209. }
  210. }
  211. return true;
  212. }
  213. /**
  214. * 根据字典的value显示label
  215. */
  216. export const findByvalue = (dic, value) => {
  217. let result = '';
  218. if (validatenull(dic)) return value;
  219. if (typeof (value) == 'string' || typeof (value) == 'number' || typeof (value) == 'boolean') {
  220. let index = 0;
  221. index = findArray(dic, value);
  222. if (index != -1) {
  223. result = dic[index].label;
  224. } else {
  225. result = value;
  226. }
  227. } else if (value instanceof Array) {
  228. result = [];
  229. let index = 0;
  230. value.forEach(ele => {
  231. index = findArray(dic, ele);
  232. if (index != -1) {
  233. result.push(dic[index].label);
  234. } else {
  235. result.push(value);
  236. }
  237. });
  238. result = result.toString();
  239. }
  240. return result;
  241. };
  242. /**
  243. * 根据字典的value查找对应的index
  244. */
  245. export const findArray = (dic, value) => {
  246. for (let i = 0; i < dic.length; i++) {
  247. if (dic[i].value == value) {
  248. return i;
  249. }
  250. }
  251. return -1;
  252. };
  253. /**
  254. * 生成随机len位数字
  255. */
  256. export const randomLenNum = (len, date) => {
  257. let random = '';
  258. random = Math.ceil(Math.random() * 100000000000000).toString().substr(0, len ? len : 4);
  259. if (date) random = random + Date.now();
  260. return random;
  261. };
  262. /**
  263. * 打开小窗口
  264. */
  265. export const openWindow = (url, title, w, h) => {
  266. // Fixes dual-screen position Most browsers Firefox
  267. const dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : screen.left
  268. const dualScreenTop = window.screenTop !== undefined ? window.screenTop : screen.top
  269. const width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width
  270. const height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height
  271. const left = ((width / 2) - (w / 2)) + dualScreenLeft
  272. const top = ((height / 2) - (h / 2)) + dualScreenTop
  273. 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)
  274. // Puts focus on the newWindow
  275. if (window.focus) {
  276. newWindow.focus()
  277. }
  278. }
  279. /**
  280. * 获取顶部地址栏地址
  281. */
  282. export const getTopUrl = () => {
  283. return window.location.href.split("/#/")[0];
  284. }
  285. /**
  286. * 获取url参数
  287. * @param name 参数名
  288. */
  289. export const getQueryString = (name) => {
  290. let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  291. let r = window.location.search.substr(1).match(reg);
  292. if (r != null) return unescape(decodeURI(r[2]));
  293. return null;
  294. }
  295. /**
  296. * 下载文件
  297. * @param {String} path - 文件地址
  298. * @param {String} name - 文件名,eg: test.png
  299. */
  300. export const downloadFileBlob = (path, name) => {
  301. const xhr = new XMLHttpRequest();
  302. xhr.open('get', path);
  303. xhr.responseType = 'blob';
  304. xhr.send();
  305. xhr.onload = function () {
  306. if (this.status === 200 || this.status === 304) {
  307. // 如果是IE10及以上,不支持download属性,采用msSaveOrOpenBlob方法,但是IE10以下也不支持msSaveOrOpenBlob
  308. if ('msSaveOrOpenBlob' in navigator) {
  309. navigator.msSaveOrOpenBlob(this.response, name);
  310. return;
  311. }
  312. const url = URL.createObjectURL(this.response);
  313. const a = document.createElement('a');
  314. a.style.display = 'none';
  315. a.href = url;
  316. a.download = name;
  317. document.body.appendChild(a);
  318. a.click();
  319. document.body.removeChild(a);
  320. URL.revokeObjectURL(url);
  321. }
  322. };
  323. }
  324. /**
  325. * 下载文件
  326. * @param {String} path - 文件地址
  327. * @param {String} name - 文件名,eg: test.png
  328. */
  329. export const downloadFileBase64 = (path, name) => {
  330. const xhr = new XMLHttpRequest();
  331. xhr.open('get', path);
  332. xhr.responseType = 'blob';
  333. xhr.send();
  334. xhr.onload = function () {
  335. if (this.status === 200 || this.status === 304) {
  336. const fileReader = new FileReader();
  337. fileReader.readAsDataURL(this.response);
  338. fileReader.onload = function () {
  339. const a = document.createElement('a');
  340. a.style.display = 'none';
  341. a.href = this.result;
  342. a.download = name;
  343. document.body.appendChild(a);
  344. a.click();
  345. document.body.removeChild(a);
  346. };
  347. }
  348. };
  349. }