ruoyi.js 6.8 KB

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