[PHP高可用后端]①⑨--修改状态功能开发

[PHP高可用后端]①⑨--修改状态功能开发_第1张图片
image.png

common.php


// +----------------------------------------------------------------------

// 应用公共文件
//function pagination($obj)
//{
//    if (!$obj) {
//        return '';
//    }
//    $param = request()->param();
//    return '
' . $obj->appends($param)->render() . '
'; //} function getCatName($catId) { if (!$catId) { return ''; } $cats = config('cat.list'); return !empty($cats[$catId] ? $cats[$catId] : ''); } function isYesNo($str) { return $str ? '' : ''; } /** * 状态 * @param $id * @param $status */ function status($id, $status) { $controller = request()->controller(); $sta = $status == 1 ? 0 : 1; // /index.php/admin/news/status/id/4/status/1.html $url = url($controller . '/status', ['id' => $id, 'status' => $sta]); if ($status === 1) { $str = "正常"; } else if ($status === 0) { $str = "待审"; } return $str; }
/**
 * 状态
 * @param $id
 * @param $status
 */
function status($id, $status)
{
    $controller = request()->controller();
    $sta = $status == 1 ? 0 : 1;
    // /index.php/admin/news/status/id/4/status/1.html
    $url = url($controller . '/status', ['id' => $id, 'status' => $sta]);
    if ($status === 1) {
        $str = "正常";
    } else if ($status === 0) {
        $str = "待审";
    }
    return $str;
}

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.status|status=$vo.id,###}
{include file="public/_footer" /}
{$vo.status|status=$vo.id,###}

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'})
        }
    }
}

function app_del(obj) {
    url = $(obj).attr('del_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});
                }
            },
            error: function (data) {
                console.log(data.msg);
            }
        });
    });
}

function app_status(obj) {
    url = $(obj).attr('status_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});
                    }
                },
                error: function (data) {
                    console.log(data.msg);
                }
            }
        )
    });
}
function app_status(obj) {
    url = $(obj).attr('status_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});
                    }
                },
                error: function (data) {
                    console.log(data.msg);
                }
            }
        )
    });
}

Base.php

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;
    }

    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不合法');
        }
        $model = $this->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, '删除失败');
    }

    /**
     * 通用化修改状态
     */
    public function status()
    {
        $data = input('param.');
        //tp5 validate 机制校验 自行完成 id status
        //通过id 去库中查询是否存在

        $id = model('News')->get(['id' => $data['id']]);
        if (!$id) {
            return $this->result('', 0, 'id不存在');
        }
        $model = $this->model ? $this->model : request()->controller();

        try {
            $res = model($model)->save(['status' => $data['status']], ['id' => $data['id']]);
        } catch (\Exception $e) {
            return $this->result('', 0, $e->getMessage());
        }
        if ($res) {
            return $this->result(['jump_url' => $_SERVER['HTTP_REFERER']], 1, 'OK');
        }
        return $this->result('', 0, '失败');
    }
}
 /**
     * 通用化修改状态
     */
    public function status()
    {
        $data = input('param.');
        //tp5 validate 机制校验 自行完成 id status
        //通过id 去库中查询是否存在

        $id = model('News')->get(['id' => $data['id']]);
        if (!$id) {
            return $this->result('', 0, 'id不存在');
        }
        $model = $this->model ? $this->model : request()->controller();

        try {
            $res = model($model)->save(['status' => $data['status']], ['id' => $data['id']]);
        } catch (\Exception $e) {
            return $this->result('', 0, $e->getMessage());
        }
        if ($res) {
            return $this->result(['jump_url' => $_SERVER['HTTP_REFERER']], 1, 'OK');
        }
        return $this->result('', 0, '失败');
    }
}
[PHP高可用后端]①⑨--修改状态功能开发_第2张图片
image.png

你可能感兴趣的:([PHP高可用后端]①⑨--修改状态功能开发)