starmusic-mobile
1<template>
2 <view class="custom-source-import">
3 <view class="mask d-mask-in" @click="handleClose"></view>
4 <view class="sheet d-slide-up" data-nav-scope>
5 <view class="sheet-header">
6 <text class="sheet-title">导入自定义音源</text>
7 <view class="sheet-close d-press" @click="handleClose">
8 <lx-icon name="close" :size="32" color="#909399" />
9 </view>
10 </view>
11 <view class="sheet-tip">
12 支持 LX Music 自定义源脚本(粘贴脚本内容、选择本地文件或输入脚本 URL)
13 </view>
14 <view class="import-tabs">
15 <view
16 v-for="tab in tabs"
17 :key="tab.id"
18 class="tab-item d-press-sm"
19 :class="{ active: currentTab === tab.id }"
20 @click="currentTab = tab.id"
21 >
22 {{ tab.name }}
23 </view>
24 </view>
25
26 <!-- 粘贴脚本 -->
27 <view v-if="currentTab === 'paste'" class="import-body">
28 <textarea
29 class="script-area"
30 v-model="scriptContent"
31 placeholder="在此粘贴自定义源脚本内容(以 /* @name ... */ 开头)"
32 maxlength="-1"
33 />
34 <button class="import-btn d-press" :disabled="!scriptContent || importing" @click="handlePasteImport">
35 {{ importing ? '导入中...' : '导入脚本' }}
36 </button>
37 </view>
38
39 <!-- 本地文件导入 -->
40 <view v-else-if="currentTab === 'file'" class="import-body">
41 <view class="file-tip">从文件管理器选择 .js 脚本文件,自动读取并导入</view>
42 <button class="import-btn d-press" :disabled="importing" @click="handleFileImport">
43 {{ importing ? '导入中...' : '选择本地 JS 文件' }}
44 </button>
45 </view>
46
47 <!-- URL 导入 -->
48 <view v-else class="import-body">
49 <input
50 class="url-input"
51 v-model="scriptUrl"
52 placeholder="https://example.com/source.js"
53 confirm-type="go"
54 @confirm="handleUrlImport"
55 />
56 <button class="import-btn d-press" :disabled="!scriptUrl || importing" @click="handleUrlImport">
57 {{ importing ? '导入中...' : '在线获取并导入' }}
58 </button>
59 <view class="url-tip">脚本需以 /* @name 名称 */ 头部注释开头,脚本大小不超过 9MB</view>
60 </view>
61 </view>
62 </view>
63</template>
64
65<script>
66import { addUserApi } from '../common/data.js'
67import { pickTextFile, PICK_CANCELED } from '../common/file-picker.js'
68import LxIcon from './lx-icon/lx-icon.vue'
69
70export default {
71 name: 'CustomSourceImport',
72 components: { LxIcon },
73 data() {
74 return {
75 currentTab: 'paste',
76 tabs: [
77 { id: 'paste', name: '粘贴脚本' },
78 { id: 'file', name: '本地文件' },
79 { id: 'url', name: 'URL 导入' },
80 ],
81 scriptContent: '',
82 scriptUrl: '',
83 importing: false,
84 }
85 },
86 emits: ['close', 'imported'],
87 methods: {
88 handleClose() {
89 this.$emit('close')
90 },
91 async handlePasteImport() {
92 if (!this.scriptContent.trim() || this.importing) return
93 this.importing = true
94 try {
95 const info = addUserApi(this.scriptContent)
96 uni.showToast({ title: `导入成功:${info.name}`, icon: 'success' })
97 this.scriptContent = ''
98 this.$emit('imported', info)
99 } catch (err) {
100 uni.showToast({ title: err.message || '导入失败', icon: 'none' })
101 } finally {
102 this.importing = false
103 }
104 },
105 /** 从本地文件管理器选择 .js 脚本并读取内容 */
106 async handleFileImport() {
107 if (this.importing) return
108 this.importing = true
109 try {
110 const { name, content } = await pickTextFile('.js')
111 if (!content || !content.trim()) throw new Error('脚本内容为空')
112 if (content.length > 9 * 1024 * 1024) throw new Error('脚本超过 9MB')
113 if (name && !/\.js$/i.test(name)) throw new Error('请选择 .js 脚本文件')
114 const info = addUserApi(content)
115 uni.showToast({ title: `导入成功:${info.name}`, icon: 'success' })
116 this.$emit('imported', info)
117 } catch (err) {
118 // 用户主动取消选择,不打扰
119 if (err && err.message === PICK_CANCELED) return
120 uni.showToast({ title: (err && err.message) || '导入失败', icon: 'none' })
121 } finally {
122 this.importing = false
123 }
124 },
125 async handleUrlImport() {
126 if (!this.scriptUrl.trim() || this.importing) return
127 if (!/^https?:\/\//.test(this.scriptUrl)) {
128 uni.showToast({ title: '请输入有效的 http/https URL', icon: 'none' })
129 return
130 }
131 this.importing = true
132 uni.showLoading({ title: '获取脚本中...' })
133 try {
134 const resp = await new Promise((resolve, reject) => {
135 uni.request({
136 url: this.scriptUrl,
137 method: 'GET',
138 success: resolve,
139 fail: reject,
140 })
141 })
142 uni.hideLoading()
143 if (resp.statusCode !== 200) throw new Error('获取失败:HTTP ' + resp.statusCode)
144 const script = typeof resp.data === 'string' ? resp.data : JSON.stringify(resp.data)
145 if (script.length > 9 * 1024 * 1024) throw new Error('脚本超过 9MB')
146 const info = addUserApi(script)
147 uni.showToast({ title: `导入成功:${info.name}`, icon: 'success' })
148 this.scriptUrl = ''
149 this.$emit('imported', info)
150 } catch (err) {
151 uni.hideLoading()
152 uni.showToast({ title: err.message || '导入失败', icon: 'none' })
153 } finally {
154 this.importing = false
155 }
156 },
157 },
158}
159</script>
160
161<style lang="scss" scoped>
162.mask {
163 position: fixed;
164 top: 0;
165 left: 0;
166 right: 0;
167 bottom: 0;
168 background: rgba(0, 0, 0, 0.5);
169 z-index: 999;
170}
171
172.sheet {
173 position: fixed;
174 left: 0;
175 right: 0;
176 bottom: 0;
177 background: var(--card-bg-strong, #fff);
178 border-radius: 24rpx 24rpx 0 0;
179 z-index: 1000;
180 max-height: 80vh;
181 display: flex;
182 flex-direction: column;
183 padding-bottom: calc(env(safe-area-inset-bottom));
184}
185
186.sheet-header {
187 display: flex;
188 align-items: center;
189 justify-content: space-between;
190 padding: 32rpx;
191}
192
193.sheet-title {
194 font-size: 32rpx;
195 font-weight: 600;
196 color: #303133;
197}
198
199.sheet-close {
200 display: flex;
201 align-items: center;
202 justify-content: center;
203 padding: 8rpx;
204}
205
206.sheet-tip {
207 padding: 0 32rpx 24rpx;
208 font-size: 24rpx;
209 color: var(--text-muted, #909399);
210}
211
212.import-tabs {
213 display: flex;
214 padding: 0 32rpx;
215 border-bottom: 1rpx solid #f0f0f0;
216}
217
218.tab-item {
219 padding: 16rpx 32rpx;
220 font-size: 28rpx;
221 color: var(--text-muted, #909399);
222 border-bottom: 4rpx solid transparent;
223}
224
225.tab-item.active {
226 color: var(--theme-color, #31c27c);
227 border-bottom-color: var(--theme-color, #31c27c);
228}
229
230.import-body {
231 padding: 32rpx;
232 display: flex;
233 flex-direction: column;
234}
235
236.script-area {
237 width: 100%;
238 height: 400rpx;
239 padding: 20rpx;
240 border-radius: 12rpx;
241 background: var(--fill-bg, #f7f8fa);
242 font-size: 24rpx;
243 color: #303133;
244 box-sizing: border-box;
245}
246
247.url-input {
248 width: 100%;
249 height: 80rpx;
250 padding: 0 24rpx;
251 border-radius: 12rpx;
252 background: var(--fill-bg, #f7f8fa);
253 font-size: 28rpx;
254 color: #303133;
255 box-sizing: border-box;
256}
257
258.import-btn {
259 margin-top: 32rpx;
260 background: var(--theme-color, #31c27c);
261 color: #fff;
262 border-radius: 40rpx;
263 font-size: 30rpx;
264}
265
266.import-btn[disabled] {
267 opacity: 0.5;
268}
269
270.url-tip {
271 margin-top: 16rpx;
272 font-size: 24rpx;
273 color: var(--text-muted, #c0c4cc);
274}
275
276.file-tip {
277 margin-top: 8rpx;
278 font-size: 24rpx;
279 color: var(--text-muted, #909399);
280 text-align: center;
281}
282</style>