util.js 9.0 KB

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