util.js 11 KB

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