Merge branch 'feature/refactor' of github.com:DLmaster361/AUTO_MAA into feature/refactor

This commit is contained in:
DLmaster361
2025-09-02 01:22:07 +08:00
7 changed files with 206 additions and 292 deletions

View File

@@ -146,7 +146,7 @@
: 'orange'
"
>
剩余:
剩余天数:
{{ user.Info.RemainedDay < 1 ? '长期有效' : user.Info.RemainedDay + '天' }}
</a-tag>
@@ -210,6 +210,13 @@
>
剩余关卡: {{ user.Info.Stage_Remain }}
</a-tag>
<a-tag
class="info-tag"
color="magenta"
>
备注: {{ truncateText(user.Info.Notes) }}
</a-tag>
</div>
</div>
</div>
@@ -374,6 +381,11 @@ function get_annihilation_name(annihilation_name) {
}
return '未开启'
}
const truncateText = (text: string, maxLength: number = 20): string => {
if (!text) return ''
return text.length > maxLength ? text.substring(0, maxLength) + '...' : text
}
</script>
<style scoped>

View File

@@ -1,223 +1,216 @@
import { ref, reactive } from 'vue'
import { ref, reactive, onUnmounted } from 'vue'
import { message, notification } from 'ant-design-vue'
import { Service } from '@/api/services/Service'
// WebSocket连接状态
export type WebSocketStatus = '连接中' | '已连接' | '已断开' | '连接错误'
// WebSocket消息类型
export interface WebSocketMessage {
type: 'Update' | 'Message' | 'Info' | 'Signal'
data?: any
message?: string
messageId?: string
export type WebSocketMessageType = 'Signal' | 'Progress' | 'Result' | 'Error' | 'Notify'
// WebSocket基础消息接口
export interface WebSocketBaseMessage {
type: WebSocketMessageType
data: any
}
// 进度消息接口
export interface ProgressMessage {
taskId: string
status: 'running' | 'waiting' | 'finished' | 'failed'
progress: number
msg: string
}
// 结果消息接口
export interface ResultMessage {
taskId: string
status: 'success' | 'failed'
result: any
}
// 错误消息接口
export interface ErrorMessage {
msg: string
code: number
}
// 通知消息接口
export interface NotifyMessage {
title: string
content: string
}
// WebSocket连接配置
export interface WebSocketConfig {
taskId: string
mode: '设置脚本' | '自动代理' | '人工排查'
onMessage?: (data: any) => void
onProgress?: (data: ProgressMessage) => void
onResult?: (data: ResultMessage) => void
onError?: (error: ErrorMessage) => void
onNotify?: (notify: NotifyMessage) => void
onStatusChange?: (status: WebSocketStatus) => void
onError?: (error: string) => void
showNotifications?: boolean
}
export function useWebSocket() {
const connections = ref<Map<string, WebSocket>>(new Map())
const statuses = ref<Map<string, WebSocketStatus>>(new Map())
const BASE_WS_URL = 'ws://localhost:36163/api/core/ws'
// 获取WebSocket地址并建立连接
// 心跳检测
const heartbeat = (ws: WebSocket) => {
const pingMessage = {
type: 'Ping',
data: {}
}
ws.send(JSON.stringify(pingMessage))
}
// 建立WebSocket连接
const connect = async (config: WebSocketConfig): Promise<string | null> => {
try {
// 调用API获取WebSocket连接ID
const response = await Service.addTaskApiDispatchStartPost({
taskId: config.taskId,
mode: config.mode as any,
})
if (response.code !== 200) {
const errorMsg = response.message || '获取WebSocket地址失败'
if (config.onError) {
config.onError(errorMsg)
} else {
message.error(errorMsg)
}
return null
}
const websocketId = response.websocketId
const wsUrl = `ws://localhost:36163/api/dispatch/ws/${websocketId}`
// 建立WebSocket连接
const ws = new WebSocket(wsUrl)
connections.value.set(websocketId, ws)
statuses.value.set(websocketId, '连接中')
// 通知状态变化
if (config.onStatusChange) {
config.onStatusChange('连接中')
}
const ws = new WebSocket(BASE_WS_URL)
const taskId = config.taskId
ws.onopen = () => {
statuses.value.set(websocketId, '已连接')
if (config.onStatusChange) {
config.onStatusChange('已连接')
}
if (config.showNotifications !== false) {
message.success('已连接到服务器')
}
statuses.value.set(taskId, '已连接')
config.onStatusChange?.('已连接')
// 启动心跳
const heartbeatInterval = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
heartbeat(ws)
}
}, 30000)
// 清理定时器
ws.addEventListener('close', () => {
clearInterval(heartbeatInterval)
})
}
ws.onmessage = (event) => {
try {
const data = JSON.parse(event.data)
handleMessage(data, config)
} catch (error) {
console.error('解析WebSocket消息失败:', error)
const errorMsg = `收到无效消息: ${event.data}`
if (config.onError) {
config.onError(errorMsg)
}
}
}
const message = JSON.parse(event.data) as WebSocketBaseMessage
ws.onclose = () => {
statuses.value.set(websocketId, '已断开')
connections.value.delete(websocketId)
if (config.onStatusChange) {
config.onStatusChange('已断开')
}
if (config.showNotifications !== false) {
message.warning('与服务器连接已断开')
switch (message.type) {
case 'Signal':
// 心跳信<E8B7B3><E4BFA1>无需特殊处理
break
case 'Progress':
config.onProgress?.(message.data as ProgressMessage)
break
case 'Result':
config.onResult?.(message.data as ResultMessage)
break
case 'Error':
const errorData = message.data as ErrorMessage
config.onError?.(errorData)
if (config.showNotifications) {
message.error(errorData.msg)
}
break
case 'Notify':
const notifyData = message.data as NotifyMessage
config.onNotify?.(notifyData)
if (config.showNotifications) {
notification.info({
message: notifyData.title,
description: notifyData.content
})
}
break
}
} catch (e) {
console.error('WebSocket消息解析错误:', e)
}
}
ws.onerror = (error) => {
statuses.value.set(websocketId, '连接错误')
const errorMsg = '连接发生错误'
if (config.onError) {
config.onError(errorMsg)
} else if (config.showNotifications !== false) {
message.error(errorMsg)
}
console.error('WebSocket错误:', error)
statuses.value.set(taskId, '连接错误')
config.onStatusChange?.('连接错误')
config.onError?.({ msg: 'WebSocket连接错误', code: 500 })
}
return websocketId
ws.onclose = () => {
statuses.value.set(taskId, '已断开')
config.onStatusChange?.('已断开')
connections.value.delete(taskId)
}
connections.value.set(taskId, ws)
statuses.value.set(taskId, '连接中')
config.onStatusChange?.('连接中')
return taskId
} catch (error) {
const errorMsg = error instanceof Error ? error.message : '连接失败'
if (config.onError) {
config.onError(errorMsg)
} else {
message.error(errorMsg)
config.onError({ msg: errorMsg, code: 500 })
}
return null
}
}
// 处理WebSocket消息
const handleMessage = (data: WebSocketMessage, config: WebSocketConfig) => {
// 调用自定义消息处理器
if (config.onMessage) {
config.onMessage(data)
}
// 默认消息处理
switch (data.type) {
case 'Info':
// 通知信息
let level = 'info'
let content = '未知通知'
// 检查数据中是否有 Error 字段
if (data.data?.Error) {
level = 'error'
content = data.data.Error
} else {
content = data.data?.val || data.data?.message || data.message || '未知通知'
// 发送任务开始指令
const startTask = (taskId: string, params: any) => {
const ws = connections.value.get(taskId)
if (ws && ws.readyState === WebSocket.OPEN) {
const message = {
type: 'StartTask',
data: {
taskId,
params
}
// 显示系统通知(仅在启用通知时)
if (config.showNotifications !== false) {
if (level === 'error') {
notification.error({ message: '任务错误', description: content })
} else if (level === 'warning') {
notification.warning({ message: '任务警告', description: content })
} else if (level === 'success') {
notification.success({ message: '任务成功', description: content })
} else {
notification.info({ message: '任务信息', description: content })
}
}
break
case 'Signal':
// 状态信号
if (data.data?.Accomplish !== undefined) {
const isSuccess = data.data.Accomplish
const statusMsg = isSuccess ? '任务已完成' : '任务已失败'
if (config.showNotifications !== false) {
if (isSuccess) {
notification.success({ message: '任务完成', description: statusMsg })
} else {
notification.error({ message: '任务失败', description: statusMsg })
}
}
}
break
}
ws.send(JSON.stringify(message))
}
}
// 断开连接
const disconnect = (websocketId: string) => {
const ws = connections.value.get(websocketId)
// 更新配置
const updateConfig = (configKey: string, value: any) => {
// 发送给所<E7BB99><E68980><EFBFBD>活跃连接
connections.value.forEach((ws) => {
if (ws.readyState === WebSocket.OPEN) {
const message = {
type: 'UpdateConfig',
data: {
configKey,
value
}
}
ws.send(JSON.stringify(message))
}
})
}
// 关闭连接
const disconnect = (taskId: string) => {
const ws = connections.value.get(taskId)
if (ws) {
ws.close()
connections.value.delete(websocketId)
statuses.value.delete(websocketId)
connections.value.delete(taskId)
statuses.value.delete(taskId)
}
}
// 断开所有连接
// 关闭所有连接
const disconnectAll = () => {
connections.value.forEach((ws) => {
ws.close()
connections.value.forEach((ws, taskId) => {
disconnect(taskId)
})
connections.value.clear()
statuses.value.clear()
}
// 发送消息
const sendMessage = (websocketId: string, message: any) => {
const ws = connections.value.get(websocketId)
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(message))
return true
}
return false
}
// 获取连接状态
const getStatus = (websocketId: string): WebSocketStatus | undefined => {
return statuses.value.get(websocketId)
}
// 检查连接是否存在
const isConnected = (websocketId: string): boolean => {
const ws = connections.value.get(websocketId)
return ws?.readyState === WebSocket.OPEN
}
// 组件卸载时清理所有连接
onUnmounted(() => {
disconnectAll()
})
return {
connections: connections.value,
statuses: statuses.value,
connect,
disconnect,
disconnectAll,
sendMessage,
getStatus,
isConnected,
startTask,
updateConfig,
statuses
}
}

View File

@@ -67,7 +67,7 @@
</a-form-item>
</a-col>
<a-col :span="6">
<a-form-item label=" " style="margin-bottom: 0">
<a-form-item label=" " style="margin-bottom: 0" :colon="false">
<a-space>
<a-button type="primary" @click="handleSearch" :loading="searchLoading">
<template #icon>

View File

@@ -1118,7 +1118,7 @@ const selectLogPath = async () => {
return
}
const path = await window.electronAPI.selectFolder()
const path = await window.electronAPI.selectFile()
if (path) {
generalConfig.Script.LogPath = path
message.success('日志路径选择成功')
@@ -1135,15 +1135,6 @@ const getCardTitle = () => {
</script>
<style scoped>
/* 脚本编辑容器 */
.script-edit-container {
padding: 32px;
min-height: 100vh;
background: var(--ant-color-bg-layout);
display: flex;
flex-direction: column;
}
/* 头部区域 */
.script-edit-header {
display: flex;
@@ -1298,10 +1289,6 @@ const getCardTitle = () => {
box-shadow: 0 0 0 4px rgba(24, 144, 255, 0.1);
}
.modern-select {
border-radius: 8px;
}
.modern-select :deep(.ant-select-selector) {
border: 2px solid var(--ant-color-border) !important;
border-radius: 8px !important;
@@ -1338,14 +1325,6 @@ const getCardTitle = () => {
box-shadow: 0 0 0 4px rgba(24, 144, 255, 0.1);
}
.modern-switch {
background: var(--ant-color-bg-layout);
}
.modern-switch.ant-switch-checked {
background: var(--ant-color-primary);
}
/* 路径输入组 */
.path-input-group {
display: flex;
@@ -1414,14 +1393,6 @@ const getCardTitle = () => {
0 1px 3px rgba(0, 0, 0, 0.4);
}
.save-button {
box-shadow: 0 4px 12px rgba(24, 144, 255, 0.4);
}
.save-button:hover {
box-shadow: 0 6px 16px rgba(24, 144, 255, 0.5);
}
.path-input-group:focus-within {
box-shadow: 0 0 0 4px rgba(24, 144, 255, 0.2);
}
@@ -1442,10 +1413,6 @@ const getCardTitle = () => {
/* 响应式设计 */
@media (max-width: 1200px) {
.script-edit-container {
padding: 24px;
}
.config-card :deep(.ant-card-body) {
padding: 24px;
}
@@ -1456,10 +1423,6 @@ const getCardTitle = () => {
}
@media (max-width: 768px) {
.script-edit-container {
padding: 16px;
}
.script-edit-header {
flex-direction: column;
gap: 16px;

View File

@@ -700,16 +700,10 @@ const handleToggleUserStatus = async (user: User) => {
overflow: hidden;
}
.type-radio-group :deep(.ant-radio-button-wrapper:hover) {
border-color: var(--ant-color-primary);
transform: translateY(-2px);
box-shadow: 0 4px 16px rgba(24, 144, 255, 0.2);
}
.type-radio-group :deep(.ant-radio-button-wrapper-checked) {
border-color: var(--ant-color-primary);
background: var(--ant-color-primary-bg);
box-shadow: 0 4px 16px rgba(24, 144, 255, 0.3);
}
.type-radio-group :deep(.ant-radio-button-wrapper::before) {
@@ -805,11 +799,6 @@ const handleToggleUserStatus = async (user: User) => {
overflow: hidden;
}
.mode-radio-group :deep(.ant-radio-button-wrapper:hover) {
border-color: var(--ant-color-primary);
transform: translateY(-2px);
box-shadow: 0 4px 16px rgba(24, 144, 255, 0.2);
}
.mode-radio-group :deep(.ant-radio-button-wrapper-checked) {
border-color: var(--ant-color-primary);
@@ -832,10 +821,6 @@ const handleToggleUserStatus = async (user: User) => {
transition: all 0.3s ease;
}
.mode-content:hover {
border-color: var(--ant-color-primary);
box-shadow: 0 4px 16px rgba(24, 144, 255, 0.2);
}
/* 模板选择弹窗样式 */
.template-select-modal :deep(.ant-modal-content) {
@@ -885,11 +870,6 @@ const handleToggleUserStatus = async (user: User) => {
background: var(--ant-color-bg-container);
}
.template-item:hover {
border-color: var(--ant-color-primary);
background: var(--ant-color-primary-bg);
box-shadow: 0 4px 16px rgba(24, 144, 255, 0.2);
}
.template-item.selected {
border-color: var(--ant-color-primary);
@@ -1012,23 +992,10 @@ const handleToggleUserStatus = async (user: User) => {
0 1px 3px rgba(0, 0, 0, 0.4);
}
.scripts-content:hover {
box-shadow:
0 8px 30px rgba(0, 0, 0, 0.4),
0 2px 6px rgba(0, 0, 0, 0.5);
}
.add-button {
box-shadow: 0 4px 12px rgba(24, 144, 255, 0.4);
}
.add-button:hover {
box-shadow: 0 6px 16px rgba(24, 144, 255, 0.5);
}
.refresh-button:hover {
box-shadow: 0 4px 12px rgba(255, 255, 255, 0.1);
}
}
/* 响应式设计 */
@@ -1193,10 +1160,6 @@ const handleToggleUserStatus = async (user: User) => {
transition: all 0.2s ease;
}
.template-item:hover {
border-color: var(--ant-color-primary);
background: var(--ant-color-bg-container);
}
.template-item.selected {
border-color: var(--ant-color-primary);
@@ -1214,18 +1177,6 @@ const handleToggleUserStatus = async (user: User) => {
position: relative;
}
.template-icon-wrapper {
width: 48px;
height: 48px;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(135deg, var(--ant-color-primary), var(--ant-color-primary-hover));
border-radius: 16px;
box-shadow: 0 4px 16px rgba(24, 144, 255, 0.2);
position: relative;
overflow: hidden;
}
.template-icon-wrapper::before {
content: '';

View File

@@ -361,13 +361,6 @@ onMounted(() => {
/>
<span class="switch-label">发送六星通知</span>
</div>
<div class="switch-item">
<Switch
v-model:checked="settings.Notify.IfPushPlyer"
@change="checked => handleSettingChange('Notify', 'IfPushPlyer', checked)"
/>
<span class="switch-label">启用PushPlus推送</span>
</div>
</Space>
</div>

View File

@@ -621,6 +621,24 @@
<span class="switch-description">启用后将发送任务通知</span>
</a-col>
</a-row>
<!-- 发送统计/六星等可选通知 -->
<a-row :gutter="24" style="margin-top: 16px">
<a-col :span="6">
<span style="font-weight: 500">通知内容</span>
</a-col>
<a-col :span="18" style="display: flex; gap: 32px">
<a-checkbox
v-model:checked="formData.Notify.IfSendStatistic"
:disabled="loading || !formData.Notify.Enabled"
>发送统计
</a-checkbox>
<a-checkbox
v-model:checked="formData.Notify.IfSendSixStar"
:disabled="loading || !formData.Notify.Enabled"
>六星掉落推送
</a-checkbox>
</a-col>
</a-row>
<!-- 邮件通知 -->
<a-row :gutter="24" style="margin-top: 16px">
@@ -684,24 +702,7 @@
</a-col>
</a-row>
<!-- 发送统计/六星等可选通知 -->
<a-row :gutter="24" style="margin-top: 16px">
<a-col :span="6">
<span style="font-weight: 500">通知内容</span>
</a-col>
<a-col :span="18" style="display: flex; gap: 32px">
<a-checkbox
v-model:checked="formData.Notify.IfSendStatistic"
:disabled="loading || !formData.Notify.Enabled"
>发送统计
</a-checkbox>
<a-checkbox
v-model:checked="formData.Notify.IfSendSixStar"
:disabled="loading || !formData.Notify.Enabled"
>六星掉落推送
</a-checkbox>
</a-col>
</a-row>
</a-card>
</template>
@@ -836,7 +837,7 @@
<a-col :span="12">
<a-form-item name="scriptBeforeTask">
<template #label>
<a-tooltip title="任务执行前要运行的脚本路径或命令">
<a-tooltip title="任务执行前要运行的脚本路径">
<span class="form-label">
任务前脚本
<QuestionCircleOutlined class="help-icon" />
@@ -845,7 +846,7 @@
</template>
<a-input
v-model:value="formData.Info.ScriptBeforeTask"
placeholder="请输入脚本路径或命令"
placeholder="请输入脚本路径"
:disabled="loading || !formData.Info.IfScriptBeforeTask"
size="large"
/>
@@ -854,7 +855,7 @@
<a-col :span="12">
<a-form-item name="scriptAfterTask">
<template #label>
<a-tooltip title="任务执行后要运行的脚本路径或命令">
<a-tooltip title="任务执行后要运行的脚本路径">
<span class="form-label">
任务后脚本
<QuestionCircleOutlined class="help-icon" />
@@ -863,7 +864,7 @@
</template>
<a-input
v-model:value="formData.Info.ScriptAfterTask"
placeholder="请输入脚本路径或命令"
placeholder="请输入脚本路径"
:disabled="loading || !formData.Info.IfScriptAfterTask"
size="large"
/>
@@ -882,6 +883,19 @@
<span class="switch-description">启用后将发送任务通知</span>
</a-col>
</a-row>
<!-- 发送统计等可选通知 -->
<a-row :gutter="24" style="margin-top: 16px">
<a-col :span="6">
<span style="font-weight: 500">通知内容</span>
</a-col>
<a-col :span="18">
<a-checkbox
v-model:checked="formData.Notify.IfSendStatistic"
:disabled="loading || !formData.Notify.Enabled"
>发送统计
</a-checkbox>
</a-col>
</a-row>
<!-- 邮件通知 -->
<a-row :gutter="24" style="margin-top: 16px">
@@ -945,19 +959,7 @@
</a-col>
</a-row>
<!-- 发送统计等可选通知 -->
<a-row :gutter="24" style="margin-top: 16px">
<a-col :span="6">
<span style="font-weight: 500">通知内容</span>
</a-col>
<a-col :span="18">
<a-checkbox
v-model:checked="formData.Notify.IfSendStatistic"
:disabled="loading || !formData.Notify.Enabled"
>发送统计
</a-checkbox>
</a-col>
</a-row>
</a-card>
</template>
</a-form>