ruoyi.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // 日期格式化
  2. export function parseTime(time: any, pattern?: string) {
  3. if (arguments.length === 0 || !time) {
  4. return null;
  5. }
  6. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}';
  7. let date;
  8. if (typeof time === 'object') {
  9. date = time;
  10. } else {
  11. if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
  12. time = parseInt(time);
  13. } else if (typeof time === 'string') {
  14. time = time
  15. .replace(new RegExp(/-/gm), '/')
  16. .replace('T', ' ')
  17. .replace(new RegExp(/\.[\d]{3}/gm), '');
  18. }
  19. if (typeof time === 'number' && time.toString().length === 10) {
  20. time = time * 1000;
  21. }
  22. date = new Date(time);
  23. }
  24. const formatObj: { [key: string]: any } = {
  25. y: date.getFullYear(),
  26. m: date.getMonth() + 1,
  27. d: date.getDate(),
  28. h: date.getHours(),
  29. i: date.getMinutes(),
  30. s: date.getSeconds(),
  31. a: date.getDay()
  32. };
  33. return format.replace(/{(y|m|d|h|i|s|a)+}/g, (result: string, key: string) => {
  34. let value = formatObj[key];
  35. // Note: getDay() returns 0 on Sunday
  36. if (key === 'a') {
  37. return ['日', '一', '二', '三', '四', '五', '六'][value];
  38. }
  39. if (result.length > 0 && value < 10) {
  40. value = '0' + value;
  41. }
  42. return value || 0;
  43. });
  44. }
  45. /**
  46. * 添加日期范围
  47. * @param params
  48. * @param dateRange
  49. * @param propName
  50. */
  51. export const addDateRange = (params: any, dateRange: any[], propName?: string) => {
  52. const search = params;
  53. search.params = typeof search.params === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
  54. dateRange = Array.isArray(dateRange) ? dateRange : [];
  55. if (typeof propName === 'undefined') {
  56. search.params['beginTime'] = dateRange[0];
  57. search.params['endTime'] = dateRange[1];
  58. } else {
  59. search.params['begin' + propName] = dateRange[0];
  60. search.params['end' + propName] = dateRange[1];
  61. }
  62. return search;
  63. };
  64. // 回显数据字典
  65. export const selectDictLabel = (datas: any, value: number | string) => {
  66. if (value === undefined) {
  67. return '';
  68. }
  69. const actions: Array<string | number> = [];
  70. Object.keys(datas).some((key) => {
  71. if (datas[key].value == '' + value) {
  72. actions.push(datas[key].label);
  73. return true;
  74. }
  75. });
  76. if (actions.length === 0) {
  77. actions.push(value);
  78. }
  79. return actions.join('');
  80. };
  81. // 回显数据字典(字符串数组)
  82. export const selectDictLabels = (datas: any, value: any, separator: any) => {
  83. if (value === undefined || value.length === 0) {
  84. return '';
  85. }
  86. if (Array.isArray(value)) {
  87. value = value.join(',');
  88. }
  89. const actions: any[] = [];
  90. const currentSeparator = undefined === separator ? ',' : separator;
  91. const temp = value.split(currentSeparator);
  92. Object.keys(value.split(currentSeparator)).some((val) => {
  93. let match = false;
  94. Object.keys(datas).some((key) => {
  95. if (datas[key].value == '' + temp[val]) {
  96. actions.push(datas[key].label + currentSeparator);
  97. match = true;
  98. }
  99. });
  100. if (!match) {
  101. actions.push(temp[val] + currentSeparator);
  102. }
  103. });
  104. return actions.join('').substring(0, actions.join('').length - 1);
  105. };
  106. // 字符串格式化(%s )
  107. export function sprintf(str: string) {
  108. if (arguments.length !== 0) {
  109. let flag = true,
  110. i = 1;
  111. str = str.replace(/%s/g, function () {
  112. const arg = arguments[i++];
  113. if (typeof arg === 'undefined') {
  114. flag = false;
  115. return '';
  116. }
  117. return arg;
  118. });
  119. return flag ? str : '';
  120. }
  121. }
  122. // 转换字符串,undefined,null等转化为""
  123. export const parseStrEmpty = (str: any) => {
  124. if (!str || str == 'undefined' || str == 'null') {
  125. return '';
  126. }
  127. return str;
  128. };
  129. // 数据合并
  130. export const mergeRecursive = (source: any, target: any) => {
  131. for (const p in target) {
  132. try {
  133. if (target[p].constructor == Object) {
  134. source[p] = mergeRecursive(source[p], target[p]);
  135. } else {
  136. source[p] = target[p];
  137. }
  138. } catch (e) {
  139. source[p] = target[p];
  140. }
  141. }
  142. return source;
  143. };
  144. /**
  145. * 构造树型结构数据
  146. * @param {*} data 数据源
  147. * @param {*} id id字段 默认 'id'
  148. * @param {*} parentId 父节点字段 默认 'parentId'
  149. * @param {*} children 孩子节点字段 默认 'children'
  150. */
  151. export const handleTree = <T>(data: any[], id?: string, parentId?: string, children?: string): T[] => {
  152. const config: {
  153. id: string;
  154. parentId: string;
  155. childrenList: string;
  156. } = {
  157. id: id || 'id',
  158. parentId: parentId || 'parentId',
  159. childrenList: children || 'children'
  160. };
  161. const childrenListMap: any = {};
  162. const nodeIds: any = {};
  163. const tree: T[] = [];
  164. for (const d of data) {
  165. const parentId = d[config.parentId];
  166. if (childrenListMap[parentId] == null) {
  167. childrenListMap[parentId] = [];
  168. }
  169. nodeIds[d[config.id]] = d;
  170. childrenListMap[parentId].push(d);
  171. }
  172. for (const d of data) {
  173. const parentId = d[config.parentId];
  174. if (nodeIds[parentId] == null) {
  175. tree.push(d);
  176. }
  177. }
  178. const adaptToChildrenList = (o: any) => {
  179. if (childrenListMap[o[config.id]] !== null) {
  180. o[config.childrenList] = childrenListMap[o[config.id]];
  181. }
  182. if (o[config.childrenList]) {
  183. for (const c of o[config.childrenList]) {
  184. adaptToChildrenList(c);
  185. }
  186. }
  187. };
  188. for (const t of tree) {
  189. adaptToChildrenList(t);
  190. }
  191. return tree;
  192. };
  193. /**
  194. * 参数处理
  195. * @param {*} params 参数
  196. */
  197. export const tansParams = (params: any) => {
  198. let result = '';
  199. for (const propName of Object.keys(params)) {
  200. const value = params[propName];
  201. const part = encodeURIComponent(propName) + '=';
  202. if (value !== null && value !== '' && typeof value !== 'undefined') {
  203. if (typeof value === 'object') {
  204. for (const key of Object.keys(value)) {
  205. if (value[key] !== null && value[key] !== '' && typeof value[key] !== 'undefined') {
  206. const params = propName + '[' + key + ']';
  207. const subPart = encodeURIComponent(params) + '=';
  208. result += subPart + encodeURIComponent(value[key]) + '&';
  209. }
  210. }
  211. } else {
  212. result += part + encodeURIComponent(value) + '&';
  213. }
  214. }
  215. }
  216. return result;
  217. };
  218. // 返回项目路径
  219. export const getNormalPath = (p: string): string => {
  220. if (p.length === 0 || !p || p === 'undefined') {
  221. return p;
  222. }
  223. const res = p.replace('//', '/');
  224. if (res[res.length - 1] === '/') {
  225. return res.slice(0, res.length - 1);
  226. }
  227. return res;
  228. };
  229. // 验证是否为blob格式
  230. export const blobValidate = (data: any) => {
  231. return data.type !== 'application/json';
  232. };
  233. export default {
  234. handleTree
  235. };