ruoyi.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import dayjs from "dayjs";
  2. /**
  3. * 通用js方法封装处理
  4. * Copyright (c) 2019 ruoyi
  5. */
  6. /*图片拼接url*/
  7. export function imageUrl(url){
  8. return process.env.NODE_ENV === "development"
  9. ? process.env.VUE_APP_BASE_API + url
  10. : window.origin + url;
  11. }
  12. // 时间比较
  13. export function timeCheck(arr) {
  14. let srcs = arr.filter((s) => s);
  15. for (let i = srcs.length - 1; i > 0; i--) {
  16. let a = timeToNumber(srcs[i]);
  17. let b = timeToNumber(srcs[i - 1]);
  18. if (a <= b) {
  19. return false;
  20. }
  21. }
  22. return true;
  23. }
  24. function timeToNumber(src) {
  25. return src.replace(":", "") * 1;
  26. }
  27. // 日期格式化 2023-12-30 00:00:00 或者获取星期几
  28. export function parseTime(value, format = "YYYY-MM-DD HH:mm:ss") {
  29. if ((value + "").length == 10 && format != 'dddd') {
  30. value *= 1000;
  31. return value && dayjs(value).format(format);
  32. }
  33. if(format == 'dddd'){
  34. const weeks = ['周曰',"周一",'周二','周三','周四','周五','周六'];
  35. return weeks[dayjs(value).day()];
  36. }
  37. }
  38. // 日期格式化
  39. export function formatTime(value, format = "YYYY-MM-DD HH:mm:ss") {
  40. return value && dayjs(value).format(format);
  41. }
  42. // 表单重置
  43. export function resetForm(refName) {
  44. if (this.$refs[refName]) {
  45. this.$refs[refName].resetFields();
  46. }
  47. }
  48. // 添加日期范围
  49. export function addDateRange(params, dateRange, propName) {
  50. let search = params;
  51. search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
  52. dateRange = Array.isArray(dateRange) ? dateRange : [];
  53. if (typeof (propName) === 'undefined') {
  54. search.params['beginTime'] = dateRange[0];
  55. search.params['endTime'] = dateRange[1];
  56. } else {
  57. search.params['begin' + propName] = dateRange[0];
  58. search.params['end' + propName] = dateRange[1];
  59. }
  60. return search;
  61. }
  62. // 回显数据字典
  63. export function selectDictLabel(datas, value) {
  64. if (value === undefined) {
  65. return "";
  66. }
  67. var actions = [];
  68. Object.keys(datas).some((key) => {
  69. if (datas[key].value == ('' + value)) {
  70. actions.push(datas[key].label);
  71. return true;
  72. }
  73. })
  74. if (actions.length === 0) {
  75. actions.push(value);
  76. }
  77. return actions.join('');
  78. }
  79. // 回显数据字典(字符串、数组)
  80. export function selectDictLabels(datas, value, separator) {
  81. if (value === undefined || value.length ===0) {
  82. return "";
  83. }
  84. if (Array.isArray(value)) {
  85. value = value.join(",");
  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. if(value[key] && Object.prototype.toString.call(value[key]) === '[object Date]'){
  200. result += subPart +dayjs(value[key]).format("YYYY-MM-DD HH:mm:ss") + "&";
  201. }else{
  202. result += subPart + encodeURIComponent(value[key]) + "&";
  203. }
  204. }
  205. }
  206. } else {
  207. result += part + encodeURIComponent(value) + "&";
  208. }
  209. }
  210. }
  211. return result
  212. }
  213. // 验证是否为blob格式
  214. export function blobValidate(data) {
  215. return data.type !== 'application/json'
  216. }
  217. //获取元素class
  218. export function getClass(ele) {
  219. let data = {}
  220. if (ele.data) {
  221. data = ele.data
  222. } else if (ele.$vnode && ele.$vnode.data) {
  223. data = ele.$vnode.data
  224. }
  225. const tempCls = data.class || {}
  226. const staticClass = data.staticClass
  227. let cls = {}
  228. staticClass &&
  229. staticClass.split(' ').forEach(c => {
  230. cls[c.trim()] = true
  231. })
  232. if (typeof tempCls === 'string') {
  233. tempCls.split(' ').forEach(c => {
  234. cls[c.trim()] = true
  235. })
  236. } else if (Array.isArray(tempCls)) {
  237. classNames(tempCls)
  238. .split(' ')
  239. .forEach(c => {
  240. cls[c.trim()] = true
  241. })
  242. } else {
  243. cls = { ...cls, ...tempCls }
  244. }
  245. return cls
  246. }
  247. //获取元素style
  248. export function getStyle(ele, camel) {
  249. getClass(ele)
  250. let data = {}
  251. if (ele.data) {
  252. data = ele.data
  253. } else if (ele.$vnode && ele.$vnode.data) {
  254. data = ele.$vnode.data
  255. }
  256. // update-begin-author:sunjianlei date:20200303 for: style 和 staticStyle 可以共存
  257. let style = data.style || {}
  258. let staticStyle = data.staticStyle
  259. staticStyle = staticStyle ? objectCamelize(data.staticStyle) : {}
  260. // update-end-author:sunjianlei date:20200303 for: style 和 staticStyle 可以共存
  261. if (typeof style === 'string') {
  262. style = parseStyleText(style, camel)
  263. } else if (camel && style) {
  264. // 驼峰化
  265. style = objectCamelize(style)
  266. }
  267. return { ...staticStyle, ...style }
  268. }