ruoyi.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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, propName) {
  54. var search = params;
  55. search.params = {};
  56. if (null != dateRange && '' != dateRange) {
  57. if (typeof(propName) === "undefined") {
  58. search.params["beginTime"] = dateRange[0];
  59. search.params["endTime"] = dateRange[1];
  60. } else {
  61. search.params["begin" + propName] = dateRange[0];
  62. search.params["end" + propName] = dateRange[1];
  63. }
  64. }
  65. return search;
  66. }
  67. // 回显数据字典
  68. export function selectDictLabel(datas, value) {
  69. var actions = [];
  70. Object.keys(datas).some((key) => {
  71. if (datas[key].dictValue == ('' + value)) {
  72. actions.push(datas[key].dictLabel);
  73. return true;
  74. }
  75. })
  76. return actions.join('');
  77. }
  78. // 回显数据字典(字符串数组)
  79. export function selectDictLabels(datas, value, separator) {
  80. var actions = [];
  81. var currentSeparator = undefined === separator ? "," : separator;
  82. var temp = value.split(currentSeparator);
  83. Object.keys(value.split(currentSeparator)).some((val) => {
  84. Object.keys(datas).some((key) => {
  85. if (datas[key].dictValue == ('' + temp[val])) {
  86. actions.push(datas[key].dictLabel + currentSeparator);
  87. }
  88. })
  89. })
  90. return actions.join('').substring(0, actions.join('').length - 1);
  91. }
  92. // 通用下载方法
  93. export function download(fileName) {
  94. window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
  95. }
  96. // 字符串格式化(%s )
  97. export function sprintf(str) {
  98. var args = arguments, flag = true, i = 1;
  99. str = str.replace(/%s/g, function () {
  100. var arg = args[i++];
  101. if (typeof arg === 'undefined') {
  102. flag = false;
  103. return '';
  104. }
  105. return arg;
  106. });
  107. return flag ? str : '';
  108. }
  109. // 转换字符串,undefined,null等转化为""
  110. export function praseStrEmpty(str) {
  111. if (!str || str == "undefined" || str == "null") {
  112. return "";
  113. }
  114. return str;
  115. }
  116. /**
  117. * 构造树型结构数据
  118. * @param {*} data 数据源
  119. * @param {*} id id字段 默认 'id'
  120. * @param {*} parentId 父节点字段 默认 'parentId'
  121. * @param {*} children 孩子节点字段 默认 'children'
  122. * @param {*} rootId 根Id 默认 0
  123. */
  124. export function handleTree(data, id, parentId, children, rootId) {
  125. id = id || 'id'
  126. parentId = parentId || 'parentId'
  127. children = children || 'children'
  128. rootId = rootId || Math.min.apply(Math, data.map(item => { return item[parentId] })) || 0
  129. //对源数据深度克隆
  130. const cloneData = JSON.parse(JSON.stringify(data))
  131. //循环所有项
  132. const treeData = cloneData.filter(father => {
  133. let branchArr = cloneData.filter(child => {
  134. //返回每一项的子级数组
  135. return father[id] === child[parentId]
  136. });
  137. branchArr.length > 0 ? father.children = branchArr : '';
  138. //返回第一层
  139. return father[parentId] === rootId;
  140. });
  141. return treeData != '' ? treeData : data;
  142. }
  143. /**
  144. * 参数处理
  145. * @param {*} params 参数
  146. */
  147. export function tansParams(params) {
  148. let result = ''
  149. Object.keys(params).forEach((key) => {
  150. if (!Object.is(params[key], undefined) && !Object.is(params[key], null) && !Object.is(JSON.stringify(params[key]), '{}')) {
  151. result += encodeURIComponent(key) + '=' + encodeURIComponent(params[key]) + '&'
  152. }
  153. })
  154. return result
  155. }