tp 搜索分页 删除

81 阅读1分钟
<?php
class Goods
{
    /**
     * 显示资源列表
     * 搜索分页
     * @return \think\Response
     */
    public function index(Request $request)
    {
        try {
            $page = input('page',1);
            if (!is_numeric($page)) {
                abort(2001,'页码只能是整数');
            }
            $limit = input('limit',5);
            if (!is_numeric($limit)) {
                abort(2001,'每页显示只能是整数');
            }
            //添加搜索条件
            $where = [];
            $goods_name = input('goods_name');
            if (!empty($goods_name)) {
                $where[]=['goods_name','like',"%$goods_name%"];
            }
            $is_on_sale = input('is_on_sale');
            if ($is_on_sale >= 0) {
                $where[] = ['is_on_sale','=',$is_on_sale];
            }
            $list = \app\auth\model\Goods::where($where)->paginate(
                [
                    'list_rows'=>$limit,
                    'query'=>[
                        'goods_name'=>$goods_name,
                        'is_on_sale'=>$is_on_sale
                    ]
                ]
            );
            return success($list);
        }catch (ErrorException $errorException){
            return fail($errorException->getMessage());
        }catch (HttpException $httpException){
            return fail($httpException->getMessage());
        }
    }
    /**
     * 删除指定资源
     * @param  int  $id
     * @return \think\Response
     */
    public function del(Request $request)
    {
        try {
            //单删 批量删
            $id = $request->post('id');
            if (is_numeric($id)) {
                $isOnSale = \app\save\model\Goods::where('is_on_sale','=',1)->find($id);
                if ($isOnSale) {
                    return fail(2001,'上架商品不能删除');
                }
               $res =  \app\save\model\Goods::destroy($id);

            }else{
                $isOnSale = \app\save\model\Goods::where('is_on_sale','=',1)->find($id);
                if ($isOnSale) {
                    return fail(2001,'上架商品不能删除');
                }
                $ids =explode(',',$id);
                $res = \app\save\model\Goods::destroy($ids);

            }
            if ($res) {
                return success();
            }
            return fail();
        }catch (FileException $fileException){
            return fail($fileException->getMessage());
        }catch (HttpException $httpException){
            return fail($httpException->getMessage());
        }

    }
}