fun.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. class utils {
  2. /**
  3. *
  4. * 时间戳转换日期
  5. *
  6. * @param {String} value 传入的时间戳
  7. * @param {String} String 返回的时间
  8. */
  9. formatDate(value) {
  10. let date = new Date(value)
  11. let y = date.getFullYear()
  12. let MM = date.getMonth() + 1
  13. MM = MM < 10 ? '0' + MM : MM
  14. let d = date.getDate()
  15. d = d < 10 ? '0' + d : d
  16. let h = date.getHours()
  17. h = h < 10 ? '0' + h : h
  18. let m = date.getMinutes()
  19. m = m < 10 ? '0' + m : m
  20. let s = date.getSeconds()
  21. s = s < 10 ? '0' + s : s
  22. return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
  23. }
  24. /**
  25. * 循环对象
  26. *
  27. * @param {Object|Array} obj 传入的值
  28. * @param {Function} fn 为每个项调用的回调
  29. */
  30. forEach(obj, fn) {
  31. if (obj === null || typeof obj === 'undefined') return
  32. // 如果还没有可写的东西,就强制一个数组
  33. if (typeof obj !== 'object') obj = [obj]
  34. if (this.isArray(obj)) {
  35. // 数组循环
  36. for (var i = 0, l = obj.length; i < l; i++) {
  37. fn.call(null, obj[i], i, obj)
  38. }
  39. } else {
  40. // 对象循环
  41. for (var key in obj) {
  42. /* 是否具有键 */
  43. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  44. fn.call(null, obj[key], key, obj)
  45. }
  46. }
  47. }
  48. }
  49. /**
  50. * 确定值是否为数组
  51. *
  52. * @param {Object} val 传入的值
  53. * @returns {boolean} 如果值是数组,则为True,否则为false
  54. */
  55. isArray(val) {
  56. return Object.prototype.toString.call(val) === '[object Array]'
  57. }
  58. /**
  59. * 确定值是否为对象
  60. *
  61. * @param {Object} obj 传入的对象
  62. * @returns {String} 返回类型
  63. */
  64. getObjClass(obj) {
  65. return Object.prototype.toString.call(obj).slice(8, -1)
  66. }
  67. /**
  68. * 深度克隆
  69. *
  70. * @param {Object} obj 传入需要克隆的对象
  71. * @returns {Object} 返回克隆好的对象
  72. */
  73. deepClone(obj) {
  74. let result
  75. let objClass = this.getObjClass(obj)
  76. if (objClass === 'Object') {
  77. result = {}
  78. } else if (objClass === 'Array') {
  79. result = []
  80. } else {
  81. return obj // 如果是其他数据类型不复制,直接将数据返回
  82. }
  83. // 遍历目标对象
  84. for (let key in obj) {
  85. let value = obj[key]
  86. result[key] = this.deepClone(value)
  87. }
  88. return result
  89. }
  90. /**
  91. * 递归合并两个对象
  92. *
  93. * @param {*} target
  94. * @param {*} sources
  95. * @return {*}
  96. * @memberof Common
  97. */
  98. assiginObj(target, sources) {
  99. let obj = target
  100. if (typeof target != 'object' || typeof sources != 'object' || typeof target) {
  101. return sources // 如果其中一个不是对象 就返回sources
  102. }
  103. for (let key in sources) {
  104. if (target.hasOwnProperty(key)) {
  105. obj[key] = this.assiginObj(target[key], sources[key])
  106. } else {
  107. // 不存在就直接添加
  108. obj[key] = sources[key]
  109. }
  110. }
  111. return obj
  112. }
  113. }
  114. export default new utils()