完善CK管理增删改导入
This commit is contained in:
@@ -1,21 +1,161 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, computed } from 'vue'
|
||||
import { ref, computed, onMounted } from 'vue'
|
||||
import { useProducts } from '@/composables/useProducts'
|
||||
import { useToast } from '@/composables/useToast'
|
||||
import { useAccounts, type Account } from '@/composables/useAccounts'
|
||||
import {
|
||||
DeleteAccounts,
|
||||
ExportAccountsWithProduct,
|
||||
ImportAccounts,
|
||||
OrganizeAccounts,
|
||||
UpdateAccount,
|
||||
VerifyAndExport
|
||||
} from '@wails/go/app/App'
|
||||
|
||||
const { selectedProductId, selectedProductName, selectedProduct } = useProducts()
|
||||
const { showToast } = useToast()
|
||||
const { accounts, loadAccounts, clearSelection } = useAccounts()
|
||||
|
||||
// 状态
|
||||
const loading = ref(false)
|
||||
const verifyResult = ref<any>(null)
|
||||
const organizeResult = ref<any>(null)
|
||||
|
||||
// 账号文件管理状态
|
||||
const accountContent = ref('')
|
||||
const accountLoading = ref(false)
|
||||
const selectedAccountNames = ref<string[]>([])
|
||||
const editingAccount = ref<Account | null>(null)
|
||||
const editName = ref('')
|
||||
const editCookie = ref('')
|
||||
|
||||
const allAccountsSelected = computed(() =>
|
||||
accounts.value.length > 0 && selectedAccountNames.value.length === accounts.value.length
|
||||
)
|
||||
|
||||
const refreshManagedAccounts = async (resetSelection = true) => {
|
||||
await loadAccounts()
|
||||
if (resetSelection) {
|
||||
selectedAccountNames.value = []
|
||||
clearSelection()
|
||||
}
|
||||
}
|
||||
|
||||
const importAccountText = async (content: string) => {
|
||||
accountLoading.value = true
|
||||
try {
|
||||
const result = await ImportAccounts(content)
|
||||
if (!result.success) {
|
||||
showToast('导入失败', result.error || '账号文件未更新', 'error')
|
||||
return
|
||||
}
|
||||
accountContent.value = ''
|
||||
await refreshManagedAccounts()
|
||||
showToast('导入成功', `已添加 ${result.count} 个账号`, 'success')
|
||||
} catch (e: any) {
|
||||
showToast('导入失败', e?.message || '', 'error')
|
||||
} finally {
|
||||
accountLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const importFromClipboard = async () => {
|
||||
if (!navigator.clipboard?.readText) {
|
||||
showToast('当前环境不支持读取剪贴板', '请直接粘贴到文本框后导入', 'warn')
|
||||
return
|
||||
}
|
||||
try {
|
||||
const content = await navigator.clipboard.readText()
|
||||
if (!content.trim()) {
|
||||
showToast('剪贴板为空', '请先复制账号文本', 'warn')
|
||||
return
|
||||
}
|
||||
accountContent.value = content
|
||||
await importAccountText(content)
|
||||
} catch (e: any) {
|
||||
showToast('无法读取剪贴板', e?.message || '请直接粘贴到文本框后导入', 'error')
|
||||
}
|
||||
}
|
||||
|
||||
const importFromTextArea = async () => {
|
||||
if (!accountContent.value.trim()) {
|
||||
showToast('提示', '请粘贴账号文本', 'warn')
|
||||
return
|
||||
}
|
||||
await importAccountText(accountContent.value)
|
||||
}
|
||||
|
||||
const toggleManagedAccount = (name: string) => {
|
||||
selectedAccountNames.value = selectedAccountNames.value.includes(name)
|
||||
? selectedAccountNames.value.filter(item => item !== name)
|
||||
: [...selectedAccountNames.value, name]
|
||||
}
|
||||
|
||||
const toggleAllManagedAccounts = () => {
|
||||
selectedAccountNames.value = allAccountsSelected.value
|
||||
? []
|
||||
: accounts.value.map(account => account.name)
|
||||
}
|
||||
|
||||
const startEdit = (account: Account) => {
|
||||
editingAccount.value = account
|
||||
editName.value = account.name
|
||||
editCookie.value = ''
|
||||
}
|
||||
|
||||
const cancelEdit = () => {
|
||||
editingAccount.value = null
|
||||
editName.value = ''
|
||||
editCookie.value = ''
|
||||
}
|
||||
|
||||
const saveEdit = async () => {
|
||||
if (!editingAccount.value) return
|
||||
accountLoading.value = true
|
||||
try {
|
||||
const result = await UpdateAccount(editingAccount.value.name, editName.value, editCookie.value)
|
||||
if (!result.success) {
|
||||
showToast('保存失败', result.error || '账号文件未更新', 'error')
|
||||
return
|
||||
}
|
||||
await refreshManagedAccounts()
|
||||
cancelEdit()
|
||||
showToast('已保存', '账号已更新', 'success')
|
||||
} catch (e: any) {
|
||||
showToast('保存失败', e?.message || '', 'error')
|
||||
} finally {
|
||||
accountLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const deleteManagedAccounts = async (names: string[]) => {
|
||||
const targetNames = [...names]
|
||||
if (targetNames.length === 0) {
|
||||
showToast('提示', '请先选择要删除的账号', 'warn')
|
||||
return
|
||||
}
|
||||
|
||||
accountLoading.value = true
|
||||
try {
|
||||
const result = await DeleteAccounts(targetNames)
|
||||
if (!result.success) {
|
||||
showToast('删除失败', result.error || '账号文件未更新', 'error')
|
||||
return
|
||||
}
|
||||
await refreshManagedAccounts()
|
||||
if (editingAccount.value && targetNames.includes(editingAccount.value.name)) cancelEdit()
|
||||
showToast('已删除', `已删除 ${result.count} 个账号`, 'success')
|
||||
} catch (e: any) {
|
||||
showToast('删除失败', e?.message || '', 'error')
|
||||
} finally {
|
||||
accountLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
void loadAccounts()
|
||||
})
|
||||
|
||||
// 时间范围(默认一年)
|
||||
const timeRange = ref<'year' | 'custom'>('year')
|
||||
const customStart = ref('')
|
||||
@@ -115,7 +255,96 @@ const handleOrganize = async () => {
|
||||
<div class="ck-manager">
|
||||
<div class="ck-header">
|
||||
<h2>CK 管理</h2>
|
||||
<p class="ck-desc">复验抢购结果、整理账号文件</p>
|
||||
<p class="ck-desc">导入、编辑、删除账号,并复验抢购结果</p>
|
||||
</div>
|
||||
|
||||
<div class="ck-section account-manager-section">
|
||||
<div class="section-heading">
|
||||
<div class="section-title">账号管理</div>
|
||||
<span class="account-count">{{ accounts.length }} 个账号</span>
|
||||
</div>
|
||||
|
||||
<div class="account-import">
|
||||
<textarea
|
||||
v-model="accountContent"
|
||||
class="account-textarea"
|
||||
placeholder="账号名----密码----Cookie"
|
||||
:disabled="accountLoading"
|
||||
/>
|
||||
<div class="account-import-actions">
|
||||
<button class="btn btn-primary" :disabled="accountLoading" @click="importFromClipboard">
|
||||
从剪贴板导入
|
||||
</button>
|
||||
<button class="btn" :disabled="accountLoading" @click="importFromTextArea">
|
||||
导入文本
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="account-toolbar">
|
||||
<label class="select-all-control">
|
||||
<input type="checkbox" :checked="allAccountsSelected" @change="toggleAllManagedAccounts">
|
||||
全选
|
||||
</label>
|
||||
<span class="selected-count">已选 {{ selectedAccountNames.length }} 个</span>
|
||||
<button
|
||||
class="btn btn-compact btn-delete"
|
||||
:disabled="accountLoading || selectedAccountNames.length === 0"
|
||||
@click="deleteManagedAccounts(selectedAccountNames)"
|
||||
>
|
||||
删除所选
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="account-table-wrap">
|
||||
<table class="managed-account-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="managed-check"></th>
|
||||
<th>账号</th>
|
||||
<th>CK</th>
|
||||
<th class="managed-actions">操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-if="accounts.length === 0">
|
||||
<td colspan="4" class="managed-empty">暂无账号</td>
|
||||
</tr>
|
||||
<template v-else>
|
||||
<tr v-for="account in accounts" :key="account.name">
|
||||
<td class="managed-check">
|
||||
<input
|
||||
type="checkbox"
|
||||
:checked="selectedAccountNames.includes(account.name)"
|
||||
@change="toggleManagedAccount(account.name)"
|
||||
>
|
||||
</td>
|
||||
<td class="managed-name">{{ account.name }}</td>
|
||||
<td>
|
||||
<span v-if="account.cookie" class="managed-cookie" :title="account.cookie">{{ account.cookie }}</span>
|
||||
<span v-else class="cookie-state missing">缺失</span>
|
||||
</td>
|
||||
<td class="managed-actions">
|
||||
<button class="text-action" :disabled="accountLoading" @click="startEdit(account)">编辑</button>
|
||||
<button class="text-action danger" :disabled="accountLoading" @click="deleteManagedAccounts([account.name])">删除</button>
|
||||
</td>
|
||||
</tr>
|
||||
</template>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div v-if="editingAccount" class="account-editor">
|
||||
<div class="editor-title">编辑账号:{{ editingAccount.name }}</div>
|
||||
<div class="editor-fields">
|
||||
<input v-model.trim="editName" class="form-input" placeholder="账号名" :disabled="accountLoading">
|
||||
<input v-model.trim="editCookie" class="form-input" placeholder="粘贴新 CK,留空保留原 CK" :disabled="accountLoading">
|
||||
</div>
|
||||
<div class="editor-actions">
|
||||
<button class="btn btn-primary btn-compact" :disabled="accountLoading || !editName" @click="saveEdit">保存</button>
|
||||
<button class="btn btn-compact" :disabled="accountLoading" @click="cancelEdit">取消</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 当前选中商品 -->
|
||||
@@ -238,7 +467,7 @@ const handleOrganize = async () => {
|
||||
<style scoped>
|
||||
.ck-manager {
|
||||
padding: 20px;
|
||||
max-width: 800px;
|
||||
max-width: 980px;
|
||||
}
|
||||
|
||||
.ck-header {
|
||||
@@ -261,6 +490,218 @@ const handleOrganize = async () => {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.account-manager-section {
|
||||
padding-bottom: 20px;
|
||||
border-bottom: 1px solid var(--line);
|
||||
}
|
||||
|
||||
.section-heading,
|
||||
.account-toolbar,
|
||||
.editor-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.section-heading {
|
||||
justify-content: space-between;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.section-heading .section-title {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.account-count,
|
||||
.selected-count {
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.account-import {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) auto;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.account-textarea {
|
||||
min-height: 76px;
|
||||
resize: vertical;
|
||||
padding: 9px 12px;
|
||||
font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
|
||||
color: var(--text-strong);
|
||||
background: var(--field);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: var(--radius-sm);
|
||||
}
|
||||
|
||||
.account-textarea:focus {
|
||||
outline: 0;
|
||||
border-color: var(--accent);
|
||||
box-shadow: var(--focus);
|
||||
}
|
||||
|
||||
.account-import-actions {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.account-toolbar {
|
||||
gap: 10px;
|
||||
min-height: 36px;
|
||||
margin-top: 12px;
|
||||
}
|
||||
|
||||
.select-all-control {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
cursor: pointer;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.select-all-control input,
|
||||
.managed-check input {
|
||||
accent-color: var(--accent);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.btn-delete,
|
||||
.text-action.danger {
|
||||
color: var(--danger);
|
||||
}
|
||||
|
||||
.btn-delete {
|
||||
margin-left: auto;
|
||||
border-color: color-mix(in srgb, var(--danger) 45%, var(--line));
|
||||
background: var(--danger-soft);
|
||||
}
|
||||
|
||||
.account-table-wrap {
|
||||
overflow: auto;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: var(--radius-sm);
|
||||
}
|
||||
|
||||
.managed-account-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
table-layout: fixed;
|
||||
}
|
||||
|
||||
.managed-account-table th,
|
||||
.managed-account-table td {
|
||||
height: 40px;
|
||||
padding: 0 12px;
|
||||
border-bottom: 1px solid var(--line);
|
||||
text-align: left;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.managed-account-table th {
|
||||
color: var(--muted);
|
||||
background: var(--surface-2);
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.managed-account-table tbody tr:last-child td {
|
||||
border-bottom: 0;
|
||||
}
|
||||
|
||||
.managed-account-table tbody tr:hover {
|
||||
background: var(--surface-2);
|
||||
}
|
||||
|
||||
.managed-check {
|
||||
width: 42px;
|
||||
text-align: center !important;
|
||||
}
|
||||
|
||||
.managed-name {
|
||||
overflow: hidden;
|
||||
color: var(--text-strong);
|
||||
font-weight: 500;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.managed-actions {
|
||||
width: 122px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.cookie-state {
|
||||
display: inline-flex;
|
||||
padding: 2px 7px;
|
||||
border: 1px solid var(--success);
|
||||
border-radius: 999px;
|
||||
color: var(--success);
|
||||
background: var(--success-soft);
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
.cookie-state.missing {
|
||||
color: var(--danger);
|
||||
border-color: var(--danger);
|
||||
background: var(--danger-soft);
|
||||
}
|
||||
|
||||
.managed-cookie {
|
||||
display: block;
|
||||
overflow: hidden;
|
||||
color: var(--muted);
|
||||
font: 11px/1.4 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.text-action {
|
||||
padding: 2px 5px;
|
||||
border: 0;
|
||||
background: transparent;
|
||||
color: var(--accent);
|
||||
cursor: pointer;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.text-action:disabled {
|
||||
cursor: not-allowed;
|
||||
opacity: .55;
|
||||
}
|
||||
|
||||
.managed-empty {
|
||||
height: 76px !important;
|
||||
color: var(--muted);
|
||||
text-align: center !important;
|
||||
}
|
||||
|
||||
.account-editor {
|
||||
margin-top: 10px;
|
||||
padding: 12px;
|
||||
border: 1px solid var(--accent-border);
|
||||
border-radius: var(--radius-sm);
|
||||
background: var(--accent-soft);
|
||||
}
|
||||
|
||||
.editor-title {
|
||||
margin-bottom: 9px;
|
||||
color: var(--text-strong);
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.editor-fields {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(140px, .45fr) minmax(0, 1fr);
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.editor-actions {
|
||||
gap: 8px;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.section-title {
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
@@ -525,4 +966,23 @@ const handleOrganize = async () => {
|
||||
.info-item.success {
|
||||
color: var(--success);
|
||||
}
|
||||
|
||||
@media (max-width: 680px) {
|
||||
.ck-manager {
|
||||
padding: 14px;
|
||||
}
|
||||
|
||||
.account-import,
|
||||
.editor-fields {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.account-import-actions {
|
||||
flex-direction: row;
|
||||
}
|
||||
|
||||
.account-import-actions .btn {
|
||||
flex: 1;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -71,6 +71,11 @@ export function useAccounts() {
|
||||
saveSelection()
|
||||
}
|
||||
|
||||
const clearSelection = () => {
|
||||
selectedIndices.value = new Set()
|
||||
saveSelection()
|
||||
}
|
||||
|
||||
const saveSelection = () => {
|
||||
localStorage.setItem('selAccs', JSON.stringify([...selectedIndices.value]))
|
||||
}
|
||||
@@ -200,6 +205,7 @@ export function useAccounts() {
|
||||
reloadAccounts,
|
||||
toggleAccount,
|
||||
toggleAll,
|
||||
clearSelection,
|
||||
restoreSelection,
|
||||
queryAllScore,
|
||||
queryAllXiaodianAssets,
|
||||
|
||||
@@ -41,6 +41,9 @@ type App struct {
|
||||
products []types.Product
|
||||
productsAt time.Time
|
||||
|
||||
// accountsMu 串行化账号文件修改,避免并发导入、编辑或删除互相覆盖。
|
||||
accountsMu sync.Mutex
|
||||
|
||||
closeLog func()
|
||||
closeTrace func()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,201 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"douyu_go/internal/logger"
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
// ImportAccounts 将剪贴板中的账号行追加到账号文件。每行格式必须为
|
||||
// 账号名----密码----Cookie,账号名不能与已有账号重复。
|
||||
func (a *App) ImportAccounts(content string) types.AccountMutationResult {
|
||||
entries, err := parseAccountImport(content)
|
||||
if err != nil {
|
||||
return types.AccountMutationResult{Error: err.Error()}
|
||||
}
|
||||
|
||||
a.accountsMu.Lock()
|
||||
defer a.accountsMu.Unlock()
|
||||
|
||||
accountFile := a.scheduler.AccountFilePath()
|
||||
lines, err := parseAccountFile(accountFile)
|
||||
if err != nil {
|
||||
return types.AccountMutationResult{Error: "读取账号文件失败: " + err.Error()}
|
||||
}
|
||||
|
||||
existingNames := make(map[string]struct{}, len(lines))
|
||||
for _, line := range lines {
|
||||
if line.Name != "" {
|
||||
existingNames[line.Name] = struct{}{}
|
||||
}
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if _, exists := existingNames[entry.Name]; exists {
|
||||
return types.AccountMutationResult{Error: fmt.Sprintf("账号 %q 已存在", entry.Name)}
|
||||
}
|
||||
existingNames[entry.Name] = struct{}{}
|
||||
lines = append(lines, entry)
|
||||
}
|
||||
|
||||
if err := a.saveAccountLines(accountFile, lines); err != nil {
|
||||
return types.AccountMutationResult{Error: err.Error()}
|
||||
}
|
||||
logger.LogEvent("账号", "success", "已从剪贴板导入 %d 个账号", len(entries))
|
||||
return types.AccountMutationResult{Success: true, Count: len(entries)}
|
||||
}
|
||||
|
||||
// UpdateAccount 更新账号名或 Cookie。newCookie 留空时保留文件中的原 Cookie。
|
||||
func (a *App) UpdateAccount(originalName, newName, newCookie string) types.AccountMutationResult {
|
||||
originalName = strings.TrimSpace(originalName)
|
||||
newName = strings.TrimSpace(newName)
|
||||
newCookie = strings.TrimSpace(newCookie)
|
||||
if originalName == "" || newName == "" {
|
||||
return types.AccountMutationResult{Error: "账号名不能为空"}
|
||||
}
|
||||
if invalidAccountField(newName) || strings.ContainsAny(newCookie, "\r\n") {
|
||||
return types.AccountMutationResult{Error: "账号名或 Cookie 包含无效字符"}
|
||||
}
|
||||
|
||||
a.accountsMu.Lock()
|
||||
defer a.accountsMu.Unlock()
|
||||
|
||||
accountFile := a.scheduler.AccountFilePath()
|
||||
lines, err := parseAccountFile(accountFile)
|
||||
if err != nil {
|
||||
return types.AccountMutationResult{Error: "读取账号文件失败: " + err.Error()}
|
||||
}
|
||||
|
||||
targetIndex := -1
|
||||
nameCount := 0
|
||||
for i, line := range lines {
|
||||
if line.Name == originalName {
|
||||
targetIndex = i
|
||||
nameCount++
|
||||
}
|
||||
if line.Name == newName && newName != originalName {
|
||||
return types.AccountMutationResult{Error: fmt.Sprintf("账号 %q 已存在", newName)}
|
||||
}
|
||||
}
|
||||
if targetIndex < 0 {
|
||||
return types.AccountMutationResult{Error: "未找到要编辑的账号"}
|
||||
}
|
||||
if nameCount > 1 {
|
||||
return types.AccountMutationResult{Error: "账号名重复,请先删除重复账号后再编辑"}
|
||||
}
|
||||
|
||||
parts := strings.SplitN(strings.TrimSpace(lines[targetIndex].Raw), "----", 3)
|
||||
if len(parts) != 3 {
|
||||
return types.AccountMutationResult{Error: "原账号格式不正确,无法保留密码段"}
|
||||
}
|
||||
oldCookie := strings.TrimSpace(parts[2])
|
||||
if newCookie == "" {
|
||||
newCookie = oldCookie
|
||||
}
|
||||
if newCookie == "" {
|
||||
return types.AccountMutationResult{Error: "Cookie 不能为空"}
|
||||
}
|
||||
lines[targetIndex] = accountLine{
|
||||
Raw: newName + "----" + strings.TrimSpace(parts[1]) + "----" + newCookie,
|
||||
Name: newName,
|
||||
}
|
||||
|
||||
if err := a.saveAccountLines(accountFile, lines); err != nil {
|
||||
return types.AccountMutationResult{Error: err.Error()}
|
||||
}
|
||||
logger.LogEvent("账号", "success", "账号已更新: %s", newName)
|
||||
return types.AccountMutationResult{Success: true, Count: 1}
|
||||
}
|
||||
|
||||
// DeleteAccounts 从账号文件中删除指定账号,可一次删除多个账号。
|
||||
func (a *App) DeleteAccounts(accountNames []string) types.AccountMutationResult {
|
||||
nameSet := make(map[string]struct{}, len(accountNames))
|
||||
for _, name := range accountNames {
|
||||
if trimmed := strings.TrimSpace(name); trimmed != "" {
|
||||
nameSet[trimmed] = struct{}{}
|
||||
}
|
||||
}
|
||||
if len(nameSet) == 0 {
|
||||
return types.AccountMutationResult{Error: "请选择要删除的账号"}
|
||||
}
|
||||
|
||||
a.accountsMu.Lock()
|
||||
defer a.accountsMu.Unlock()
|
||||
|
||||
accountFile := a.scheduler.AccountFilePath()
|
||||
lines, err := parseAccountFile(accountFile)
|
||||
if err != nil {
|
||||
return types.AccountMutationResult{Error: "读取账号文件失败: " + err.Error()}
|
||||
}
|
||||
kept := make([]accountLine, 0, len(lines))
|
||||
deleted := 0
|
||||
for _, line := range lines {
|
||||
if _, remove := nameSet[line.Name]; remove && !line.Comment {
|
||||
deleted++
|
||||
continue
|
||||
}
|
||||
kept = append(kept, line)
|
||||
}
|
||||
if deleted == 0 {
|
||||
return types.AccountMutationResult{Error: "未找到要删除的账号"}
|
||||
}
|
||||
|
||||
if err := a.saveAccountLines(accountFile, kept); err != nil {
|
||||
return types.AccountMutationResult{Error: err.Error()}
|
||||
}
|
||||
logger.LogEvent("账号", "success", "已删除 %d 个账号", deleted)
|
||||
return types.AccountMutationResult{Success: true, Count: deleted}
|
||||
}
|
||||
|
||||
func parseAccountImport(content string) ([]accountLine, error) {
|
||||
content = strings.ReplaceAll(content, "\r\n", "\n")
|
||||
content = strings.ReplaceAll(content, "\r", "\n")
|
||||
entries := make([]accountLine, 0)
|
||||
names := make(map[string]struct{})
|
||||
for index, raw := range strings.Split(content, "\n") {
|
||||
trimmed := strings.TrimSpace(raw)
|
||||
if trimmed == "" || strings.HasPrefix(trimmed, "#") {
|
||||
continue
|
||||
}
|
||||
parts := strings.SplitN(trimmed, "----", 3)
|
||||
if len(parts) != 3 {
|
||||
return nil, fmt.Errorf("第 %d 行格式不正确,应为 账号名----密码----Cookie", index+1)
|
||||
}
|
||||
name := strings.TrimSpace(parts[0])
|
||||
password := strings.TrimSpace(parts[1])
|
||||
cookie := strings.TrimSpace(parts[2])
|
||||
if name == "" || cookie == "" || invalidAccountField(name) || strings.ContainsAny(cookie, "\r\n") {
|
||||
return nil, fmt.Errorf("第 %d 行账号名或 Cookie 无效", index+1)
|
||||
}
|
||||
if _, exists := names[name]; exists {
|
||||
return nil, fmt.Errorf("剪贴板中账号 %q 重复", name)
|
||||
}
|
||||
names[name] = struct{}{}
|
||||
entries = append(entries, accountLine{Raw: name + "----" + password + "----" + cookie, Name: name})
|
||||
}
|
||||
if len(entries) == 0 {
|
||||
return nil, fmt.Errorf("未识别到可导入的账号")
|
||||
}
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
func invalidAccountField(value string) bool {
|
||||
return strings.ContainsAny(value, "\r\n") || strings.Contains(value, "----")
|
||||
}
|
||||
|
||||
func (a *App) saveAccountLines(accountFile string, lines []accountLine) error {
|
||||
rawLines := make([]string, 0, len(lines))
|
||||
for _, line := range lines {
|
||||
rawLines = append(rawLines, line.Raw)
|
||||
}
|
||||
content := strings.Join(rawLines, "\r\n")
|
||||
if content != "" {
|
||||
content += "\r\n"
|
||||
}
|
||||
if _, err := writeFileWithBackup(accountFile, []byte(content)); err != nil {
|
||||
return fmt.Errorf("更新账号文件失败: %w", err)
|
||||
}
|
||||
a.scheduler.LoadAccounts()
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"douyu_go/internal/seckill"
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
func TestParseAccountImport(t *testing.T) {
|
||||
entries, err := parseAccountImport("# 注释\r\nfoo----pass----cookie-a\nbar----pass----cookie-b\n")
|
||||
if err != nil {
|
||||
t.Fatalf("解析账号失败: %v", err)
|
||||
}
|
||||
if len(entries) != 2 || entries[0].Raw != "foo----pass----cookie-a" {
|
||||
t.Fatalf("解析结果异常: %#v", entries)
|
||||
}
|
||||
|
||||
if _, err := parseAccountImport("bad-line"); err == nil {
|
||||
t.Fatal("格式错误的账号行应该被拒绝")
|
||||
}
|
||||
if _, err := parseAccountImport("foo----pass----cookie\nfoo----pass----cookie2"); err == nil {
|
||||
t.Fatal("重复账号应该被拒绝")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountMutations(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "user.txt")
|
||||
if err := os.WriteFile(path, []byte("# 账号\r\nfoo----pass----cookie-a\r\n"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
scheduler := seckill.New(nil, types.AppPaths{AccountFile: path}, nil, 1, 1, nil, types.SeckillModeNormal)
|
||||
scheduler.LoadAccounts()
|
||||
a := &App{scheduler: scheduler}
|
||||
|
||||
result := a.ImportAccounts("bar----pass----cookie-b")
|
||||
if !result.Success || result.Count != 1 {
|
||||
t.Fatalf("导入失败: %#v", result)
|
||||
}
|
||||
result = a.UpdateAccount("bar", "baz", "")
|
||||
if !result.Success {
|
||||
t.Fatalf("编辑失败: %#v", result)
|
||||
}
|
||||
result = a.DeleteAccounts([]string{"foo"})
|
||||
if !result.Success || result.Count != 1 {
|
||||
t.Fatalf("删除失败: %#v", result)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
content := string(data)
|
||||
if strings.Contains(content, "foo----") || !strings.Contains(content, "baz----pass----cookie-b") {
|
||||
t.Fatalf("账号文件内容异常: %q", content)
|
||||
}
|
||||
if len(content) == 0 || !strings.HasSuffix(content, "\r\n") {
|
||||
t.Fatalf("账号文件未使用规范换行: %q", content)
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,13 @@ type ReloadAccountsResult struct {
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
// AccountMutationResult 是账号文件新增、编辑和删除操作的结果。
|
||||
type AccountMutationResult struct {
|
||||
Success bool `json:"success"`
|
||||
Count int `json:"count"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// CancelScheduleResult 取消定时抢购结果。
|
||||
type CancelScheduleResult struct {
|
||||
Success bool `json:"success"`
|
||||
|
||||
Reference in New Issue
Block a user