常用的小工具函数

  1. import _ from 'lodash'
  2. export const isEmpty = params => {
  3. let result = false
  4. if (
  5. params === undefined ||
  6. params === null ||
  7. params === [] ||
  8. params === ''
  9. ) {
  10. result = true
  11. }
  12. if (typeof params === 'object') {
  13. result = _.isEmpty(params)
  14. }
  15. if (typeof params === 'string') {
  16. params = _.trim(params)
  17. result = params === ''
  18. }
  19. return result
  20. }
  21. export const hasStarSymbol = params => {
  22. return params.indexOf('****') === -1
  23. }
  24. export const isNotEmpty = params => {
  25. return !isEmpty(params)
  26. }
  27. export const isUrl = url => {
  28. return url && url.includes('http')
  29. }
  30. // 验证区号是否是三到四位数的合法区号
  31. export const isValidAreacode = str => {
  32. let result = false
  33. if (!str || isEmpty(str)) {
  34. result = false
  35. }
  36. if (str && str.length > 0) {
  37. result = _.every(_.split(str, '-'), v => {
  38. return (v.length === 3 || v.length === 4) && v.charAt(0) === '0'
  39. })
  40. }
  41. return result
  42. }
  43. // 用于生成 key 防止组件缓存
  44. export const getUUID = () => {
  45. let key = Math.random()
  46. .toString(36)
  47. .substring(3, 8)
  48. return `crm${key}`
  49. }
  50. // 过滤掉无用请求参数
  51. export const filterQueryParams = (obj, ...args) => {
  52. const queryParams = JSON.parse(JSON.stringify(obj))
  53. args.forEach(x => {
  54. Reflect.deleteProperty(queryParams, x)
  55. })
  56. return queryParams
  57. }
  58. // 提取有用的请求参数
  59. export const filterUseQueryParams = (obj, args = []) => {
  60. const queryParams = {}
  61. const objCopy = JSON.parse(JSON.stringify(obj))
  62. args.forEach(x => {
  63. queryParams[x] = objCopy[x]
  64. if (objCopy.hasOwnProperty(x + 'MaskSec')) {
  65. queryParams[x + 'MaskSec'] = objCopy[x + 'MaskSec']
  66. }
  67. if (objCopy[x + 'Mask']) {
  68. queryParams[x + 'Mask'] = objCopy[x + 'Mask']
  69. }
  70. })
  71. return queryParams
  72. }
  73. // 清空obj中对应的属性
  74. export const clearObjValues = (obj, ...args) => {
  75. args &&
  76. args.forEach(x => {
  77. if (obj && x && obj[x]) {
  78. obj[x] = ''
  79. }
  80. })
  81. return obj
  82. }
  83. // 处理显示的串undefined null为空串
  84. export const handlerDisplay = str => {
  85. if (str && typeof str === 'string') {
  86. str = str.replace(/(undefined)|(null)/gi, '').trim()
  87. }
  88. return str
  89. }
  90. // 处理 loading 的显示效果
  91. let cacheEl = document.querySelector('.el-loading-mask.is-fullscreen')
  92. export const loading = (flag = true) => {
  93. if (!cacheEl) {
  94. cacheEl = document.querySelector('.el-loading-mask.is-fullscreen')
  95. }
  96. if (flag) {
  97. // 修复loading 框偏移的问题
  98. // cacheEl.style.width = '1296px'
  99. cacheEl.style.display = 'block'
  100. } else {
  101. cacheEl.style.display = 'none'
  102. }
  103. }
  104. // 处理选项排到第一位
  105. export const getFmsBalanceAccountModuleList = (arr, text = 'DefaultChooseLabel') => {
  106. let result = arr
  107. if (Array.isArray(arr) && arr.length > 0) {
  108. let index = arr.findIndex(x => x.area === text)
  109. if (index >= 0) {
  110. let matchOne = arr[index]
  111. arr.splice(index, 1)
  112. arr.unshift(matchOne)
  113. }
  114. }
  115. return result
  116. }
  117. // 全部为空
  118. export const isAllEmpty = (obj, arr = []) => {
  119. let flag = true
  120. let values = arr.map(x => obj[x]) || []
  121. flag = values.every(x => isEmpty(x))
  122. return flag
  123. }
  124. // 全部必填
  125. export const isAllFill = (obj, arr = []) => {
  126. let flag = true
  127. let values = arr.map(x => obj[x]) || []
  128. flag = values.every(x => !isEmpty(x))
  129. return flag
  130. }
  131. // 保留 2 位小数
  132. // 计提保留2位小数 由后台数据库存储的时候处理好 3-> 3.00
  133. export const twoDec = (val, precision = 2) => {
  134. let res = ''
  135. if (isEmpty(val)) {
  136. return res
  137. }
  138. if (val) {
  139. val = Number(val).toFixed(precision)
  140. res = `${val}`
  141. } else if (val === 0 || val === 0.0) {
  142. val = Number(val).toFixed(precision)
  143. res = `${val}`
  144. } else {
  145. res = ''
  146. }
  147. return res
  148. }
  149. // 根据组件名字查找子组件,深度优先遍历
  150. // 向下找到最近的指定组件
  151. export function findComponentDownward (context, componentName) {
  152. const childrens = context.$children
  153. let children = null
  154. if (childrens.length) {
  155. for (const child of childrens) {
  156. const name = child.$options.name
  157. if (name === componentName) {
  158. children = child
  159. break
  160. } else {
  161. children = findComponentDownward(child,
  162. componentName)
  163. if (children) break
  164. }
  165. }
  166. }
  167. return children
  168. }
  169. // 计算多个值的求和
  170. export const calcTotal = (arr) => {
  171. let myarr = _.compact(arr).map(x => Number(x))
  172. let res = myarr.reduce((prev, curr, index, array) => {
  173. if (!_.isFinite(curr)) {
  174. curr = 0
  175. }
  176. return Number(curr) + Number(prev)
  177. }, 0)
  178. return res
  179. }
  180. // 用于判断区号重复
  181. export const duplicates = (arr) => {
  182. var newArr = []
  183. for (var i = 0; i < arr.length; i++) {
  184. var count = 0
  185. for (var j = 0; j < arr.length; j++) {
  186. if (arr[i] === arr[j]) {
  187. count++
  188. }
  189. }
  190. if (count > 1 && newArr.indexOf(arr[i]) === -1) {
  191. newArr.push(arr[i])
  192. }
  193. }
  194. return newArr.join(',')
  195. }
  196. // 判断权限 在列表中就不去发请求
  197. export const isIntoNOAuthSilentMethodList = (vm, currentMethod) => {
  198. let menus = vm.$store.getters.permission.menus
  199. let result = false
  200. result = (!menus.includes(currentMethod) && NO_AUTH_SILENT_METHOD_LIST.includes(currentMethod))
  201. console.log(`${currentMethod} is ${result}`)
  202. return result
  203. }
  204. function customFilter (x, key, currentOptions) {
  205. let result = false
  206. if (!currentOptions[key]) {
  207. result = true
  208. } else {
  209. result = x['extObj'][key] + '' === currentOptions[key]
  210. }
  211. return result
  212. }
  213. // 服务方式过滤
  214. // 观察DOM
  215. export const observeDom = (el, options, callback) => {
  216. var defaultOptions = {
  217. childList: true,
  218. subtree: true,
  219. attributes: true,
  220. attributeOldValue: true,
  221. characterData: true
  222. }
  223. var currentOptions = Object.assign(defaultOptions, options)
  224. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver
  225. var observer = new MutationObserver(callback)
  226. observer.observe(el, currentOptions)
  227. }