File size: 6,752 Bytes
81d8726 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | <template>
<el-dialog
v-model="visible"
width="600px"
top="225px"
modal-class="import_script_dialog"
append-to-body
title="导入脚本配置"
:close-on-click-modal="false"
>
<h2>1. 选择要导入的分组</h2>
<el-select v-model="targetGroup" placeholder="请选择分组" style="width: 50%;margin-bottom: 10px;">
<el-option
v-for="item in groupList"
:key="item.id"
:label="item.name"
:value="item.id"
/>
</el-select>
<h2>2. 选择要导入的文件类型</h2>
<ul class="type_list">
<li @click="handleFromJson">
<svg-icon name="icon-json" class="icon" />
<span class="from">JSON</span>
<input
ref="jsonInputRef"
type="file"
accept=".json"
multiple
name="jsonInput"
style="display: none;"
@change="handleJsonFile"
>
</li>
<li @click="() => manualInputVisible = true">
<svg-icon name="icon-bianji1" class="icon" />
<span class="from">手动输入</span>
</li>
</ul>
</el-dialog>
<el-dialog
v-model="manualInputVisible"
width="600px"
top="150px"
title="手动输入"
:close-on-click-modal="false"
append-to-body
>
<el-input
v-model="manualInput"
type="textarea"
:autosize="{ minRows: 15 }"
placeholder="请输入脚本内容,每行一条脚本"
/>
<template #footer>
<div class="manual-input-footer">
<el-button @click="manualInputVisible = false">取消</el-button>
<el-button type="primary" @click="handleManualImport">导入</el-button>
</div>
</template>
</el-dialog>
</template>
<script setup>
import { ref, computed, getCurrentInstance } from 'vue'
const { proxy: { $api, $message, $store } } = getCurrentInstance()
const props = defineProps({
show: {
required: true,
type: Boolean
}
})
const emit = defineEmits(['update:show', 'update-list',])
const jsonInputRef = ref(null)
const manualInputVisible = ref(false)
const manualInput = ref('')
const targetGroup = ref('default')
let visible = computed({
get: () => props.show,
set: (newVal) => emit('update:show', newVal)
})
let scriptList = computed(() => $store.scriptList)
const groupList = computed(() => $store.scriptGroupList.filter(item => item.id !== 'builtin'))
function handleFromJson() {
jsonInputRef.value.click()
}
const handleJsonFile = (event) => {
let files = event.target.files
let jsonFiles = Array.from(files).filter(file => file.name.endsWith('.json'))
if (jsonFiles.length === 0) return $message.warning('未选择有效的JSON文件')
let readerPromises = jsonFiles.map(file => {
return new Promise((resolve, reject) => {
let reader = new FileReader()
reader.onload = (e) => {
try {
let jsonContent = JSON.parse(e.target.result)
resolve(jsonContent)
} catch (error) {
reject(new Error(`Failed to parse JSON file: ${ file.name }`))
}
}
reader.onerror = () => {
reject(new Error(`Failed to read file: ${ file.name }`))
}
reader.readAsText(file)
})
})
Promise.all(readerPromises)
.then(async jsonContents => {
let formatJson = jsonContents.flat(Infinity)
let existCommand = scriptList.value.map(item => item.command)
let existId = scriptList.value.map(item => item.id)
formatJson = formatJson.filter(({ _id, command }) => {
return !existCommand.includes(command) && !existId.includes(_id)
})
if (formatJson.length === 0) return $message.warning('导入的脚本已存在')
formatJson = formatJson.map((item) => {
return {
...item,
group: targetGroup.value
}
})
try {
let { data: { len } } = await $api.importScript({ scripts: formatJson })
$message({ type: 'success', center: true, message: `成功导入脚本: ${ len }条` })
emit('update-list')
visible.value = false
} catch (error) {
$message.error('导入失败: ' + error.message)
}
})
.catch(error => {
$message.error('导入失败: ' + error.message)
console.error('导入失败: ', error)
})
.finally(() => {
event.target.value = null
})
}
const handleManualImport = async () => {
if (!manualInput.value.trim()) {
return $message.warning('请输入脚本内容')
}
try {
let scripts = manualInput.value.split('\n')
scripts = [...new Set(scripts),]
.filter(line => line.trim())
.map((command) => ({ command: command.trim() }))
if (scripts.length === 0) {
return $message.warning('未检测到有效的脚本内容')
}
let existCommand = scriptList.value.map(item => item.command)
let filterScripts = scripts.filter(({ command }) => {
return !existCommand.includes(command)
})
let filterScriptsLen = filterScripts.length
if (filterScriptsLen !== 0 && filterScriptsLen < scripts.length) $message.warning('已过滤重复的脚本')
if (filterScriptsLen === 0) return $message.warning('导入的脚本已存在')
filterScripts = filterScripts.map((item, index) => {
return {
...item,
name: `${ item.command.slice(0, 15) || `脚本${ index + 1 }` }`,
index: scriptList.value.length + index + 1,
description: '手动输入',
group: targetGroup.value
}
})
let { data: { len } } = await $api.importScript({ scripts: filterScripts })
$message({ type: 'success', center: true, message: `成功导入脚本: ${ len }条` })
emit('update-list')
manualInputVisible.value = false
visible.value = false
manualInput.value = ''
} catch (error) {
$message.error('导入失败: ' + error.message)
}
}
</script>
<style lang="scss">
.import_script_dialog {
h2 {
font-size: 14px;
font-weight: 600;
margin: 15px 0 25px 0;
}
.type_list {
display: flex;
align-items: center;
justify-content: center;
user-select: none;
li {
margin: 0 25px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 150px;
height: 120px;
cursor: pointer;
border-radius: 3px;
&:hover {
color: var(--el-menu-active-color);
}
.icon {
width: 35px;
height: 35px;
}
span {
display: inline-block;
}
.from {
font-size: 14px;
margin: 15px 0;
}
.type {
font-size: 12px;
}
}
}
}
.manual-input-footer {
display: flex;
justify-content: center;
gap: 20px;
}
</style> |