数据同步

This commit is contained in:
2026-01-05 10:19:51 +08:00
parent ba0ebcf273
commit 408c6a2029
206 changed files with 52458 additions and 0 deletions

View File

@@ -0,0 +1,212 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { DataCollectionTask, DataSource } from '@/types'
import * as dataCollectionApi from '@/api/dataCollection'
export const useDataCollectionStore = defineStore('dataCollection', () => {
const tasks = ref<DataCollectionTask[]>([])
const currentTask = ref<DataCollectionTask | null>(null)
const dataSources = ref<DataSource[]>([])
const loading = ref(false)
// 获取任务列表
const fetchTasks = async (params?: {
name?: string
status?: string
page?: number
page_size?: number
}) => {
loading.value = true
try {
const response = await dataCollectionApi.getDataCollectionTaskList(params || {})
tasks.value = response.data.tasks
return response.data
} catch (error) {
console.error('获取任务列表失败:', error)
throw error
} finally {
loading.value = false
}
}
// 获取任务详情
const fetchTaskDetail = async (taskId: string) => {
loading.value = true
try {
const response = await dataCollectionApi.getDataCollectionTaskDetail(taskId)
currentTask.value = response.data
return response.data
} catch (error) {
console.error('获取任务详情失败:', error)
throw error
} finally {
loading.value = false
}
}
// 创建任务
const createTask = async (data: Partial<DataCollectionTask>) => {
try {
const response = await dataCollectionApi.createDataCollectionTask(data)
await fetchTasks()
return response.data
} catch (error) {
console.error('创建任务失败:', error)
throw error
}
}
// 更新任务
const updateTask = async (taskId: string, data: Partial<DataCollectionTask>) => {
try {
const response = await dataCollectionApi.updateDataCollectionTask(taskId, data)
await fetchTasks()
return response.data
} catch (error) {
console.error('更新任务失败:', error)
throw error
}
}
// 删除任务
const deleteTask = async (taskId: string, params?: {
name?: string
status?: string
page?: number
page_size?: number
}) => {
try {
await dataCollectionApi.deleteDataCollectionTask(taskId)
// 刷新列表时保持当前的筛选和分页状态,并返回结果以便更新分页信息
return await fetchTasks(params)
} catch (error) {
console.error('删除任务失败:', error)
throw error
}
}
// 启动任务
const startTask = async (taskId: string) => {
try {
await dataCollectionApi.startDataCollectionTask(taskId)
await fetchTaskDetail(taskId)
} catch (error) {
console.error('启动任务失败:', error)
throw error
}
}
// 暂停任务
const pauseTask = async (taskId: string) => {
try {
await dataCollectionApi.pauseDataCollectionTask(taskId)
await fetchTaskDetail(taskId)
} catch (error) {
console.error('暂停任务失败:', error)
throw error
}
}
// 停止任务
const stopTask = async (taskId: string) => {
try {
await dataCollectionApi.stopDataCollectionTask(taskId)
await fetchTaskDetail(taskId)
} catch (error) {
console.error('停止任务失败:', error)
throw error
}
}
// 获取数据源列表
const fetchDataSources = async () => {
try {
const response = await dataCollectionApi.getDataSources()
dataSources.value = response.data
return response.data
} catch (error) {
console.error('获取数据源列表失败:', error)
throw error
}
}
// 获取任务进度
const fetchTaskProgress = async (taskId: string) => {
try {
const response = await dataCollectionApi.getDataCollectionTaskProgress(taskId)
if (currentTask.value && currentTask.value.task_id === taskId) {
currentTask.value.progress = response.data
}
return response.data
} catch (error) {
console.error('获取任务进度失败:', error)
throw error
}
}
// 复制任务
const duplicateTask = async (taskId: string) => {
try {
// 获取原任务详情
const originalTask = await fetchTaskDetail(taskId)
if (!originalTask) {
throw new Error('任务不存在')
}
// 复制任务数据,但清除运行时数据
const taskData: Partial<DataCollectionTask> = {
name: `${originalTask.name}_副本`,
description: originalTask.description || '',
data_source_id: originalTask.data_source_id,
database: originalTask.database,
collection: originalTask.collection || null,
collections: originalTask.collections || null,
target_type: originalTask.target_type || 'generic',
target_data_source_id: originalTask.target_data_source_id || null,
target_database: originalTask.target_database || null,
target_collection: originalTask.target_collection || null,
mode: originalTask.mode || 'batch',
field_mappings: originalTask.field_mappings || [],
collection_field_mappings: originalTask.collection_field_mappings || {},
lookups: originalTask.lookups || [],
collection_lookups: originalTask.collection_lookups || {},
filter_conditions: originalTask.filter_conditions || [],
schedule: originalTask.schedule || { enabled: false, cron: '' },
status: 'pending' // 复制后的任务状态为待启动
}
// 创建新任务
const response = await dataCollectionApi.createDataCollectionTask(taskData)
await fetchTasks()
return response.data
} catch (error) {
console.error('复制任务失败:', error)
throw error
}
}
// 重置当前任务
const resetCurrentTask = () => {
currentTask.value = null
}
return {
tasks,
currentTask,
dataSources,
loading,
fetchTasks,
fetchTaskDetail,
createTask,
updateTask,
deleteTask,
startTask,
pauseTask,
stopTask,
fetchDataSources,
fetchTaskProgress,
duplicateTask,
resetCurrentTask
}
})

View File

@@ -0,0 +1,107 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import * as dataSourceApi from '@/api/dataSource'
import type { DataSource } from '@/api/dataSource'
import { ElMessage } from 'element-plus'
export const useDataSourceStore = defineStore('dataSource', () => {
const dataSources = ref<DataSource[]>([])
const currentDataSource = ref<DataSource | null>(null)
// 获取数据源列表
const fetchDataSources = async (params?: {
type?: string
status?: number
name?: string
page?: number
page_size?: number
}) => {
try {
const response = await dataSourceApi.getDataSourceList(params)
dataSources.value = response.data.data_sources
return response.data
} catch (error: any) {
ElMessage.error(error.message || '获取数据源列表失败')
throw error
}
}
// 获取数据源详情
const fetchDataSourceDetail = async (dataSourceId: string) => {
try {
const response = await dataSourceApi.getDataSourceDetail(dataSourceId)
currentDataSource.value = response.data
return response.data
} catch (error: any) {
ElMessage.error(error.message || '获取数据源详情失败')
throw error
}
}
// 创建数据源
const createDataSource = async (data: Partial<DataSource>) => {
try {
const response = await dataSourceApi.createDataSource(data)
ElMessage.success('数据源创建成功')
return response.data
} catch (error: any) {
ElMessage.error(error.message || '创建数据源失败')
throw error
}
}
// 更新数据源
const updateDataSource = async (dataSourceId: string, data: Partial<DataSource>) => {
try {
await dataSourceApi.updateDataSource(dataSourceId, data)
ElMessage.success('数据源更新成功')
} catch (error: any) {
ElMessage.error(error.message || '更新数据源失败')
throw error
}
}
// 删除数据源
const deleteDataSource = async (dataSourceId: string) => {
try {
await dataSourceApi.deleteDataSource(dataSourceId)
ElMessage.success('数据源删除成功')
} catch (error: any) {
ElMessage.error(error.message || '删除数据源失败')
throw error
}
}
// 测试连接
const testConnection = async (data: {
type: string
host: string
port: number
database: string
username?: string
password?: string
auth_source?: string
options?: Record<string, any>
}) => {
try {
const response = await dataSourceApi.testDataSourceConnection(data)
// 只返回结果,不显示消息,由调用方决定是否显示
return response.data.connected
} catch (error: any) {
ElMessage.error(error.message || '连接测试失败')
return false
}
}
return {
dataSources,
currentDataSource,
fetchDataSources,
fetchDataSourceDetail,
createDataSource,
updateDataSource,
deleteDataSource,
testConnection,
}
})

View File

@@ -0,0 +1,6 @@
// Store 统一导出
export { useUserStore } from './user'
export * from './dataCollection'
export * from './tagTask'
export * from './tagDefinition'
export { useDataSourceStore } from './dataSource'

View File

@@ -0,0 +1,105 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { TagDefinition } from '@/types'
import * as tagDefinitionApi from '@/api/tagDefinition'
export const useTagDefinitionStore = defineStore('tagDefinition', () => {
const definitions = ref<TagDefinition[]>([])
const currentDefinition = ref<TagDefinition | null>(null)
const loading = ref(false)
// 获取标签定义列表
const fetchDefinitions = async (params?: {
name?: string
category?: string
status?: number
page?: number
page_size?: number
}) => {
loading.value = true
try {
const response = await tagDefinitionApi.getTagDefinitionList(params)
definitions.value = response.data.definitions
return response.data
} catch (error) {
console.error('获取标签定义列表失败:', error)
throw error
} finally {
loading.value = false
}
}
// 获取标签定义详情
const fetchDefinitionDetail = async (tagId: string) => {
loading.value = true
try {
const response = await tagDefinitionApi.getTagDefinitionDetail(tagId)
currentDefinition.value = response.data
return response.data
} catch (error) {
console.error('获取标签定义详情失败:', error)
throw error
} finally {
loading.value = false
}
}
// 创建标签定义
const createDefinition = async (data: Partial<TagDefinition>) => {
try {
const response = await tagDefinitionApi.createTagDefinition(data)
await fetchDefinitions()
return response.data
} catch (error) {
console.error('创建标签定义失败:', error)
throw error
}
}
// 更新标签定义
const updateDefinition = async (tagId: string, data: Partial<TagDefinition>) => {
try {
const response = await tagDefinitionApi.updateTagDefinition(tagId, data)
await fetchDefinitions()
return response.data
} catch (error) {
console.error('更新标签定义失败:', error)
throw error
}
}
// 删除标签定义
const deleteDefinition = async (tagId: string) => {
try {
await tagDefinitionApi.deleteTagDefinition(tagId)
await fetchDefinitions()
} catch (error) {
console.error('删除标签定义失败:', error)
throw error
}
}
// 获取启用的标签定义列表(用于下拉选择)
const getActiveDefinitions = async () => {
return await fetchDefinitions({ status: 0 })
}
// 重置当前标签定义
const resetCurrentDefinition = () => {
currentDefinition.value = null
}
return {
definitions,
currentDefinition,
loading,
fetchDefinitions,
fetchDefinitionDetail,
createDefinition,
updateDefinition,
deleteDefinition,
getActiveDefinitions,
resetCurrentDefinition
}
})

View File

@@ -0,0 +1,154 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { TagTask, TaskExecution } from '@/types'
import * as tagTaskApi from '@/api/tagTask'
export const useTagTaskStore = defineStore('tagTask', () => {
const tasks = ref<TagTask[]>([])
const currentTask = ref<TagTask | null>(null)
const executions = ref<TaskExecution[]>([])
const loading = ref(false)
// 获取任务列表
const fetchTasks = async (params?: {
name?: string
task_type?: string
status?: string
page?: number
page_size?: number
}) => {
loading.value = true
try {
const response = await tagTaskApi.getTagTaskList(params || {})
tasks.value = response.data.tasks
return response.data
} catch (error) {
console.error('获取任务列表失败:', error)
throw error
} finally {
loading.value = false
}
}
// 获取任务详情
const fetchTaskDetail = async (taskId: string) => {
loading.value = true
try {
const response = await tagTaskApi.getTagTaskDetail(taskId)
currentTask.value = response.data
return response.data
} catch (error) {
console.error('获取任务详情失败:', error)
throw error
} finally {
loading.value = false
}
}
// 创建任务
const createTask = async (data: Partial<TagTask>) => {
try {
const response = await tagTaskApi.createTagTask(data)
await fetchTasks()
return response.data
} catch (error) {
console.error('创建任务失败:', error)
throw error
}
}
// 更新任务
const updateTask = async (taskId: string, data: Partial<TagTask>) => {
try {
const response = await tagTaskApi.updateTagTask(taskId, data)
await fetchTasks()
return response.data
} catch (error) {
console.error('更新任务失败:', error)
throw error
}
}
// 删除任务
const deleteTask = async (taskId: string) => {
try {
await tagTaskApi.deleteTagTask(taskId)
await fetchTasks()
} catch (error) {
console.error('删除任务失败:', error)
throw error
}
}
// 启动任务
const startTask = async (taskId: string) => {
try {
await tagTaskApi.startTagTask(taskId)
await fetchTaskDetail(taskId)
} catch (error) {
console.error('启动任务失败:', error)
throw error
}
}
// 暂停任务
const pauseTask = async (taskId: string) => {
try {
await tagTaskApi.pauseTagTask(taskId)
await fetchTaskDetail(taskId)
} catch (error) {
console.error('暂停任务失败:', error)
throw error
}
}
// 停止任务
const stopTask = async (taskId: string) => {
try {
await tagTaskApi.stopTagTask(taskId)
await fetchTaskDetail(taskId)
} catch (error) {
console.error('停止任务失败:', error)
throw error
}
}
// 获取执行记录
const fetchExecutions = async (taskId: string, params?: {
page?: number
page_size?: number
}) => {
try {
const response = await tagTaskApi.getTagTaskExecutions(taskId, params)
executions.value = response.data.executions
return response.data
} catch (error) {
console.error('获取执行记录失败:', error)
throw error
}
}
// 重置当前任务
const resetCurrentTask = () => {
currentTask.value = null
executions.value = []
}
return {
tasks,
currentTask,
executions,
loading,
fetchTasks,
fetchTaskDetail,
createTask,
updateTask,
deleteTask,
startTask,
pauseTask,
stopTask,
fetchExecutions,
resetCurrentTask
}
})

View File

@@ -0,0 +1,35 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useUserStore = defineStore('user', () => {
// 用户 token
const token = ref<string | null>(null)
// 初始化 token从 localStorage 读取)
const initToken = () => {
const storedToken = localStorage.getItem('token')
if (storedToken) {
token.value = storedToken
}
}
// 设置 token
const setToken = (newToken: string) => {
token.value = newToken
localStorage.setItem('token', newToken)
}
// 清除用户信息
const clearUser = () => {
token.value = null
localStorage.removeItem('token')
}
return {
token,
initToken,
setToken,
clearUser
}
})