auth.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import Cookies from "js-cookie";
  2. import dayjs from "dayjs";
  3. const timeKey = "hrsaas-timestamp-key";
  4. // 设置时间戳的存储变量
  5. const TokenKey = "accessToken";
  6. const UserIdKey = "userId";
  7. const Username = "username";
  8. export function getCookie(name) {
  9. return Cookies.get(name);
  10. }
  11. export function setCookie(name, value) {
  12. return Cookies.set(name, value);
  13. }
  14. // cookie存储Username
  15. export function setName(name) {
  16. return Cookies.set(Username, name);
  17. }
  18. // cookie存储token
  19. export function setToken(token) {
  20. return Cookies.set(TokenKey, token);
  21. }
  22. // cookie存储UserId
  23. export function setUserId(userId) {
  24. return Cookies.set(UserIdKey, userId);
  25. }
  26. // cookie删除token
  27. export function removeToken() {
  28. return Cookies.remove(TokenKey);
  29. }
  30. // cookie删除UserId
  31. export function removeUserId() {
  32. return Cookies.remove(UserIdKey);
  33. }
  34. // cookie删除Username
  35. export function removeName() {
  36. return Cookies.remove(Username);
  37. }
  38. // 获取时间戳
  39. export function getTimeStamp() {
  40. return Cookies.get(timeKey);
  41. }
  42. // 设置时间戳
  43. export function setTimeStamp() {
  44. return Cookies.set(timeKey, Date.now());
  45. }
  46. // 时间格式化
  47. export function parseTime() {
  48. var myDate = new Date();
  49. const formatObj = {
  50. year: myDate.getFullYear(),
  51. month:
  52. myDate.getMonth() + 1 >= 10
  53. ? myDate.getMonth() + 1
  54. : "0" + (myDate.getMonth() + 1),
  55. day: myDate.getDate() >= 10 ? myDate.getDate() : "0" + myDate.getDate(),
  56. };
  57. return formatObj.year + "-" + formatObj.month + "-" + formatObj.day;
  58. }
  59. //获取1天前时间
  60. export function getStampTime() {
  61. let stamp1 = new Date(new Date().setHours(0, 0, 0, 0));
  62. stamp1 = dayjs(stamp1).format("YYYY-MM-DD HH:mm:ss");
  63. let stamp2 = new Date(new Date().setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000);
  64. stamp2 = dayjs(stamp2).format("YYYY-MM-DD HH:mm:ss");
  65. return {
  66. startTime: stamp1,
  67. endTime: stamp2,
  68. };
  69. }
  70. export function transTreeData(arr, idStr, pidStr, chindrenStr) {
  71. let r = [],
  72. hash = {},
  73. id = idStr,
  74. pid = pidStr,
  75. children = chindrenStr,
  76. len = arr.length;
  77. for (let i = 0; i < len; i++) {
  78. hash[arr[i][id]] = arr[i];
  79. }
  80. for (let j = 0; j < len; j++) {
  81. let aVal = arr[j],
  82. hashVP = hash[aVal[pid]];
  83. if (hashVP) {
  84. !hashVP[children] && (hashVP[children] = []);
  85. hashVP[children].push(aVal);
  86. } else {
  87. r.push(aVal);
  88. }
  89. }
  90. return r;
  91. }
  92. export function checkIn(item, arr) {
  93. const result = arr.find((value) => {
  94. return value.name == item.meta.title;
  95. });
  96. return result;
  97. }