index.vue 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. <template>
  2. <div class="works-page">
  3. <div class="page-header">
  4. <h2>作品管理</h2>
  5. <div class="header-stats">
  6. <span>总作品: {{ stats.totalCount }}</span>
  7. <span>总播放: {{ formatNumber(stats.totalPlayCount) }}</span>
  8. <span>总点赞: {{ formatNumber(stats.totalLikeCount) }}</span>
  9. </div>
  10. </div>
  11. <!-- 筛选栏 -->
  12. <div class="page-card filter-bar">
  13. <el-select v-model="filter.accountId" placeholder="选择账号" clearable style="width: 200px">
  14. <el-option
  15. v-for="account in accounts"
  16. :key="account.id"
  17. :label="`${account.accountName} (${getPlatformName(account.platform)})`"
  18. :value="account.id"
  19. />
  20. </el-select>
  21. <el-select v-model="filter.platform" placeholder="平台" clearable style="width: 120px">
  22. <el-option
  23. v-for="platform in platforms"
  24. :key="platform.type"
  25. :label="platform.name"
  26. :value="platform.type"
  27. />
  28. </el-select>
  29. <el-select v-model="filter.status" placeholder="状态" clearable style="width: 120px">
  30. <el-option label="已发布" value="published" />
  31. <el-option label="审核中" value="reviewing" />
  32. <el-option label="未通过" value="rejected" />
  33. </el-select>
  34. <el-input
  35. v-model="filter.keyword"
  36. placeholder="搜索作品标题"
  37. clearable
  38. style="width: 200px"
  39. @keyup.enter="loadWorks"
  40. />
  41. <el-button type="primary" @click="loadWorks">
  42. <el-icon><Search /></el-icon>
  43. 搜索
  44. </el-button>
  45. <el-button @click="refreshAllWorks" :loading="refreshing">
  46. <el-icon><Refresh /></el-icon>
  47. 同步作品
  48. </el-button>
  49. <el-button type="success" @click="syncAllComments" :loading="syncingComments">
  50. <el-icon><ChatDotSquare /></el-icon>
  51. 同步评论
  52. </el-button>
  53. </div>
  54. <!-- 作品列表 -->
  55. <div class="page-card">
  56. <div class="works-grid" v-loading="loading">
  57. <div v-if="works.length === 0 && !loading" class="empty-state">
  58. <el-empty description="暂无作品数据">
  59. <el-button type="primary" @click="refreshAllWorks">同步作品</el-button>
  60. </el-empty>
  61. </div>
  62. <div
  63. v-for="work in works"
  64. :key="work.id"
  65. class="work-card"
  66. @click="openWorkDetail(work)"
  67. >
  68. <div class="work-cover">
  69. <img :src="work.coverUrl" :alt="work.title" @error="handleImageError" />
  70. <span class="work-duration">{{ work.duration }}</span>
  71. <el-tag
  72. class="work-status"
  73. :type="getStatusType(work.status)"
  74. size="small"
  75. >
  76. {{ getStatusText(work.status) }}
  77. </el-tag>
  78. </div>
  79. <div class="work-info">
  80. <div class="work-title" :title="work.title">{{ work.title || '无标题' }}</div>
  81. <div class="work-meta">
  82. <el-tag size="small" type="info">{{ getPlatformName(work.platform) }}</el-tag>
  83. <span class="work-time">{{ formatDate(work.publishTime) }}</span>
  84. </div>
  85. <div class="work-stats">
  86. <span><el-icon><VideoPlay /></el-icon> {{ formatNumber(work.playCount) }}</span>
  87. <span><el-icon><Star /></el-icon> {{ formatNumber(work.likeCount) }}</span>
  88. <span><el-icon><ChatDotSquare /></el-icon> {{ work.commentCount }}</span>
  89. <span><el-icon><Share /></el-icon> {{ work.shareCount }}</span>
  90. </div>
  91. </div>
  92. <div class="work-actions">
  93. <el-button type="primary" link size="small" @click.stop="viewComments(work)">
  94. 查看评论 ({{ work.commentCount }})
  95. </el-button>
  96. </div>
  97. </div>
  98. </div>
  99. <el-pagination
  100. v-if="pagination.total > 0"
  101. v-model:current-page="pagination.page"
  102. v-model:page-size="pagination.pageSize"
  103. :total="pagination.total"
  104. :page-sizes="[12, 24, 48]"
  105. layout="total, sizes, prev, pager, next"
  106. style="margin-top: 20px"
  107. @change="loadWorks"
  108. />
  109. </div>
  110. <!-- 作品详情对话框 -->
  111. <el-dialog
  112. v-model="showDetailDialog"
  113. :title="currentWork?.title || '作品详情'"
  114. width="800px"
  115. destroy-on-close
  116. >
  117. <div class="work-detail" v-if="currentWork">
  118. <div class="detail-left">
  119. <div class="detail-cover">
  120. <img :src="currentWork.coverUrl" :alt="currentWork.title" />
  121. </div>
  122. </div>
  123. <div class="detail-right">
  124. <div class="detail-row">
  125. <label>平台:</label>
  126. <el-tag>{{ getPlatformName(currentWork.platform) }}</el-tag>
  127. </div>
  128. <div class="detail-row">
  129. <label>状态:</label>
  130. <el-tag :type="getStatusType(currentWork.status)">
  131. {{ getStatusText(currentWork.status) }}
  132. </el-tag>
  133. </div>
  134. <div class="detail-row">
  135. <label>发布时间:</label>
  136. <span>{{ formatDate(currentWork.publishTime) }}</span>
  137. </div>
  138. <div class="detail-row">
  139. <label>时长:</label>
  140. <span>{{ currentWork.duration }}</span>
  141. </div>
  142. <div class="detail-stats">
  143. <div class="stat-item">
  144. <div class="stat-value">{{ formatNumber(currentWork.playCount) }}</div>
  145. <div class="stat-label">播放</div>
  146. </div>
  147. <div class="stat-item">
  148. <div class="stat-value">{{ formatNumber(currentWork.likeCount) }}</div>
  149. <div class="stat-label">点赞</div>
  150. </div>
  151. <div class="stat-item">
  152. <div class="stat-value">{{ currentWork.commentCount }}</div>
  153. <div class="stat-label">评论</div>
  154. </div>
  155. <div class="stat-item">
  156. <div class="stat-value">{{ currentWork.shareCount }}</div>
  157. <div class="stat-label">分享</div>
  158. </div>
  159. </div>
  160. <div class="detail-description" v-if="currentWork.description">
  161. <label>描述:</label>
  162. <p>{{ currentWork.description }}</p>
  163. </div>
  164. </div>
  165. </div>
  166. <template #footer>
  167. <el-button @click="showDetailDialog = false">关闭</el-button>
  168. <el-button type="primary" @click="viewComments(currentWork!)">
  169. 查看评论
  170. </el-button>
  171. <el-button
  172. type="danger"
  173. @click="deletePlatformWork(currentWork!)"
  174. v-if="currentWork?.platform === 'douyin'"
  175. >
  176. <el-icon><Delete /></el-icon>
  177. 删除平台作品
  178. </el-button>
  179. </template>
  180. </el-dialog>
  181. <!-- 评论抽屉 -->
  182. <el-drawer
  183. v-model="showCommentsDrawer"
  184. :title="commentsWork ? `评论 - ${commentsWork.title || '作品'}` : '所有评论'"
  185. size="500px"
  186. destroy-on-close
  187. >
  188. <div class="comments-drawer-header" v-if="commentsWork">
  189. <img :src="commentsWork.coverUrl" class="work-thumb" @error="handleImageError" />
  190. <div class="work-brief">
  191. <div class="work-brief-title">{{ commentsWork.title || '无标题' }}</div>
  192. <div class="work-brief-meta">
  193. <el-tag size="small">{{ getPlatformName(commentsWork.platform) }}</el-tag>
  194. <span>{{ commentsWork.commentCount }} 条评论</span>
  195. </div>
  196. </div>
  197. </div>
  198. <div class="comments-drawer-header" v-else>
  199. <div class="work-brief">
  200. <div class="work-brief-title">所有评论</div>
  201. <div class="work-brief-meta">
  202. <span>共 {{ commentsPagination.total }} 条评论</span>
  203. </div>
  204. </div>
  205. </div>
  206. <el-divider />
  207. <div class="comments-list" v-loading="commentsLoading">
  208. <div v-if="comments.length === 0 && !commentsLoading" class="empty-comments">
  209. <el-empty description="暂无评论" :image-size="80" />
  210. </div>
  211. <div v-for="comment in comments" :key="comment.id" class="comment-item">
  212. <el-avatar :size="36" :src="comment.authorAvatar || undefined">
  213. {{ comment.authorName?.[0] }}
  214. </el-avatar>
  215. <div class="comment-body">
  216. <div class="comment-header">
  217. <span class="author-name">{{ comment.authorName }}</span>
  218. <span class="comment-time">{{ formatDate(comment.commentTime) }}</span>
  219. </div>
  220. <div class="comment-text">{{ comment.content }}</div>
  221. <div class="comment-actions">
  222. <span class="like-count">
  223. <el-icon><Star /></el-icon> {{ comment.likeCount }}
  224. </span>
  225. <el-button
  226. v-if="!comment.replyContent"
  227. type="primary"
  228. link
  229. size="small"
  230. @click="openReplyDialog(comment)"
  231. >
  232. 回复
  233. </el-button>
  234. </div>
  235. <div v-if="comment.replyContent" class="reply-box">
  236. <strong>已回复:</strong>{{ comment.replyContent }}
  237. </div>
  238. </div>
  239. </div>
  240. </div>
  241. <div class="comments-pagination" v-if="commentsPagination.total > commentsPagination.pageSize">
  242. <el-pagination
  243. v-model:current-page="commentsPagination.page"
  244. :page-size="commentsPagination.pageSize"
  245. :total="commentsPagination.total"
  246. layout="prev, pager, next"
  247. small
  248. @change="loadComments"
  249. />
  250. </div>
  251. </el-drawer>
  252. <!-- 回复评论对话框 -->
  253. <el-dialog v-model="showReplyDialog" title="回复评论" width="500px">
  254. <div class="reply-original">
  255. <strong>原评论:</strong>
  256. <p>{{ replyTarget?.content }}</p>
  257. </div>
  258. <el-input
  259. v-model="replyContent"
  260. type="textarea"
  261. :rows="4"
  262. placeholder="输入回复内容"
  263. />
  264. <template #footer>
  265. <el-button @click="showReplyDialog = false">取消</el-button>
  266. <el-button type="primary" @click="handleReply" :loading="replying">
  267. 回复
  268. </el-button>
  269. </template>
  270. </el-dialog>
  271. <!-- 评论同步对话框 -->
  272. <el-dialog
  273. v-model="showSyncDialog"
  274. title="同步评论"
  275. width="450px"
  276. :close-on-click-modal="false"
  277. :close-on-press-escape="false"
  278. :show-close="syncState.status !== 'syncing'"
  279. >
  280. <div class="sync-status">
  281. <!-- 同步中 -->
  282. <template v-if="syncState.status === 'syncing'">
  283. <div class="sync-animation">
  284. <el-icon class="is-loading" :size="48" color="#409eff"><Loading /></el-icon>
  285. </div>
  286. <div class="sync-text">
  287. 正在同步评论<span class="sync-dots">{{ syncDots }}</span>
  288. </div>
  289. <el-progress
  290. :percentage="Math.floor(syncState.progress)"
  291. :stroke-width="8"
  292. style="margin: 16px 0"
  293. />
  294. <div class="sync-hint">
  295. 正在从平台获取评论数据,请耐心等待...
  296. </div>
  297. <div class="sync-steps">
  298. <span :class="{ active: syncState.step >= 1 }">连接平台</span>
  299. <span :class="{ active: syncState.step >= 2 }">获取作品</span>
  300. <span :class="{ active: syncState.step >= 3 }">提取评论</span>
  301. </div>
  302. </template>
  303. <!-- 同步成功 -->
  304. <template v-else-if="syncState.status === 'success'">
  305. <div class="sync-animation">
  306. <el-icon :size="48" color="#67c23a"><CircleCheckFilled /></el-icon>
  307. </div>
  308. <div class="sync-text success">同步完成</div>
  309. <div class="sync-result">
  310. <p>成功同步 <strong>{{ syncState.syncedCount }}</strong> 条评论</p>
  311. <p v-if="syncState.accountsCount">涉及 {{ syncState.accountsCount }} 个账号</p>
  312. </div>
  313. <el-button type="primary" link @click="viewAllComments" style="margin-top: 12px">
  314. 查看所有评论
  315. </el-button>
  316. </template>
  317. <!-- 同步失败 -->
  318. <template v-else-if="syncState.status === 'failed'">
  319. <div class="sync-animation">
  320. <el-icon :size="48" color="#f56c6c"><CircleCloseFilled /></el-icon>
  321. </div>
  322. <div class="sync-text error">同步失败</div>
  323. <div class="sync-error">{{ syncState.error }}</div>
  324. </template>
  325. <!-- 无评论 -->
  326. <template v-else-if="syncState.status === 'empty'">
  327. <div class="sync-animation">
  328. <el-icon :size="48" color="#909399"><WarningFilled /></el-icon>
  329. </div>
  330. <div class="sync-text">未获取到新评论</div>
  331. <div class="sync-hint">可能原因:平台暂无新评论,或 Cookie 已过期</div>
  332. </template>
  333. </div>
  334. <template #footer v-if="syncState.status !== 'syncing'">
  335. <el-button type="primary" @click="closeSyncDialog">确定</el-button>
  336. </template>
  337. </el-dialog>
  338. </div>
  339. </template>
  340. <script setup lang="ts">
  341. import { ref, reactive, onMounted, onUnmounted, computed } from 'vue';
  342. import { Search, Refresh, VideoPlay, Star, ChatDotSquare, Share, Loading, CircleCheckFilled, CircleCloseFilled, WarningFilled, Delete } from '@element-plus/icons-vue';
  343. import { ElMessageBox } from 'element-plus';
  344. import { ElMessage } from 'element-plus';
  345. import request from '@/api/request';
  346. import { accountsApi } from '@/api/accounts';
  347. import { PLATFORMS, PLATFORM_TYPES, WS_EVENTS } from '@media-manager/shared';
  348. import type { Work, WorkStats, PlatformAccount, PlatformType, Comment } from '@media-manager/shared';
  349. import { useServerStore } from '@/stores/server';
  350. import { useAuthStore } from '@/stores/auth';
  351. import { useTaskQueueStore } from '@/stores/taskQueue';
  352. import dayjs from 'dayjs';
  353. const serverStore = useServerStore();
  354. const authStore = useAuthStore();
  355. const taskStore = useTaskQueueStore();
  356. const loading = ref(false);
  357. const refreshing = ref(false);
  358. const syncingComments = ref(false);
  359. const showDetailDialog = ref(false);
  360. const showCommentsDrawer = ref(false);
  361. const showReplyDialog = ref(false);
  362. const showSyncDialog = ref(false);
  363. const commentsLoading = ref(false);
  364. const replying = ref(false);
  365. // 评论同步状态
  366. const syncDots = ref('');
  367. const syncState = reactive({
  368. status: 'syncing' as 'syncing' | 'success' | 'failed' | 'empty',
  369. progress: 0,
  370. step: 1,
  371. syncedCount: 0,
  372. accountsCount: 0,
  373. error: '',
  374. });
  375. let syncTimer: ReturnType<typeof setInterval> | null = null;
  376. const works = ref<Work[]>([]);
  377. const accounts = ref<PlatformAccount[]>([]);
  378. const currentWork = ref<Work | null>(null);
  379. const commentsWork = ref<Work | null>(null);
  380. const comments = ref<Comment[]>([]);
  381. const replyTarget = ref<Comment | null>(null);
  382. const replyContent = ref('');
  383. const stats = ref<WorkStats>({
  384. totalCount: 0,
  385. publishedCount: 0,
  386. totalPlayCount: 0,
  387. totalLikeCount: 0,
  388. totalCommentCount: 0,
  389. });
  390. const commentsPagination = reactive({
  391. page: 1,
  392. pageSize: 20,
  393. total: 0,
  394. });
  395. const platforms = computed(() =>
  396. PLATFORM_TYPES.map(type => PLATFORMS[type]).filter(p => p.supported)
  397. );
  398. const filter = reactive({
  399. accountId: undefined as number | undefined,
  400. platform: '' as PlatformType | '',
  401. status: '',
  402. keyword: '',
  403. });
  404. const pagination = reactive({
  405. page: 1,
  406. pageSize: 12,
  407. total: 0,
  408. });
  409. function getPlatformName(platform: PlatformType) {
  410. return PLATFORMS[platform]?.name || platform;
  411. }
  412. function getStatusType(status: string) {
  413. const map: Record<string, 'success' | 'warning' | 'danger' | 'info'> = {
  414. published: 'success',
  415. reviewing: 'warning',
  416. rejected: 'danger',
  417. draft: 'info',
  418. };
  419. return map[status] || 'info';
  420. }
  421. function getStatusText(status: string) {
  422. const map: Record<string, string> = {
  423. published: '已发布',
  424. reviewing: '审核中',
  425. rejected: '未通过',
  426. draft: '草稿',
  427. };
  428. return map[status] || status;
  429. }
  430. function formatDate(date: string) {
  431. if (!date) return '-';
  432. return dayjs(date).format('YYYY-MM-DD HH:mm');
  433. }
  434. function formatNumber(num: number) {
  435. if (num >= 10000) {
  436. return (num / 10000).toFixed(1) + '万';
  437. }
  438. return num?.toString() || '0';
  439. }
  440. function handleImageError(e: Event) {
  441. const img = e.target as HTMLImageElement;
  442. img.src = 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><rect fill="%23f0f0f0" width="100" height="100"/><text x="50" y="55" text-anchor="middle" fill="%23999" font-size="12">无封面</text></svg>';
  443. }
  444. async function loadWorks() {
  445. loading.value = true;
  446. try {
  447. const result = await request.get('/api/works', {
  448. params: {
  449. page: pagination.page,
  450. pageSize: pagination.pageSize,
  451. accountId: filter.accountId,
  452. platform: filter.platform || undefined,
  453. status: filter.status || undefined,
  454. keyword: filter.keyword || undefined,
  455. },
  456. }) as { items: Work[]; total: number };
  457. works.value = result?.items || [];
  458. pagination.total = result?.total || 0;
  459. } catch {
  460. works.value = [];
  461. } finally {
  462. loading.value = false;
  463. }
  464. }
  465. async function loadStats() {
  466. try {
  467. const result = await request.get('/api/works/stats') as WorkStats;
  468. if (result) {
  469. stats.value = result;
  470. }
  471. } catch {
  472. // 忽略错误
  473. }
  474. }
  475. async function loadAccounts() {
  476. try {
  477. accounts.value = await accountsApi.getAccounts();
  478. } catch {
  479. // 忽略错误
  480. }
  481. }
  482. async function refreshAllWorks() {
  483. if (!accounts.value.length) {
  484. ElMessage.warning('请先添加平台账号');
  485. return;
  486. }
  487. refreshing.value = true;
  488. try {
  489. // 使用任务队列
  490. await taskStore.syncWorks();
  491. ElMessage.success('作品同步任务已创建,请在任务队列中查看进度');
  492. // 打开任务队列弹框
  493. taskStore.openDialog();
  494. // 延迟刷新
  495. setTimeout(() => {
  496. loadWorks();
  497. loadStats();
  498. }, 5000);
  499. } catch (error) {
  500. ElMessage.error((error as Error)?.message || '创建同步任务失败');
  501. } finally {
  502. refreshing.value = false;
  503. }
  504. }
  505. // WebSocket 连接用于接收同步结果
  506. let ws: WebSocket | null = null;
  507. let wsReconnectTimer: ReturnType<typeof setTimeout> | null = null;
  508. let syncTimeoutTimer: ReturnType<typeof setTimeout> | null = null;
  509. function setupWebSocket() {
  510. // 检查是否已连接
  511. if (ws && ws.readyState === WebSocket.OPEN) {
  512. console.log('[WS] Already connected');
  513. return;
  514. }
  515. // 检查 token 是否可用
  516. const token = authStore.accessToken;
  517. if (!token) {
  518. console.warn('[WS] No token available, cannot setup WebSocket');
  519. return;
  520. }
  521. const serverUrl = serverStore.currentServer?.url || 'http://localhost:3000';
  522. const wsUrl = serverUrl.replace(/^http/, 'ws') + '/ws';
  523. console.log('[WS] Connecting to:', wsUrl);
  524. console.log('[WS] Token available:', token.slice(0, 20) + '...');
  525. try {
  526. ws = new WebSocket(wsUrl);
  527. ws.onopen = () => {
  528. console.log('[WS] Connected, sending auth...');
  529. if (ws) {
  530. ws.send(JSON.stringify({ type: WS_EVENTS.AUTH, payload: { token } }));
  531. }
  532. };
  533. ws.onmessage = (event) => {
  534. console.log('[WS] Raw message received:', event.data);
  535. try {
  536. const data = JSON.parse(event.data);
  537. console.log('[WS] Parsed message:', data);
  538. console.log('[WS] Message type:', data.type);
  539. handleWebSocketMessage(data);
  540. console.log('[WS] After handling, syncState:', JSON.stringify(syncState));
  541. } catch (e) {
  542. console.error('[WS] Error processing message:', e);
  543. }
  544. };
  545. ws.onclose = () => {
  546. console.log('[WS] Disconnected');
  547. // 5秒后尝试重连
  548. wsReconnectTimer = setTimeout(setupWebSocket, 5000);
  549. };
  550. ws.onerror = (e) => {
  551. console.error('[WS] Error:', e);
  552. ws?.close();
  553. };
  554. } catch (e) {
  555. console.error('[WS] Setup error:', e);
  556. }
  557. }
  558. function handleWebSocketMessage(data: { type?: string; payload?: Record<string, unknown> }) {
  559. console.log('[WS] Message received:', JSON.stringify(data));
  560. // 优先从 payload.event 获取事件类型,其次从 type 获取
  561. const event = (data.payload?.event as string) || data.type || '';
  562. console.log('[WS] Event:', event);
  563. // 清除超时定时器
  564. if (syncTimeoutTimer && (event === 'synced' || event === 'sync_failed')) {
  565. clearTimeout(syncTimeoutTimer);
  566. syncTimeoutTimer = null;
  567. }
  568. switch (event) {
  569. case 'sync_started':
  570. console.log('[WS] Sync started');
  571. syncState.progress = 5;
  572. syncState.step = 1;
  573. break;
  574. case 'sync_progress':
  575. if (data.payload) {
  576. const { current, total, progress } = data.payload as { current: number; total: number; progress: number };
  577. console.log(`[WS] Progress: ${current}/${total} (${progress}%)`);
  578. syncState.progress = Math.min(90, Math.round(progress * 0.9));
  579. if (current >= 1) syncState.step = 2;
  580. if (current > 1) syncState.step = 3;
  581. }
  582. break;
  583. case 'synced':
  584. console.log('[WS] Sync completed:', data.payload);
  585. stopSyncAnimation();
  586. syncState.progress = 100;
  587. syncState.step = 3;
  588. syncState.syncedCount = (data.payload?.syncedCount as number) || 0;
  589. syncState.accountsCount = (data.payload?.accountCount as number) || 0;
  590. syncState.status = syncState.syncedCount > 0 ? 'success' : 'empty';
  591. syncingComments.value = false;
  592. if (commentsWork.value) loadComments();
  593. break;
  594. case 'sync_failed':
  595. console.log('[WS] Sync failed:', data.payload);
  596. stopSyncAnimation();
  597. syncState.status = 'failed';
  598. syncState.error = (data.payload?.message as string) || '同步失败';
  599. syncingComments.value = false;
  600. break;
  601. }
  602. }
  603. // 超时处理:如果2分钟内没收到 WebSocket 消息,显示完成状态
  604. function startSyncTimeout() {
  605. if (syncTimeoutTimer) {
  606. clearTimeout(syncTimeoutTimer);
  607. }
  608. syncTimeoutTimer = setTimeout(() => {
  609. if (syncState.status === 'syncing') {
  610. console.log('[Sync] Timeout, assuming completed');
  611. stopSyncAnimation();
  612. syncState.status = 'success';
  613. syncState.syncedCount = 0;
  614. syncingComments.value = false;
  615. ElMessage.info('同步已完成,请刷新页面查看结果');
  616. }
  617. }, 120000); // 2分钟超时
  618. }
  619. function cleanupWebSocket() {
  620. if (wsReconnectTimer) {
  621. clearTimeout(wsReconnectTimer);
  622. wsReconnectTimer = null;
  623. }
  624. if (syncTimeoutTimer) {
  625. clearTimeout(syncTimeoutTimer);
  626. syncTimeoutTimer = null;
  627. }
  628. if (ws) {
  629. ws.close();
  630. ws = null;
  631. }
  632. }
  633. async function syncAllComments() {
  634. if (!accounts.value.length) {
  635. ElMessage.warning('请先添加平台账号');
  636. return;
  637. }
  638. // 使用任务队列
  639. try {
  640. await request.post('/api/comments/sync');
  641. ElMessage.success('评论同步任务已创建,请在任务队列中查看进度');
  642. // 打开任务队列弹框
  643. taskStore.openDialog();
  644. } catch (error) {
  645. ElMessage.error((error as Error)?.message || '创建同步任务失败');
  646. }
  647. }
  648. function startSyncAnimation() {
  649. let dotCount = 0;
  650. syncTimer = setInterval(() => {
  651. dotCount = (dotCount + 1) % 4;
  652. syncDots.value = '.'.repeat(dotCount);
  653. // 只在没有收到 WebSocket 进度时才慢慢增加进度(作为备用)
  654. // 真实进度由 WebSocket 消息更新
  655. if (syncState.progress < 20 && syncState.step === 1) {
  656. // 连接平台阶段,缓慢增加
  657. syncState.progress += 1;
  658. }
  659. }, 500);
  660. }
  661. function stopSyncAnimation() {
  662. if (syncTimer) {
  663. clearInterval(syncTimer);
  664. syncTimer = null;
  665. }
  666. syncState.progress = 100;
  667. syncState.step = 3;
  668. }
  669. function closeSyncDialog() {
  670. showSyncDialog.value = false;
  671. stopSyncAnimation();
  672. }
  673. function viewAllComments() {
  674. closeSyncDialog();
  675. commentsWork.value = null; // 不筛选特定作品
  676. commentsPagination.page = 1;
  677. showCommentsDrawer.value = true;
  678. loadComments();
  679. }
  680. function openWorkDetail(work: Work) {
  681. currentWork.value = work;
  682. showDetailDialog.value = true;
  683. }
  684. function viewComments(work: Work) {
  685. showDetailDialog.value = false;
  686. commentsWork.value = work;
  687. commentsPagination.page = 1;
  688. showCommentsDrawer.value = true;
  689. loadComments();
  690. }
  691. async function deletePlatformWork(work: Work) {
  692. try {
  693. await ElMessageBox.confirm(
  694. '确定要从平台删除该作品吗?此操作不可恢复!',
  695. '删除确认',
  696. { type: 'warning' }
  697. );
  698. showDetailDialog.value = false;
  699. // 调用删除平台作品 API
  700. await request.post(`/api/works/${work.id}/delete-platform`);
  701. ElMessage.success('删除任务已创建,请在任务队列中查看进度');
  702. taskStore.openDialog();
  703. } catch {
  704. // 取消或错误
  705. }
  706. }
  707. async function loadComments() {
  708. commentsLoading.value = true;
  709. try {
  710. const params: Record<string, unknown> = {
  711. page: commentsPagination.page,
  712. pageSize: commentsPagination.pageSize,
  713. };
  714. // 如果有选中的作品,按作品筛选;否则查询所有评论
  715. if (commentsWork.value) {
  716. params.workId = commentsWork.value.id;
  717. }
  718. const result = await request.get('/api/comments', { params });
  719. comments.value = result.items || [];
  720. commentsPagination.total = result.total || 0;
  721. } catch {
  722. comments.value = [];
  723. } finally {
  724. commentsLoading.value = false;
  725. }
  726. }
  727. function openReplyDialog(comment: Comment) {
  728. replyTarget.value = comment;
  729. replyContent.value = '';
  730. showReplyDialog.value = true;
  731. }
  732. async function handleReply() {
  733. if (!replyTarget.value || !replyContent.value.trim()) {
  734. ElMessage.warning('请输入回复内容');
  735. return;
  736. }
  737. replying.value = true;
  738. try {
  739. await request.post('/api/comments/reply', {
  740. commentId: replyTarget.value.id,
  741. content: replyContent.value,
  742. });
  743. ElMessage.success('回复成功');
  744. showReplyDialog.value = false;
  745. loadComments();
  746. } catch {
  747. // 错误已处理
  748. } finally {
  749. replying.value = false;
  750. }
  751. }
  752. onMounted(() => {
  753. loadAccounts();
  754. loadWorks();
  755. loadStats();
  756. // WebSocket 连接改为在需要时才建立(点击同步评论按钮时)
  757. });
  758. onUnmounted(() => {
  759. stopSyncAnimation();
  760. cleanupWebSocket();
  761. });
  762. </script>
  763. <style lang="scss" scoped>
  764. @use '@/styles/variables.scss' as *;
  765. .page-header {
  766. display: flex;
  767. align-items: center;
  768. justify-content: space-between;
  769. margin-bottom: 20px;
  770. h2 { margin: 0; }
  771. .header-stats {
  772. display: flex;
  773. gap: 20px;
  774. color: $text-secondary;
  775. }
  776. }
  777. .filter-bar {
  778. display: flex;
  779. flex-wrap: wrap;
  780. gap: 12px;
  781. margin-bottom: 20px;
  782. }
  783. .works-grid {
  784. display: grid;
  785. grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  786. gap: 20px;
  787. min-height: 200px;
  788. }
  789. .work-card {
  790. background: #fff;
  791. border-radius: 8px;
  792. overflow: hidden;
  793. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  794. transition: all 0.3s;
  795. cursor: pointer;
  796. &:hover {
  797. box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  798. transform: translateY(-2px);
  799. }
  800. .work-cover {
  801. position: relative;
  802. width: 100%;
  803. padding-top: 56.25%; // 16:9
  804. background: #f0f0f0;
  805. img {
  806. position: absolute;
  807. top: 0;
  808. left: 0;
  809. width: 100%;
  810. height: 100%;
  811. object-fit: cover;
  812. }
  813. .work-duration {
  814. position: absolute;
  815. bottom: 8px;
  816. right: 8px;
  817. padding: 2px 6px;
  818. background: rgba(0, 0, 0, 0.7);
  819. color: #fff;
  820. font-size: 12px;
  821. border-radius: 4px;
  822. }
  823. .work-status {
  824. position: absolute;
  825. top: 8px;
  826. left: 8px;
  827. }
  828. }
  829. .work-info {
  830. padding: 12px;
  831. .work-title {
  832. font-size: 14px;
  833. font-weight: 500;
  834. color: $text-primary;
  835. overflow: hidden;
  836. text-overflow: ellipsis;
  837. white-space: nowrap;
  838. margin-bottom: 8px;
  839. }
  840. .work-meta {
  841. display: flex;
  842. align-items: center;
  843. gap: 8px;
  844. margin-bottom: 8px;
  845. .work-time {
  846. font-size: 12px;
  847. color: $text-secondary;
  848. }
  849. }
  850. .work-stats {
  851. display: flex;
  852. gap: 12px;
  853. font-size: 12px;
  854. color: $text-secondary;
  855. span {
  856. display: flex;
  857. align-items: center;
  858. gap: 4px;
  859. }
  860. }
  861. }
  862. .work-actions {
  863. padding: 8px 12px;
  864. border-top: 1px solid $border-lighter;
  865. text-align: right;
  866. }
  867. }
  868. .empty-state {
  869. grid-column: 1 / -1;
  870. padding: 60px 0;
  871. }
  872. .work-detail {
  873. display: flex;
  874. gap: 24px;
  875. .detail-left {
  876. flex-shrink: 0;
  877. width: 300px;
  878. .detail-cover {
  879. width: 100%;
  880. border-radius: 8px;
  881. overflow: hidden;
  882. background: #f0f0f0;
  883. img {
  884. width: 100%;
  885. display: block;
  886. }
  887. }
  888. }
  889. .detail-right {
  890. flex: 1;
  891. .detail-row {
  892. display: flex;
  893. align-items: center;
  894. margin-bottom: 12px;
  895. label {
  896. width: 80px;
  897. color: $text-secondary;
  898. }
  899. }
  900. .detail-stats {
  901. display: flex;
  902. gap: 20px;
  903. margin: 20px 0;
  904. padding: 16px;
  905. background: $bg-base;
  906. border-radius: 8px;
  907. .stat-item {
  908. text-align: center;
  909. .stat-value {
  910. font-size: 20px;
  911. font-weight: 600;
  912. color: $primary-color;
  913. }
  914. .stat-label {
  915. font-size: 12px;
  916. color: $text-secondary;
  917. margin-top: 4px;
  918. }
  919. }
  920. }
  921. .detail-description {
  922. label {
  923. display: block;
  924. color: $text-secondary;
  925. margin-bottom: 8px;
  926. }
  927. p {
  928. margin: 0;
  929. color: $text-regular;
  930. line-height: 1.6;
  931. }
  932. }
  933. }
  934. }
  935. // 评论抽屉样式
  936. .comments-drawer-header {
  937. display: flex;
  938. gap: 12px;
  939. .work-thumb {
  940. width: 80px;
  941. height: 45px;
  942. object-fit: cover;
  943. border-radius: 4px;
  944. background: #f0f0f0;
  945. }
  946. .work-brief {
  947. flex: 1;
  948. .work-brief-title {
  949. font-weight: 500;
  950. margin-bottom: 4px;
  951. overflow: hidden;
  952. text-overflow: ellipsis;
  953. white-space: nowrap;
  954. }
  955. .work-brief-meta {
  956. display: flex;
  957. align-items: center;
  958. gap: 8px;
  959. font-size: 12px;
  960. color: $text-secondary;
  961. }
  962. }
  963. }
  964. .comments-list {
  965. min-height: 200px;
  966. }
  967. .empty-comments {
  968. padding: 40px 0;
  969. }
  970. .comment-item {
  971. display: flex;
  972. gap: 12px;
  973. padding: 12px 0;
  974. border-bottom: 1px solid $border-lighter;
  975. &:last-child {
  976. border-bottom: none;
  977. }
  978. .comment-body {
  979. flex: 1;
  980. min-width: 0;
  981. .comment-header {
  982. display: flex;
  983. align-items: center;
  984. gap: 8px;
  985. margin-bottom: 6px;
  986. .author-name {
  987. font-weight: 500;
  988. font-size: 14px;
  989. }
  990. .comment-time {
  991. font-size: 12px;
  992. color: $text-secondary;
  993. }
  994. }
  995. .comment-text {
  996. font-size: 14px;
  997. line-height: 1.5;
  998. color: $text-primary;
  999. word-break: break-word;
  1000. }
  1001. .comment-actions {
  1002. display: flex;
  1003. align-items: center;
  1004. gap: 12px;
  1005. margin-top: 8px;
  1006. .like-count {
  1007. display: flex;
  1008. align-items: center;
  1009. gap: 4px;
  1010. font-size: 12px;
  1011. color: $text-secondary;
  1012. }
  1013. }
  1014. .reply-box {
  1015. margin-top: 8px;
  1016. padding: 8px 12px;
  1017. background: $bg-base;
  1018. border-radius: 4px;
  1019. font-size: 13px;
  1020. color: $text-regular;
  1021. }
  1022. }
  1023. }
  1024. .comments-pagination {
  1025. padding: 16px 0;
  1026. display: flex;
  1027. justify-content: center;
  1028. }
  1029. .reply-original {
  1030. margin-bottom: 16px;
  1031. padding: 12px;
  1032. background: $bg-base;
  1033. border-radius: 4px;
  1034. p {
  1035. margin: 8px 0 0;
  1036. }
  1037. }
  1038. // 同步对话框样式
  1039. .sync-status {
  1040. text-align: center;
  1041. padding: 20px 0;
  1042. .sync-animation {
  1043. margin-bottom: 16px;
  1044. }
  1045. .sync-text {
  1046. font-size: 18px;
  1047. font-weight: 500;
  1048. color: $text-primary;
  1049. margin-bottom: 8px;
  1050. &.success { color: #67c23a; }
  1051. &.error { color: #f56c6c; }
  1052. .sync-dots {
  1053. display: inline-block;
  1054. width: 24px;
  1055. text-align: left;
  1056. }
  1057. }
  1058. .sync-hint {
  1059. font-size: 13px;
  1060. color: $text-secondary;
  1061. margin: 12px 0;
  1062. }
  1063. .sync-steps {
  1064. display: flex;
  1065. justify-content: center;
  1066. gap: 24px;
  1067. margin-top: 16px;
  1068. span {
  1069. font-size: 13px;
  1070. color: $text-placeholder;
  1071. position: relative;
  1072. &.active {
  1073. color: $primary-color;
  1074. font-weight: 500;
  1075. }
  1076. &:not(:last-child)::after {
  1077. content: '→';
  1078. position: absolute;
  1079. right: -16px;
  1080. color: $text-placeholder;
  1081. }
  1082. }
  1083. }
  1084. .sync-result {
  1085. margin-top: 16px;
  1086. font-size: 14px;
  1087. color: $text-regular;
  1088. p {
  1089. margin: 4px 0;
  1090. }
  1091. strong {
  1092. color: $primary-color;
  1093. font-size: 20px;
  1094. }
  1095. }
  1096. .sync-error {
  1097. margin-top: 12px;
  1098. font-size: 13px;
  1099. color: $text-secondary;
  1100. }
  1101. }
  1102. </style>