海南旅游项目 前端仓库
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.

81 lines
2.3 KiB

4 years ago
4 years ago
4 years ago
  1. /*
  2. 全局共享实用方法 shared.js
  3. */
  4. // 判断对错/是否显示,万能校验
  5. export function isRight(obj) {
  6. if (isValueType(obj) === 'string') {
  7. obj = obj.trim();
  8. if (obj === 'null' || obj === 'undefined') {
  9. return false;
  10. }
  11. } else if (isValueType(obj) === 'number' && (isValueType(obj) === "number" && !isNaN(obj)) && obj !== 0) {
  12. return true;
  13. } else if (isValueType(obj) === 'boolean') {
  14. return obj
  15. }
  16. for (var key in obj) {
  17. return true;
  18. }
  19. return false;
  20. }
  21. // 判断一个值所属的类型,返回一个字符串
  22. export function isValueType(value) {
  23. let str = Object.prototype.toString.call(value);
  24. return str.match(/\[object (.*?)\]/)[1].toLowerCase();
  25. }
  26. // 解析时间戳,参数非必传,不传参时默认显示当前最新日期+时间;
  27. // 第一个参数为当前日期时间戳,第二个日期分隔符,第三个不传参或传all显示日期+时间,传date显示日期,time显示时间
  28. export function recordTime(time = new Date(), separator = "-", swf = 'all'){
  29. let year = time.getFullYear();
  30. let month = time.getMonth() + 1;
  31. let day = time.getDate();
  32. let hour = time.getHours();
  33. let min = time.getMinutes();
  34. let ppn = time.getSeconds();
  35. if(swf === "time"){
  36. return [hour, min, ppn].map(cover).join(":");
  37. }else if(swf === "date"){
  38. return [year, month, day].map(cover).join(String(separator));
  39. }else{
  40. return [year, month, day].map(cover).join(String(separator)) +" "+ [hour, min, ppn].map(cover).join(":");
  41. }
  42. }
  43. export const timer = (value, fmt) => {
  44. if(!value) return;
  45. let newTime = new Date(value)
  46. if(!fmt){
  47. fmt = 'yyyy-MM-dd hh:mm';
  48. }
  49. if(/(y+)/.test(fmt)) {
  50. fmt = fmt.replace(RegExp.$1, (newTime.getFullYear() + '').substr(4 - RegExp.$1.length));
  51. }
  52. let o = {
  53. 'M+': newTime.getMonth() + 1,
  54. 'd+': newTime.getDate(),
  55. 'h+': newTime.getHours(),
  56. 'm+': newTime.getMinutes(),
  57. 's+': newTime.getSeconds()
  58. };
  59. function padLeftZero(str) {
  60. return ('00'+str).substr(str.length);
  61. }
  62. // 遍历这个对象
  63. for (let k in o) {
  64. if (new RegExp(`(${k})`).test(fmt)) {
  65. // console.log(`${k}`)
  66. let str = o[k] + '';
  67. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
  68. }
  69. }
  70. return fmt;
  71. }
  72. // 位数不足2,前面补0
  73. var cover = function(par) {
  74. par = par.toString()[1] ? par : "0" + par;
  75. return par;
  76. };