tools.js 3.7 KB

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