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