ruoyi.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/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') {
  39. return ['日', '一', '二', '三', '四', '五', '六'][value]
  40. }
  41. if (result.length > 0 && value < 10) {
  42. value = '0' + value
  43. }
  44. return value || 0
  45. })
  46. return time_str
  47. }
  48. // 表单重置
  49. export function resetForm(refName) {
  50. if (this.$refs[refName]) {
  51. this.$refs[refName].resetFields();
  52. }
  53. }
  54. // 添加日期范围
  55. export function addDateRange(params, dateRange, propName) {
  56. const search = params;
  57. search.params = {};
  58. if (null != dateRange && '' !== dateRange) {
  59. if (typeof (propName) === "undefined") {
  60. search["beginTime"] = dateRange[0];
  61. search["endTime"] = dateRange[1];
  62. } else {
  63. search["begin" + propName] = dateRange[0];
  64. search["end" + propName] = dateRange[1];
  65. }
  66. }
  67. return search;
  68. }
  69. /**
  70. * 添加开始和结束时间到 params 参数中
  71. *
  72. * @param params 参数
  73. * @param dateRange 时间范围。
  74. * 大小为 2 的数组,每个时间为 yyyy-MM-dd 格式
  75. * @param propName 加入的参数名,可以为空
  76. */
  77. export function addBeginAndEndTime(params, dateRange, propName) {
  78. // 必须传入参数
  79. if (!dateRange) {
  80. return params;
  81. }
  82. // 如果未传递 propName 属性,默认为 time
  83. if (!propName) {
  84. propName = 'Time';
  85. } else {
  86. propName = propName.charAt(0).toUpperCase() + propName.slice(1);
  87. }
  88. // 设置参数
  89. if (dateRange[0]) {
  90. params['begin' + propName] = dateRange[0] + ' 00:00:00';
  91. }
  92. if (dateRange[1]) {
  93. params['end' + propName] = dateRange[1] + ' 23:59:59';
  94. }
  95. return params;
  96. }
  97. // 回显数据字典 原若依所保留,请使用 dict.js 中的新方法
  98. export function selectDictLabel(datas, value) {
  99. var actions = [];
  100. Object.keys(datas).some((key) => {
  101. if (datas[key].dictValue == ('' + value)) {
  102. actions.push(datas[key].dictLabel);
  103. return true;
  104. }
  105. })
  106. return actions.join('');
  107. }
  108. // 通用下载方法
  109. export function download(fileName) {
  110. window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
  111. }
  112. // 下载 Excel 方法
  113. export function downloadExcel(data, fileName) {
  114. download0(data, fileName, 'application/vnd.ms-excel');
  115. }
  116. // 下载 Word 方法
  117. export function downloadWord(data, fileName) {
  118. download0(data, fileName, 'application/msword');
  119. }
  120. // 下载 Zip 方法
  121. export function downloadZip(data, fileName) {
  122. download0(data, fileName, 'application/zip');
  123. }
  124. // 下载 Html 方法
  125. export function downloadHtml(data, fileName) {
  126. download0(data, fileName, 'text/html');
  127. }
  128. // 下载 Markdown 方法
  129. export function downloadMarkdown(data, fileName) {
  130. download0(data, fileName, 'text/markdown');
  131. }
  132. function download0(data, fileName, mineType) {
  133. // 创建 blob
  134. let blob = new Blob([data], {type: mineType});
  135. // 创建 href 超链接,点击进行下载
  136. window.URL = window.URL || window.webkitURL;
  137. let href = URL.createObjectURL(blob);
  138. let downA = document.createElement("a");
  139. downA.href = href;
  140. downA.download = fileName;
  141. downA.click();
  142. // 销毁超连接
  143. window.URL.revokeObjectURL(href);
  144. }
  145. // 字符串格式化(%s )
  146. export function sprintf(str) {
  147. var args = arguments, flag = true, i = 1;
  148. str = str.replace(/%s/g, function () {
  149. var arg = args[i++];
  150. if (typeof arg === 'undefined') {
  151. flag = false;
  152. return '';
  153. }
  154. return arg;
  155. });
  156. return flag ? str : '';
  157. }
  158. // 转换字符串,undefined,null等转化为""
  159. export function praseStrEmpty(str) {
  160. if (!str || str == "undefined" || str == "null") {
  161. return "";
  162. }
  163. return str;
  164. }
  165. /**
  166. * 构造树型结构数据
  167. * @param {*} data 数据源
  168. * @param {*} id id字段 默认 'id'
  169. * @param {*} parentId 父节点字段 默认 'parentId'
  170. * @param {*} children 孩子节点字段 默认 'children'
  171. * @param {*} rootId 根Id 默认 0
  172. */
  173. export function handleTree(data, id, parentId, children, rootId) {
  174. id = id || 'id'
  175. parentId = parentId || 'parentId'
  176. children = children || 'children'
  177. rootId = rootId || Math.min.apply(Math, data.map(item => {
  178. return item[parentId]
  179. })) || 0
  180. //对源数据深度克隆
  181. const cloneData = JSON.parse(JSON.stringify(data))
  182. //循环所有项
  183. const treeData = cloneData.filter(father => {
  184. let branchArr = cloneData.filter(child => {
  185. //返回每一项的子级数组
  186. return father[id] === child[parentId]
  187. });
  188. branchArr.length > 0 ? father.children = branchArr : '';
  189. //返回第一层
  190. return father[parentId] === rootId;
  191. });
  192. return treeData !== '' ? treeData : data;
  193. }
  194. /**
  195. * 获取当前时间
  196. * @param timeStr 时分秒 字符串 格式为 xx:xx:xx
  197. */
  198. export function getNowDateTime(timeStr) {
  199. let now = new Date();
  200. let year = now.getFullYear(); //得到年份
  201. let month = (now.getMonth() + 1).toString().padStart(2, "0"); //得到月份
  202. let day = now.getDate().toString().padStart(2, "0"); //得到日期
  203. if (timeStr != null) {
  204. return `${year}-${month}-${day} ${timeStr}`;
  205. }
  206. let hours = now.getHours().toString().padStart(2, "0") // 得到小时;
  207. let minutes = now.getMinutes().toString().padStart(2, "0") // 得到分钟;
  208. let seconds = now.getSeconds().toString().padStart(2, "0") // 得到秒;
  209. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  210. }