ruoyi.js 6.2 KB

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