index.vue 36 KB

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