tencentDocCrawler.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <template>
  2. <div class="tencent-doc-crawler">
  3. <h3>腾讯文档内容获取</h3>
  4. <div class="form-section">
  5. <div class="form-group">
  6. <label for="documentUrl">文档URL:</label>
  7. <input
  8. id="documentUrl"
  9. v-model="documentUrl"
  10. type="text"
  11. placeholder="https://docs.qq.com/sheet/DQVZpaFVqdnJFU3hn"
  12. />
  13. </div>
  14. <div class="form-group">
  15. <label for="cookie">Cookie:</label>
  16. <textarea
  17. id="cookie"
  18. v-model="cookie"
  19. placeholder="从浏览器复制Cookie,确保已登录腾讯文档"
  20. rows="3"
  21. ></textarea>
  22. <div class="help-text">
  23. <p>提示: 获取Cookie方法</p>
  24. <ol>
  25. <li>登录腾讯文档</li>
  26. <li>按F12打开开发者工具</li>
  27. <li>选择Network(网络)选项卡</li>
  28. <li>刷新页面</li>
  29. <li>选择任意请求,在Headers中找到Cookie</li>
  30. </ol>
  31. </div>
  32. </div>
  33. <div class="actions">
  34. <button
  35. class="primary-btn"
  36. @click="startExport"
  37. :disabled="isLoading || !isFormValid"
  38. >
  39. {{ isLoading ? '处理中...' : '获取文档内容' }}
  40. </button>
  41. </div>
  42. </div>
  43. <!-- 进度显示 -->
  44. <div v-if="isLoading" class="progress-section">
  45. <div class="progress-bar">
  46. <div class="progress-fill" :style="{ width: `${progress}%` }"></div>
  47. </div>
  48. <p>{{ progressText }}</p>
  49. </div>
  50. <!-- 错误信息 -->
  51. <div v-if="errorMessage" class="error-message">
  52. <p>{{ errorMessage }}</p>
  53. </div>
  54. <!-- 结果显示 -->
  55. <div v-if="exportedData" class="result-section">
  56. <h4>导出成功</h4>
  57. <div class="action-buttons">
  58. <button class="secondary-btn" @click="downloadExcel">
  59. 下载Excel文件
  60. </button>
  61. </div>
  62. </div>
  63. </div>
  64. </template>
  65. <script setup lang="ts">
  66. import { ref, computed } from 'vue';
  67. import { TencentDocClient } from '@/utils/tencentDocApi';
  68. defineOptions({
  69. name: 'TencentDocCrawler',
  70. });
  71. // 表单数据
  72. const documentUrl = ref<string>('');
  73. const cookie = ref<string>('');
  74. const isLoading = ref<boolean>(false);
  75. const progress = ref<number>(0);
  76. const progressText = ref<string>('');
  77. const errorMessage = ref<string>('');
  78. const exportedData = ref<ArrayBuffer | null>(null);
  79. // 表单验证
  80. const isFormValid = computed(() => {
  81. return documentUrl.value.trim() !== '' &&
  82. cookie.value.trim() !== '' &&
  83. documentUrl.value.includes('docs.qq.com');
  84. });
  85. // 开始导出
  86. const startExport = async () => {
  87. if (!isFormValid.value) return;
  88. try {
  89. isLoading.value = true;
  90. errorMessage.value = '';
  91. exportedData.value = null;
  92. progress.value = 0;
  93. progressText.value = '创建导出任务...';
  94. // 创建API客户端
  95. const client = new TencentDocClient({
  96. cookie: cookie.value,
  97. documentUrl: documentUrl.value
  98. });
  99. // 创建导出任务
  100. const operationResult = await client.createExportTask();
  101. console.log(operationResult,'22222')
  102. if (!operationResult.status) {
  103. throw new Error(`创建导出任务失败: ${operationResult.message}`);
  104. }
  105. const operationId = operationResult.operationId;
  106. let retries = 0;
  107. const maxRetries = 20;
  108. let fileUrl = '';
  109. progressText.value = '等待导出处理...';
  110. // 轮询查询进度
  111. while (retries < maxRetries) {
  112. const progressResult = await client.queryExportProgress(operationId);
  113. progress.value = progressResult.progress || 0;
  114. progressText.value = `导出进度: ${progress.value}%`;
  115. if (progressResult.progress === 100 && progressResult.file_url) {
  116. fileUrl = progressResult.file_url;
  117. break;
  118. }
  119. retries++;
  120. await new Promise(resolve => setTimeout(resolve, 1000));
  121. }
  122. if (!fileUrl) {
  123. throw new Error('导出超时或未获取到文件下载地址');
  124. }
  125. progressText.value = '下载文件中...';
  126. // 下载文件
  127. exportedData.value = await client.downloadExportedFile(fileUrl);
  128. progressText.value = '导出成功!';
  129. } catch (error) {
  130. console.error('导出失败:', error);
  131. errorMessage.value = error instanceof Error ? error.message : '未知错误';
  132. } finally {
  133. isLoading.value = false;
  134. }
  135. };
  136. // 下载Excel文件
  137. const downloadExcel = () => {
  138. if (!exportedData.value) return;
  139. const blob = new Blob([exportedData.value], {
  140. type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  141. });
  142. const url = window.URL.createObjectURL(blob);
  143. const link = document.createElement('a');
  144. // 从文档URL中提取文件名
  145. const urlParts = documentUrl.value.split('/');
  146. const docId = urlParts[urlParts.length - 1].split('?')[0];
  147. const fileName = `tencent_doc_${docId}.xlsx`;
  148. link.href = url;
  149. link.setAttribute('download', fileName);
  150. document.body.appendChild(link);
  151. link.click();
  152. document.body.removeChild(link);
  153. window.URL.revokeObjectURL(url);
  154. };
  155. </script>
  156. <style scoped>
  157. .tencent-doc-crawler {
  158. max-width: 800px;
  159. margin: 0 auto;
  160. padding: 20px;
  161. }
  162. .form-section {
  163. background-color: #f9f9f9;
  164. border-radius: 8px;
  165. padding: 20px;
  166. margin-bottom: 20px;
  167. }
  168. .form-group {
  169. margin-bottom: 15px;
  170. }
  171. .form-group label {
  172. display: block;
  173. margin-bottom: 5px;
  174. font-weight: bold;
  175. }
  176. input, textarea {
  177. width: 100%;
  178. padding: 8px;
  179. border: 1px solid #ddd;
  180. border-radius: 4px;
  181. font-size: 14px;
  182. }
  183. .help-text {
  184. margin-top: 5px;
  185. font-size: 12px;
  186. color: #666;
  187. }
  188. .help-text ol {
  189. padding-left: 20px;
  190. margin-top: 5px;
  191. }
  192. .actions {
  193. margin-top: 20px;
  194. }
  195. .primary-btn, .secondary-btn {
  196. padding: 10px 16px;
  197. border: none;
  198. border-radius: 4px;
  199. cursor: pointer;
  200. font-size: 14px;
  201. transition: background-color 0.3s;
  202. }
  203. .primary-btn {
  204. background-color: #4CAF50;
  205. color: white;
  206. }
  207. .primary-btn:hover {
  208. background-color: #45a049;
  209. }
  210. .primary-btn:disabled {
  211. background-color: #cccccc;
  212. cursor: not-allowed;
  213. }
  214. .secondary-btn {
  215. background-color: #2196F3;
  216. color: white;
  217. margin-right: 10px;
  218. }
  219. .secondary-btn:hover {
  220. background-color: #0b7dda;
  221. }
  222. .progress-section {
  223. margin: 20px 0;
  224. }
  225. .progress-bar {
  226. height: 20px;
  227. background-color: #f1f1f1;
  228. border-radius: 4px;
  229. overflow: hidden;
  230. }
  231. .progress-fill {
  232. height: 100%;
  233. background-color: #4CAF50;
  234. transition: width 0.3s;
  235. }
  236. .error-message {
  237. background-color: #ffebee;
  238. color: #c62828;
  239. padding: 10px;
  240. border-radius: 4px;
  241. margin: 20px 0;
  242. }
  243. .result-section {
  244. margin-top: 20px;
  245. border-top: 1px solid #eee;
  246. padding-top: 20px;
  247. }
  248. .action-buttons {
  249. margin-top: 15px;
  250. }
  251. </style>