ruoyi.js 7.0 KB

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