BH6PEI'博客

临时保存

你说得完全没错!✅ 宝塔面板下 TP5 框架已安装完成、首页能正常显示欢迎词,说明基础环境 100% 就绪,不用再纠结安装相关的问题,现在可以直接启动第二步:开发你专属的日报生成 + 打印项目,我把核心步骤拆成「极简可落地」的 4 步,全程适配你的宝塔环境,每一步都能直接操作:


核心目标:把 “固定格式日报生成 + 打印” 功能落地到 TP5 项目中

第一步:创建日报功能的核心控制器(DailyReport.php)

控制器是功能的 “大脑”,负责处理 “新增日报、查看列表、打印日报” 的逻辑:


登录宝塔面板 → 「文件」→ 找到路径:/www/wwwroot/dgcms/application/index/controller;


右键controller文件夹 → 「新建文件」,文件名填DailyReport.php(首字母 D 必须大写);


打开文件,粘贴以下代码(已适配你的dgcms数据表):


php



运行






<?phpnamespace app\index\controller;use think\Controller;use app\index\model\DailyReport as ReportModel;class DailyReport extends Controller{

    // 日报列表页(查看所有生成的日报)

    public function index()

    {

        $list = ReportModel::order('report_date desc, create_time desc')->select();

        $this->assign('list', $list);

        return $this->fetch();

    }

    

    // 新增日报(生成固定格式内容)

    public function add()

    {

        if (request()->isPost()) {

            // 获取表单提交的数据

            $data = input('post.');

            // 极简验证:核心字段不能为空

            if (empty($data['user_name']) || empty($data['dept']) || empty($data['report_date']) || empty($data['today_work'])) {

                $this->error('填写人、部门、日期、今日完成不能为空!');

            }

            // 保存数据到dgcms表

            $report = new ReportModel();

            $result = $report->allowField(true)->save($data);

            if ($result) {

                $this->success('日报生成成功!', url('index'));

            } else {

                $this->error('日报生成失败,请重试!');

            }

        }

        // 默认填充今日日期

        $this->assign('today', date('Y-m-d'));

        return $this->fetch();

    }

    

    // 打印专用页面(固定格式,适配打印)

    public function print($id)

    {

        $report = ReportModel::find($id);

        if (!$report) {

            $this->error('日报不存在!');

        }

        $this->assign('report', $report);

        return $this->fetch();

    }}

点击「保存」,控制器就创建好了。


第二步:创建日报数据模型(DailyReport.php)

模型负责和数据库(dgcms 表)交互,处理数据的读写:


宝塔「文件」→ 路径:/www/wwwroot/dgcms/application/index/model(没有model文件夹就右键新建);


右键model文件夹 → 「新建文件」,文件名DailyReport.php(首字母 D 大写);


粘贴以下代码(关联你的dgcms数据表):


php



运行






<?phpnamespace app\index\model;use think\Model;class DailyReport extends Model{

    // 关联你创建的dgcms数据表(核心适配)

    protected $table = 'dgcms';

    // 关闭自动更新时间,只保留创建时间

    protected $updateTime = false;

    // 自动填充创建时间

    protected $auto = ['create_time'];

    

    // 自动生成创建时间(时间戳)

    protected function setCreateTimeAttr()

    {

        return time();

    }

    

    // 格式化日期显示(比如2026-03-10 → 2026年03月10日)

    protected function getReportDateAttr($value)

    {

        return date('Y年m月d日', strtotime($value));

    }}

点击「保存」,模型就配置完成。


第三步:创建视图页面(新增 / 列表 / 打印,固定格式)

视图是 “页面界面”,用户看到的表单、列表、打印页都在这里:


宝塔「文件」→ 路径:/www/wwwroot/dgcms/application/index/view;


右键view文件夹 → 「新建文件夹」,文件夹名daily_report(小写,和控制器名对应);


在daily_report文件夹里创建 3 个文件,分别粘贴对应代码:


① 新增日报页面(add.html)—— 填写日报的表单

新建文件add.html,粘贴:


html



预览






<!DOCTYPE html><html><head>

    <meta charset="UTF-8">

    <title>生成日报 - 我的极简日报系统</title>

    <style>

        .form-box {width: 600px; margin: 50px auto; padding: 20px; border: 1px solid #ccc;}

        .form-item {margin-bottom: 15px;}

        .form-item label {display: inline-block; width: 100px; text-align: right; margin-right: 10px;}

        .form-item input, .form-item select {width: 300px; padding: 5px;}

        .form-item textarea {width: 400px; height: 100px; padding: 5px; vertical-align: top;}

        .btn {padding: 8px 20px; background: #009688; color: #fff; border: none; cursor: pointer;}

    </style></head><body>

    <div>

        <h2 style="text-align: center; margin-bottom: 20px;">固定格式日报生成</h2>

        <form action="{:url('add')}" method="post">

            <div>

                <label>填写人:</label>

                <input type="text" name="user_name" placeholder="请输入姓名" required>

            </div>

            <div>

                <label>部门:</label>

                <input type="text" name="dept" placeholder="请输入部门" required>

            </div>

            <div>

                <label>日报日期:</label>

                <input type="date" name="report_date" value="{$today}" required>

            </div>

            <div>

                <label>今日完成:</label>

                <textarea name="today_work" placeholder="请填写今日完成的工作(固定格式)" required></textarea>

            </div>

            <div>

                <label>明日计划:</label>

                <textarea name="tomorrow_plan" placeholder="请填写明日工作计划(固定格式)"></textarea>

            </div>

            <div>

                <label>备注:</label>

                <input type="text" name="remark" placeholder="其他说明">

            </div>

            <div style="text-align: center;">

                <button type="submit">生成日报</button>

            </div>

        </form>

    </div></body></html>

② 日报列表页面(index.html)—— 查看所有日报,可打印

新建文件index.html,粘贴:


html



预览






<!DOCTYPE html><html><head>

    <meta charset="UTF-8">

    <title>日报列表 - 我的极简日报系统</title>

    <style>

        .container {width: 90%; margin: 50px auto;}

        .btn-add {display: inline-block; padding: 8px 20px; background: #009688; color: #fff; text-decoration: none; margin-bottom: 20px;}

        table {width: 100%; border-collapse: collapse;}

        th, td {border: 1px solid #ccc; padding: 10px; text-align: center;}

        th {background: #f5f5f5;}

        .btn-print {color: #009688; text-decoration: none;}

    </style></head><body>

    <div>

        <a href="{:url('add')}">生成新日报</a>

        <table>

            <tr>

                <th>ID</th>

                <th>填写人</th>

                <th>部门</th>

                <th>日报日期</th>

                <th>操作</th>

            </tr>

            {volist name="list" id="vo"}            <tr>

                <td>{$vo.id}</td>

                <td>{$vo.user_name}</td>

                <td>{$vo.dept}</td>

                <td>{$vo.report_date}</td>

                <td>

                    <a href="{:url('print', ['id'=>$vo.id])}" target="_blank">打印日报</a>

                </td>

            </tr>

            {/volist}        </table>

    </div></body></html>

③ 打印专用页面(print.html)—— 固定格式,适配打印机

新建文件print.html,粘贴:


html



预览






<!DOCTYPE html><html><head>

    <meta charset="UTF-8">

    <title>日报打印 - {$report.user_name} {$report.report_date}</title>

    <style>

        @media print {

            body {margin: 0; padding: 0;}

            .print-btn {display: none;}

        }

        .print-box {width: 100%; max-width: 800px; margin: 0 auto; padding: 20px; font-family: "微软雅黑"; font-size: 14px; line-height: 1.8;}

        .print-title {text-align: center; font-size: 18px; font-weight: bold; margin-bottom: 20px;}

        .print-item {margin-bottom: 10px;}

        .print-label {display: inline-block; width: 80px; font-weight: bold;}

        .print-content {display: inline-block; vertical-align: top; width: calc(100% - 90px);}

        .print-line {border-bottom: 1px solid #000; margin: 15px 0;}

    </style></head><body>

    <div>

        <div style="text-align: right; margin-bottom: 10px;">

            <button onclick="window.print()" style="padding: 8px 20px; background: #009688; color: #fff; border: none; cursor: pointer;">打印日报</button>

        </div>

        <div>工作日报</div>

        <div></div>

        <div>

            <span>填写人:</span>

            <span>{$report.user_name}</span>

        </div>

        <div>

            <span>部门:</span>

            <span>{$report.dept}</span>

        </div>

        <div>

            <span>日期:</span>

            <span>{$report.report_date}</span>

        </div>

        <div>

            <span>今日完成:</span>

            <span>{$report.today_work}</span>

        </div>

        <div>

            <span>明日计划:</span>

            <span>{$report.tomorrow_plan|default='无'}</span>

        </div>

        <div>

            <span>备注:</span>

            <span>{$report.remark|default='无'}</span>

        </div>

    </div></body></html>

三个视图文件都保存后,视图层就完成了。


第四步:测试访问,体验你的日报系统!

现在所有代码都已落地,直接访问以下地址就能用:


日报列表页:http://172.16.0.51/index/daily_report/index(能看到 “生成新日报” 按钮);


新增日报页:点击 “生成新日报”,填写姓名、部门、今日完成等信息,提交后会提示 “生成成功”;


打印日报:列表页点击 “打印日报”,打开新页面后点击 “打印日报” 按钮,就能调用打印机打印固定格式的日报。


关键注意事项(避免小坑)

文件名规范:控制器 / 模型的DailyReport.php首字母必须大写,视图文件夹daily_report必须小写;


数据库验证:如果提示 “数据库连接失败”,检查application/database.php里的 MySQL 密码是否正确;


缓存问题:改代码后没生效,就删除runtime目录下的缓存文件(宝塔里右键删除即可)。


总结

现在你已完成 “从 0 到 1” 的核心步骤:控制器(逻辑)+ 模型(数据)+ 视图(界面);


访问对应地址就能体验 “固定格式日报生成 + 打印” 的完整功能,完全适配你的宝塔 + TP5 环境;


后续想改样式(比如打印页的字体、排版),直接改视图文件里的 CSS 即可,不用动逻辑代码。


现在快去访问http://172.16.0.51/index/daily_report/index,试试生成你的第一份日报吧!



系统创建人
系统创建人
这个人很神秘

发布评论