ruoyi.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * 通用js方法封装处理
  3. * Copyright (c) 2019 ruoyi
  4. */
  5. // 日期格式化
  6. export function parseTime(time, pattern) {
  7. if (arguments.length === 0 || !time) {
  8. return null;
  9. }
  10. const format = pattern || "{y}-{m}-{d} {h}:{i}:{s}";
  11. let date;
  12. if (typeof time === "object") {
  13. date = time;
  14. } else {
  15. if (typeof time === "string" && /^[0-9]+$/.test(time)) {
  16. time = parseInt(time);
  17. } else if (typeof time === "string") {
  18. time = time
  19. .replace(new RegExp(/-/gm), "/")
  20. .replace("T", " ")
  21. .replace(new RegExp(/\.[\d]{3}/gm), "");
  22. }
  23. if (typeof time === "number" && time.toString().length === 10) {
  24. time = time * 1000;
  25. }
  26. date = new Date(time);
  27. }
  28. const formatObj = {
  29. y: date.getFullYear(),
  30. m: date.getMonth() + 1,
  31. d: date.getDate(),
  32. h: date.getHours(),
  33. i: date.getMinutes(),
  34. s: date.getSeconds(),
  35. a: date.getDay(),
  36. };
  37. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  38. let value = formatObj[key];
  39. // Note: getDay() returns 0 on Sunday
  40. if (key === "a") {
  41. return ["日", "一", "二", "三", "四", "五", "六"][value];
  42. }
  43. if (result.length > 0 && value < 10) {
  44. value = "0" + value;
  45. }
  46. return value || 0;
  47. });
  48. return time_str;
  49. }
  50. // 表单重置
  51. export function resetForm(refName) {
  52. if (this.$refs[refName]) {
  53. this.$refs[refName].resetFields();
  54. }
  55. }
  56. // 添加日期范围
  57. export function addDateRange(params, dateRange, propName) {
  58. let search = params;
  59. search.params =
  60. typeof search.params === "object" &&
  61. search.params !== null &&
  62. !Array.isArray(search.params)
  63. ? search.params
  64. : {};
  65. dateRange = Array.isArray(dateRange) ? dateRange : [];
  66. if (typeof propName === "undefined") {
  67. search.params["beginTime"] = dateRange[0];
  68. search.params["endTime"] = dateRange[1];
  69. } else {
  70. search.params["begin" + propName] = dateRange[0];
  71. search.params["end" + propName] = dateRange[1];
  72. }
  73. return search;
  74. }
  75. // 回显数据字典
  76. export function selectDictLabel(datas, value) {
  77. if (value === undefined) {
  78. return "";
  79. }
  80. var actions = [];
  81. Object.keys(datas).some((key) => {
  82. if (datas[key].value == "" + value) {
  83. actions.push(datas[key].label);
  84. return true;
  85. }
  86. });
  87. if (actions.length === 0) {
  88. actions.push(value);
  89. }
  90. return actions.join("");
  91. }
  92. // 回显数据字典(字符串、数组)
  93. export function selectDictLabels(datas, value, separator) {
  94. if (value === undefined || value.length === 0) {
  95. return "";
  96. }
  97. if (Array.isArray(value)) {
  98. value = value.join(",");
  99. }
  100. var actions = [];
  101. var currentSeparator = undefined === separator ? "," : separator;
  102. var temp = value.split(currentSeparator);
  103. Object.keys(value.split(currentSeparator)).some((val) => {
  104. var match = false;
  105. Object.keys(datas).some((key) => {
  106. if (datas[key].value == "" + temp[val]) {
  107. actions.push(datas[key].label + currentSeparator);
  108. match = true;
  109. }
  110. });
  111. if (!match) {
  112. actions.push(temp[val] + currentSeparator);
  113. }
  114. });
  115. return actions.join("").substring(0, actions.join("").length - 1);
  116. }
  117. // 字符串格式化(%s )
  118. export function sprintf(str) {
  119. var args = arguments,
  120. flag = true,
  121. i = 1;
  122. str = str.replace(/%s/g, function () {
  123. var arg = args[i++];
  124. if (typeof arg === "undefined") {
  125. flag = false;
  126. return "";
  127. }
  128. return arg;
  129. });
  130. return flag ? str : "";
  131. }
  132. // 转换字符串,undefined,null等转化为""
  133. export function parseStrEmpty(str) {
  134. if (!str || str == "undefined" || str == "null") {
  135. return "";
  136. }
  137. return str;
  138. }
  139. // 数据合并
  140. export function mergeRecursive(source, target) {
  141. for (var p in target) {
  142. try {
  143. if (target[p].constructor == Object) {
  144. source[p] = mergeRecursive(source[p], target[p]);
  145. } else {
  146. source[p] = target[p];
  147. }
  148. } catch (e) {
  149. source[p] = target[p];
  150. }
  151. }
  152. return source;
  153. }
  154. /**
  155. * 构造树型结构数据
  156. * @param {*} data 数据源
  157. * @param {*} id id字段 默认 'id'
  158. * @param {*} parentId 父节点字段 默认 'parentId'
  159. * @param {*} children 孩子节点字段 默认 'children'
  160. */
  161. export function handleTree(data, id, parentId, children) {
  162. let config = {
  163. id: id || "id",
  164. parentId: parentId || "parentId",
  165. childrenList: children || "children",
  166. };
  167. var childrenListMap = {};
  168. var nodeIds = {};
  169. var tree = [];
  170. for (let d of data) {
  171. let parentId = d[config.parentId];
  172. if (childrenListMap[parentId] == null) {
  173. childrenListMap[parentId] = [];
  174. }
  175. nodeIds[d[config.id]] = d;
  176. childrenListMap[parentId].push(d);
  177. }
  178. for (let d of data) {
  179. let parentId = d[config.parentId];
  180. if (nodeIds[parentId] == null) {
  181. tree.push(d);
  182. }
  183. }
  184. for (let t of tree) {
  185. adaptToChildrenList(t);
  186. }
  187. function adaptToChildrenList(o) {
  188. if (childrenListMap[o[config.id]] !== null) {
  189. o[config.childrenList] = childrenListMap[o[config.id]];
  190. }
  191. if (o[config.childrenList]) {
  192. for (let c of o[config.childrenList]) {
  193. adaptToChildrenList(c);
  194. }
  195. }
  196. }
  197. return tree;
  198. }
  199. /**
  200. * 参数处理
  201. * @param {*} params 参数
  202. */
  203. export function tansParams(params) {
  204. let result = "";
  205. for (const propName of Object.keys(params)) {
  206. const value = params[propName];
  207. var part = encodeURIComponent(propName) + "=";
  208. if (value !== null && value !== "" && typeof value !== "undefined") {
  209. if (typeof value === "object") {
  210. for (const key of Object.keys(value)) {
  211. if (
  212. value[key] !== null &&
  213. value[key] !== "" &&
  214. typeof value[key] !== "undefined"
  215. ) {
  216. let params = propName + "[" + key + "]";
  217. var subPart = encodeURIComponent(params) + "=";
  218. result += subPart + encodeURIComponent(value[key]) + "&";
  219. }
  220. }
  221. } else {
  222. result += part + encodeURIComponent(value) + "&";
  223. }
  224. }
  225. }
  226. return result;
  227. }
  228. // 验证是否为blob格式
  229. export function blobValidate(data) {
  230. return data.type !== "application/json";
  231. }