diff --git a/src/components/EditSites.vue b/src/components/EditSites.vue index 5270e94..be8afe3 100644 --- a/src/components/EditSites.vue +++ b/src/components/EditSites.vue @@ -379,31 +379,58 @@ export default { } const options = { filters: [ - { name: 'JSON file', extensions: ['json'] } + { name: 'JSON file', extensions: ['json'] }, + { name: 'Text file', extensions: ['txt'] } ], properties: ['openFile', 'multiSelections'] } remote.dialog.showOpenDialog(options).then(result => { if (!result.canceled) { result.filePaths.forEach(file => { - const str = fs.readFileSync(file) - const json = JSON.parse(str) - json.forEach(ele => { - if (ele.api && this.sites.filter(x => x.key === ele.key).length === 0 && this.sites.filter(x => x.name === ele.name && x.api === ele.api).length === 0) { - // 不含该key 同时也不含名字和url一样的 - if (ele.isActive === undefined) { - ele.isActive = true + if (file.endsWith('json')) { + const str = fs.readFileSync(file) + const json = JSON.parse(str) + json.forEach(ele => { + if (ele.api && this.sites.filter(x => x.key === ele.key).length === 0 && this.sites.filter(x => x.name === ele.name && x.api === ele.api).length === 0) { + // 不含该key 同时也不含名字和url一样的 + if (ele.isActive === undefined) { + ele.isActive = true + } + if (ele.group === undefined) { + ele.group = '导入' + } + this.sites.push(ele) } - if (ele.group === undefined) { - ele.group = '导入' - } - this.sites.push(ele) + }) + this.resetId(this.sites) + sites.clear().then(sites.bulkAdd(this.sites)) + this.$message.success('导入成功') + this.getSites() + } + if (file.endsWith('txt')) { + try { + const txt = fs.readFileSync(file, 'utf8') + const json = JSON.parse(txt) + json.forEach(ele => { + if (ele.api && this.sites.filter(x => x.key === ele.key).length === 0 && this.sites.filter(x => x.name === ele.name && x.api === ele.api).length === 0) { + // 不含该key 同时也不含名字和url一样的 + if (ele.isActive === undefined) { + ele.isActive = true + } + if (ele.group === undefined) { + ele.group = '导入' + } + this.sites.push(ele) + } + }) + this.resetId(this.sites) + sites.clear().then(sites.bulkAdd(this.sites)) + this.$message.success('导入成功') + this.getSites() + } catch (error) { + this.$message.warning('导入失败') } - }) - this.resetId(this.sites) - sites.clear().then(sites.bulkAdd(this.sites)) - this.$message.success('导入成功') - this.getSites() + } }) } }) diff --git a/src/components/IPTV.vue b/src/components/IPTV.vue index 91447aa..80b65eb 100644 --- a/src/components/IPTV.vue +++ b/src/components/IPTV.vue @@ -352,7 +352,8 @@ export default { const options = { filters: [ { name: 'm3u file', extensions: ['m3u', 'm3u8'] }, - { name: 'JSON file', extensions: ['json'] } + { name: 'JSON file', extensions: ['json'] }, + { name: 'Text file', extensions: ['txt'] } ], properties: ['openFile', 'multiSelections'] } @@ -389,8 +390,8 @@ export default { this.updateChannelList() }) }) - } else { - // Import Json file + } + if (file.endsWith('json')) { const importedList = JSON.parse(fs.readFileSync(file)) importedList.forEach(ele => { const commonEle = this.channelList.find(e => e.name === ele.name) @@ -405,6 +406,26 @@ export default { }) this.updateDatabase() } + if (file.endsWith('txt')) { + try { + const txt = fs.readFileSync(file, 'utf8') + const importedList = JSON.parse(txt) + importedList.forEach(ele => { + const commonEle = this.channelList.find(e => e.name === ele.name) + if (commonEle) { + const urls = commonEle.channels.map(c => c.url) + const channels = ele.channels.filter(e => !urls.includes(e.url)) + commonEle.channels = commonEle.channels.concat(channels) + } else { + ele.id = this.channelList.length ? this.channelList.slice(-1)[0].id + 1 : 1 + this.channelList.push(ele) + } + }) + this.updateDatabase() + } catch (error) { + this.$message.warning('导入失败') + } + } }) this.$message.success('导入成功') }