TP6指定时间段内填写指定内容?

使用框架:TP6

根据时间段下发任务到用户,就是后台管理设定本月份要上报资料,然后用户在这个月的时间段里面就要上报指定资料。

我完全没有思路,不知道有没有什么大神提供一下思路给我!!!

我试过了订阅推送,但实际操作不太懂,也没有什么概念!

阅读 222
1 个回答

数据库设计

  1. 任务表 (task)
    sql

CREATE TABLE task (
id int(11) NOT NULL AUTO_INCREMENT,
title varchar(255) NOT NULL COMMENT '任务标题',
description text COMMENT '任务描述',
start_time datetime NOT NULL COMMENT '任务开始时间',
end_time datetime NOT NULL COMMENT '任务结束时间',
status tinyint(1) DEFAULT '1' COMMENT '状态:1-启用,0-禁用',
created_at datetime DEFAULT NULL COMMENT '创建时间',
updated_at datetime DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='任务表';

  1. 用户任务关联表 (user_task)
    sql
    CREATE TABLE user_task (
    id int(11) NOT NULL AUTO_INCREMENT,
    user_id int(11) NOT NULL COMMENT '用户ID',
    task_id int(11) NOT NULL COMMENT '任务ID',
    status tinyint(1) DEFAULT '0' COMMENT '状态:0-未完成,1-已完成',
    report_content text COMMENT '上报内容',
    report_time datetime DEFAULT NULL COMMENT '上报时间',
    created_at datetime DEFAULT NULL COMMENT '创建时间',
    updated_at datetime DEFAULT NULL COMMENT '更新时间',
    PRIMARY KEY (id),
    KEY user_id (user_id),
    KEY task_id (task_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户任务关联表';
  2. 任务管理控制器 (TaskController.php)
    <?php
    namespace app\admin\controller;

use think\facade\View;
use think\Request;
use app\common\model\Task as TaskModel;

class TaskController
{

// 任务列表
public function index()
{
    $tasks = TaskModel::order('id', 'desc')->paginate(10);
    return View::fetch('index', ['tasks' => $tasks]);
}

// 创建任务
public function create()
{
    return View::fetch('create');
}

// 保存任务
public function save(Request $request)
{
    $data = $request->post();
    
    $validate = new \app\admin\validate\Task;
    if (!$validate->check($data)) {
        return json(['code' => 0, 'msg' => $validate->getError()]);
    }
    
    try {
        $task = TaskModel::create($data);
        return json(['code' => 1, 'msg' => '任务创建成功']);
    } catch (\Exception $e) {
        return json(['code' => 0, 'msg' => '任务创建失败: '.$e->getMessage()]);
    }
}

// 编辑任务
public function edit($id)
{
    $task = TaskModel::find($id);
    return View::fetch('edit', ['task' => $task]);
}

// 更新任务
public function update(Request $request, $id)
{
    $data = $request->post();
    
    $validate = new \app\admin\validate\Task;
    if (!$validate->check($data)) {
        return json(['code' => 0, 'msg' => $validate->getError()]);
    }
    
    try {
        TaskModel::update($data, ['id' => $id]);
        return json(['code' => 1, 'msg' => '任务更新成功']);
    } catch (\Exception $e) {
        return json(['code' => 0, 'msg' => '任务更新失败: '.$e->getMessage()]);
    }
}

// 删除任务
public function delete($id)
{
    try {
        TaskModel::destroy($id);
        return json(['code' => 1, 'msg' => '任务删除成功']);
    } catch (\Exception $e) {
        return json(['code' => 0, 'msg' => '任务删除失败: '.$e->getMessage()]);
    }
}

}

  1. 任务验证器 (Task.php)
    <?php
    namespace app\admin\validate;

use think\Validate;

class Task extends Validate
{

protected $rule = [
    'title'      => 'require|max:100',
    'start_time' => 'require|date',
    'end_time'   => 'require|date|after:start_time',
];

protected $message = [
    'title.require'      => '任务标题不能为空',
    'title.max'          => '任务标题不能超过100个字符',
    'start_time.require' => '开始时间不能为空',
    'start_time.date'    => '开始时间格式不正确',
    'end_time.require'   => '结束时间不能为空',
    'end_time.date'      => '结束时间格式不正确',
    'end_time.after'     => '结束时间必须大于开始时间',
];

}

  1. 用户任务控制器 (UserTaskController.php)
    <?php
    namespace app\controller;

use think\facade\View;
use think\Request;
use app\common\model\Task as TaskModel;
use app\common\model\UserTask as UserTaskModel;

class UserTaskController
{

// 获取当前用户的任务列表
public function index(Request $request)
{
    $user_id = $request->user_id; // 假设从请求中获取用户ID
    
    // 获取当前时间
    $now = date('Y-m-d H:i:s');
    
    // 查询当前用户的有效任务
    $tasks = TaskModel::where('status', 1)
        ->where('start_time', '<=', $now)
        ->where('end_time', '>=', $now)
        ->order('id', 'desc')
        ->select();
        
    // 检查用户是否已经完成这些任务
    foreach ($tasks as &$task) {
        $userTask = UserTaskModel::where('user_id', $user_id)
            ->where('task_id', $task->id)
            ->find();
            
        $task->user_task_status = $userTask ? $userTask->status : 0;
        $task->user_task_id = $userTask ? $userTask->id : 0;
    }
    
    return View::fetch('index', ['tasks' => $tasks]);
}

// 上报任务
public function report(Request $request)
{
    $user_id = $request->user_id; // 假设从请求中获取用户ID
    $task_id = $request->post('task_id');
    $content = $request->post('content');
    
    // 验证任务是否存在且在有效期内
    $now = date('Y-m-d H:i:s');
    $task = TaskModel::where('id', $task_id)
        ->where('status', 1)
        ->where('start_time', '<=', $now)
        ->where('end_time', '>=', $now)
        ->find();
        
    if (!$task) {
        return json(['code' => 0, 'msg' => '任务不存在或已过期']);
    }
    
    try {
        // 检查是否已经上报过
        $userTask = UserTaskModel::where('user_id', $user_id)
            ->where('task_id', $task_id)
            ->find();
            
        if ($userTask) {
            // 更新上报内容
            $userTask->report_content = $content;
            $userTask->report_time = $now;
            $userTask->status = 1;
            $userTask->save();
        } else {
            // 创建新的上报记录
            UserTaskModel::create([
                'user_id' => $user_id,
                'task_id' => $task_id,
                'status' => 1,
                'report_content' => $content,
                'report_time' => $now
            ]);
        }
        
        return json(['code' => 1, 'msg' => '任务上报成功']);
    } catch (\Exception $e) {
        return json(['code' => 0, 'msg' => '任务上报失败: '.$e->getMessage()]);
    }
}

}

  1. 模型文件
    Task.php (任务模型)
    <?php
    namespace app\common\model;

use think\Model;

class Task extends Model
{

// 自动时间戳
protected $autoWriteTimestamp = 'datetime';

// 定义时间戳字段名
protected $createTime = 'created_at';
protected $updateTime = 'updated_at';

}

UserTask.php (用户任务模型)
<?php
namespace app\common\model;

use think\Model;

class UserTask extends Model
{

// 自动时间戳
protected $autoWriteTimestamp = 'datetime';

// 定义时间戳字段名
protected $createTime = 'created_at';
protected $updateTime = 'updated_at';

// 关联任务
public function task()
{
    return $this->belongsTo('app\common\model\Task', 'task_id');
}

}

前端页面示例

  1. 任务列表页面 (admin/task/index.html)
    <!-- 省略HTML结构 -->

    {volist name="tasks" id="task"} {/volist}
    ID 任务标题 开始时间 结束时间 状态 操作
    {$task.id} {$task.title} {$task.start_time} {$task.end_time} {$task.status ? '启用' : '禁用'} $task.id])}">编辑 删除

    {$tasks|raw}

<script>
function deleteTask(id) {

if (confirm('确定要删除这个任务吗?')) {
    $.post("{:url('admin/task/delete')}", {id: id}, function(res) {
        if (res.code == 1) {
            location.reload();
        } else {
            alert(res.msg);
        }
    });
}

}
</script>

  1. 用户任务上报页面 (user/task/index.html)
    <!-- 省略HTML结构 -->
    <div class="task-list">
    {volist name="tasks" id="task"}
    <div class="task-item">

     <h3>{$task.title}</h3>
     <p>任务时间: {$task.start_time} 至 {$task.end_time}</p>
     <p>状态: {$task.user_task_status ? '已完成' : '未完成'}</p>
     
     {if $task.user_task_status == 0}
     <div class="report-form">
         <textarea class="report-content" placeholder="请输入上报内容"></textarea>
         <button class="btn-report" data-task-id="{$task.id}">上报</button>
     </div>
     {else}
     <div class="reported-content">
         <p>上报内容: {$task.report_content}</p>
         <p>上报时间: {$task.report_time}</p>
     </div>
     {/if}

    </div>
    {/volist}
    </div>

<script>
$(function() {

$('.btn-report').click(function() {
    var task_id = $(this).data('task-id');
    var content = $(this).siblings('.report-content').val();
    
    if (!content) {
        alert('请填写上报内容');
        return;
    }
    
    $.post("{:url('user/task/report')}", {
        task_id: task_id,
        content: content
    }, function(res) {
        if (res.code == 1) {
            location.reload();
        } else {
            alert(res.msg);
        }
    });
});

});
</script>

定时任务检查
可以添加一个定时任务,每天检查是否有新任务需要下发:
// 在 app/command/TaskCheck.php 中创建命令
namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\common\model\Task;
use app\common\model\User;

class TaskCheck extends Command
{

protected function configure()
{
    $this->setName('task:check')->setDescription('检查并下发新任务');
}

protected function execute(Input $input, Output $output)
{
    // 获取当前时间
    $now = date('Y-m-d H:i:s');
    
    // 查询今天开始的任务
    $tasks = Task::where('status', 1)
        ->where('start_time', '<=', $now)
        ->where('end_time', '>=', $now)
        ->select();
        
    // 获取所有活跃用户
    $users = User::where('status', 1)->select();
    
    // 为每个用户创建任务关联
    foreach ($tasks as $task) {
        foreach ($users as $user) {
            // 检查是否已经存在关联
            $exists = \app\common\model\UserTask::where('user_id', $user->id)
                ->where('task_id', $task->id)
                ->find();
                
            if (!$exists) {
                // 创建新的用户任务关联
                \app\common\model\UserTask::create([
                    'user_id' => $user->id,
                    'task_id' => $task->id,
                    'status' => 0
                ]);
            }
        }
    }
    
    $output->writeln('任务检查完成');
}

}

然后在 crontab 中添加:
0 0 * php think task:check

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题