ruoyi.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. 
  2. /**
  3. * 通用js方法封装处理
  4. * Copyright (c) 2019 ruoyi
  5. */
  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') { 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. let search = params;
  55. search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
  56. dateRange = Array.isArray(dateRange) ? dateRange : [];
  57. if (typeof (propName) === 'undefined') {
  58. search.params['beginTime'] = dateRange[0];
  59. search.params['endTime'] = dateRange[1];
  60. } else {
  61. search.params['begin' + propName] = dateRange[0];
  62. search.params['end' + propName] = dateRange[1];
  63. }
  64. return search;
  65. }
  66. // 回显数据字典
  67. export function selectDictLabel(datas, value) {
  68. if (value === undefined) {
  69. return "";
  70. }
  71. var actions = [];
  72. Object.keys(datas).some((key) => {
  73. if (datas[key].value == ('' + value)) {
  74. actions.push(datas[key].label);
  75. return true;
  76. }
  77. })
  78. if (actions.length === 0) {
  79. actions.push(value);
  80. }
  81. return actions.join('');
  82. }
  83. // 回显数据字典(字符串数组)
  84. export function selectDictLabels(datas, value, separator) {
  85. if (value === undefined) {
  86. return "";
  87. }
  88. var actions = [];
  89. var currentSeparator = undefined === separator ? "," : separator;
  90. var temp = value.split(currentSeparator);
  91. Object.keys(value.split(currentSeparator)).some((val) => {
  92. var match = false;
  93. Object.keys(datas).some((key) => {
  94. if (datas[key].value == ('' + temp[val])) {
  95. actions.push(datas[key].label + currentSeparator);
  96. match = true;
  97. }
  98. })
  99. if (!match) {
  100. actions.push(temp[val] + currentSeparator);
  101. }
  102. })
  103. return actions.join('').substring(0, actions.join('').length - 1);
  104. }
  105. // 字符串格式化(%s )
  106. export function sprintf(str) {
  107. var args = arguments, flag = true, i = 1;
  108. str = str.replace(/%s/g, function () {
  109. var arg = args[i++];
  110. if (typeof arg === 'undefined') {
  111. flag = false;
  112. return '';
  113. }
  114. return arg;
  115. });
  116. return flag ? str : '';
  117. }
  118. // 转换字符串,undefined,null等转化为""
  119. export function parseStrEmpty(str) {
  120. if (!str || str == "undefined" || str == "null") {
  121. return "";
  122. }
  123. return str;
  124. }
  125. // 数据合并
  126. export function mergeRecursive(source, target) {
  127. for (var p in target) {
  128. try {
  129. if (target[p].constructor == Object) {
  130. source[p] = mergeRecursive(source[p], target[p]);
  131. } else {
  132. source[p] = target[p];
  133. }
  134. } catch (e) {
  135. source[p] = target[p];
  136. }
  137. }
  138. return source;
  139. };
  140. /**
  141. * 构造树型结构数据
  142. * @param {*} data 数据源
  143. * @param {*} id id字段 默认 'id'
  144. * @param {*} parentId 父节点字段 默认 'parentId'
  145. * @param {*} children 孩子节点字段 默认 'children'
  146. */
  147. export function handleTree(data, id, parentId, children) {
  148. let config = {
  149. id: id || 'id',
  150. parentId: parentId || 'parentId',
  151. childrenList: children || 'children'
  152. };
  153. var childrenListMap = {};
  154. var nodeIds = {};
  155. var tree = [];
  156. for (let d of data) {
  157. let parentId = d[config.parentId];
  158. if (childrenListMap[parentId] == null) {
  159. childrenListMap[parentId] = [];
  160. }
  161. nodeIds[d[config.id]] = d;
  162. childrenListMap[parentId].push(d);
  163. }
  164. for (let d of data) {
  165. let parentId = d[config.parentId];
  166. if (nodeIds[parentId] == null) {
  167. tree.push(d);
  168. }
  169. }
  170. for (let t of tree) {
  171. adaptToChildrenList(t);
  172. }
  173. function adaptToChildrenList(o) {
  174. if (childrenListMap[o[config.id]] !== null) {
  175. o[config.childrenList] = childrenListMap[o[config.id]];
  176. }
  177. if (o[config.childrenList]) {
  178. for (let c of o[config.childrenList]) {
  179. adaptToChildrenList(c);
  180. }
  181. }
  182. }
  183. return tree;
  184. }
  185. /**
  186. * 参数处理
  187. * @param {*} params 参数
  188. */
  189. export function tansParams(params) {
  190. let result = ''
  191. for (const propName of Object.keys(params)) {
  192. const value = params[propName];
  193. var part = encodeURIComponent(propName) + "=";
  194. if (value !== null && typeof (value) !== "undefined") {
  195. if (typeof value === 'object') {
  196. for (const key of Object.keys(value)) {
  197. if (value[key] !== null && typeof (value[key]) !== 'undefined') {
  198. let params = propName + '[' + key + ']';
  199. var subPart = encodeURIComponent(params) + "=";
  200. result += subPart + encodeURIComponent(value[key]) + "&";
  201. }
  202. }
  203. } else {
  204. result += part + encodeURIComponent(value) + "&";
  205. }
  206. }
  207. }
  208. return result
  209. }
  210. // 验证是否为blob格式
  211. export async function blobValidate(data) {
  212. try {
  213. const text = await data.text();
  214. JSON.parse(text);
  215. return false;
  216. } catch (error) {
  217. return true;
  218. }
  219. }