ruoyi.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import dayjs from "dayjs";
  2. /**
  3. * 通用js方法封装处理
  4. * Copyright (c) 2019 ruoyi
  5. */
  6. // 时间比较
  7. export function timeCheck(arr) {
  8. let srcs = arr.filter((s) => s);
  9. for (let i = srcs.length - 1; i > 0; i--) {
  10. let a = timeToNumber(srcs[i]);
  11. let b = timeToNumber(srcs[i - 1]);
  12. if (a <= b) {
  13. return false;
  14. }
  15. }
  16. return true;
  17. }
  18. function timeToNumber(src) {
  19. return src.replace(":", "") * 1;
  20. }
  21. // 日期格式化 2023-12-30 00:00:00 或者获取星期几
  22. export function parseTime(value, format = "YYYY-MM-DD HH:mm:ss") {
  23. if ((value + "").length == 10 && format != 'dddd') {
  24. value *= 1000;
  25. return value && dayjs(value).format(format);
  26. }
  27. if(format == 'dddd'){
  28. const weeks = ['周曰',"周一",'周二','周三','周四','周五','周六'];
  29. return weeks[dayjs(value).day()];
  30. }
  31. }
  32. // 日期格式化
  33. export function formatTime(value, format = "YYYY-MM-DD HH:mm:ss") {
  34. return value && dayjs(value).format(format);
  35. }
  36. // 表单重置
  37. export function resetForm(refName) {
  38. if (this.$refs[refName]) {
  39. this.$refs[refName].resetFields();
  40. }
  41. }
  42. // 添加日期范围
  43. export function addDateRange(params, dateRange, propName) {
  44. let search = params;
  45. search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
  46. dateRange = Array.isArray(dateRange) ? dateRange : [];
  47. if (typeof (propName) === 'undefined') {
  48. search.params['beginTime'] = dateRange[0];
  49. search.params['endTime'] = dateRange[1];
  50. } else {
  51. search.params['begin' + propName] = dateRange[0];
  52. search.params['end' + propName] = dateRange[1];
  53. }
  54. return search;
  55. }
  56. // 回显数据字典
  57. export function selectDictLabel(datas, value) {
  58. if (value === undefined) {
  59. return "";
  60. }
  61. var actions = [];
  62. Object.keys(datas).some((key) => {
  63. if (datas[key].value == ('' + value)) {
  64. actions.push(datas[key].label);
  65. return true;
  66. }
  67. })
  68. if (actions.length === 0) {
  69. actions.push(value);
  70. }
  71. return actions.join('');
  72. }
  73. // 回显数据字典(字符串、数组)
  74. export function selectDictLabels(datas, value, separator) {
  75. if (value === undefined || value.length ===0) {
  76. return "";
  77. }
  78. if (Array.isArray(value)) {
  79. value = value.join(",");
  80. }
  81. var actions = [];
  82. var currentSeparator = undefined === separator ? "," : separator;
  83. var temp = value.split(currentSeparator);
  84. Object.keys(value.split(currentSeparator)).some((val) => {
  85. var match = false;
  86. Object.keys(datas).some((key) => {
  87. if (datas[key].value == ('' + temp[val])) {
  88. actions.push(datas[key].label + currentSeparator);
  89. match = true;
  90. }
  91. })
  92. if (!match) {
  93. actions.push(temp[val] + currentSeparator);
  94. }
  95. })
  96. return actions.join('').substring(0, actions.join('').length - 1);
  97. }
  98. // 字符串格式化(%s )
  99. export function sprintf(str) {
  100. var args = arguments, flag = true, i = 1;
  101. str = str.replace(/%s/g, function () {
  102. var arg = args[i++];
  103. if (typeof arg === 'undefined') {
  104. flag = false;
  105. return '';
  106. }
  107. return arg;
  108. });
  109. return flag ? str : '';
  110. }
  111. // 转换字符串,undefined,null等转化为""
  112. export function parseStrEmpty(str) {
  113. if (!str || str == "undefined" || str == "null") {
  114. return "";
  115. }
  116. return str;
  117. }
  118. // 数据合并
  119. export function mergeRecursive(source, target) {
  120. for (var p in target) {
  121. try {
  122. if (target[p].constructor == Object) {
  123. source[p] = mergeRecursive(source[p], target[p]);
  124. } else {
  125. source[p] = target[p];
  126. }
  127. } catch (e) {
  128. source[p] = target[p];
  129. }
  130. }
  131. return source;
  132. };
  133. /**
  134. * 构造树型结构数据
  135. * @param {*} data 数据源
  136. * @param {*} id id字段 默认 'id'
  137. * @param {*} parentId 父节点字段 默认 'parentId'
  138. * @param {*} children 孩子节点字段 默认 'children'
  139. */
  140. export function handleTree(data, id, parentId, children) {
  141. let config = {
  142. id: id || 'id',
  143. parentId: parentId || 'parentId',
  144. childrenList: children || 'children'
  145. };
  146. var childrenListMap = {};
  147. var nodeIds = {};
  148. var tree = [];
  149. for (let d of data) {
  150. let parentId = d[config.parentId];
  151. if (childrenListMap[parentId] == null) {
  152. childrenListMap[parentId] = [];
  153. }
  154. nodeIds[d[config.id]] = d;
  155. childrenListMap[parentId].push(d);
  156. }
  157. for (let d of data) {
  158. let parentId = d[config.parentId];
  159. if (nodeIds[parentId] == null) {
  160. tree.push(d);
  161. }
  162. }
  163. for (let t of tree) {
  164. adaptToChildrenList(t);
  165. }
  166. function adaptToChildrenList(o) {
  167. if (childrenListMap[o[config.id]] !== null) {
  168. o[config.childrenList] = childrenListMap[o[config.id]];
  169. }
  170. if (o[config.childrenList]) {
  171. for (let c of o[config.childrenList]) {
  172. adaptToChildrenList(c);
  173. }
  174. }
  175. }
  176. return tree;
  177. }
  178. /**
  179. * 参数处理
  180. * @param {*} params 参数
  181. */
  182. export function tansParams(params) {
  183. let result = ''
  184. for (const propName of Object.keys(params)) {
  185. const value = params[propName];
  186. var part = encodeURIComponent(propName) + "=";
  187. if (value !== null && value !== "" && typeof (value) !== "undefined") {
  188. if (typeof value === 'object') {
  189. for (const key of Object.keys(value)) {
  190. if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
  191. let params = propName + '[' + key + ']';
  192. var subPart = encodeURIComponent(params) + "=";
  193. if(value[key] && Object.prototype.toString.call(value[key]) === '[object Date]'){
  194. result += subPart +dayjs(value[key]).format("YYYY-MM-DD HH:mm:ss") + "&";
  195. }else{
  196. result += subPart + encodeURIComponent(value[key]) + "&";
  197. }
  198. }
  199. }
  200. } else {
  201. result += part + encodeURIComponent(value) + "&";
  202. }
  203. }
  204. }
  205. return result
  206. }
  207. // 验证是否为blob格式
  208. export function blobValidate(data) {
  209. return data.type !== 'application/json'
  210. }
  211. //获取元素class
  212. export function getClass(ele) {
  213. let data = {}
  214. if (ele.data) {
  215. data = ele.data
  216. } else if (ele.$vnode && ele.$vnode.data) {
  217. data = ele.$vnode.data
  218. }
  219. const tempCls = data.class || {}
  220. const staticClass = data.staticClass
  221. let cls = {}
  222. staticClass &&
  223. staticClass.split(' ').forEach(c => {
  224. cls[c.trim()] = true
  225. })
  226. if (typeof tempCls === 'string') {
  227. tempCls.split(' ').forEach(c => {
  228. cls[c.trim()] = true
  229. })
  230. } else if (Array.isArray(tempCls)) {
  231. classNames(tempCls)
  232. .split(' ')
  233. .forEach(c => {
  234. cls[c.trim()] = true
  235. })
  236. } else {
  237. cls = { ...cls, ...tempCls }
  238. }
  239. return cls
  240. }
  241. //获取元素style
  242. export function getStyle(ele, camel) {
  243. getClass(ele)
  244. let data = {}
  245. if (ele.data) {
  246. data = ele.data
  247. } else if (ele.$vnode && ele.$vnode.data) {
  248. data = ele.$vnode.data
  249. }
  250. // update-begin-author:sunjianlei date:20200303 for: style 和 staticStyle 可以共存
  251. let style = data.style || {}
  252. let staticStyle = data.staticStyle
  253. staticStyle = staticStyle ? objectCamelize(data.staticStyle) : {}
  254. // update-end-author:sunjianlei date:20200303 for: style 和 staticStyle 可以共存
  255. if (typeof style === 'string') {
  256. style = parseStyleText(style, camel)
  257. } else if (camel && style) {
  258. // 驼峰化
  259. style = objectCamelize(style)
  260. }
  261. return { ...staticStyle, ...style }
  262. }