index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <template>
  2. <view class="page">
  3. <view class="nav-bar">
  4. <view class="nav-btn" @click="goBack">
  5. <text class="nav-icon">←</text>
  6. </view>
  7. <text class="nav-title">草稿箱</text>
  8. <view class="nav-btn" />
  9. </view>
  10. <scroll-view scroll-y class="content" :scroll-top="scrollTop">
  11. <view v-if="drafts.length === 0 && !loading" class="empty">
  12. <text class="empty-icon">📝</text>
  13. <text class="empty-text">暂无草稿</text>
  14. <text class="empty-hint">在生成页面未保存的内容会自动保存到这里</text>
  15. </view>
  16. <view v-else class="draft-list">
  17. <view
  18. v-for="item in drafts"
  19. :key="item.id"
  20. class="draft-card"
  21. @click="restoreDraft(item)"
  22. >
  23. <view class="draft-header">
  24. <text class="draft-title">{{ item.title || '无标题' }}</text>
  25. <text class="draft-type">{{ getTypeLabel(item.type) }}</text>
  26. </view>
  27. <text class="draft-content">{{ item.content || '暂无内容' }}</text>
  28. <view class="draft-footer">
  29. <text class="draft-time">{{ formatTime(item.updatedAt) }}</text>
  30. <view class="draft-actions">
  31. <view class="action-btn delete" @click.stop="deleteDraft(item.id)">
  32. <text>删除</text>
  33. </view>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. <view v-if="loading" class="loading">
  39. <text>加载中...</text>
  40. </view>
  41. </scroll-view>
  42. </view>
  43. </template>
  44. <script setup lang="ts">
  45. import { ref, onMounted } from 'vue';
  46. import { get, del } from '../../utils/request';
  47. // scroll-top 用于避免 scrollTop 错误
  48. const scrollTop = ref(0);
  49. const drafts = ref<any[]>([]);
  50. const loading = ref(false);
  51. function goBack() {
  52. const pages = getCurrentPages();
  53. if (pages.length > 1) {
  54. uni.navigateBack();
  55. } else {
  56. uni.switchTab({ url: '/pages/index/index' });
  57. }
  58. }
  59. function getTypeLabel(type: string): string {
  60. const labels: Record<string, string> = {
  61. audio: '音频',
  62. book: '书籍',
  63. chapter: '章节',
  64. };
  65. return labels[type] || type;
  66. }
  67. function formatTime(dateStr: string): string {
  68. const date = new Date(dateStr);
  69. const now = new Date();
  70. const diff = now.getTime() - date.getTime();
  71. const minutes = Math.floor(diff / (1000 * 60));
  72. const hours = Math.floor(diff / (1000 * 60 * 60));
  73. const days = Math.floor(diff / (1000 * 60 * 60 * 24));
  74. if (minutes < 1) return '刚刚';
  75. if (minutes < 60) return `${minutes}分钟前`;
  76. if (hours < 24) return `${hours}小时前`;
  77. if (days < 7) return `${days}天前`;
  78. return `${date.getMonth() + 1}/${date.getDate()}`;
  79. }
  80. async function fetchDrafts() {
  81. loading.value = true;
  82. try {
  83. const data = await get('/drafts');
  84. drafts.value = data || [];
  85. } catch (error) {
  86. console.error('获取草稿失败:', error);
  87. } finally {
  88. loading.value = false;
  89. }
  90. }
  91. function restoreDraft(item: any) {
  92. uni.showModal({
  93. title: '恢复草稿',
  94. content: '确定要恢复这个草稿吗?',
  95. success: (res) => {
  96. if (res.confirm) {
  97. uni.setStorageSync('draft', item);
  98. uni.showToast({ title: '已恢复,去生成页面查看', icon: 'success' });
  99. setTimeout(() => {
  100. uni.switchTab({ url: '/pages/create/index' });
  101. }, 1500);
  102. }
  103. },
  104. });
  105. }
  106. async function deleteDraft(id: number) {
  107. uni.showModal({
  108. title: '确认删除',
  109. content: '确定要删除这个草稿吗?',
  110. success: async (res) => {
  111. if (res.confirm) {
  112. try {
  113. await del(`/drafts/${id}`);
  114. uni.showToast({ title: '删除成功', icon: 'success' });
  115. await fetchDrafts();
  116. } catch (error) {
  117. uni.showToast({ title: '删除失败', icon: 'none' });
  118. }
  119. }
  120. },
  121. });
  122. }
  123. onMounted(() => {
  124. fetchDrafts();
  125. });
  126. </script>
  127. <style scoped>
  128. .page {
  129. min-height: 100vh;
  130. background: #f5f5f5;
  131. }
  132. .nav-bar {
  133. display: flex;
  134. align-items: center;
  135. justify-content: space-between;
  136. padding: 44rpx 32rpx 24rpx;
  137. background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  138. position: sticky;
  139. top: 0;
  140. z-index: 100;
  141. }
  142. .nav-btn {
  143. width: 64rpx;
  144. height: 64rpx;
  145. display: flex;
  146. align-items: center;
  147. justify-content: center;
  148. }
  149. .nav-icon {
  150. font-size: 40rpx;
  151. color: #ffffff;
  152. }
  153. .nav-title {
  154. font-size: 32rpx;
  155. font-weight: 600;
  156. color: #ffffff;
  157. }
  158. .content {
  159. padding: 24rpx;
  160. height: calc(100vh - 160rpx);
  161. }
  162. .empty {
  163. display: flex;
  164. flex-direction: column;
  165. align-items: center;
  166. justify-content: center;
  167. padding: 200rpx 0;
  168. }
  169. .empty-icon {
  170. font-size: 120rpx;
  171. margin-bottom: 24rpx;
  172. }
  173. .empty-text {
  174. font-size: 32rpx;
  175. color: #6b7280;
  176. margin-bottom: 12rpx;
  177. }
  178. .empty-hint {
  179. font-size: 26rpx;
  180. color: #9ca3af;
  181. text-align: center;
  182. max-width: 500rpx;
  183. }
  184. .draft-list {
  185. display: flex;
  186. flex-direction: column;
  187. gap: 16rpx;
  188. }
  189. .draft-card {
  190. background: #ffffff;
  191. border-radius: 16rpx;
  192. padding: 24rpx;
  193. }
  194. .draft-header {
  195. display: flex;
  196. align-items: center;
  197. justify-content: space-between;
  198. margin-bottom: 12rpx;
  199. }
  200. .draft-title {
  201. font-size: 30rpx;
  202. font-weight: 600;
  203. color: #1f2937;
  204. flex: 1;
  205. overflow: hidden;
  206. text-overflow: ellipsis;
  207. white-space: nowrap;
  208. }
  209. .draft-type {
  210. padding: 4rpx 12rpx;
  211. background: #f3f4f6;
  212. color: #6b7280;
  213. font-size: 22rpx;
  214. border-radius: 8rpx;
  215. margin-left: 12rpx;
  216. }
  217. .draft-content {
  218. font-size: 26rpx;
  219. color: #6b7280;
  220. line-height: 1.6;
  221. display: -webkit-box;
  222. -webkit-line-clamp: 2;
  223. -webkit-box-orient: vertical;
  224. overflow: hidden;
  225. margin-bottom: 16rpx;
  226. }
  227. .draft-footer {
  228. display: flex;
  229. align-items: center;
  230. justify-content: space-between;
  231. }
  232. .draft-time {
  233. font-size: 24rpx;
  234. color: #9ca3af;
  235. }
  236. .action-btn {
  237. padding: 8rpx 20rpx;
  238. border-radius: 8rpx;
  239. font-size: 24rpx;
  240. }
  241. .action-btn.delete {
  242. background: #fef2f2;
  243. color: #ef4444;
  244. }
  245. .loading {
  246. text-align: center;
  247. padding: 32rpx;
  248. }
  249. .loading text {
  250. font-size: 24rpx;
  251. color: #9ca3af;
  252. }
  253. </style>