tp 5 框架 ajax软删除,回收站,数据恢复

//HTML代码:

  <td>
            <span onclick="del({$v.id})">删除</span>
  </td>
 //ajax删除
    function del(id) {
        var token = localStorage.getItem('token')
        $.ajax({
            url: "/admin/order/delete",
            type: "DELETE",
            data: {
                id: id
            },
            dataType: "JSON",
            success: function (res) {
                console.log(res)
                if (res.code == 500) {
                    alert('参数不正确')
                }
                if (res.code == 200) {
                    alert('删除成功')
                }
//跳转至订单页面/admin/order/,携带token
                location.href = '/admin/order/order?token=' + token
            }


        })


    }

//控制器代码

    public function delete($id)
    {
        $data = input();
        $id = $data['id'];
        //验证参数
        if (!intval($id)) {
            return json(['code' => 500, 'massage' => '参数不正确']);
        }
        $result = OrderModel::del($id);
        return json(['code' => 200, 'massage' => 'success', 'data' => $result]);
    }
}

软删除模型代码:

//    软删除
    public static function del($id)
    {
        return self::destroy($id);
    }

 

回收站HTML代码

 

span onclick="restore()">回收站</span>


  function restore() {
//携带token
var token = localStorage.getItem('token')
//跳转地址 location.href
= '/admin/order/getRell?token=' + token }

去模型查询软删除数据的代码:

//软删除回收站数据
    public static function getRell()
    {
        return self::onlyTrashed()->select();

    }

控制器代码:

    //查询回收站数据,并将数据携带至页面
    public function getRell()
    {
//调用模型,提取数据
        $restore = OrderModel::getRell();
//向视图发送数据
        $this->assign('restore', $restore);
        return view();
  

 

 

 

 回收站数据恢复

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
</head>
<body>
<table class="table">
  <tr>
    <td>订单号</td>
    <td>支付方式</td>
    <td>收货人姓名</td>
    <td>支付状态</td>
    <td>软删除时间</td>
      <td>操作</td>
  </tr>
    {foreach $restore as $k=>$v}
    <tr>
        <td>{$v.order}</td>
        <td>{$v.pay}</td>
        <td>{$v.consignee}</td>
        <td>{$v.status}</td>
        <td>{$v.delete_time}</td>
        <td>
<!--            <a href="/admin/order/eovery/id/{$v.id}/{$Think.session.token}">数据恢复</a>-->
           <span onclick="data({$v.id})">数据恢复</span>
        </td>
    </tr>
    {/foreach}
</table>

</body>
</html>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
<script>
    function data(id){
        //获取token;
     var token=localStorage.getItem('token');
        $.ajax({
            url: "/admin/order/eovery",
            type: "POST",
            data: {
               id:id,
            },
            dataType: "JSON",
            success: function (res) {
                console.log(res)
//跳转至订单页面/admin/order/,携带token
                location.href = '/admin/order/order?token='+token;
            }
        })
    }

</script>

 

 

 

 数据恢复控制器代码:

    //软删除恢复
    public function eovery()
    {
        //接收需要恢复的软删除id
        $id = input('id');
//实例化模型
        $obj = new OrderModel();
//根据id恢复数据
        $restore = $obj->restore(['id' => $id]);
        if (!$restore) {
            return json(['code' => 200, 'massage' => 'success']);
        }
       
    }

 

posted @ 2021-09-02 21:28  王越666  阅读(125)  评论(0编辑  收藏  举报