Files
JoyD/Claw/client/wechat_app/pages/task/task.js
2026-03-16 15:47:55 +08:00

309 lines
7.4 KiB
JavaScript

// 任务页面逻辑
const { API, util, constants } = require('../../utils/api.js')
const { TASK_STATUS, TASK_TYPE } = constants
Page({
data: {
taskTitle: '',
taskDescription: '',
taskTypeIndex: 0,
priorityIndex: 0,
tasks: [],
filteredTasks: [],
filterStatus: 'all',
canSubmit: false,
taskTypes: [
{ name: '文本处理', value: 'text' },
{ name: '图像识别', value: 'image' },
{ name: '文件分析', value: 'file' },
{ name: '数据查询', value: 'data' },
{ name: '其他', value: 'other' }
],
priorities: [
{ name: '低', value: 'low' },
{ name: '中', value: 'medium' },
{ name: '高', value: 'high' },
{ name: '紧急', value: 'urgent' }
]
},
onLoad() {
this.loadTasks()
},
onShow() {
this.loadTasks()
},
// 监听输入变化
observers: {
'taskTitle, taskDescription': function(title, description) {
this.setData({
canSubmit: title.trim().length > 0 && description.trim().length > 0
})
}
},
// 标题输入
onTitleInput(e) {
this.setData({
taskTitle: e.detail.value
})
},
// 描述输入
onDescriptionInput(e) {
this.setData({
taskDescription: e.detail.value
})
},
// 类型选择
onTypeChange(e) {
this.setData({
taskTypeIndex: parseInt(e.detail.value)
})
},
// 优先级选择
onPriorityChange(e) {
this.setData({
priorityIndex: parseInt(e.detail.value)
})
},
// 提交任务
async submitTask() {
if (!this.data.canSubmit) {
return
}
util.showLoading('提交中...')
try {
const taskData = {
title: this.data.taskTitle.trim(),
description: this.data.taskDescription.trim(),
type: this.data.taskTypes[this.data.taskTypeIndex].value,
priority: this.data.priorities[this.data.priorityIndex].value,
timestamp: Date.now()
}
const result = await API.submitTask(taskData)
util.hideLoading()
if (result.success) {
util.showSuccess('任务提交成功')
// 清空表单
this.setData({
taskTitle: '',
taskDescription: '',
taskTypeIndex: 0,
priorityIndex: 0
})
// 重新加载任务列表
this.loadTasks()
// 滚动到顶部
wx.pageScrollTo({
scrollTop: 0,
duration: 300
})
} else {
util.showError(result.message || '任务提交失败')
}
} catch (error) {
util.hideLoading()
console.error('提交任务失败:', error)
util.showError('提交失败,请重试')
}
},
// 加载任务列表
async loadTasks() {
try {
const result = await API.getTaskList(1, 50)
if (result.success && result.data) {
const tasks = result.data.tasks.map(task => ({
id: task.id,
title: task.title,
description: task.description,
type: task.type,
status: task.status,
priority: task.priority,
createdAt: util.formatRelativeTime(new Date(task.createdAt)),
updatedAt: task.updatedAt ? util.formatRelativeTime(new Date(task.updatedAt)) : '',
result: task.result || '',
progress: task.progress || 0
}))
this.setData({
tasks: tasks
})
this.filterTasks()
}
} catch (error) {
console.error('加载任务列表失败:', error)
util.showError('加载失败')
}
},
// 设置过滤器
setFilter(e) {
const status = e.currentTarget.dataset.status
this.setData({
filterStatus: status
})
this.filterTasks()
},
// 过滤任务
filterTasks() {
const { tasks, filterStatus } = this.data
if (filterStatus === 'all') {
this.setData({
filteredTasks: tasks
})
} else {
const filtered = tasks.filter(task => task.status === filterStatus)
this.setData({
filteredTasks: filtered
})
}
},
// 开始任务
async onStartTask(e) {
const { taskId, title } = e.detail
try {
const confirmed = await util.showModal('确认开始', `确定要开始处理任务"${title}"吗?`)
if (confirmed) {
util.showLoading('处理中...')
// 这里可以调用开始任务的API
// await API.startTask(taskId)
util.hideLoading()
util.showSuccess('任务已开始')
// 重新加载任务列表
this.loadTasks()
}
} catch (error) {
util.hideLoading()
console.error('开始任务失败:', error)
util.showError('操作失败')
}
},
// 完成任务
async onCompleteTask(e) {
const { taskId, title } = e.detail
try {
const confirmed = await util.showModal('确认完成', `确定要标记任务"${title}"为已完成吗?`)
if (confirmed) {
util.showLoading('处理中...')
// 这里可以调用完成任务的API
// await API.completeTask(taskId)
util.hideLoading()
util.showSuccess('任务已完成')
// 重新加载任务列表
this.loadTasks()
}
} catch (error) {
util.hideLoading()
console.error('完成任务失败:', error)
util.showError('操作失败')
}
},
// 重试任务
async onRetryTask(e) {
const { taskId, title } = e.detail
try {
const confirmed = await util.showModal('确认重试', `确定要重试任务"${title}"吗?`)
if (confirmed) {
util.showLoading('处理中...')
// 这里可以调用重试任务的API
// await API.retryTask(taskId)
util.hideLoading()
util.showSuccess('任务已重试')
// 重新加载任务列表
this.loadTasks()
}
} catch (error) {
util.hideLoading()
console.error('重试任务失败:', error)
util.showError('操作失败')
}
},
// 取消任务
async onCancelTask(e) {
const { taskId, title } = e.detail
try {
const confirmed = await util.showModal('确认取消', `确定要取消任务"${title}"吗?`)
if (confirmed) {
util.showLoading('处理中...')
// 这里可以调用取消任务的API
// await API.cancelTask(taskId)
util.hideLoading()
util.showSuccess('任务已取消')
// 重新加载任务列表
this.loadTasks()
}
} catch (error) {
util.hideLoading()
console.error('取消任务失败:', error)
util.showError('操作失败')
}
},
// 查看任务详情
onTaskDetail(e) {
const { taskId, title, description, status, result, createdAt, updatedAt } = e.detail
wx.navigateTo({
url: `/pages/task-detail/task-detail?taskId=${taskId}&title=${encodeURIComponent(title)}&description=${encodeURIComponent(description)}&status=${status}&result=${encodeURIComponent(result)}&createdAt=${encodeURIComponent(createdAt)}&updatedAt=${encodeURIComponent(updatedAt)}`
})
},
// 下拉刷新
async onPullDownRefresh() {
await this.loadTasks()
wx.stopPullDownRefresh()
},
// 分享
onShareAppMessage() {
return {
title: '智控未来 - 任务管理',
path: '/pages/task/task'
}
}
})