CustomMessage.tsx 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import React from 'react'
  2. // 自定义消息组件
  3. export const CustomMessage = {
  4. success: (content: string) => {
  5. showCustomMessage('success', content);
  6. },
  7. error: (content: string) => {
  8. showCustomMessage('error', content);
  9. },
  10. warning: (content: string) => {
  11. showCustomMessage('warning', content);
  12. },
  13. info: (content: string) => {
  14. showCustomMessage('info', content);
  15. }
  16. };
  17. type MessageType = 'success' | 'error' | 'warning' | 'info';
  18. // 自定义消息显示函数
  19. const showCustomMessage = (type: MessageType, message: string) => {
  20. // 移除已存在的消息
  21. const existingMessages = document.querySelectorAll('.custom-message');
  22. existingMessages.forEach(msg => msg.remove());
  23. const messageContainer = document.createElement('div');
  24. messageContainer.className = `custom-message fixed top-5 left-1/2 gap-2 flex justify-center items-center leading-[18px] rounded-lg px-4 py-2
  25. bg-[#fff] min-w-[200px] z-[10000] transition-all duration-300 shadow-[0_4px_12px_rgb(0_0_0/0.15)] -translate-x-1/2`;
  26. // messageContainer.style.cssText = `
  27. // font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  28. // `;
  29. let icon = '';
  30. // 根据不同类型设置样式和图标
  31. switch (type) {
  32. case 'success':
  33. messageContainer.style.color = '#52c41a';
  34. icon = '<svg t="1756881717120" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8894" width="18" height="18"><path d="M699 353h-46.9c-10.2 0-19.9 4.9-25.9 13.3L469 584.3l-71.2-98.8c-6-8.3-15.6-13.3-25.9-13.3H325c-6.5 0-10.3 7.4-6.5 12.7l124.6 172.8c12.7 17.7 39 17.7 51.7 0l210.6-292c3.9-5.3 0.1-12.7-6.4-12.7z" p-id="8895" fill="#52c41a"></path><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64z m0 820c-205.4 0-372-166.6-372-372s166.6-372 372-372 372 166.6 372 372-166.6 372-372 372z" p-id="8896" fill="#52c41a"></path></svg>';
  35. break;
  36. case 'error':
  37. messageContainer.style.color = '#ff4d4f';
  38. icon = '<svg t="1756881654441" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8531" width="18" height="18"><path d="M685.4 354.8c0-4.4-3.6-8-8-8l-66 0.3L512 465.6l-99.3-118.4-66.1-0.3c-4.4 0-8 3.5-8 8 0 1.9 0.7 3.7 1.9 5.2l130.1 155L340.5 670c-1.2 1.5-1.9 3.3-1.9 5.2 0 4.4 3.6 8 8 8l66.1-0.3L512 564.4l99.3 118.4 66 0.3c4.4 0 8-3.5 8-8 0-1.9-0.7-3.7-1.9-5.2L553.5 515l130.1-155c1.2-1.4 1.8-3.3 1.8-5.2z" p-id="8532" fill="#ff4d4f"></path><path d="M512 65C264.6 65 64 265.6 64 513s200.6 448 448 448 448-200.6 448-448S759.4 65 512 65z m0 820c-205.4 0-372-166.6-372-372s166.6-372 372-372 372 166.6 372 372-166.6 372-372 372z" p-id="8533" fill="#ff4d4f"></path></svg>';
  39. break;
  40. case 'warning':
  41. messageContainer.style.color = '#faad14';
  42. icon = '<svg t="1756881803623" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9161" width="18" height="18"><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64z m0 820c-205.4 0-372-166.6-372-372s166.6-372 372-372 372 166.6 372 372-166.6 372-372 372z" p-id="9162" fill="#faad14"></path><path d="M512 688m-48 0a48 48 0 1 0 96 0 48 48 0 1 0-96 0Z" p-id="9163" fill="#faad14"></path><path d="M488 576h48c4.4 0 8-3.6 8-8V296c0-4.4-3.6-8-8-8h-48c-4.4 0-8 3.6-8 8v272c0 4.4 3.6 8 8 8z" p-id="9164" fill="#faad14"></path></svg>';
  43. break;
  44. default:
  45. messageContainer.style.color = '#1890ff';
  46. icon = '<svg t="1756881872206" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9401" width="18" height="18"><path d="M168 504.2c1-43.7 10-86.1 26.9-126 17.3-41 42.1-77.7 73.7-109.4S337 212.3 378 195c42.4-17.9 87.4-27 133.9-27s91.5 9.1 133.8 27c40.9 17.3 77.7 42.1 109.3 73.8 9.9 9.9 19.2 20.4 27.8 31.4l-60.2 47c-5.3 4.1-3.5 12.5 3 14.1l175.7 43c5 1.2 9.9-2.6 9.9-7.7l0.8-180.9c0-6.7-7.7-10.5-12.9-6.3l-56.4 44.1C765.8 155.1 646.2 92 511.8 92 282.7 92 96.3 275.6 92 503.8c-0.1 4.5 3.5 8.2 8 8.2h60c4.4 0 7.9-3.5 8-7.8zM924 512h-60c-4.4 0-7.9 3.5-8 7.8-1 43.7-10 86.1-26.9 126-17.3 41-42.1 77.8-73.7 109.4S687 811.7 646 829c-42.4 17.9-87.4 27-133.9 27s-91.5-9.1-133.9-27c-40.9-17.3-77.7-42.1-109.3-73.8-9.9-9.9-19.2-20.4-27.8-31.4l60.2-47c5.3-4.1 3.5-12.5-3-14.1l-175.7-43c-5-1.2-9.9 2.6-9.9 7.7l-0.7 181c0 6.7 7.7 10.5 12.9 6.3l56.4-44.1C258.2 868.9 377.8 932 512.2 932c229.2 0 415.5-183.7 419.8-411.8 0.1-4.5-3.5-8.2-8-8.2z" p-id="9402" fill="#1890ff"></path></svg>';
  47. }
  48. messageContainer.innerHTML = `
  49. <span style="font-size: 16px;">${icon}</span>
  50. <span style="color: #181818;">${message}</span>
  51. `;
  52. document.body.appendChild(messageContainer);
  53. // 3秒后自动移除
  54. setTimeout(() => {
  55. if (messageContainer.parentNode) {
  56. messageContainer.parentNode.removeChild(messageContainer);
  57. }
  58. }, 3000);
  59. };