ruoyi.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. if (!value) return '-';
  63. const dataArr = datas.filter(item => item.dictValue === value.toString());
  64. return dataArr.length ? dataArr[0].dictLabel : 'Error Dict';
  65. }
  66. // 回显数据字典(字符串数组)
  67. export function selectDictLabels(datas = [], value = '', separator = ',') {
  68. const actions = [];
  69. const temp = value.split(separator).filter(item => item);
  70. temp.forEach((_, index) => {
  71. actions.push(selectDictLabel(datas, temp[index]));
  72. })
  73. return actions.join(separator);
  74. }
  75. // 通用下载方法
  76. export function download(fileName) {
  77. window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
  78. }
  79. // 字符串格式化(%s )
  80. export function sprintf(str) {
  81. var args = arguments, flag = true, i = 1;
  82. str = str.replace(/%s/g, function () {
  83. var arg = args[i++];
  84. if (typeof arg === 'undefined') {
  85. flag = false;
  86. return '';
  87. }
  88. return arg;
  89. });
  90. return flag ? str : '';
  91. }
  92. // 转换字符串,undefined,null等转化为""
  93. export function praseStrEmpty(str) {
  94. if (!str || str == "undefined" || str == "null") {
  95. return "";
  96. }
  97. return str;
  98. }
  99. /**
  100. * 构造树型结构数据
  101. * @param {*} data 数据源
  102. * @param {*} id id字段 默认 'id'
  103. * @param {*} parentId 父节点字段 默认 'parentId'
  104. * @param {*} children 孩子节点字段 默认 'children'
  105. * @param {*} rootId 根Id 默认 0
  106. */
  107. export function handleTree(data = [], id = 'id', parentId = 'parentId', children = 'children', rootId = 0) {
  108. //对源数据深度克隆
  109. const cloneData = JSON.parse(JSON.stringify(data))
  110. //循环所有项
  111. const treeData = cloneData.filter(father => {
  112. const branchArr = cloneData.filter(child => {
  113. //返回每一项的子级数组
  114. return father[id] === child[parentId]
  115. });
  116. branchArr.length && (father.children = branchArr);
  117. //返回第一层
  118. return father[parentId] === rootId;
  119. });
  120. return treeData !== '' ? treeData : data;
  121. }