[PHP高可用后端]①⑧--新闻删除功能

[PHP高可用后端]①⑧--新闻删除功能_第1张图片
222.png

http://demo.h-ui.net/H-ui.admin/3.1/index.html

[PHP高可用后端]①⑧--新闻删除功能_第2张图片
222.png

右键 查看框架源代码

common.js

function singwaapp_save(form) {
    var data = $(form).serialize();
    url = $(form).attr('url');
    $.post(url, data, function (result) {
        if (result.code == 0) {
            layer.msg(result.msg, {icon: 5, time: 2000});
        } else if (result.code == 1) {
            self.location = result.data.jump_url;
        }
    }, 'JSON');

}

function selecttime(flag) {
    if (flag == 1) {
        var endTime = $("#countTimeend").val();
        if (endTime != "") {
            WdatePicker({dateFmt: 'yyyy-MM-dd HH:mm', maxDate: endTime})
        } else {
            WdatePicker({dateFmt: 'yyyy-MM-dd HH:mm'})
        }
    } else {
        var startTime = $("#countTimestart").val();
        if (startTime != "") {
            WdatePicker({dateFmt: 'yyyy-MM-dd HH:mm', minDate: startTime})
        } else {
            WdatePicker({dateFmt: 'yyyy-MM-dd HH:mm'})
        }
    }
}

/**
 * 通用化删除操作
 * @param obj
 */
function app_del(obj) {
    //获取模板当中的url地址
    url = $(obj).attr('del_url');
    //index.php/admin/news/delete/id/-1.html
    //alert(url);
    layer.confirm('确认要删除吗?', function (index) {
        $.ajax({
            type: 'POST',
            url: url,
            dataType: 'json',
            success: function (data) {
                if (data.code == 1) {
                    //执行跳转
                    self.location = data.data.jump_url;
                } else if (data.code == 0) {
                    layer.msg(data.msg, {icon: 2, time: 2000});
                }
                // $(obj).parents("tr").remove();
                // layer.msg('已删除!', {icon: 1, time: 1000});
            },
            error: function (data) {
                console.log(data.msg);
            },
        });
    });
}

index.html


{include file="public/_meta" title="娱乐资讯"/}


日期范围: -
{volist name="news" id="vo"} {/volist}
ID 标题 分类 缩图 更新时间 是否推荐 发布状态 操作
{$vo.id} {$vo.title} {$vo.catid|getCatName} {$vo.update_time} {$vo.is_position|isYesNo} {$vo.is_position}
{include file="public/_footer" /}
 

Base.php(Controller)

isLogin();
        if (!$isLogin) {
            $this->redirect('login/index');
        }
    }

    public function isLogin()
    {
        $user = session(config('admin.session_user'), '',
            config('admin.session_user_scope'));
        if ($user && $user->id) {
            return true;
        }
        return false;
    }

    /**
     * 获取分页page size内容
     */
    public function getPageAndSize($data)
    {
        $this->page = !empty($data['page']) ? $data['page'] : 1;
        $this->size = !empty($data['size']) ? $data['size'] : config('paginate.list_rows');
        $this->from = ($this->page - 1) * $this->size;
    }


    /**
     * 删除逻辑
     */
    public function delete($id = 0)
    {
        if (!intval($id)) {
            return $this->result('', 0, 'ID不合法');
        }
        //如果你的表和控制器文件名一样 new enws
        //admin_user Admin
        $model = $this->model ? $this->model : request()->controller();
        //如果 php php7 $model=$this->>model ?? request()->controller();

        try {
            $res = model($model)->save(['status' => -1], ['id' => $id]);
        } catch (\Exception $e) {
            $this->result('', 0, $e->getMessage());
        }

        if ($res) {
            return $this->result(['jump_url' => $_SERVER['HTTP_REFERER']], 1, 'OK');
        }
        return $this->result('', 0, '删除失败');

    }
}

News.php

 string '0' (length=1)
         * 'start_time' => string '2017-10-02 14:23' (length=16)
         * 'end_time' => string '2017-11-09 14:23' (length=16)
         * 'title' => string '1233333' (length=7)
         */
        //halt($data);

        //转换查询条件
        if (!empty($data['start_time']) &&
            !empty($data['end_time']) &&
            $data['end_time'] > $data['start_time']
        ) {
            $whereData['create_time'] = [
                ['gt', strtotime($data['start_time'])],
                ['lt', strtotime($data['end_time'])],
            ];
        }
        if (!empty($data['catid'])) {
            $whereData['catid'] = intval($data['catid']);
        }
        if (!empty($data['title'])) {
            $whereData['title'] = [
                'like', '%' . $data['title'] . '%'
            ];
        }

        $this->getPageAndSize($data);

        //获取表里面的数据
        $news = model('News')->getNewsByCondition($whereData, $this->from, $this->size);
        //获取满足条件的数据总数=》有多少页
        $total = model('News')->getNewsCountCondition($whereData);
        //结合总数+size =>有多少页
        //1.1=>2
        $pageTotal = ceil($total / $this->size);


        return $this->fetch('', [
            'cats' => config('cat.list'),
            'news' => $news,
            'pageTotal' => $pageTotal,
            'curr' => $this->page,
            'start_time' => empty($data['start_time']) ? '' : $data['start_time'],
            'end_time' => empty($data['end_time']) ? '' : $data['end_time'],
            'catid' => empty($data['catid']) ? '' : $data['catid'],
            'title' => empty($data['title']) ? '' : $data['title'],
            'query' => $query,
        ]);
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            try {
                $id = model('News')->add($data);
            } catch (\Exception $e) {
                return $this->result('', 0, '新增失败:' . $e->getMessage());
            }

            if ($id) {
                return $this->result([
                    'jump_url' => url('news/index')], 1, 'OK');
            } else {
                return $this->result('', 0, '新增失败');
            }
        } else {
            return $this->fetch('', [
                'cats' => config('cat.list')
            ]);
        }
    }

}
[PHP高可用后端]①⑧--新闻删除功能_第3张图片
222.png

你可能感兴趣的:([PHP高可用后端]①⑧--新闻删除功能)