ruoyi.js 6.1 KB

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