index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <template>
  2. <div class="chat-container">
  3. <div class="header">
  4. <div class="toggle-buttons">
  5. <button :class="['toggle-btn', { active: selectedPlatform === 'whatsapp' }]" @click="selectPlatform('whatsapp')">
  6. <img src="@/assets/onlineChat/whatsapp-icon.png" alt="" />
  7. WhatsApp
  8. </button>
  9. <button :class="['toggle-btn', { active: selectedPlatform === 'facebook' }]" @click="selectPlatform('facebook')">
  10. <img src="@/assets/onlineChat/facebook-icon.png" alt="" />
  11. Facebook
  12. </button>
  13. </div>
  14. </div>
  15. <!-- 主体内容区 -->
  16. <div class="main-content">
  17. <!-- 左侧面板 -->
  18. <div class="left-panel">
  19. <!-- 聊天列表 -->
  20. <div class="chat-list">
  21. <a-spin v-if="loading" style="width: 100%; padding: 20px" />
  22. <template v-else>
  23. <div
  24. v-for="chat in filteredChats"
  25. :key="chat.id"
  26. :class="['chat-item', { active: selectedChatId === chat.id }]"
  27. @click="selectChat(chat.id)"
  28. >
  29. <a-avatar :src="chat.avatar" />
  30. <div class="chat-info">
  31. <div class="chat-header">
  32. <span class="name">{{ chat.name }}</span>
  33. <span class="time">{{ formatChatTime(chat.lastMessageTime) }}</span>
  34. </div>
  35. <div class="last-message">{{ chat.lastMessage }}</div>
  36. </div>
  37. </div>
  38. </template>
  39. </div>
  40. </div>
  41. <!-- 右侧对话详情 -->
  42. <div class="chat-detail" v-if="selectedChatId">
  43. <!-- 对话头部信息 -->
  44. <div class="chat-header">
  45. <div class="chat-user-info">
  46. <a-avatar :src="currentChat.avatar" />
  47. <span class="user-name">{{ currentChat.name }}</span>
  48. </div>
  49. </div>
  50. <!-- 聊天记录区域 -->
  51. <div class="chat-messages">
  52. <div v-for="message in currentChatMessages" :key="message.id" :class="['message', message.type]">
  53. <a-avatar :src="message.avatar" />
  54. <div class="message-content">
  55. <div class="message-info">
  56. <span class="sender">{{ message.sender }}</span>
  57. <span class="time">{{ formatChatTime(message.time) }}</span>
  58. </div>
  59. <div class="message-text">{{ message.content }}</div>
  60. </div>
  61. </div>
  62. </div>
  63. <!-- 回复输入框 -->
  64. <div class="chat-input">
  65. <a-textarea v-model:value="replyMessage" placeholder="请输入消息..." :auto-size="{ minRows: 2, maxRows: 5 }" />
  66. <a-button type="primary" @click="sendMessage">发送</a-button>
  67. </div>
  68. </div>
  69. <!-- 未选择对话时的提示 -->
  70. <div class="no-chat-selected" v-else>
  71. <MessageOutlined style="font-size: 48px" />
  72. <p>快来选择联系人进入会话吧</p>
  73. </div>
  74. </div>
  75. </div>
  76. </template>
  77. <script>
  78. import { DownOutlined, MessageOutlined, SearchOutlined } from '@ant-design/icons-vue';
  79. import axios from 'axios';
  80. import { getAction } from '/@/api/manage/manage';
  81. export default {
  82. components: {
  83. DownOutlined,
  84. SearchOutlined,
  85. MessageOutlined,
  86. },
  87. data() {
  88. return {
  89. selectedPlatform: 'whatsapp', // 默认选中WhatsApp
  90. showAccountDropdown: false,
  91. currentAccount: {
  92. id: 1,
  93. avatar: '@/assets/onlineChat/whatsapp-icon.png',
  94. name: 'Adweb AI',
  95. },
  96. accounts: [
  97. {
  98. id: 1,
  99. avatar: '@/assets/onlineChat/whatsapp-icon.png',
  100. name: 'Adweb AI',
  101. },
  102. {
  103. id: 2,
  104. avatar: '@/assets/onlineChat/whatsapp-icon.png',
  105. name: 'Adweb AI 2',
  106. },
  107. ],
  108. searchKeyword: '',
  109. currentTab: 'all',
  110. selectedChatId: null,
  111. chats: [],
  112. messages: {
  113. 1: [
  114. {
  115. id: 1,
  116. type: 'received',
  117. sender: 'John Doe',
  118. avatar: '/path/to/avatar1.jpg',
  119. content: 'Hello, how are you?',
  120. time: '10:30',
  121. },
  122. {
  123. id: 2,
  124. type: 'sent',
  125. sender: 'Me',
  126. avatar: '/path/to/my-avatar.jpg',
  127. content: "I'm good, thanks!",
  128. time: '10:31',
  129. },
  130. ],
  131. 2: [
  132. // messages for chat 2
  133. ],
  134. },
  135. replyMessage: '',
  136. loading: false,
  137. };
  138. },
  139. computed: {
  140. filteredChats() {
  141. return this.chats.filter((chat) => chat.name.toLowerCase().includes(this.searchKeyword.toLowerCase()));
  142. },
  143. currentChatMessages() {
  144. return this.messages[this.selectedChatId] || [];
  145. },
  146. currentChat() {
  147. return this.chats.find((chat) => chat.id === this.selectedChatId) || {};
  148. },
  149. },
  150. created() {
  151. this.fetchChats();
  152. },
  153. methods: {
  154. selectPlatform(platform) {
  155. this.selectedPlatform = platform;
  156. this.fetchChats();
  157. },
  158. selectChat(chatId) {
  159. this.selectedChatId = chatId;
  160. },
  161. switchAccount(account) {
  162. this.currentAccount = account;
  163. this.showAccountDropdown = false;
  164. },
  165. sendMessage() {
  166. if (!this.replyMessage.trim()) return;
  167. const newMessage = {
  168. id: Date.now(),
  169. type: 'sent',
  170. sender: 'Me',
  171. avatar: this.currentAccount.avatar,
  172. content: this.replyMessage,
  173. time: new Date().toISOString(),
  174. };
  175. if (!this.messages[this.selectedChatId]) {
  176. this.messages[this.selectedChatId] = [];
  177. }
  178. this.messages[this.selectedChatId].push(newMessage);
  179. this.replyMessage = '';
  180. },
  181. async fetchChats() {
  182. this.loading = true;
  183. try {
  184. const siteCode = localStorage.getItem('siteCode');
  185. const response = await getAction('/marketing/facebook/chat/list', {siteCode:siteCode });
  186. console.log(response);
  187. this.chats = response.result.map(chat => ({
  188. ...chat,
  189. lastMessageTime: chat.lastMessageTime // 假设接口返回的是有效时间字符串
  190. }));
  191. } catch (error) {
  192. console.error('获取消息列表失败:', error);
  193. } finally {
  194. this.loading = false;
  195. }
  196. },
  197. formatChatTime(isoTime) {
  198. const date = new Date(isoTime);
  199. const now = new Date();
  200. const diffInSeconds = Math.floor((now - date) / 1000);
  201. // 当天时间格式
  202. const timeOptions = { hour: 'numeric', minute: '2-digit', hour12: true };
  203. // 1分钟内显示"刚刚"
  204. if (diffInSeconds < 60) {
  205. return '刚刚';
  206. }
  207. // 今天
  208. if (date.toDateString() === now.toDateString()) {
  209. return date.toLocaleTimeString('zh-CN', timeOptions).replace('上午', 'AM').replace('下午', 'PM');
  210. }
  211. // 昨天
  212. const yesterday = new Date(now);
  213. yesterday.setDate(now.getDate() - 1);
  214. if (date.toDateString() === yesterday.toDateString()) {
  215. return `昨天 ${date.toLocaleTimeString('zh-CN', timeOptions)}`;
  216. }
  217. // 本周内显示星期几
  218. const diffDays = Math.floor(diffInSeconds / 86400);
  219. if (diffDays < 7) {
  220. const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
  221. return `${weekdays[date.getDay()]} ${date.toLocaleTimeString('zh-CN', timeOptions)}`;
  222. }
  223. // 不同年份显示完整日期
  224. if (date.getFullYear() !== now.getFullYear()) {
  225. return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()} ${date.toLocaleTimeString('zh-CN', timeOptions)}`;
  226. }
  227. // 默认格式:月/日 时间
  228. return `${date.getMonth() + 1}/${date.getDate()} ${date.toLocaleTimeString('zh-CN', timeOptions)}`;
  229. }
  230. },
  231. };
  232. </script>
  233. <style scoped>
  234. .chat-container {
  235. padding: 20px;
  236. }
  237. .header {
  238. display: flex;
  239. justify-content: space-between;
  240. align-items: center;
  241. margin-bottom: 20px;
  242. }
  243. .toggle-buttons {
  244. display: flex;
  245. gap: 10px;
  246. }
  247. .toggle-btn {
  248. padding: 8px 16px;
  249. border: 1px solid #dcdfe6;
  250. background-color: #fff;
  251. border-radius: 20px;
  252. cursor: pointer;
  253. transition: all 0.3s;
  254. display: flex;
  255. align-items: center;
  256. gap: 8px;
  257. }
  258. .toggle-btn:hover {
  259. background-color: #f5f7fa;
  260. }
  261. .toggle-btn.active {
  262. background-color: #409eff;
  263. color: #fff;
  264. border-color: #409eff;
  265. }
  266. /* 图标样式 */
  267. .icon-whatsapp,
  268. .icon-whatsapp-business,
  269. .icon-facebook {
  270. display: inline-block;
  271. width: 20px;
  272. height: 20px;
  273. background-size: contain;
  274. background-repeat: no-repeat;
  275. }
  276. .social-management-btn {
  277. padding: 8px 16px;
  278. border: 1px solid #dcdfe6;
  279. background-color: #fff;
  280. border-radius: 20px;
  281. cursor: pointer;
  282. transition: all 0.3s;
  283. display: flex;
  284. align-items: center;
  285. gap: 8px;
  286. }
  287. .social-management-btn:hover {
  288. background-color: #f5f7fa;
  289. }
  290. .main-content {
  291. display: flex;
  292. height: calc(100vh - 220px);
  293. margin-top: 20px;
  294. background: #fff;
  295. border-radius: 4px;
  296. border: 1px solid #e4e7ed;
  297. }
  298. .left-panel {
  299. width: 300px;
  300. border-right: 1px solid #e4e7ed;
  301. display: flex;
  302. flex-direction: column;
  303. }
  304. .current-account {
  305. padding: 16px;
  306. border-bottom: 1px solid #e4e7ed;
  307. display: flex;
  308. align-items: center;
  309. gap: 8px;
  310. cursor: pointer;
  311. position: relative;
  312. }
  313. .current-account .avatar {
  314. width: 32px;
  315. height: 32px;
  316. border-radius: 50%;
  317. }
  318. .chat-list {
  319. flex: 1;
  320. overflow-y: auto;
  321. }
  322. .chat-item {
  323. display: flex;
  324. padding: 12px 16px;
  325. cursor: pointer;
  326. transition: background-color 0.3s;
  327. }
  328. .chat-item:hover {
  329. background-color: #f5f7fa;
  330. }
  331. .chat-item.active {
  332. background-color: #ecf5ff;
  333. }
  334. .chat-info {
  335. margin-left: 12px;
  336. flex: 1;
  337. }
  338. .chat-header {
  339. display: flex;
  340. justify-content: space-between;
  341. margin-bottom: 4px;
  342. }
  343. .chat-header .time {
  344. font-size: 12px;
  345. color: #909399;
  346. }
  347. .last-message {
  348. font-size: 13px;
  349. color: #606266;
  350. overflow: hidden;
  351. text-overflow: ellipsis;
  352. white-space: nowrap;
  353. }
  354. .chat-detail {
  355. flex: 1;
  356. display: flex;
  357. flex-direction: column;
  358. height: 100%;
  359. }
  360. .chat-header {
  361. padding: 16px 20px;
  362. border-bottom: 1px solid #e4e7ed;
  363. }
  364. .chat-user-info {
  365. display: flex;
  366. align-items: center;
  367. gap: 12px;
  368. }
  369. .user-name {
  370. font-size: 16px;
  371. font-weight: 500;
  372. }
  373. .chat-messages {
  374. flex: 1;
  375. padding: 20px;
  376. overflow-y: auto;
  377. }
  378. .chat-input {
  379. padding: 16px 20px;
  380. border-top: 1px solid #e4e7ed;
  381. display: flex;
  382. gap: 12px;
  383. align-items: flex-end;
  384. }
  385. .chat-input .ant-textarea-wrapper {
  386. flex: 1;
  387. }
  388. .chat-input .ant-btn {
  389. height: 40px;
  390. padding: 0 24px;
  391. }
  392. .message {
  393. display: flex;
  394. margin-bottom: 20px;
  395. }
  396. .message.sent {
  397. flex-direction: row-reverse;
  398. }
  399. .message-content {
  400. margin: 0 12px;
  401. max-width: 60%;
  402. }
  403. .message.sent .message-content {
  404. text-align: right;
  405. }
  406. .message-info {
  407. margin-bottom: 4px;
  408. }
  409. .message-text {
  410. padding: 12px;
  411. background: #f5f7fa;
  412. border-radius: 4px;
  413. }
  414. .message.sent .message-text {
  415. background: #409eff;
  416. color: white;
  417. }
  418. .no-chat-selected {
  419. flex: 1;
  420. display: flex;
  421. flex-direction: column;
  422. align-items: center;
  423. justify-content: center;
  424. color: #909399;
  425. }
  426. .no-chat-selected .anticon {
  427. font-size: 48px;
  428. margin-bottom: 16px;
  429. color: #bfbfbf;
  430. }
  431. /* 调整一些样式以匹配 Ant Design 风格 */
  432. .search-box .ant-input-affix-wrapper {
  433. border-radius: 4px;
  434. }
  435. .chat-item .ant-avatar {
  436. width: 40px;
  437. height: 40px;
  438. }
  439. .dropdown-icon {
  440. font-size: 12px;
  441. color: #909399;
  442. transition: transform 0.3s;
  443. }
  444. .dropdown-icon.is-open {
  445. transform: rotate(180deg);
  446. }
  447. .account-dropdown {
  448. position: absolute;
  449. top: 100%;
  450. left: 0;
  451. right: 0;
  452. background: #fff;
  453. border: 1px solid #e4e7ed;
  454. border-radius: 4px;
  455. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  456. z-index: 10;
  457. }
  458. .account-option {
  459. display: flex;
  460. align-items: center;
  461. gap: 8px;
  462. padding: 8px 16px;
  463. cursor: pointer;
  464. }
  465. .account-option:hover {
  466. background-color: #f5f7fa;
  467. }
  468. </style>