index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * This is just a simple version of deep copy
  3. * Has a lot of edge cases bug
  4. * If you want to use a perfect deep copy, use lodash's _.cloneDeep
  5. * @param {Object} source 源数据
  6. * @returns {Object} targetObj 复制后的数据
  7. */
  8. export function deepClone (source) {
  9. if (!source && typeof source !== 'object') {
  10. throw new Error('error arguments', 'deepClone')
  11. }
  12. const targetObj = source.constructor === Array ? [] : {}
  13. Object.keys(source).forEach(keys => {
  14. if (source[keys] && typeof source[keys] === 'object') {
  15. targetObj[keys] = deepClone(source[keys])
  16. } else {
  17. targetObj[keys] = source[keys]
  18. }
  19. })
  20. return targetObj
  21. }
  22. /**
  23. * 判断val是否是空对象
  24. * @param {*} val js变量
  25. * @returns {Boolean} 返回boolean值
  26. */
  27. export const isEmpty = function (val) {
  28. // null or undefined
  29. if (val === null) return true
  30. if (typeof val === 'boolean') return false
  31. if (typeof val === 'number') return !val
  32. if (val instanceof Error) return val.message === ''
  33. switch (Object.prototype.toString.call(val)) {
  34. // String or Array
  35. case '[object String]':
  36. case '[object Array]':
  37. return !val.length
  38. // Map or Set or File
  39. case '[object File]':
  40. case '[object Map]':
  41. case '[object Set]': {
  42. return !val.size
  43. }
  44. // Plain Object
  45. case '[object Object]': {
  46. return !Object.keys(val).length
  47. }
  48. }
  49. return false
  50. }
  51. /**
  52. * 验证手机号
  53. * @param {*} phone 手机号
  54. * @returns {Boolean} boolean值
  55. */
  56. export function validatePhone (phone) {
  57. const reg = /^[1]([3-9])[0-9]{9}$/
  58. return reg.test(phone)
  59. }
  60. /**
  61. * 获取URL中的参数
  62. * @param {String} url
  63. * @returns {String} 要获取的参数name
  64. */
  65. export function getQueryString (url, name) {
  66. const reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')
  67. const r = url.substr(1).match(reg)
  68. if (r !== null) {
  69. return r[2]
  70. }
  71. return null
  72. }
  73. /**
  74. * 根据对象的某个key排序
  75. * @param {*} 对象
  76. */
  77. export function sortObject (property) {
  78. return function (obj1, obj2) {
  79. const value1 = obj1[property]
  80. const value2 = obj2[property]
  81. return value1 - value2
  82. }
  83. }
  84. /**
  85. * 根据对象的某个key倒序排序
  86. * @param {*} 对象
  87. */
  88. export function sortDescObject (property) {
  89. return function (obj1, obj2) {
  90. const value1 = obj1[property]
  91. const value2 = obj2[property]
  92. return value2 - value1
  93. }
  94. }
  95. /**
  96. * 判断商标是否是无效的
  97. * @param {String} 商标状态
  98. * @returns {Boolean} boolean值
  99. */
  100. export function tmIsIneffective (status) {
  101. return status === '商标无效'
  102. }
  103. /**
  104. *将json对象转化为url要拼接的字符串
  105. */
  106. export function encodeParam (link, params) {
  107. let url = ''
  108. if (params && Object.keys(params).length > 0) {
  109. for (const k in params) {
  110. const value = params[k] !== undefined ? params[k] : ''
  111. url += '&' + k + '=' + encodeURIComponent(value)
  112. }
  113. url = url ? url.substring(1) : ''
  114. if (params) {
  115. link += (link.indexOf('?') < 0 && params ? '?' : '&') + url
  116. }
  117. }
  118. return link
  119. }
  120. /**
  121. * 计算时间
  122. * @param {*} time
  123. * @param {*} cFormat
  124. */
  125. export function parseTime(time, cFormat) {
  126. if (arguments.length === 0) {
  127. return null
  128. }
  129. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  130. let date
  131. if (typeof time === 'object') {
  132. date = time
  133. } else {
  134. if (('' + time).length === 10) time = parseInt(time) * 1000
  135. date = new Date(time)
  136. }
  137. const formatObj = {
  138. y: date.getFullYear(),
  139. m: date.getMonth() + 1,
  140. d: date.getDate(),
  141. h: date.getHours(),
  142. i: date.getMinutes(),
  143. s: date.getSeconds(),
  144. a: date.getDay()
  145. }
  146. const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  147. let value = formatObj[key]
  148. if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
  149. if (result.length > 0 && value < 10) {
  150. value = '0' + value
  151. }
  152. return value || 0
  153. })
  154. return timeStr
  155. }
  156. // 获取任意时间
  157. export function getDate(date, AddDayCount = 0) {
  158. if (!date) {
  159. date = new Date()
  160. }
  161. if (typeof date !== 'object') {
  162. date = date.replace(/-/g, '/')
  163. }
  164. const dd = new Date(date)
  165. dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期
  166. const y = dd.getFullYear()
  167. const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期,不足10补0
  168. const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号,不足10补0
  169. return {
  170. fullDate: y + '-' + m + '-' + d,
  171. fullMonth: y + '-' + m,
  172. year: y,
  173. month: m,
  174. date: d,
  175. day: dd.getDay()
  176. }
  177. }
  178. // 检测文件类型
  179. export function checkTypes(source, type) {
  180. const types = {
  181. video: ['m3u8', 'ts', 'mp4', '3gp', 'asf', 'wmv', 'avi', 'mkv', 'rmvb', 'flv', 'mp3', 'ogg', 'amr'],
  182. imgs: ['png', 'jpg', 'jpeg']
  183. }
  184. if (!types[type]) return false// 不存在检测类型
  185. const arr = source.split('.')
  186. return types[type].includes(arr[arr.length - 1])
  187. }