util.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. * 全局的工具类
  3. * <pre>
  4. * 作者:hugh zhuang
  5. * 邮箱:3378340995@qq.com
  6. * 日期:2018-07-02-下午3:29:34
  7. * 版权:广州流辰信息技术有限公司
  8. * </pre>
  9. *
  10. * 可以使用 this.$utils.xx
  11. * 如: 判断是否为空
  12. * this.$utils.isEmpty()
  13. */
  14. import log from './util.log.js'
  15. import cookies from './util.cookies.js'
  16. import XSS from 'xss'
  17. import { mapValues } from 'lodash'
  18. const rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g
  19. const util = {
  20. cookies,
  21. log,
  22. /**
  23. * 判断是否为空
  24. * @param {*} obj
  25. */
  26. isEmpty: function (obj, allowBlank = false) {
  27. if (util.isNull(obj)) return true
  28. if (util.isArray(obj)) return obj.length === 0
  29. if (util.isString(obj)) return (!(allowBlank || obj.length > 0))
  30. if (util.isObject(obj)) return util.isEmptyObject(obj)
  31. for (var key in obj) if (obj.hasOwnProperty(key)) return false
  32. return obj === undefined || (!allowBlank ? obj === '' : false)
  33. },
  34. /**
  35. * 判断是否不为空
  36. * @param {*} obj
  37. */
  38. isNotEmpty: function (obj, allowBlank = false) {
  39. return !util.isEmpty(obj, allowBlank)
  40. },
  41. /**
  42. * 判断是否为空对象
  43. * @param {*} obj
  44. */
  45. isEmptyObject: function (obj) {
  46. if (!obj) return true
  47. for (const name in obj) {
  48. return false
  49. }
  50. return true
  51. },
  52. /**
  53. * 判断是否为不空对象
  54. * @param {*} obj
  55. */
  56. isNotEmptyObject: function (obj) {
  57. return util.isNotEmptyObject(obj)
  58. },
  59. /**
  60. * 是否是对象
  61. * @param {*} input
  62. */
  63. isObject: function (input) {
  64. return Object.prototype.toString.call(input) === '[object Object]'
  65. },
  66. /**
  67. * 是否是数组
  68. * @param {*} input
  69. */
  70. isArray: function (input) {
  71. return input instanceof Array || Object.prototype.toString.call(input) === '[object Array]'
  72. },
  73. isDate: function (input) {
  74. return input instanceof Date || Object.prototype.toString.call(input) === '[object Date]'
  75. },
  76. isNumber: function (input) {
  77. return input instanceof Number || Object.prototype.toString.call(input) === '[object Number]'
  78. },
  79. isString: function (input) {
  80. return input instanceof String || Object.prototype.toString.call(input) === '[object String]'
  81. },
  82. isBoolean: function (input) {
  83. return typeof input === 'boolean'
  84. },
  85. isFunction: function (input) {
  86. return typeof input === 'function'
  87. },
  88. isNull: function (input) {
  89. return input === undefined || input === null
  90. },
  91. isNum: function (input) {
  92. if (util.isEmpty(input)) {
  93. return false
  94. } else {
  95. if (util.isString(input)) {
  96. input = Number(input)
  97. }
  98. if (isNaN(input)) {
  99. return false
  100. }
  101. return util.isNumber(input)
  102. }
  103. },
  104. isValidNumber: function (t) {
  105. return typeof t === 'number' && !isNaN(t) && isFinite(t)
  106. },
  107. isPlainObject: function (obj) {
  108. if (obj && Object.prototype.toString.call(obj) === '[object Object]' && obj.constructor === Object && !hasOwnProperty.call(obj, 'constructor')) {
  109. var key
  110. for (var k in obj) {
  111. key = k
  112. }
  113. return key === undefined || hasOwnProperty.call(obj, key)
  114. }
  115. return false
  116. },
  117. isJSON: function (str) {
  118. if (util.isString(str)) {
  119. try {
  120. const obj = JSON.parse(str)
  121. return util.isPlainObject(obj) || util.isArray(obj)
  122. } catch (e) {
  123. return false
  124. }
  125. }
  126. return false
  127. },
  128. trim: function (text) {
  129. return text == null ? '' : (text + '').replace(rtrim, '')
  130. },
  131. /**
  132. * 判断参数是否是其中之一
  133. */
  134. oneOf: function (value, validList) {
  135. for (let i = 0; i < validList.length; i++) {
  136. if (value === validList[i]) {
  137. return true
  138. }
  139. }
  140. return false
  141. },
  142. /**
  143. * 判断参数是否是数组对象其中之一
  144. */
  145. oneOfObj: function (value, validList, key) {
  146. for (let i = 0; i < validList.length; i++) {
  147. if (value === validList[i][key]) {
  148. return true
  149. }
  150. }
  151. return false
  152. },
  153. /**
  154. * 全局唯一标识符
  155. */
  156. guid: function () {
  157. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  158. const r = Math.random() * 16 | 0
  159. const v = c === 'x' ? r : (r & 0x3 | 0x8)
  160. return v.toString(16)
  161. })
  162. },
  163. /**
  164. * 转boolean 值
  165. * @param {*} obj
  166. * @param {*} defaultValue
  167. */
  168. toBoolean: function (obj, defaultValue = false) {
  169. if (util.isEmpty(obj)) {
  170. return defaultValue
  171. }
  172. return util.oneOf(obj, [true, 'True', 'Yes', 'true', '1', 1, 'yes', 'Y', 'y', 'T', 't'])
  173. },
  174. /**
  175. * 创建新数据,避免对象引用
  176. * @param {*} data
  177. * @param {*} defaultValue
  178. */
  179. newData: function (data, defaultValue) {
  180. return util.isNotEmpty(data) ? JSON.parse(JSON.stringify(data)) : (defaultValue || data)
  181. },
  182. /**
  183. * 转换json字符串的转换
  184. * @param {*} data
  185. * @param {*} defaultValue
  186. */
  187. parseData: function (data, defaultValue) {
  188. if (util.isNotEmpty(data)) {
  189. // eslint-disable-next-line no-eval
  190. return util.isPlainObject(data) || util.isArray(data) ? data : window.eval('(' + data + ')')
  191. } else {
  192. return (defaultValue || data)
  193. }
  194. },
  195. /**
  196. * 转换json字符串的转换
  197. * @param {*} data
  198. * @param {*} defaultValue
  199. */
  200. parseJSON: function (data, defaultValue) {
  201. if (util.isNotEmpty(data)) {
  202. return util.isJSON(data) ? JSON.parse(data) : data
  203. } else {
  204. return (defaultValue || data)
  205. }
  206. },
  207. /**
  208. * eval 数据
  209. */
  210. evalData: function (data) {
  211. // eslint-disable-next-line no-eval
  212. return window.eval(data)
  213. },
  214. /**
  215. * 格式化文件大小, 输出成带单位的字符串
  216. * @method formatSize
  217. * @grammar util.formatSize( size ) => String
  218. * @grammar util.formatSize( size, pointLength ) => String
  219. * @grammar util.formatSize( size, pointLength, units ) => String
  220. * @param {Number} size 文件大小
  221. * @param {Number} [pointLength=2] 精确到的小数点数。
  222. * @param {Array} [units=[ 'B', 'K', 'M', 'G', 'TB' ]] 单位数组。从字节,到千字节,一直往上指定。如果单位数组里面只指定了到了K(千字节),同时文件大小大于M, 此方法的输出将还是显示成多少K.
  223. * @example
  224. * console.log( util.formatSize( 100 ) ); // => 100B
  225. * console.log( util.formatSize( 1024 ) ); // => 1.00K
  226. * console.log( util.formatSize( 1024, 0 ) ); // => 1K
  227. * console.log( util.formatSize( 1024 * 1024 ) ); // => 1.00M
  228. * console.log( util.formatSize( 1024 * 1024 * 1024 ) ); // => 1.00G
  229. * console.log( util.formatSize( 1024 * 1024 * 1024, 0, ['B', 'KB', 'MB'] ) ); // => 1024MB
  230. */
  231. formatSize: function (size, pointLength, units) {
  232. units = units || ['B', 'K', 'M', 'G', 'TB']
  233. let unit
  234. while ((unit = units.shift()) && size > 1024) {
  235. size = size / 1024
  236. }
  237. return (unit === 'B' ? size : size.toFixed(pointLength || 2)) + unit
  238. },
  239. /**
  240. * 格式化文本
  241. * @param {*} text
  242. */
  243. formatText: function (text) {
  244. return text !== null ? ('' + XSS(text)).replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '<br />' + '$2') : ''
  245. },
  246. /**
  247. * @description 打开新页面
  248. * @param {String} url 地址
  249. */
  250. open: function (url) {
  251. var a = document.createElement('a')
  252. a.setAttribute('href', url)
  253. a.setAttribute('target', '_blank')
  254. a.setAttribute('id', 'ibps-open-link')
  255. document.body.appendChild(a)
  256. a.click()
  257. document.body.removeChild(document.getElementById('ibps-open-link'))
  258. },
  259. /**
  260. * 将array递归为一维数组。
  261. * @param {*} ary
  262. * @param {*} predicate
  263. * @param {*} result
  264. */
  265. flatten: function (ary, predicate, result) {
  266. result = result || []
  267. if (ary) {
  268. for (let i = 0; i < ary.length; i++) {
  269. const value = ary[i]
  270. if (Array.isArray(value)) {
  271. util.flatten(value, predicate, result)
  272. } else {
  273. (predicate && !predicate(value)) || result.push(value)
  274. }
  275. }
  276. }
  277. return result
  278. },
  279. /**
  280. * 替换对象中的null为空字符串
  281. * @param {Object} obj 目标对象
  282. */
  283. // replaceNullWithEmpty: function (obj) {
  284. // return mapValues(obj, value => {
  285. // if (value === null) {
  286. // return ''
  287. // } else if (typeof value === 'object') {
  288. // return mapValues(value, v => (v === null ? '' : v))
  289. // } else {
  290. // return value
  291. // }
  292. // })
  293. // },
  294. replaceNullWithEmpty: function (obj) {
  295. function replaceValue(value) {
  296. if (value === null) {
  297. return ''
  298. } else if (typeof value === 'object') {
  299. if (Array.isArray(value)) {
  300. return value.map(item => replaceValue(item))
  301. } else {
  302. return mapValues(value, v => replaceValue(v))
  303. }
  304. } else {
  305. return value
  306. }
  307. }
  308. return replaceValue(obj)
  309. }
  310. }
  311. export default util