tool.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * Copyright [2022] [https://www.xiaonuo.vip]
  3. * Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
  4. * 1.请不要删除和修改根目录下的LICENSE文件。
  5. * 2.请不要删除和修改Snowy源码头部的版权声明。
  6. * 3.本项目代码可免费商业使用,商业使用请保留源码和相关描述文件的项目出处,作者声明等。
  7. * 4.分发源码时候,请注明软件出处 https://www.xiaonuo.vip
  8. * 5.不可二次分发开源参与同类竞品,如有想法可联系团队xiaonuobase@qq.com商议合作。
  9. * 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
  10. */
  11. /*
  12. * @Descripttion: 工具集
  13. * @version: 1.1
  14. * @LastEditors: yubaoshan
  15. * @LastEditTime: 2022年4月19日10:58:41
  16. */
  17. const tool = {}
  18. // localStorage
  19. tool.data = {
  20. set(table, settings) {
  21. const _set = JSON.stringify(settings)
  22. return localStorage.setItem(table, _set)
  23. },
  24. get(table) {
  25. let data = localStorage.getItem(table)
  26. try {
  27. data = JSON.parse(data)
  28. } catch (err) {
  29. return null
  30. }
  31. return data
  32. },
  33. remove(table) {
  34. return localStorage.removeItem(table)
  35. },
  36. clear() {
  37. return localStorage.clear()
  38. }
  39. }
  40. // sessionStorage
  41. tool.session = {
  42. set(table, settings) {
  43. const _set = JSON.stringify(settings)
  44. return sessionStorage.setItem(table, _set)
  45. },
  46. get(table) {
  47. let data = sessionStorage.getItem(table)
  48. try {
  49. data = JSON.parse(data)
  50. } catch (err) {
  51. return null
  52. }
  53. return data
  54. },
  55. remove(table) {
  56. return sessionStorage.removeItem(table)
  57. },
  58. clear() {
  59. return sessionStorage.clear()
  60. }
  61. }
  62. // 千分符
  63. tool.groupSeparator = (num) => {
  64. num = `${num}`
  65. if (!num.includes('.')) num += '.'
  66. return num
  67. .replace(/(\d)(?=(\d{3})+\.)/g, ($0, $1) => {
  68. return `${$1},`
  69. })
  70. .replace(/\.$/, '')
  71. }
  72. // 获取所有字典数组
  73. tool.dictDataAll = () => {
  74. return tool.data.get('DICT_TYPE_TREE_DATA')
  75. }
  76. // 字典翻译方法,界面插槽使用方法 {{ $TOOL.dictType('sex', record.sex) }}
  77. tool.dictTypeData = (dictValue, value) => {
  78. const dictTypeTree = tool.dictDataAll()
  79. if (!dictTypeTree) {
  80. return '需重新登录'
  81. }
  82. const tree = dictTypeTree.find((item) => item.dictValue === dictValue)
  83. if (!tree) {
  84. return '无此字典'
  85. }
  86. const children = tree.children
  87. const dict = children.find((item) => item.dictValue === value)
  88. return dict ? dict.dictLabel : '无此字典项'
  89. }
  90. // 获取某个code下字典的列表,多用于字典下拉框
  91. tool.dictTypeList = (dictValue) => {
  92. const dictTypeTree = tool.dictDataAll()
  93. if (!dictTypeTree) {
  94. return []
  95. }
  96. const tree = dictTypeTree.find((item) => item.dictValue === dictValue)
  97. if (tree && tree.children) {
  98. return tree.children
  99. }
  100. return []
  101. }
  102. // 获取某个code下字典的列表,基于dictTypeList 改进,保留老的,逐步替换
  103. tool.dictList = (dictValue) => {
  104. const dictTypeTree = tool.dictDataAll()
  105. if (!dictTypeTree) {
  106. return []
  107. }
  108. const tree = dictTypeTree.find((item) => item.dictValue === dictValue)
  109. if (tree) {
  110. return tree.children.map((item) => {
  111. return {
  112. value: item['dictValue'],
  113. label: item['name']
  114. }
  115. })
  116. }
  117. return []
  118. }
  119. // 树形翻译 需要指定最顶级的 parentValue 和当级的value
  120. tool.translateTree = (parentValue, value) => {
  121. const tree = tool.dictDataAll().find((item) => item.dictValue === parentValue)
  122. const targetNode = findNodeByValue(tree, value)
  123. return targetNode ? targetNode.dictLabel : ''
  124. }
  125. const findNodeByValue = (node, value) => {
  126. if (node.dictValue === value) {
  127. return node
  128. }
  129. if (node.children) {
  130. for (let i = 0; i < node.children.length; i++) {
  131. const result = findNodeByValue(node.children[i], value)
  132. if (result) {
  133. return result
  134. }
  135. }
  136. }
  137. return null
  138. }
  139. // 生成UUID
  140. tool.snowyUuid = () => {
  141. let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
  142. let r = (Math.random() * 16) | 0,
  143. v = c === 'x' ? r : (r & 0x3) | 0x8
  144. return v.toString(16)
  145. })
  146. // 首字符转换成字母
  147. return 'xn' + uuid.slice(2)
  148. }
  149. export default tool