ruoyi.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * 通用js方法封装处理
  3. * Copyright (c) 2019 ruoyi
  4. */
  5. const baseURL = process.env.VUE_APP_BASE_API
  6. // 日期格式化
  7. export function parseTime(time, pattern) {
  8. if (arguments.length === 0 || !time) {
  9. return null
  10. }
  11. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  12. let date
  13. if (typeof time === 'object') {
  14. date = time
  15. } else {
  16. if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
  17. time = parseInt(time)
  18. } else if (typeof time === 'string') {
  19. time = time.replace(new RegExp(/-/gm), '/');
  20. }
  21. if ((typeof time === 'number') && (time.toString().length === 10)) {
  22. time = time * 1000
  23. }
  24. date = new Date(time)
  25. }
  26. const formatObj = {
  27. y: date.getFullYear(),
  28. m: date.getMonth() + 1,
  29. d: date.getDate(),
  30. h: date.getHours(),
  31. i: date.getMinutes(),
  32. s: date.getSeconds(),
  33. a: date.getDay()
  34. }
  35. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  36. let value = formatObj[key]
  37. // Note: getDay() returns 0 on Sunday
  38. if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
  39. if (result.length > 0 && value < 10) {
  40. value = '0' + value
  41. }
  42. return value || 0
  43. })
  44. return time_str
  45. }
  46. // 表单重置
  47. export function resetForm(refName) {
  48. if (this.$refs[refName]) {
  49. this.$refs[refName].resetFields();
  50. }
  51. }
  52. // 添加日期范围
  53. export function addDateRange (params = {}, dateRange) {
  54. if (dateRange != null && dateRange !== '') {
  55. params.beginTime = this.dateRange[0]
  56. params.endTime = this.dateRange[1]
  57. }
  58. return params
  59. }
  60. // 回显数据字典
  61. export function selectDictLabel(datas, value) {
  62. var actions = [];
  63. Object.keys(datas).some((key) => {
  64. if (datas[key].dictValue == ('' + value)) {
  65. actions.push(datas[key].dictLabel);
  66. return true;
  67. }
  68. })
  69. return actions.join('');
  70. }
  71. // 回显数据字典(字符串数组)
  72. export function selectDictLabels (datas = {}, value = '', separator = ',') {
  73. const actions = []
  74. const temp = value.split(separator)
  75. temp.forEach((_, index) => {
  76. Object.keys(datas).forEach(key => {
  77. if (datas[key].dictValue === temp[index].toString()) {
  78. actions.push(datas[key].dictLabel)
  79. }
  80. })
  81. })
  82. return actions.join(separator)
  83. }
  84. // 通用下载方法
  85. export function download(fileName) {
  86. window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
  87. }
  88. // 字符串格式化(%s )
  89. export function sprintf(str) {
  90. var args = arguments, flag = true, i = 1;
  91. str = str.replace(/%s/g, function () {
  92. var arg = args[i++];
  93. if (typeof arg === 'undefined') {
  94. flag = false;
  95. return '';
  96. }
  97. return arg;
  98. });
  99. return flag ? str : '';
  100. }
  101. // 转换字符串,undefined,null等转化为""
  102. export function praseStrEmpty(str) {
  103. if (!str || str == "undefined" || str == "null") {
  104. return "";
  105. }
  106. return str;
  107. }
  108. /**
  109. * 构造树型结构数据
  110. * @param {*} data 数据源
  111. * @param {*} id id字段 默认 'id'
  112. * @param {*} parentId 父节点字段 默认 'parentId'
  113. * @param {*} children 孩子节点字段 默认 'children'
  114. * @param {*} rootId 根Id 默认 0
  115. */
  116. export function handleTree(data = [], id = 'id', parentId = 'parentId', children = 'children', rootId = 0) {
  117. //对源数据深度克隆
  118. const cloneData = JSON.parse(JSON.stringify(data))
  119. //循环所有项
  120. const treeData = cloneData.filter(father => {
  121. const branchArr = cloneData.filter(child => {
  122. //返回每一项的子级数组
  123. return father[id] === child[parentId]
  124. });
  125. branchArr.length && (father.children = branchArr);
  126. //返回第一层
  127. return father[parentId] === rootId;
  128. });
  129. return treeData !== '' ? treeData : data;
  130. }