index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. import { parseTime } from "./ruoyi";
  2. import * as baseUrls from "@/utils/request.js";
  3. /**
  4. * 表格时间格式化
  5. */
  6. export function formatDate(cellValue) {
  7. if (cellValue == null || cellValue == "") return "";
  8. var date = new Date(cellValue);
  9. var year = date.getFullYear();
  10. var month =
  11. date.getMonth() + 1 < 10
  12. ? "0" + (date.getMonth() + 1)
  13. : date.getMonth() + 1;
  14. var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  15. var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  16. var minutes =
  17. date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  18. var seconds =
  19. date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  20. return (
  21. year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds
  22. );
  23. }
  24. /**
  25. * @param {number} time
  26. * @param {string} option
  27. * @returns {string}
  28. */
  29. export function formatTime(time, option) {
  30. if (("" + time).length === 10) {
  31. time = parseInt(time) * 1000;
  32. } else {
  33. time = +time;
  34. }
  35. const d = new Date(time);
  36. const now = Date.now();
  37. const diff = (now - d) / 1000;
  38. if (diff < 30) {
  39. return "刚刚";
  40. } else if (diff < 3600) {
  41. // less 1 hour
  42. return Math.ceil(diff / 60) + "分钟前";
  43. } else if (diff < 3600 * 24) {
  44. return Math.ceil(diff / 3600) + "小时前";
  45. } else if (diff < 3600 * 24 * 2) {
  46. return "1天前";
  47. }
  48. if (option) {
  49. return parseTime(time, option);
  50. } else {
  51. return (
  52. d.getMonth() +
  53. 1 +
  54. "月" +
  55. d.getDate() +
  56. "日" +
  57. d.getHours() +
  58. "时" +
  59. d.getMinutes() +
  60. "分"
  61. );
  62. }
  63. }
  64. /**
  65. * @param {string} url
  66. * @returns {Object}
  67. */
  68. export function getQueryObject(url) {
  69. url = url == null ? window.location.href : url;
  70. const search = url.substring(url.lastIndexOf("?") + 1);
  71. const obj = {};
  72. const reg = /([^?&=]+)=([^?&=]*)/g;
  73. search.replace(reg, (rs, $1, $2) => {
  74. const name = decodeURIComponent($1);
  75. let val = decodeURIComponent($2);
  76. val = String(val);
  77. obj[name] = val;
  78. return rs;
  79. });
  80. return obj;
  81. }
  82. /**
  83. * @param {string} input value
  84. * @returns {number} output value
  85. */
  86. export function byteLength(str) {
  87. // returns the byte length of an utf8 string
  88. let s = str.length;
  89. for (var i = str.length - 1; i >= 0; i--) {
  90. const code = str.charCodeAt(i);
  91. if (code > 0x7f && code <= 0x7ff) s++;
  92. else if (code > 0x7ff && code <= 0xffff) s += 2;
  93. if (code >= 0xdc00 && code <= 0xdfff) i--;
  94. }
  95. return s;
  96. }
  97. /**
  98. * @param {Array} actual
  99. * @returns {Array}
  100. */
  101. export function cleanArray(actual) {
  102. const newArray = [];
  103. for (let i = 0; i < actual.length; i++) {
  104. if (actual[i]) {
  105. newArray.push(actual[i]);
  106. }
  107. }
  108. return newArray;
  109. }
  110. /**
  111. * @param {Object} json
  112. * @returns {Array}
  113. */
  114. export function param(json) {
  115. if (!json) return "";
  116. return cleanArray(
  117. Object.keys(json).map((key) => {
  118. if (json[key] === undefined) return "";
  119. return encodeURIComponent(key) + "=" + encodeURIComponent(json[key]);
  120. })
  121. ).join("&");
  122. }
  123. /**
  124. * @param {string} url
  125. * @returns {Object}
  126. */
  127. export function param2Obj(url) {
  128. const search = decodeURIComponent(url.split("?")[1]).replace(/\+/g, " ");
  129. if (!search) {
  130. return {};
  131. }
  132. const obj = {};
  133. const searchArr = search.split("&");
  134. searchArr.forEach((v) => {
  135. const index = v.indexOf("=");
  136. if (index !== -1) {
  137. const name = v.substring(0, index);
  138. const val = v.substring(index + 1, v.length);
  139. obj[name] = val;
  140. }
  141. });
  142. return obj;
  143. }
  144. /**
  145. * @param {string} val
  146. * @returns {string}
  147. */
  148. export function html2Text(val) {
  149. const div = document.createElement("div");
  150. div.innerHTML = val;
  151. return div.textContent || div.innerText;
  152. }
  153. /**
  154. * Merges two objects, giving the last one precedence
  155. * @param {Object} target
  156. * @param {(Object|Array)} source
  157. * @returns {Object}
  158. */
  159. export function objectMerge(target, source) {
  160. if (typeof target !== "object") {
  161. target = {};
  162. }
  163. if (Array.isArray(source)) {
  164. return source.slice();
  165. }
  166. Object.keys(source).forEach((property) => {
  167. const sourceProperty = source[property];
  168. if (typeof sourceProperty === "object") {
  169. target[property] = objectMerge(target[property], sourceProperty);
  170. } else {
  171. target[property] = sourceProperty;
  172. }
  173. });
  174. return target;
  175. }
  176. /**
  177. * @param {HTMLElement} element
  178. * @param {string} className
  179. */
  180. export function toggleClass(element, className) {
  181. if (!element || !className) {
  182. return;
  183. }
  184. let classString = element.className;
  185. const nameIndex = classString.indexOf(className);
  186. if (nameIndex === -1) {
  187. classString += "" + className;
  188. } else {
  189. classString =
  190. classString.substr(0, nameIndex) +
  191. classString.substr(nameIndex + className.length);
  192. }
  193. element.className = classString;
  194. }
  195. /**
  196. * @param {string} type
  197. * @returns {Date}
  198. */
  199. export function getTime(type) {
  200. if (type === "start") {
  201. return new Date().getTime() - 3600 * 1000 * 24 * 90;
  202. } else {
  203. return new Date(new Date().toDateString());
  204. }
  205. }
  206. /**
  207. * @param {Function} func
  208. * @param {number} wait
  209. * @param {boolean} immediate
  210. * @return {*}
  211. */
  212. export function debounce(func, wait, immediate) {
  213. let timeout, args, context, timestamp, result;
  214. const later = function () {
  215. // 据上一次触发时间间隔
  216. const last = +new Date() - timestamp;
  217. // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
  218. if (last < wait && last > 0) {
  219. timeout = setTimeout(later, wait - last);
  220. } else {
  221. timeout = null;
  222. // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
  223. if (!immediate) {
  224. result = func.apply(context, args);
  225. if (!timeout) context = args = null;
  226. }
  227. }
  228. };
  229. return function (...args) {
  230. context = this;
  231. timestamp = +new Date();
  232. const callNow = immediate && !timeout;
  233. // 如果延时不存在,重新设定延时
  234. if (!timeout) timeout = setTimeout(later, wait);
  235. if (callNow) {
  236. result = func.apply(context, args);
  237. context = args = null;
  238. }
  239. return result;
  240. };
  241. }
  242. /**
  243. * This is just a simple version of deep copy
  244. * Has a lot of edge cases bug
  245. * If you want to use a perfect deep copy, use lodash's _.cloneDeep
  246. * @param {Object} source
  247. * @returns {Object}
  248. */
  249. export function deepClone(source) {
  250. if (!source && typeof source !== "object") {
  251. throw new Error("error arguments", "deepClone");
  252. }
  253. const targetObj = source.constructor === Array ? [] : {};
  254. Object.keys(source).forEach((keys) => {
  255. if (source[keys] && typeof source[keys] === "object") {
  256. targetObj[keys] = deepClone(source[keys]);
  257. } else {
  258. targetObj[keys] = source[keys];
  259. }
  260. });
  261. return targetObj;
  262. }
  263. /**
  264. * @param {Array} arr
  265. * @returns {Array}
  266. */
  267. export function uniqueArr(arr) {
  268. return Array.from(new Set(arr));
  269. }
  270. /**
  271. * @returns {string}
  272. */
  273. export function createUniqueString() {
  274. const timestamp = +new Date() + "";
  275. const randomNum = parseInt((1 + Math.random()) * 65536) + "";
  276. return (+(randomNum + timestamp)).toString(32);
  277. }
  278. /**
  279. * Check if an element has a class
  280. * @param {HTMLElement} elm
  281. * @param {string} cls
  282. * @returns {boolean}
  283. */
  284. export function hasClass(ele, cls) {
  285. return !!ele.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
  286. }
  287. /**
  288. * Add class to element
  289. * @param {HTMLElement} elm
  290. * @param {string} cls
  291. */
  292. export function addClass(ele, cls) {
  293. if (!hasClass(ele, cls)) ele.className += " " + cls;
  294. }
  295. /**
  296. * Remove class from element
  297. * @param {HTMLElement} elm
  298. * @param {string} cls
  299. */
  300. export function removeClass(ele, cls) {
  301. if (hasClass(ele, cls)) {
  302. const reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
  303. ele.className = ele.className.replace(reg, " ");
  304. }
  305. }
  306. export function makeMap(str, expectsLowerCase) {
  307. const map = Object.create(null);
  308. const list = str.split(",");
  309. for (let i = 0; i < list.length; i++) {
  310. map[list[i]] = true;
  311. }
  312. return expectsLowerCase ? (val) => map[val.toLowerCase()] : (val) => map[val];
  313. }
  314. export const exportDefault = "export default ";
  315. export const beautifierConf = {
  316. html: {
  317. indent_size: "2",
  318. indent_char: " ",
  319. max_preserve_newlines: "-1",
  320. preserve_newlines: false,
  321. keep_array_indentation: false,
  322. break_chained_methods: false,
  323. indent_scripts: "separate",
  324. brace_style: "end-expand",
  325. space_before_conditional: true,
  326. unescape_strings: false,
  327. jslint_happy: false,
  328. end_with_newline: true,
  329. wrap_line_length: "110",
  330. indent_inner_html: true,
  331. comma_first: false,
  332. e4x: true,
  333. indent_empty_lines: true,
  334. },
  335. js: {
  336. indent_size: "2",
  337. indent_char: " ",
  338. max_preserve_newlines: "-1",
  339. preserve_newlines: false,
  340. keep_array_indentation: false,
  341. break_chained_methods: false,
  342. indent_scripts: "normal",
  343. brace_style: "end-expand",
  344. space_before_conditional: true,
  345. unescape_strings: false,
  346. jslint_happy: true,
  347. end_with_newline: true,
  348. wrap_line_length: "110",
  349. indent_inner_html: true,
  350. comma_first: false,
  351. e4x: true,
  352. indent_empty_lines: true,
  353. },
  354. };
  355. // 首字母大小
  356. export function titleCase(str) {
  357. return str.replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
  358. }
  359. // 下划转驼峰
  360. export function camelCase(str) {
  361. return str.replace(/-[a-z]/g, (str1) => str1.substr(-1).toUpperCase());
  362. }
  363. export function isNumberStr(str) {
  364. return /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(str);
  365. }
  366. //导出数据
  367. export function exportFn(str, name) {
  368. let url = process.env.VUE_APP_BASE_API + "sys/common/download?fileName=" + str;
  369. let link = document.createElement("a");
  370. let fileName = name + ".xlsx";
  371. document.body.appendChild(link);
  372. link.href = url;
  373. link.dowmload = fileName;
  374. link.click();
  375. link.remove();
  376. }
  377. //下载模板
  378. export function downFn(str, name = 'xx.xlsx') {
  379. let url = process.env.VUE_APP_IMG_API + "/" + str;
  380. let link = document.createElement("a");
  381. let fileName = name;
  382. document.body.appendChild(link);
  383. link.href = url;
  384. link.dowmload = fileName;
  385. link.click();
  386. link.remove();
  387. }
  388. // 保留x位小数
  389. export function checkNum(val, int = 2) {
  390. if (int === 0) {
  391. return val.replace(/[^0-9]/g, "");
  392. } else if (int === 1) {
  393. return val.replace(/^\D*(\d*(?:\.\d{0,1})?).*$/g, "$1");
  394. } else {
  395. return val.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, "$1");
  396. }
  397. }