ruoyi.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. const search = params;
  55. search.params = {};
  56. if (null != dateRange && '' !== dateRange) {
  57. if (typeof(propName) === "undefined") {
  58. search["beginTime"] = dateRange[0];
  59. search["endTime"] = dateRange[1];
  60. } else {
  61. search["begin" + propName] = dateRange[0];
  62. search["end" + propName] = dateRange[1];
  63. }
  64. }
  65. return search;
  66. }
  67. /**
  68. * 添加开始和结束时间到 params 参数中
  69. *
  70. * @param params 参数
  71. * @param dateRange 时间范围。
  72. * 大小为 2 的数组,每个时间为 yyyy-MM-dd 格式
  73. * @param propName 加入的参数名,可以为空
  74. */
  75. export function addBeginAndEndTime(params, dateRange, propName) {
  76. // 必须传入参数
  77. if (!dateRange) {
  78. return params;
  79. }
  80. // 如果未传递 propName 属性,默认为 time
  81. if (!propName) {
  82. propName = 'Time';
  83. } else {
  84. propName = propName.charAt(0).toUpperCase() + propName.slice(1);
  85. }
  86. // 设置参数
  87. if (dateRange[0]) {
  88. params['begin' + propName] = dateRange[0] + ' 00:00:00';
  89. }
  90. if (dateRange[1]) {
  91. params['end' + propName] = dateRange[1] + ' 23:59:59';
  92. }
  93. return params;
  94. }
  95. // 回显数据字典
  96. export function selectDictLabel(datas, value) {
  97. var actions = [];
  98. Object.keys(datas).some((key) => {
  99. if (datas[key].dictValue == ('' + value)) {
  100. actions.push(datas[key].dictLabel);
  101. return true;
  102. }
  103. })
  104. return actions.join('');
  105. }
  106. // 通用下载方法
  107. export function download(fileName) {
  108. window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
  109. }
  110. // 下载 Excel 方法
  111. export function downloadExcel(data, fileName) {
  112. download0(data, fileName, 'application/vnd.ms-excel');
  113. }
  114. // 下载 Word 方法
  115. export function downloadWord(data, fileName) {
  116. download0(data, fileName, 'application/msword');
  117. }
  118. // 下载 Zip 方法
  119. export function downloadZip(data, fileName) {
  120. download0(data, fileName, 'application/zip');
  121. }
  122. // 下载 Html 方法
  123. export function downloadHtml(data, fileName) {
  124. download0(data, fileName, 'text/html');
  125. }
  126. // 下载 Markdown 方法
  127. export function downloadMarkdown(data, fileName) {
  128. download0(data, fileName, 'text/markdown');
  129. }
  130. function download0(data, fileName, mineType) {
  131. // 创建 blob
  132. let blob = new Blob([data], {type: mineType});
  133. // 创建 href 超链接,点击进行下载
  134. window.URL = window.URL || window.webkitURL;
  135. let href = URL.createObjectURL(blob);
  136. let downA = document.createElement("a");
  137. downA.href = href;
  138. downA.download = fileName;
  139. downA.click();
  140. // 销毁超连接
  141. window.URL.revokeObjectURL(href);
  142. }
  143. // 字符串格式化(%s )
  144. export function sprintf(str) {
  145. var args = arguments, flag = true, i = 1;
  146. str = str.replace(/%s/g, function () {
  147. var arg = args[i++];
  148. if (typeof arg === 'undefined') {
  149. flag = false;
  150. return '';
  151. }
  152. return arg;
  153. });
  154. return flag ? str : '';
  155. }
  156. // 转换字符串,undefined,null等转化为""
  157. export function praseStrEmpty(str) {
  158. if (!str || str == "undefined" || str == "null") {
  159. return "";
  160. }
  161. return str;
  162. }
  163. /**
  164. * 构造树型结构数据
  165. * @param {*} data 数据源
  166. * @param {*} id id字段 默认 'id'
  167. * @param {*} parentId 父节点字段 默认 'parentId'
  168. * @param {*} children 孩子节点字段 默认 'children'
  169. * @param {*} rootId 根Id 默认 0
  170. */
  171. export function handleTree(data, id, parentId, children, rootId) {
  172. id = id || 'id'
  173. parentId = parentId || 'parentId'
  174. children = children || 'children'
  175. rootId = rootId || Math.min.apply(Math, data.map(item => { return item[parentId] })) || 0
  176. //对源数据深度克隆
  177. const cloneData = JSON.parse(JSON.stringify(data))
  178. //循环所有项
  179. const treeData = cloneData.filter(father => {
  180. let branchArr = cloneData.filter(child => {
  181. //返回每一项的子级数组
  182. return father[id] === child[parentId]
  183. });
  184. branchArr.length > 0 ? father.children = branchArr : '';
  185. //返回第一层
  186. return father[parentId] === rootId;
  187. });
  188. return treeData !== '' ? treeData : data;
  189. }