tools.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { getArrValue } from 'js-fast-way'
  2. import { getDictionary, getDictionaryBiz } from '~api/other'
  3. /**
  4. * 效验是否为数字或小数的数字
  5. * @param text 字符串内容
  6. * @param lose 是否允许负数,默认允许
  7. * @returns {boolean}
  8. */
  9. export const isNumberReg = (text, lose = true) => {
  10. let pattern = lose ? /^-?\d+(.\d+)?$/ : /^\d+(.\d+)?$/
  11. return pattern.test(text)
  12. }
  13. //获取字典数据
  14. export const getDictionaryData = async (code, biz = false) => {
  15. let res = []
  16. if (biz) {
  17. const { data } = await getDictionaryBiz({ code: code })
  18. res = getArrValue(data)
  19. } else {
  20. const { data } = await getDictionary({ code: code })
  21. res = getArrValue(data)
  22. }
  23. //处理数据
  24. let newArr = []
  25. for (let i = 0; i < res.length; i++) {
  26. const val = isNumberReg(res[i]['dictKey']) ? Number(res[i]['dictKey']) : res[i]['dictKey']
  27. newArr.push({
  28. label: res[i]['dictValue'],
  29. value: val,
  30. })
  31. }
  32. return newArr
  33. }
  34. //操作确认
  35. export const actionConfirm = (cbk) => {
  36. window?.$messageBox?.alert('请确认要执行此操作吗?', '操作确认', {
  37. showCancelButton: true,
  38. confirmButtonText: '确认',
  39. cancelButtonText: '取消',
  40. type: 'warning',
  41. callback: (action) => {
  42. if (action === 'confirm') {
  43. cbk()
  44. }
  45. },
  46. })
  47. }
  48. //删除提醒
  49. export const delMessage = (cbk) => {
  50. window?.$messageBox?.alert('请谨慎考虑后,确认是否需要删除?', '删除提醒', {
  51. showCancelButton: true,
  52. confirmButtonText: '确认删除',
  53. cancelButtonText: '取消',
  54. type: 'warning',
  55. callback: (action) => {
  56. if (action === 'confirm') {
  57. cbk()
  58. }
  59. },
  60. })
  61. }
  62. //动态加载线上js文件
  63. export const addDocumentsJs = () => {
  64. return new Promise((resolve) => {
  65. const script = document.createElement('script')
  66. script.src = 'http://47.110.251.215:6831/web-apps/apps/api/documents/api.js'
  67. script.type = 'text/javascript'
  68. document.head.appendChild(script)
  69. script.onload = () => {
  70. resolve()
  71. }
  72. })
  73. }
  74. //判断是否为网址
  75. export const isPathUrl = (path) => {
  76. return /^(https?:|mailto:|tel:)/.test(path)
  77. }
  78. //获取当前域名
  79. export const getTopUrl = () => {
  80. return window.location.href.split('/#/')[0]
  81. }
  82. //设置系统名称
  83. export const setAppName = (name) => {
  84. const title = window.document.title
  85. window.document.title = `${title}${name ? ' - ' + name : ''}`
  86. }
  87. //保留两位小数并返回
  88. export const keepdecimal = (str)=>{
  89. // 判断是否有小数点
  90. if (str.indexOf('.') !== -1) {
  91. // 将数字字符串拆分为整数部分和小数部分
  92. let parts = str.split('.')
  93. let decimalPart = parts[1]
  94. // 判断小数部分是否超过两位
  95. if (decimalPart.length > 2) {
  96. // 保留两位小数并返回
  97. return parseFloat(Number(str).toFixed(2)).toString()
  98. } else {
  99. return str
  100. }
  101. } else {
  102. return str
  103. }
  104. }
  105. //是否重复
  106. export const hasDuplicates = (array) =>{
  107. let counts = {} // 用于存储每个元素的出现次数
  108. for (let i = 0; i < array.length; i++) {
  109. let element = array[i]
  110. // 如果 counts 中已经有了这个元素,则将其出现次数加 1;否则,将其出现次数设为 1
  111. counts[element] = (counts[element] || 0) + 1
  112. // 如果某个元素的出现次数大于 1,则数组中存在重复元素,返回 true
  113. if (counts[element] > 1) {
  114. return {
  115. isWarn:true,
  116. element:array[i],
  117. }
  118. }
  119. }
  120. // 遍历完成后未发现重复元素,返回 false
  121. return false
  122. }