utils.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { toast } from 'react-toastify';
  2. import { toastConstants } from '../constants';
  3. export function isAdmin() {
  4. let user = localStorage.getItem('user');
  5. if (!user) return false;
  6. user = JSON.parse(user);
  7. return user.role >= 10;
  8. }
  9. export function isRoot() {
  10. let user = localStorage.getItem('user');
  11. if (!user) return false;
  12. user = JSON.parse(user);
  13. return user.role >= 100;
  14. }
  15. export async function copy(text) {
  16. let okay = true;
  17. try {
  18. await navigator.clipboard.writeText(text);
  19. } catch (e) {
  20. okay = false;
  21. console.error(e);
  22. }
  23. return okay;
  24. }
  25. export function isMobile() {
  26. return window.innerWidth <= 600;
  27. }
  28. let showErrorOptions = { autoClose: toastConstants.ERROR_TIMEOUT };
  29. let showWarningOptions = { autoClose: toastConstants.WARNING_TIMEOUT };
  30. let showSuccessOptions = { autoClose: toastConstants.SUCCESS_TIMEOUT };
  31. let showInfoOptions = { autoClose: toastConstants.INFO_TIMEOUT };
  32. let showNoticeOptions = { autoClose: false };
  33. if (isMobile()) {
  34. showErrorOptions.position = 'top-center';
  35. // showErrorOptions.transition = 'flip';
  36. showSuccessOptions.position = 'top-center';
  37. // showSuccessOptions.transition = 'flip';
  38. showInfoOptions.position = 'top-center';
  39. // showInfoOptions.transition = 'flip';
  40. showNoticeOptions.position = 'top-center';
  41. // showNoticeOptions.transition = 'flip';
  42. }
  43. export function showError(error) {
  44. console.error(error);
  45. if (error.message) {
  46. if (error.name === 'AxiosError') {
  47. switch (error.response.status) {
  48. case 401:
  49. // toast.error('错误:未登录或登录已过期,请重新登录!', showErrorOptions);
  50. window.location.href = '/login?expired=true';
  51. break;
  52. case 429:
  53. toast.error('错误:请求次数过多,请稍后再试!', showErrorOptions);
  54. break;
  55. case 500:
  56. toast.error('错误:服务器内部错误,请联系管理员!', showErrorOptions);
  57. break;
  58. case 405:
  59. toast.info('本站仅作演示之用,无服务端!');
  60. break;
  61. default:
  62. toast.error('错误:' + error.message, showErrorOptions);
  63. }
  64. return;
  65. }
  66. toast.error('错误:' + error.message, showErrorOptions);
  67. } else {
  68. toast.error('错误:' + error, showErrorOptions);
  69. }
  70. }
  71. export function showWarning(message) {
  72. toast.warn(message, showWarningOptions);
  73. }
  74. export function showSuccess(message) {
  75. toast.success(message, showSuccessOptions);
  76. }
  77. export function showInfo(message) {
  78. toast.info(message, showInfoOptions);
  79. }
  80. export function showNotice(message) {
  81. toast.info(message, showNoticeOptions);
  82. }
  83. export function openPage(url) {
  84. window.open(url);
  85. }
  86. export function removeTrailingSlash(url) {
  87. if (url.endsWith('/')) {
  88. return url.slice(0, -1);
  89. } else {
  90. return url;
  91. }
  92. }
  93. export function timestamp2string(timestamp) {
  94. let date = new Date(timestamp * 1000);
  95. let year = date.getFullYear().toString();
  96. let month = (date.getMonth() + 1).toString();
  97. let day = date.getDate().toString();
  98. let hour = date.getHours().toString();
  99. let minute = date.getMinutes().toString();
  100. let second = date.getSeconds().toString();
  101. if (month.length === 1) {
  102. month = '0' + month;
  103. }
  104. if (day.length === 1) {
  105. day = '0' + day;
  106. }
  107. if (hour.length === 1) {
  108. hour = '0' + hour;
  109. }
  110. if (minute.length === 1) {
  111. minute = '0' + minute;
  112. }
  113. if (second.length === 1) {
  114. second = '0' + second;
  115. }
  116. return (
  117. year +
  118. '-' +
  119. month +
  120. '-' +
  121. day +
  122. ' ' +
  123. hour +
  124. ':' +
  125. minute +
  126. ':' +
  127. second
  128. );
  129. }