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