index.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <template>
  2. <view class="page">
  3. <!-- 顶部导航 -->
  4. <view class="nav-bar">
  5. <view class="nav-btn" @click="goBack">
  6. <text class="nav-icon">←</text>
  7. </view>
  8. <text class="nav-title">我的播放列表</text>
  9. <view class="nav-btn" @click="showCreateModal = true">
  10. <text class="nav-icon">+</text>
  11. </view>
  12. </view>
  13. <!-- 播放列表 -->
  14. <scroll-view scroll-y class="content" :scroll-top="scrollTop">
  15. <view v-if="playlists.length === 0 && !loading" class="empty">
  16. <text class="empty-icon">📋</text>
  17. <text class="empty-text">暂无播放列表</text>
  18. <text class="empty-hint">点击右上角创建第一个播放列表</text>
  19. </view>
  20. <view v-else class="playlist-grid">
  21. <view
  22. v-for="item in playlists"
  23. :key="item.id"
  24. class="playlist-card"
  25. @click="goToDetail(item.id)"
  26. >
  27. <view class="playlist-cover">
  28. <text class="cover-icon">🎵</text>
  29. <view class="count-badge">
  30. <text>{{ item._count?.items || 0 }}</text>
  31. </view>
  32. </view>
  33. <view class="playlist-info">
  34. <text class="playlist-title">{{ item.name }}</text>
  35. <text class="playlist-meta">{{ item.description || '暂无描述' }}</text>
  36. </view>
  37. </view>
  38. </view>
  39. <view v-if="loading" class="loading">
  40. <text>加载中...</text>
  41. </view>
  42. </scroll-view>
  43. <!-- 创建播放列表弹窗 -->
  44. <view v-if="showCreateModal" class="modal">
  45. <view class="modal-mask" @click="showCreateModal = false"></view>
  46. <view class="modal-content">
  47. <view class="modal-header">
  48. <text class="modal-title">新建播放列表</text>
  49. </view>
  50. <view class="modal-body">
  51. <input
  52. v-model="newPlaylistName"
  53. class="input"
  54. placeholder="请输入播放列表名称"
  55. maxlength="50"
  56. />
  57. <textarea
  58. v-model="newPlaylistDesc"
  59. class="textarea"
  60. placeholder="请输入描述(可选)"
  61. maxlength="200"
  62. />
  63. </view>
  64. <view class="modal-footer">
  65. <view class="modal-btn cancel" @click="showCreateModal = false">
  66. <text>取消</text>
  67. </view>
  68. <view class="modal-btn confirm" @click="createPlaylist" :disabled="!newPlaylistName.trim()">
  69. <text>创建</text>
  70. </view>
  71. </view>
  72. </view>
  73. </view>
  74. </view>
  75. </template>
  76. <script setup lang="ts">
  77. import { ref, onMounted } from 'vue';
  78. import { get, post } from '../../utils/request';
  79. // scroll-top 用于避免 scrollTop 错误
  80. const scrollTop = ref(0);
  81. const playlists = ref<any[]>([]);
  82. const loading = ref(false);
  83. const showCreateModal = ref(false);
  84. const newPlaylistName = ref('');
  85. const newPlaylistDesc = ref('');
  86. // 返回
  87. function goBack() {
  88. const pages = getCurrentPages();
  89. if (pages.length > 1) {
  90. uni.navigateBack();
  91. } else {
  92. uni.switchTab({ url: '/pages/index/index' });
  93. }
  94. }
  95. // 跳转到详情
  96. function goToDetail(id: number) {
  97. uni.navigateTo({ url: `/pages/playlists/detail?id=${id}` });
  98. }
  99. // 获取播放列表
  100. async function fetchPlaylists() {
  101. loading.value = true;
  102. try {
  103. const data = await get('/playlists');
  104. playlists.value = data || [];
  105. } catch (error) {
  106. console.error('获取播放列表失败:', error);
  107. } finally {
  108. loading.value = false;
  109. }
  110. }
  111. // 创建播放列表
  112. async function createPlaylist() {
  113. if (!newPlaylistName.value.trim()) return;
  114. try {
  115. await post('/playlists', {
  116. name: newPlaylistName.value.trim(),
  117. description: newPlaylistDesc.value.trim(),
  118. });
  119. uni.showToast({ title: '创建成功', icon: 'success' });
  120. showCreateModal.value = false;
  121. newPlaylistName.value = '';
  122. newPlaylistDesc.value = '';
  123. await fetchPlaylists();
  124. } catch (error) {
  125. uni.showToast({ title: '创建失败', icon: 'none' });
  126. }
  127. }
  128. onMounted(() => {
  129. fetchPlaylists();
  130. });
  131. </script>
  132. <style scoped>
  133. .page {
  134. min-height: 100vh;
  135. background: #f5f5f5;
  136. }
  137. .nav-bar {
  138. display: flex;
  139. align-items: center;
  140. justify-content: space-between;
  141. padding: 44rpx 32rpx 24rpx;
  142. background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  143. position: sticky;
  144. top: 0;
  145. z-index: 100;
  146. }
  147. .nav-btn {
  148. width: 64rpx;
  149. height: 64rpx;
  150. display: flex;
  151. align-items: center;
  152. justify-content: center;
  153. }
  154. .nav-icon {
  155. font-size: 40rpx;
  156. color: #ffffff;
  157. }
  158. .nav-title {
  159. font-size: 32rpx;
  160. font-weight: 600;
  161. color: #ffffff;
  162. }
  163. .content {
  164. padding: 24rpx;
  165. height: calc(100vh - 160rpx);
  166. }
  167. .empty {
  168. display: flex;
  169. flex-direction: column;
  170. align-items: center;
  171. justify-content: center;
  172. padding: 200rpx 0;
  173. }
  174. .empty-icon {
  175. font-size: 120rpx;
  176. margin-bottom: 24rpx;
  177. }
  178. .empty-text {
  179. font-size: 32rpx;
  180. color: #6b7280;
  181. margin-bottom: 12rpx;
  182. }
  183. .empty-hint {
  184. font-size: 26rpx;
  185. color: #9ca3af;
  186. }
  187. .playlist-grid {
  188. display: flex;
  189. flex-wrap: wrap;
  190. gap: 20rpx;
  191. }
  192. .playlist-card {
  193. width: calc(50% - 10rpx);
  194. background: #ffffff;
  195. border-radius: 16rpx;
  196. overflow: hidden;
  197. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  198. }
  199. .playlist-cover {
  200. position: relative;
  201. width: 100%;
  202. height: 240rpx;
  203. background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  204. display: flex;
  205. align-items: center;
  206. justify-content: center;
  207. }
  208. .cover-icon {
  209. font-size: 80rpx;
  210. opacity: 0.8;
  211. }
  212. .count-badge {
  213. position: absolute;
  214. bottom: 12rpx;
  215. right: 12rpx;
  216. background: rgba(0, 0, 0, 0.6);
  217. color: #ffffff;
  218. font-size: 22rpx;
  219. padding: 4rpx 12rpx;
  220. border-radius: 8rpx;
  221. }
  222. .playlist-info {
  223. padding: 20rpx;
  224. }
  225. .playlist-title {
  226. font-size: 28rpx;
  227. font-weight: 500;
  228. color: #1f2937;
  229. display: block;
  230. overflow: hidden;
  231. text-overflow: ellipsis;
  232. white-space: nowrap;
  233. margin-bottom: 8rpx;
  234. }
  235. .playlist-meta {
  236. font-size: 22rpx;
  237. color: #9ca3af;
  238. display: block;
  239. overflow: hidden;
  240. text-overflow: ellipsis;
  241. white-space: nowrap;
  242. }
  243. .loading {
  244. text-align: center;
  245. padding: 32rpx;
  246. }
  247. .loading text {
  248. font-size: 24rpx;
  249. color: #9ca3af;
  250. }
  251. .modal {
  252. position: fixed;
  253. top: 0;
  254. left: 0;
  255. right: 0;
  256. bottom: 0;
  257. z-index: 1000;
  258. display: flex;
  259. align-items: center;
  260. justify-content: center;
  261. }
  262. .modal-mask {
  263. position: absolute;
  264. top: 0;
  265. left: 0;
  266. right: 0;
  267. bottom: 0;
  268. background: rgba(0, 0, 0, 0.5);
  269. }
  270. .modal-content {
  271. position: relative;
  272. width: 85%;
  273. max-width: 600rpx;
  274. background: #ffffff;
  275. border-radius: 24rpx;
  276. overflow: hidden;
  277. }
  278. .modal-header {
  279. padding: 32rpx;
  280. border-bottom: 1rpx solid #f3f4f6;
  281. }
  282. .modal-title {
  283. font-size: 32rpx;
  284. font-weight: 600;
  285. color: #1f2937;
  286. }
  287. .modal-body {
  288. padding: 32rpx;
  289. }
  290. .input {
  291. width: 100%;
  292. height: 80rpx;
  293. padding: 0 24rpx;
  294. border: 1rpx solid #e5e7eb;
  295. border-radius: 12rpx;
  296. font-size: 28rpx;
  297. margin-bottom: 24rpx;
  298. }
  299. .textarea {
  300. width: 100%;
  301. min-height: 160rpx;
  302. padding: 20rpx 24rpx;
  303. border: 1rpx solid #e5e7eb;
  304. border-radius: 12rpx;
  305. font-size: 28rpx;
  306. }
  307. .modal-footer {
  308. display: flex;
  309. border-top: 1rpx solid #f3f4f6;
  310. }
  311. .modal-btn {
  312. flex: 1;
  313. height: 96rpx;
  314. display: flex;
  315. align-items: center;
  316. justify-content: center;
  317. font-size: 30rpx;
  318. }
  319. .modal-btn.cancel {
  320. color: #6b7280;
  321. border-right: 1rpx solid #f3f4f6;
  322. }
  323. .modal-btn.confirm {
  324. color: #4f46e5;
  325. font-weight: 600;
  326. }
  327. .modal-btn.confirm[disabled] {
  328. opacity: 0.5;
  329. }
  330. </style>