You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

414 lines
11 KiB

  1. import { validatenull } from './validate'
  2. import request from '@/router/axios'
  3. import * as CryptoJS from 'crypto-js'
  4. // 表单序列化
  5. export const serialize = data => {
  6. const list = []
  7. Object.keys(data).forEach(ele => {
  8. list.push(`${ele}=${data[ele]}`)
  9. })
  10. return list.join('&')
  11. }
  12. export const getObjType = obj => {
  13. var toString = Object.prototype.toString
  14. var map = {
  15. '[object Boolean]': 'boolean',
  16. '[object Number]': 'number',
  17. '[object String]': 'string',
  18. '[object Function]': 'function',
  19. '[object Array]': 'array',
  20. '[object Date]': 'date',
  21. '[object RegExp]': 'regExp',
  22. '[object Undefined]': 'undefined',
  23. '[object Null]': 'null',
  24. '[object Object]': 'object'
  25. }
  26. if (obj instanceof Element) {
  27. return 'element'
  28. }
  29. return map[toString.call(obj)]
  30. }
  31. /**
  32. * 对象深拷贝
  33. */
  34. export const deepClone = data => {
  35. var type = getObjType(data)
  36. var obj
  37. if (type === 'array') {
  38. obj = []
  39. } else if (type === 'object') {
  40. obj = {}
  41. } else {
  42. // 不再具有下一层次
  43. return data
  44. }
  45. if (type === 'array') {
  46. for (var i = 0, len = data.length; i < len; i++) {
  47. obj.push(deepClone(data[i]))
  48. }
  49. } else if (type === 'object') {
  50. for (var key in data) {
  51. obj[key] = deepClone(data[key])
  52. }
  53. }
  54. return obj
  55. }
  56. /**
  57. * 判断路由是否相等
  58. */
  59. export const diff = (obj1, obj2) => {
  60. delete obj1.close
  61. var o1 = obj1 instanceof Object
  62. var o2 = obj2 instanceof Object
  63. if (!o1 || !o2) { /* 判断不是对象 */
  64. // console.log(1);
  65. return obj1 === obj2
  66. }
  67. if (Object.keys(obj1).length !== Object.keys(obj2).length) {
  68. // console.log(2);
  69. console.log(Object.keys(obj1).length,Object.keys(obj2).length);
  70. return false
  71. // Object.keys() 返回一个由对象的自身可枚举属性(key值)组成的数组,例如:数组返回下表:let arr = ["a", "b", "c"];console.log(Object.keys(arr))->0,1,2;
  72. }
  73. for (var attr in obj1) {
  74. var t1 = obj1[attr] instanceof Object
  75. var t2 = obj2[attr] instanceof Object
  76. if (t1 && t2) {
  77. // console.log(3);
  78. return diff(obj1[attr], obj2[attr])
  79. } else if (obj1[attr] !== obj2[attr]) {
  80. // console.log(obj1[attr],obj2[attr]);
  81. // console.log(4);
  82. return false
  83. }
  84. }
  85. // console.log(5);
  86. return true
  87. }
  88. /**
  89. * 设置灰度模式
  90. */
  91. export const toggleGrayMode = (status) => {
  92. if (status) {
  93. document.body.className = document.body.className + ' grayMode'
  94. } else {
  95. document.body.className = document.body.className.replace(' grayMode', '')
  96. }
  97. }
  98. /**
  99. * 设置主题
  100. */
  101. export const setTheme = (name) => {
  102. document.body.className = name
  103. }
  104. /**
  105. *加密处理
  106. */
  107. export const encryption = (params) => {
  108. let {
  109. data,
  110. type,
  111. param,
  112. key
  113. } = params
  114. const result = JSON.parse(JSON.stringify(data))
  115. if (type === 'Base64') {
  116. param.forEach(ele => {
  117. result[ele] = btoa(result[ele])
  118. })
  119. } else {
  120. param.forEach(ele => {
  121. var data = result[ele]
  122. key = CryptoJS.enc.Latin1.parse(key)
  123. var iv = key
  124. // 加密
  125. var encrypted = CryptoJS.AES.encrypt(
  126. data,
  127. key, {
  128. iv: iv,
  129. mode: CryptoJS.mode.CBC,
  130. padding: CryptoJS.pad.ZeroPadding
  131. })
  132. result[ele] = encrypted.toString()
  133. })
  134. }
  135. return result
  136. }
  137. /**
  138. * 浏览器判断是否全屏
  139. */
  140. export const fullscreenToggel = () => {
  141. if (fullscreenEnable()) {
  142. exitFullScreen()
  143. } else {
  144. reqFullScreen()
  145. }
  146. }
  147. /**
  148. * esc监听全屏
  149. */
  150. export const listenfullscreen = (callback) => {
  151. function listen() {
  152. callback()
  153. }
  154. document.addEventListener('fullscreenchange', function() {
  155. listen()
  156. })
  157. document.addEventListener('mozfullscreenchange', function() {
  158. listen()
  159. })
  160. document.addEventListener('webkitfullscreenchange', function() {
  161. listen()
  162. })
  163. document.addEventListener('msfullscreenchange', function() {
  164. listen()
  165. })
  166. }
  167. /**
  168. * 浏览器判断是否全屏
  169. */
  170. export const fullscreenEnable = () => {
  171. return document.isFullScreen || document.mozIsFullScreen || document.webkitIsFullScreen
  172. }
  173. /**
  174. * 浏览器全屏
  175. */
  176. export const reqFullScreen = () => {
  177. if (document.documentElement.requestFullScreen) {
  178. document.documentElement.requestFullScreen()
  179. } else if (document.documentElement.webkitRequestFullScreen) {
  180. document.documentElement.webkitRequestFullScreen()
  181. } else if (document.documentElement.mozRequestFullScreen) {
  182. document.documentElement.mozRequestFullScreen()
  183. }
  184. }
  185. /**
  186. * 浏览器退出全屏
  187. */
  188. export const exitFullScreen = () => {
  189. if (document.documentElement.requestFullScreen) {
  190. document.exitFullScreen()
  191. } else if (document.documentElement.webkitRequestFullScreen) {
  192. document.webkitCancelFullScreen()
  193. } else if (document.documentElement.mozRequestFullScreen) {
  194. document.mozCancelFullScreen()
  195. }
  196. }
  197. /**
  198. * 递归寻找子类的父类
  199. */
  200. export const findParent = (menu, id) => {
  201. for (let i = 0; i < menu.length; i++) {
  202. if (menu[i].children.length !== 0) {
  203. for (let j = 0; j < menu[i].children.length; j++) {
  204. if (menu[i].children[j].id === id) {
  205. return menu[i]
  206. } else {
  207. if (menu[i].children[j].children.length !== 0) {
  208. return findParent(menu[i].children[j].children, id)
  209. }
  210. }
  211. }
  212. }
  213. }
  214. }
  215. /**
  216. * 动态插入css
  217. */
  218. export const loadStyle = url => {
  219. const link = document.createElement('link')
  220. link.type = 'text/css'
  221. link.rel = 'stylesheet'
  222. link.href = url
  223. const head = document.getElementsByTagName('head')[0]
  224. head.appendChild(link)
  225. }
  226. /**
  227. * 判断路由是否相等
  228. */
  229. export const isObjectValueEqual = (a, b) => {
  230. let result = true
  231. Object.keys(a).forEach(ele => {
  232. const type = typeof (a[ele])
  233. if (type === 'string' && a[ele] !== b[ele]) result = false
  234. else if (type === 'object' && JSON.stringify(a[ele]) !== JSON.stringify(b[ele])) result = false
  235. })
  236. return result
  237. }
  238. /**
  239. * 根据字典的value显示label
  240. */
  241. export const findByvalue = (dic, value) => {
  242. let result = ''
  243. if (validatenull(dic)) return value
  244. if (typeof (value) === 'string' || typeof (value) === 'number' || typeof (value) === 'boolean') {
  245. let index = 0
  246. index = findArray(dic, value)
  247. if (index !== -1) {
  248. result = dic[index].label
  249. } else {
  250. result = value
  251. }
  252. } else if (value instanceof Array) {
  253. result = []
  254. let index = 0
  255. value.forEach(ele => {
  256. index = findArray(dic, ele)
  257. if (index !== -1) {
  258. result.push(dic[index].label)
  259. } else {
  260. result.push(value)
  261. }
  262. })
  263. result = result.toString()
  264. }
  265. return result
  266. }
  267. /**
  268. * 根据字典的value查找对应的index
  269. */
  270. export const findArray = (dic, value) => {
  271. for (let i = 0; i < dic.length; i++) {
  272. if (dic[i].value === value) {
  273. return i
  274. }
  275. }
  276. return -1
  277. }
  278. /**
  279. * 生成随机len位数字
  280. */
  281. export const randomLenNum = (len, date) => {
  282. let random = ''
  283. random = Math.ceil(Math.random() * 100000000000000).toString().substr(0, len || 4)
  284. if (date) random = random + Date.now()
  285. return random
  286. }
  287. /**
  288. * 打开小窗口
  289. */
  290. export const openWindow = (url, title, w, h) => {
  291. // Fixes dual-screen position Most browsers Firefox
  292. const dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : screen.left
  293. const dualScreenTop = window.screenTop !== undefined ? window.screenTop : screen.top
  294. const width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width
  295. const height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height
  296. const left = ((width / 2) - (w / 2)) + dualScreenLeft
  297. const top = ((height / 2) - (h / 2)) + dualScreenTop
  298. 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)
  299. // Puts focus on the newWindow
  300. if (window.focus) {
  301. newWindow.focus()
  302. }
  303. }
  304. /**
  305. * <img> <a> src 处理
  306. * @returns {PromiseLike<T | never> | Promise<T | never>}
  307. */
  308. export function handleImg(url, id) {
  309. return validatenull(url) ? null : request({
  310. url: url,
  311. method: 'get',
  312. responseType: 'blob'
  313. }).then((response) => { // 处理返回的文件流
  314. const blob = response.data
  315. const img = document.getElementById(id)
  316. img.src = URL.createObjectURL(blob)
  317. window.setTimeout(function() {
  318. window.URL.revokeObjectURL(blob)
  319. }, 0)
  320. })
  321. }
  322. export function handleDown(filename, bucket) {
  323. return request({
  324. url: '/admin/sys-file/' + bucket + '/' + filename,
  325. method: 'get',
  326. responseType: 'blob'
  327. }).then((response) => { // 处理返回的文件流
  328. const blob = response.data
  329. const link = document.createElement('a')
  330. link.href = URL.createObjectURL(blob)
  331. link.download = filename
  332. document.body.appendChild(link)
  333. link.click()
  334. window.setTimeout(function() {
  335. URL.revokeObjectURL(blob)
  336. document.body.removeChild(link)
  337. }, 0)
  338. })
  339. }
  340. export function getQueryString(url, paraName) {
  341. const arrObj = url.split('?')
  342. if (arrObj.length > 1) {
  343. const arrPara = arrObj[1].split('&')
  344. let arr
  345. for (let i = 0; i < arrPara.length; i++) {
  346. arr = arrPara[i].split('=')
  347. // eslint-disable-next-line eqeqeq
  348. if (arr != null && arr[0] == paraName) {
  349. return arr[1]
  350. }
  351. }
  352. return ''
  353. } else {
  354. return ''
  355. }
  356. }
  357. // 导出.Excel公用方法
  358. export function exportMethodPost(url, name, data = {}) {
  359. axios({
  360. method: "get",
  361. url: url,
  362. params:data,
  363. responseType: "blob",
  364. })
  365. .then((res) => {
  366. console.log(res,'数据');
  367. if(res.size==0){
  368. this.$message.warning('当前报表没数据')
  369. console.log('没数据');
  370. return
  371. }
  372. let blob = new Blob([res], { type: "application/vnd.ms-excel" });
  373. let date = new Date();
  374. let time = date.toLocaleDateString();
  375. // console.log(time, "时间");
  376. if ("download" in document.createElement("a")) {
  377. const link = document.createElement("a");
  378. link.style.display = "none";
  379. link.href = URL.createObjectURL(blob);
  380. // link.download = res.headers['content-disposition'] //下载后文件名
  381. link.download = (name || "导出文件") + time + ".xlsx"; //下载的文件名
  382. document.body.appendChild(link);
  383. link.click();
  384. document.body.removeChild(link);
  385. } else {
  386. // console.log("--------------------jingla")
  387. let fileName = (name || "导出文件") + time + ".xlsx"; //下载的文件名
  388. navigator.msSaveBlob(blob, fileName);
  389. }
  390. })
  391. .catch((error) => {
  392. // Message.error({
  393. // message: '网络连接错误'
  394. // })
  395. console.log(error);
  396. });
  397. }