ruoyi.js 7.9 KB

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