tool.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. const SNOWYSTRING = table.slice(0, 6) === 'SNOWY_' && table !== 'SNOWY_SYS_BASE_CONFIG'
  23. if (SNOWYSTRING) {
  24. let localSetting = JSON.parse(localStorage.getItem('SNOWY_SETTING')) || {}
  25. let newSetting = {}
  26. newSetting[table] = _set
  27. return localStorage.setItem('SNOWY_SETTING', JSON.stringify(Object.assign(localSetting, newSetting)))
  28. } else return localStorage.setItem(table, _set)
  29. },
  30. get(table) {
  31. const SNOWYSTRING = table.slice(0, 6) === 'SNOWY_' && table !== 'SNOWY_SYS_BASE_CONFIG'
  32. const SNOWY_SETTING = JSON.parse(localStorage.getItem('SNOWY_SETTING')) || {}
  33. let data = SNOWYSTRING ? SNOWY_SETTING[table] : localStorage.getItem(table)
  34. try {
  35. data = JSON.parse(data)
  36. } catch (err) {
  37. return null
  38. }
  39. return data
  40. },
  41. remove(table) {
  42. return localStorage.removeItem(table)
  43. },
  44. clear() {
  45. return localStorage.clear()
  46. }
  47. }
  48. // sessionStorage
  49. tool.session = {
  50. set(table, settings) {
  51. const _set = JSON.stringify(settings)
  52. return sessionStorage.setItem(table, _set)
  53. },
  54. get(table) {
  55. let data = sessionStorage.getItem(table)
  56. try {
  57. data = JSON.parse(data)
  58. } catch (err) {
  59. return null
  60. }
  61. return data
  62. },
  63. remove(table) {
  64. return sessionStorage.removeItem(table)
  65. },
  66. clear() {
  67. return sessionStorage.clear()
  68. }
  69. }
  70. // 千分符
  71. tool.groupSeparator = (num) => {
  72. num = `${num}`
  73. if (!num.includes('.')) num += '.'
  74. return num
  75. .replace(/(\d)(?=(\d{3})+\.)/g, ($0, $1) => {
  76. return `${$1},`
  77. })
  78. .replace(/\.$/, '')
  79. }
  80. // 获取所有字典数组
  81. tool.dictDataAll = () => {
  82. return tool.data.get('DICT_TYPE_TREE_DATA')
  83. }
  84. // 字典翻译方法,界面插槽使用方法 {{ $TOOL.dictType('sex', record.sex) }}
  85. tool.dictTypeData = (dictValue, value) => {
  86. const dictTypeTree = tool.dictDataAll()
  87. if (!dictTypeTree) {
  88. return '需重新登录'
  89. }
  90. const tree = dictTypeTree.find((item) => item.dictValue === dictValue)
  91. if (!tree) {
  92. return '无此字典'
  93. }
  94. const children = tree.children
  95. const dict = children.find((item) => item.dictValue === value)
  96. return dict ? dict.dictLabel : '无此字典项'
  97. }
  98. // 获取某个code下字典的列表,多用于字典下拉框
  99. tool.dictTypeList = (dictValue) => {
  100. const dictTypeTree = tool.dictDataAll()
  101. if (!dictTypeTree) {
  102. return []
  103. }
  104. const tree = dictTypeTree.find((item) => item.dictValue === dictValue)
  105. if (tree && tree.children) {
  106. return tree.children
  107. }
  108. return []
  109. }
  110. // 获取某个code下字典的列表,基于dictTypeList 改进,保留老的,逐步替换
  111. tool.dictList = (dictValue) => {
  112. const dictTypeTree = tool.dictDataAll()
  113. if (!dictTypeTree) {
  114. return []
  115. }
  116. const tree = dictTypeTree.find((item) => item.dictValue === dictValue)
  117. if (tree) {
  118. return tree.children.map((item) => {
  119. return {
  120. value: item['dictValue'],
  121. label: item['name']
  122. }
  123. })
  124. }
  125. return []
  126. }
  127. // 树形翻译 需要指定最顶级的 parentValue 和当级的value
  128. tool.translateTree = (parentValue, value) => {
  129. const tree = tool.dictDataAll().find((item) => item.dictValue === parentValue)
  130. const targetNode = findNodeByValue(tree, value)
  131. return targetNode ? targetNode.dictLabel : ''
  132. }
  133. const findNodeByValue = (node, value) => {
  134. if (node.dictValue === value) {
  135. return node
  136. }
  137. if (node.children) {
  138. for (let i = 0; i < node.children.length; i++) {
  139. const result = findNodeByValue(node.children[i], value)
  140. if (result) {
  141. return result
  142. }
  143. }
  144. }
  145. return null
  146. }
  147. // 解析数据,用于列表回显,通过字典value值获取name名称
  148. tool.transform = function (source, value) {
  149. if (Array.isArray(source)) {
  150. const item = source.find((e) => e.value == value)
  151. if (item) {
  152. return item.label
  153. } else {
  154. return value
  155. }
  156. }
  157. return value
  158. }
  159. // 生成UUID
  160. tool.snowyUuid = () => {
  161. let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
  162. let r = (Math.random() * 16) | 0,
  163. v = c === 'x' ? r : (r & 0x3) | 0x8
  164. return v.toString(16)
  165. })
  166. // 首字符转换成字母
  167. return 'xn' + uuid.slice(2)
  168. }
  169. tool.parseTime = (time, cFormat) => {
  170. if (time == null || time.length === 0) {
  171. return ''
  172. }
  173. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  174. let date
  175. if (typeof time === 'object') {
  176. date = time
  177. } else {
  178. if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
  179. time = parseInt(time)
  180. }
  181. if (typeof time === 'number' && time.toString().length === 10) {
  182. time = time * 1000
  183. }
  184. date = new Date(time)
  185. }
  186. const formatObj = {
  187. y: date.getFullYear(),
  188. m: date.getMonth() + 1,
  189. d: date.getDate(),
  190. h: date.getHours(),
  191. i: date.getMinutes(),
  192. s: date.getSeconds(),
  193. a: date.getDay()
  194. }
  195. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  196. let value = formatObj[key]
  197. // Note: getDay() returns 0 on Sunday
  198. if (key === 'a') {
  199. return ['日', '一', '二', '三', '四', '五', '六'][value]
  200. }
  201. if (result.length > 0 && value < 10) {
  202. value = '0' + value
  203. }
  204. return value || 0
  205. })
  206. return time_str
  207. }
  208. export default tool