yongtian.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * 通用js方法封装处理
  3. * Copyright (c) 2019 yongtian
  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 if (propName === "section") {
  61. search["startTime"] = dateRange[0];
  62. search["endTime"] = dateRange[1];
  63. } else {
  64. search.params["begin" + propName] = dateRange[0];
  65. search.params["end" + propName] = dateRange[1];
  66. }
  67. }
  68. return search;
  69. }
  70. // 回显数据字典
  71. export function selectDictLabel(datas, value) {
  72. var actions = [];
  73. Object.keys(datas).some((key) => {
  74. if (datas[key].dictValue == ('' + value)) {
  75. actions.push(datas[key].dictLabel);
  76. return true;
  77. }
  78. })
  79. return actions.join('');
  80. }
  81. // 回显数据字典(字符串数组)
  82. export function selectDictLabels(datas, value, separator) {
  83. var actions = [];
  84. var currentSeparator = undefined === separator ? "," : separator;
  85. var temp = value.split(currentSeparator);
  86. Object.keys(value.split(currentSeparator)).some((val) => {
  87. Object.keys(datas).some((key) => {
  88. if (datas[key].dictValue == ('' + temp[val])) {
  89. actions.push(datas[key].dictLabel + currentSeparator);
  90. }
  91. })
  92. })
  93. return actions.join('').substring(0, actions.join('').length - 1);
  94. }
  95. // 通用下载方法
  96. export function download(fileName) {
  97. window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
  98. }
  99. // 通用下载方法
  100. export function downloadBlob(file) {
  101. if (!file) return this.msgSuccess("导出失败")
  102. let url = window.URL.createObjectURL(new Blob([file.data]));
  103. let aLink = document.createElement('a');
  104. aLink.style.display = 'none';
  105. aLink.href = url;
  106. aLink.setAttribute('download', decodeURI(file.headers['content-disposition'].split('filename=')[1]));
  107. document.body.appendChild(aLink);
  108. aLink.click();
  109. document.body.removeChild(aLink); // 下载完成移除元素
  110. window.URL.revokeObjectURL(url); // 释放掉blob对象
  111. }
  112. // 字符串格式化(%s )
  113. export function sprintf(str) {
  114. var args = arguments,
  115. flag = true,
  116. i = 1;
  117. str = str.replace(/%s/g, function() {
  118. var arg = args[i++];
  119. if (typeof arg === 'undefined') {
  120. flag = false;
  121. return '';
  122. }
  123. return arg;
  124. });
  125. return flag ? str : '';
  126. }
  127. // 转换字符串,undefined,null等转化为""
  128. export function praseStrEmpty(str) {
  129. if (!str || str == "undefined" || str == "null") {
  130. return "";
  131. }
  132. return str;
  133. }
  134. /**
  135. * 构造树型结构数据
  136. * @param {*} data 数据源
  137. * @param {*} id id字段 默认 'id'
  138. * @param {*} parentId 父节点字段 默认 'parentId'
  139. * @param {*} children 孩子节点字段 默认 'children'
  140. */
  141. export function handleTree(data, id, parentId, children) {
  142. let config = {
  143. id: id || 'id',
  144. parentId: parentId || 'parentId',
  145. childrenList: children || 'children'
  146. };
  147. var childrenListMap = {};
  148. var nodeIds = {};
  149. var tree = [];
  150. for (let d of data) {
  151. let parentId = d[config.parentId];
  152. if (childrenListMap[parentId] == null) {
  153. childrenListMap[parentId] = [];
  154. }
  155. nodeIds[d[config.id]] = d;
  156. childrenListMap[parentId].push(d);
  157. }
  158. for (let d of data) {
  159. let parentId = d[config.parentId];
  160. if (nodeIds[parentId] == null) {
  161. tree.push(d);
  162. }
  163. }
  164. for (let t of tree) {
  165. adaptToChildrenList(t);
  166. }
  167. function adaptToChildrenList(o) {
  168. if (childrenListMap[o[config.id]] !== null) {
  169. o[config.childrenList] = childrenListMap[o[config.id]];
  170. }
  171. if (o[config.childrenList]) {
  172. for (let c of o[config.childrenList]) {
  173. adaptToChildrenList(c);
  174. }
  175. }
  176. }
  177. return tree;
  178. }