globalMessage.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // 定义消息类型
  2. type MessageType = 'success' | 'error' | 'default' | 'warning' | 'info';
  3. // 定义消息配置接口
  4. interface MessageConfig {
  5. content: string;
  6. type?: MessageType;
  7. duration?: number;
  8. }
  9. // 定义消息返回接口
  10. interface MessageInstance {
  11. close: () => void;
  12. }
  13. // 消息容器样式
  14. const containerStyle = `
  15. position: fixed;
  16. top: 20px;
  17. right: 20px;
  18. z-index: 9999;
  19. display: flex;
  20. flex-direction: column;
  21. gap: 8px;
  22. `;
  23. // 消息基础样式
  24. const baseMessageStyle = `
  25. padding: 12px 16px;
  26. border-radius: 4px;
  27. display: flex;
  28. align-items: center;
  29. gap: 8px;
  30. min-width: 300px;
  31. max-width: 500px;
  32. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  33. transition: all 0.3s ease;
  34. opacity: 0;
  35. transform: translateX(100%);
  36. word-break: break-word;
  37. `;
  38. // 成功消息样式
  39. const successStyle = `
  40. ${baseMessageStyle}
  41. background-color: #f0f9eb;
  42. border-left: 4px solid #52c41a;
  43. color: #1f6d31;
  44. `;
  45. // 警告消息样式
  46. const warningStyle = ` ${baseMessageStyle} background-color: #fffbe6;
  47. border-left: 4px solid #faad14;
  48. color: #d48806;`;
  49. // 错误消息样式
  50. const errorStyle = `
  51. ${baseMessageStyle}
  52. background-color: #fff2f0;
  53. border-left: 4px solid #ff4d4f;
  54. color: #b3261e;
  55. `;
  56. // 信息消息样式
  57. const infoStyle = `
  58. ${baseMessageStyle}
  59. background-color: #e6f7ff;
  60. border-left: 4px solid #1890ff;
  61. color: #0c5460;
  62. `;
  63. // 默认消息样式
  64. const defaultStyle = `
  65. ${baseMessageStyle}
  66. background-color: #f0f2f5;
  67. border-left: 4px solid #1890ff;
  68. color: #1890ff;
  69. `;
  70. // 图标样式
  71. const iconStyle = `
  72. font-size: 16px;
  73. `;
  74. // 消息容器
  75. let container: HTMLDivElement | null = null;
  76. let messageCount = 0;
  77. // 创建消息容器
  78. function createContainer(): void {
  79. if (container) return;
  80. container = document.createElement('div');
  81. container.style.cssText = containerStyle;
  82. document.body.appendChild(container);
  83. }
  84. // 创建图标元素
  85. function createIcon(type: MessageType): HTMLSpanElement {
  86. const icon = document.createElement('span');
  87. icon.style.cssText = iconStyle;
  88. switch(type) {
  89. case 'success':
  90. icon.innerHTML = '✓'; // 成功图标
  91. break;
  92. case 'error':
  93. icon.innerHTML = '✕'; // 错误图标
  94. break;
  95. case 'warning':
  96. icon.innerHTML = '⚠'; // 警告图标
  97. break;
  98. case 'info':
  99. icon.innerHTML = 'ℹ'; // 信息图标
  100. break;
  101. default:
  102. icon.innerHTML = 'i'; // 信息图标
  103. }
  104. return icon;
  105. }
  106. // 显示消息
  107. function showMessage(
  108. content: string,
  109. type: MessageType = 'default',
  110. duration: number = 3000
  111. ): MessageInstance {
  112. createContainer();
  113. // 创建消息元素
  114. const messageId = `message-${Date.now()}-${messageCount++}`;
  115. const messageEl = document.createElement('div');
  116. messageEl.id = messageId;
  117. // 设置样式
  118. switch(type) {
  119. case 'success':
  120. messageEl.style.cssText = successStyle;
  121. break;
  122. case 'error':
  123. messageEl.style.cssText = errorStyle;
  124. break;
  125. case 'warning':
  126. messageEl.style.cssText = warningStyle;
  127. break;
  128. case 'info':
  129. messageEl.style.cssText = infoStyle;
  130. break;
  131. default:
  132. messageEl.style.cssText = defaultStyle;
  133. }
  134. // 创建图标和内容
  135. const icon = createIcon(type);
  136. const contentEl = document.createElement('span');
  137. contentEl.textContent = content;
  138. // 组装消息元素
  139. messageEl.appendChild(icon);
  140. messageEl.appendChild(contentEl);
  141. container!.appendChild(messageEl);
  142. // 触发动画
  143. setTimeout(() => {
  144. messageEl.style.opacity = '1';
  145. messageEl.style.transform = 'translateX(0)';
  146. }, 10);
  147. // 自动关闭
  148. const timer = setTimeout(() => {
  149. closeMessage(messageId);
  150. }, duration);
  151. // 点击关闭
  152. messageEl.addEventListener('click', () => {
  153. clearTimeout(timer);
  154. closeMessage(messageId);
  155. });
  156. return {
  157. close: () => {
  158. clearTimeout(timer);
  159. closeMessage(messageId);
  160. }
  161. };
  162. }
  163. // 关闭消息
  164. function closeMessage(id: string): void {
  165. const messageEl = document.getElementById(id);
  166. if (!messageEl) return;
  167. // 触发退出动画
  168. messageEl.style.opacity = '0';
  169. messageEl.style.transform = 'translateX(100%)';
  170. // 移除元素
  171. setTimeout(() => {
  172. if (container && messageEl.parentNode === container) {
  173. container.removeChild(messageEl);
  174. }
  175. }, 300);
  176. }
  177. // 导出公共方法
  178. export const message = {
  179. /**
  180. * 显示成功消息
  181. * @param content 消息内容
  182. * @param duration 显示时长(毫秒),默认3000
  183. * @returns 包含close方法的消息实例
  184. */
  185. success: (content: string, duration: number = 3000): MessageInstance => {
  186. return showMessage(content, 'success', duration);
  187. },
  188. /**
  189. * 显示错误消息
  190. * @param content 消息内容
  191. * @param duration 显示时长(毫秒),默认3000
  192. * @returns 包含close方法的消息实例
  193. */
  194. error: (content: string, duration: number = 3000): MessageInstance => {
  195. return showMessage(content, 'error', duration);
  196. },
  197. /**
  198. * 显示警告消息
  199. * @param content 消息内容
  200. * @param duration 显示时长(毫秒),默认3000
  201. * @returns 包含close方法的消息实例
  202. */
  203. warning: (content: string, duration: number = 3000): MessageInstance => {
  204. return showMessage(content, 'warning', duration);
  205. },
  206. /**
  207. * 显示信息消息
  208. * @param content 消息内容
  209. * @param duration 显示时长(毫秒),默认3000
  210. * @returns 包含close方法的消息实例
  211. */
  212. info: (content: string, duration: number = 3000): MessageInstance => {
  213. return showMessage(content, 'info', duration);
  214. },
  215. /**
  216. * 显示自定义消息
  217. * @param config 消息配置
  218. * @returns 包含close方法的消息实例
  219. */
  220. open: (config: MessageConfig): MessageInstance => {
  221. const { content, type = 'default', duration = 3000 } = config;
  222. return showMessage(content, type, duration);
  223. },
  224. /**
  225. * 关闭所有消息
  226. */
  227. destroy: (): void => {
  228. if (container) {
  229. container.innerHTML = '';
  230. }
  231. }
  232. };
  233. export default message;