14文档回收站功能的实现

14文档回收站功能的实现

溅难抚辖纱剩娶胜臼辱灿继仓


要实现文档回收站的功能必须在Article模型类中引入软删除类SoftDelete  并且必须在类中显视使用use SoftDelete;

这样才可以使用onlyTrashed()方法查询被软删除的文档(模型使用destory()方法不引入软删除类也行)

14文档回收站功能的实现第1张

<?php
namespace app\common\model;
//引入系统模型
use think\Model;
use think\model\concern\SoftDelete;//引入软删除类
//要使用模型必须继承系统模型
class Article extends Model
{
    use SoftDelete;
    //配饰数据库的连接参数,因为使用了个性化的字符串连接  所以这里要写
    //直接使用tp的数据库连接方式 这里就不用写
    protected $connection="db_qiye";

    //配置数据表
    protected $table="article";
    //设置主键
    protected $pk="art_id";

    //开启自动时间戳,除开单独开启之外  还可以在config\database.php中全局开启
    
    //自动时间戳功能会将1528183378  转化为:2018-06-05 15:22:58
    protected $autoWriteTimestamp=true;

    //设置添加时候的时间字段名称
    protected $createTime="create_time";
    //自动时间戳更新的字段
    protected $updateTime="update_time";
    //自动时间戳删除时的字段
    protected $deleteTime="delete_time";

    //上方的三个刚好与数据表中的字段一致  自动就能找到相应字段  可以不用写

    //设置软删除字段的默认值(tp51\thinkphp\library\think\Model.php有定义,直接复制属性即可)
    protected $defaultSoftDelete=0;



}

14文档回收站功能的实现第2张

<?php
namespace app\admin\controller;
use app\common\controller\Base;
use app\common\model\Article;
class Trash extends Base
{
    //前置方法
    //所有操作都必须执行'isLogin'操作:检测是否已登录


    //回收站中的文档列表与操作入口
    public function index()
    {
        //查询被软删除的数据,注意必须在模型中引入SoftDelete类
        //并且必须use SoftDelete类
        $arts=Article::onlyTrashed()->order('delete_time','desc')->paginate(8);

        //模板变量赋值
        $this->view->arts=$arts;

        //渲染模板
        return $this->view->fetch();


    }

    //处理彻底删除单条数据的操作
    public function delTrue()
    {
        $art_id=$this->request->param('art_id');
        if(Article::destroy($art_id,true))
        {
            return ['status'=>0,'msg'=>'删除成功'];
        }

    }

    //恢复单条数据
    public function restore()
    {
        $art_id=$this->request->param('art_id');
        //根据art_id查找信息
        $art=Article::onlyTrashed()->where('art_id',$art_id)->find();
        //
        if($art->restore())
        {
            return ['status'=>0,'msg'=>'恢复成功'];
        }
    }

    //批量彻底删除文章
    public function delTrueAll()
    {
        $artIdList=$this->request->param('id_list');
        $artIdList=json_decode($artIdList);

        //批量删除
        if(Article::destroy($artIdList,true))
        {
            return ['status'=>0,'msg'=>"批量彻底删除成功"];
        }


    }

    //批量恢复文章
    public function resAll()
    {
        $artIdList=$this->request->param('id_list');
        $artIdList=json_decode($artIdList);

        //查询
        $arts=Article::onlyTrashed()->select($artIdList);

        foreach ($arts as $art)
        {
            $art->restore();
        }
        return ['status'=>0,'msg'=>'恢复成功'];


    }

}

14文档回收站功能的实现第3张

{include file="common@header"}
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">文档</a>
        <a>
          <cite>文档列表</cite></a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <xblock>
        <button class="layui-btn layui-btn-danger" onclick="delTrueAll()"><i class="layui-icon"></i>批量彻底删除</button>
        <button class="layui-btn layui-btn-normal" onclick="resAll()"><i class="layui-icon">&#x1005;</i>   批量恢复</button>

    </xblock>
    <table class="layui-table">
        <thead>
        <tr>
            <th>
                <div class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
            </th>
            <th>ID</th>
            <th>标题</th>
            <th>分类</th>
            <th>排序</th>
            <th>推荐</th>
            <th>发布时间</th>
            <th>更新时间</th>
            <th >操作</th>
        </tr>
        </thead>
        <tbody>

        {volist  name="arts" id="art"}
        <tr>
            <td>
                <div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id='{$art.art_id}'><i class="layui-icon">&#xe605;</i></div>
            </td>
            <td>{$art.art_id}</td>

            <!-- 这里使用系统函数mb_substr只获取标题的25个字符 -->
            <td>{:mb_substr($art.art_title,0,25,'utf8')}</td>
            {//为了直观,这里调用了自定义函数:根据分类id,自动获取对应的分类名称}
            <td>{:getCateName($art.cate_id)}</td>
            <td>{$art.art_order}</td>
            <td>
                {//如果变量art.art_rec值为1  输出推荐  否则输出普通}
                {eq name="art.art_rec" value="1"}
                推荐
                {else/}
                普通
                {/eq}
            </td>
            <td>{$art.create_time}</td>
            <td>{$art.update_time}</td>
            <td class="td-manage">

                <a title="删除" onclick="member_del(this,'{$art.art_id}')" href="javascript:;">
                    <i class="layui-icon">&#xe640;</i>
                </a>
                <a title="恢复" onclick="member_res(this,'{$art.art_id}')" href="javascript:;">
                    <i class="layui-icon">&#x1005;</i>
                </a>

            </td>
        </tr>

        {/volist}
        </tbody>
    </table>
    <div class="page">
        <div>
            {//显示分页   raw将html代码进行解析}
            {$arts|raw}
        </div>
    </div>

</div>
<script>
    layui.use('laydate', function(){
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
            elem: '#end' //指定元素
        });
    });

    /*文章-彻底删除*/
    function member_del(obj,id){
        layer.confirm('确认要删除吗?',function(index){
            //发异步删除数据
            $.get("{:url('delTrue')}",{
                'art_id':id
            },function (data){
                if(data.status==0)
                {
                    $(obj).parents("tr").remove();
                    layer.msg(data.msg,{icon:1,time:1000});
                }
                else
                {
                    layer.msg("删除失败",{icon:1,time:1000});
                }
            })

        });
    }

    /*文章-恢复*/
    function member_res(obj,id){
        layer.confirm('确认要恢复吗?',function(index){
            //发异步删除数据
            $.get("{:url('restore')}",{
                'art_id':id
            },function (data){
                if(data.status==0)
                {
                    $(obj).parents("tr").remove();
                    layer.msg(data.msg,{icon:1,time:1000});
                }
                else
                {
                    layer.msg("删除失败",{icon:1,time:1000});
                }
            })

        });
    }




    //批量彻底删除选中的文章
    //批量删除需要在将数据前面的选择框的属性这样写才能获取到数据的id:data-id='{$art.art_id}'
    function delTrueAll (argument) {

        var data = tableCheck.getData();

        layer.confirm('确认批量彻底删除吗?'+data,function(index){
            //捕捉到所有被选中的,发异步进行删除
            $.get("{:url('delTrueAll')}",{
                //将js数组转为json发送给服务器
                'id_list':JSON.stringify(data)
            },function(data){
                if(data.status==0)
                {
                    layer.msg(data.msg, {icon: 1});
                    $(".layui-form-checked").not('.header').parents('tr').remove();
                }
                else
                {
                    layer.msg("删除失败", {icon: 1});
                }

            })

        });
    }

    //批量恢复选中的文章
    function resAll (argument) {

        var data = tableCheck.getData();

        layer.confirm('确认要批量恢复吗?'+data,function(index){
            //捕捉到所有被选中的,发异步进行删除
            $.get("{:url('resAll')}",{
                //将js数组转为json发送给服务器
                'id_list':JSON.stringify(data)
            },function(data){
                if(data.status==0)
                {
                    layer.msg(data.msg, {icon: 1});
                    $(".layui-form-checked").not('.header').parents('tr').remove();
                }
                else
                {
                    layer.msg("删除失败", {icon: 1});
                }

            })

        });
    }
</script>
</body>

</html>

执行效果:

14文档回收站功能的实现第4张


稍其山闷笔揽潮们险删豁苍浅