ruoyi.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. */
  123. export function handleTree(data, id, parentId, children) {
  124. let config = {
  125. id: id || 'id',
  126. parentId: parentId || 'parentId',
  127. childrenList: children || 'children'
  128. };
  129. var childrenListMap = {};
  130. var nodeIds = {};
  131. var tree = [];
  132. for (let d of data) {
  133. let parentId = d[config.parentId];
  134. if (childrenListMap[parentId] == null) {
  135. childrenListMap[parentId] = [];
  136. }
  137. nodeIds[d[config.id]] = d;
  138. childrenListMap[parentId].push(d);
  139. }
  140. for (let d of data) {
  141. let parentId = d[config.parentId];
  142. if (nodeIds[parentId] == null) {
  143. tree.push(d);
  144. }
  145. }
  146. for (let t of tree) {
  147. adaptToChildrenList(t);
  148. }
  149. function adaptToChildrenList(o) {
  150. if (childrenListMap[o[config.id]] !== null) {
  151. o[config.childrenList] = childrenListMap[o[config.id]];
  152. }
  153. if (o[config.childrenList]) {
  154. for (let c of o[config.childrenList]) {
  155. adaptToChildrenList(c);
  156. }
  157. }
  158. }
  159. return tree;
  160. }