index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. import { parseTime } from './ruoyi'
  2. /**
  3. * 表格时间格式化
  4. */
  5. export function formatDate(cellValue) {
  6. if (cellValue == null || cellValue == "") return "";
  7. var date = new Date(cellValue)
  8. var year = date.getFullYear()
  9. var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  10. var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  11. var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  12. var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  13. var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  14. return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
  15. }
  16. /**
  17. * 表格日期格式化
  18. */
  19. export function formatDateYMD(cellValue) {
  20. if (cellValue == null || cellValue == "") return "";
  21. var date = new Date(cellValue)
  22. var year = date.getFullYear()
  23. var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  24. var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  25. return year + '-' + month + '-' + day
  26. }
  27. /**
  28. * @param {number} time
  29. * @param {string} option
  30. * @returns {string}
  31. */
  32. export function formatTime(time, option) {
  33. if (('' + time).length === 10) {
  34. time = parseInt(time) * 1000
  35. } else {
  36. time = +time
  37. }
  38. const d = new Date(time)
  39. const now = Date.now()
  40. const diff = (now - d) / 1000
  41. if (diff < 30) {
  42. return '刚刚'
  43. } else if (diff < 3600) {
  44. // less 1 hour
  45. return Math.ceil(diff / 60) + '分钟前'
  46. } else if (diff < 3600 * 24) {
  47. return Math.ceil(diff / 3600) + '小时前'
  48. } else if (diff < 3600 * 24 * 2) {
  49. return '1天前'
  50. }
  51. if (option) {
  52. return parseTime(time, option)
  53. } else {
  54. return (
  55. d.getMonth() +
  56. 1 +
  57. '月' +
  58. d.getDate() +
  59. '日' +
  60. d.getHours() +
  61. '时' +
  62. d.getMinutes() +
  63. '分'
  64. )
  65. }
  66. }
  67. /**
  68. * @param {string} url
  69. * @returns {Object}
  70. */
  71. export function getQueryObject(url) {
  72. url = url == null ? window.location.href : url
  73. const search = url.substring(url.lastIndexOf('?') + 1)
  74. const obj = {}
  75. const reg = /([^?&=]+)=([^?&=]*)/g
  76. search.replace(reg, (rs, $1, $2) => {
  77. const name = decodeURIComponent($1)
  78. let val = decodeURIComponent($2)
  79. val = String(val)
  80. obj[name] = val
  81. return rs
  82. })
  83. return obj
  84. }
  85. /**
  86. * 腾讯授权
  87. * @param {string} url
  88. * @returns {Object}
  89. */
  90. export function getQueryObjectSearch() {
  91. const search = window.location.search
  92. const obj = {}
  93. const reg = /([^?&=]+)=([^?&=]*)/g
  94. search.replace(reg, (rs, $1, $2) => {
  95. const name = decodeURIComponent($1)
  96. let val = decodeURIComponent($2)
  97. val = String(val)
  98. obj[name] = val
  99. return rs
  100. })
  101. return obj
  102. }
  103. /**
  104. * @param {string} input value
  105. * @returns {number} output value
  106. */
  107. export function byteLength(str) {
  108. // returns the byte length of an utf8 string
  109. let s = str.length
  110. for (var i = str.length - 1; i >= 0; i--) {
  111. const code = str.charCodeAt(i)
  112. if (code > 0x7f && code <= 0x7ff) s++
  113. else if (code > 0x7ff && code <= 0xffff) s += 2
  114. if (code >= 0xDC00 && code <= 0xDFFF) i--
  115. }
  116. return s
  117. }
  118. /**
  119. * @param {Array} actual
  120. * @returns {Array}
  121. */
  122. export function cleanArray(actual) {
  123. const newArray = []
  124. for (let i = 0; i < actual.length; i++) {
  125. if (actual[i]) {
  126. newArray.push(actual[i])
  127. }
  128. }
  129. return newArray
  130. }
  131. /**
  132. * @param {Object} json
  133. * @returns {Array}
  134. */
  135. export function param(json) {
  136. if (!json) return ''
  137. return cleanArray(
  138. Object.keys(json).map(key => {
  139. if (json[key] === undefined) return ''
  140. return encodeURIComponent(key) + '=' + encodeURIComponent(json[key])
  141. })
  142. ).join('&')
  143. }
  144. /**
  145. * @param {string} url
  146. * @returns {Object}
  147. */
  148. export function param2Obj(url) {
  149. const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  150. if (!search) {
  151. return {}
  152. }
  153. const obj = {}
  154. const searchArr = search.split('&')
  155. searchArr.forEach(v => {
  156. const index = v.indexOf('=')
  157. if (index !== -1) {
  158. const name = v.substring(0, index)
  159. const val = v.substring(index + 1, v.length)
  160. obj[name] = val
  161. }
  162. })
  163. return obj
  164. }
  165. /**
  166. * @param {string} val
  167. * @returns {string}
  168. */
  169. export function html2Text(val) {
  170. const div = document.createElement('div')
  171. div.innerHTML = val
  172. return div.textContent || div.innerText
  173. }
  174. /**
  175. * Merges two objects, giving the last one precedence
  176. * @param {Object} target
  177. * @param {(Object|Array)} source
  178. * @returns {Object}
  179. */
  180. export function objectMerge(target, source) {
  181. if (typeof target !== 'object') {
  182. target = {}
  183. }
  184. if (Array.isArray(source)) {
  185. return source.slice()
  186. }
  187. Object.keys(source).forEach(property => {
  188. const sourceProperty = source[property]
  189. if (typeof sourceProperty === 'object') {
  190. target[property] = objectMerge(target[property], sourceProperty)
  191. } else {
  192. target[property] = sourceProperty
  193. }
  194. })
  195. return target
  196. }
  197. /**
  198. * @param {HTMLElement} element
  199. * @param {string} className
  200. */
  201. export function toggleClass(element, className) {
  202. if (!element || !className) {
  203. return
  204. }
  205. let classString = element.className
  206. const nameIndex = classString.indexOf(className)
  207. if (nameIndex === -1) {
  208. classString += '' + className
  209. } else {
  210. classString =
  211. classString.substr(0, nameIndex) +
  212. classString.substr(nameIndex + className.length)
  213. }
  214. element.className = classString
  215. }
  216. /**
  217. * @param {string} type
  218. * @returns {Date}
  219. */
  220. export function getTime(type) {
  221. if (type === 'start') {
  222. return new Date().getTime() - 3600 * 1000 * 24 * 90
  223. } else {
  224. return new Date(new Date().toDateString())
  225. }
  226. }
  227. /**
  228. * @param {Function} func
  229. * @param {number} wait
  230. * @param {boolean} immediate
  231. * @return {*}
  232. */
  233. export function debounce(func, wait, immediate) {
  234. let timeout, args, context, timestamp, result
  235. const later = function() {
  236. // 据上一次触发时间间隔
  237. const last = +new Date() - timestamp
  238. // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
  239. if (last < wait && last > 0) {
  240. timeout = setTimeout(later, wait - last)
  241. } else {
  242. timeout = null
  243. // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
  244. if (!immediate) {
  245. result = func.apply(context, args)
  246. if (!timeout) context = args = null
  247. }
  248. }
  249. }
  250. return function(...args) {
  251. context = this
  252. timestamp = +new Date()
  253. const callNow = immediate && !timeout
  254. // 如果延时不存在,重新设定延时
  255. if (!timeout) timeout = setTimeout(later, wait)
  256. if (callNow) {
  257. result = func.apply(context, args)
  258. context = args = null
  259. }
  260. return result
  261. }
  262. }
  263. /**
  264. * This is just a simple version of deep copy
  265. * Has a lot of edge cases bug
  266. * If you want to use a perfect deep copy, use lodash's _.cloneDeep
  267. * @param {Object} source
  268. * @returns {Object}
  269. */
  270. export function deepClone(source) {
  271. if (!source && typeof source !== 'object') {
  272. throw new Error('error arguments', 'deepClone')
  273. }
  274. const targetObj = source.constructor === Array ? [] : {}
  275. Object.keys(source).forEach(keys => {
  276. if (source[keys] && typeof source[keys] === 'object') {
  277. targetObj[keys] = deepClone(source[keys])
  278. } else {
  279. targetObj[keys] = source[keys]
  280. }
  281. })
  282. return targetObj
  283. }
  284. /**
  285. * @param {Array} arr
  286. * @returns {Array}
  287. */
  288. export function uniqueArr(arr) {
  289. return Array.from(new Set(arr))
  290. }
  291. /**
  292. * @returns {string}
  293. */
  294. export function createUniqueString() {
  295. const timestamp = +new Date() + ''
  296. const randomNum = parseInt((1 + Math.random()) * 65536) + ''
  297. return (+(randomNum + timestamp)).toString(32)
  298. }
  299. /**
  300. * Check if an element has a class
  301. * @param {HTMLElement} elm
  302. * @param {string} cls
  303. * @returns {boolean}
  304. */
  305. export function hasClass(ele, cls) {
  306. return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'))
  307. }
  308. /**
  309. * Add class to element
  310. * @param {HTMLElement} elm
  311. * @param {string} cls
  312. */
  313. export function addClass(ele, cls) {
  314. if (!hasClass(ele, cls)) ele.className += ' ' + cls
  315. }
  316. /**
  317. * Remove class from element
  318. * @param {HTMLElement} elm
  319. * @param {string} cls
  320. */
  321. export function removeClass(ele, cls) {
  322. if (hasClass(ele, cls)) {
  323. const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
  324. ele.className = ele.className.replace(reg, ' ')
  325. }
  326. }
  327. export function makeMap(str, expectsLowerCase) {
  328. const map = Object.create(null)
  329. const list = str.split(',')
  330. for (let i = 0; i < list.length; i++) {
  331. map[list[i]] = true
  332. }
  333. return expectsLowerCase
  334. ? val => map[val.toLowerCase()]
  335. : val => map[val]
  336. }
  337. export const exportDefault = 'export default '
  338. export const beautifierConf = {
  339. html: {
  340. indent_size: '2',
  341. indent_char: ' ',
  342. max_preserve_newlines: '-1',
  343. preserve_newlines: false,
  344. keep_array_indentation: false,
  345. break_chained_methods: false,
  346. indent_scripts: 'separate',
  347. brace_style: 'end-expand',
  348. space_before_conditional: true,
  349. unescape_strings: false,
  350. jslint_happy: false,
  351. end_with_newline: true,
  352. wrap_line_length: '110',
  353. indent_inner_html: true,
  354. comma_first: false,
  355. e4x: true,
  356. indent_empty_lines: true
  357. },
  358. js: {
  359. indent_size: '2',
  360. indent_char: ' ',
  361. max_preserve_newlines: '-1',
  362. preserve_newlines: false,
  363. keep_array_indentation: false,
  364. break_chained_methods: false,
  365. indent_scripts: 'normal',
  366. brace_style: 'end-expand',
  367. space_before_conditional: true,
  368. unescape_strings: false,
  369. jslint_happy: true,
  370. end_with_newline: true,
  371. wrap_line_length: '110',
  372. indent_inner_html: true,
  373. comma_first: false,
  374. e4x: true,
  375. indent_empty_lines: true
  376. }
  377. }
  378. // 首字母大小
  379. export function titleCase(str) {
  380. return str.replace(/( |^)[a-z]/g, L => L.toUpperCase())
  381. }
  382. // 下划转驼峰
  383. export function camelCase(str) {
  384. return str.replace(/-[a-z]/g, str1 => str1.substr(-1).toUpperCase())
  385. }
  386. export function isNumberStr(str) {
  387. return /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(str)
  388. }
  389. import { Message } from 'element-ui';
  390. /**
  391. * Copy
  392. */
  393. export function Copy(data) {
  394. let url = data;
  395. let oInput = document.createElement('input');
  396. oInput.value = url;
  397. document.body.appendChild(oInput);
  398. oInput.select(); // 选择对象;
  399. console.log(oInput.value)
  400. document.execCommand("Copy"); // 执行浏览器复制命令
  401. Message({
  402. message: '复制成功',
  403. type: 'success'
  404. });
  405. oInput.remove()
  406. }