You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
3.4 KiB
98 lines
3.4 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Mobile;
|
|
|
|
use App\Helpers\ResponseCode;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Book;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class BookController extends Controller
|
|
{
|
|
/**
|
|
* @OA\Get(
|
|
* path="/api/mobile/book/other",
|
|
* tags={"小程序-图书"},
|
|
* summary="分类配置",
|
|
* description="",
|
|
* @OA\Response(
|
|
* response="200",
|
|
* description="暂无"
|
|
* )
|
|
* )
|
|
*/
|
|
public function other()
|
|
{
|
|
// 分类数据
|
|
$category = Book::get()->implode('category', ',');
|
|
$category = explode(',', $category);
|
|
$category = array_unique($category);
|
|
return $this->success(compact('category'));
|
|
}
|
|
|
|
/**
|
|
* @OA\Get(
|
|
* path="/api/mobile/book/index",
|
|
* tags={"小程序-图书"},
|
|
* summary="图书列表",
|
|
* description="",
|
|
* @OA\Parameter(name="category", in="query", @OA\Schema(type="integer"), required=true, description="分类"),
|
|
* @OA\Parameter(name="keyword", in="query", @OA\Schema(type="integer"), required=true, description="关键词"),
|
|
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="string"), required=false, description="每页显示的条数"),
|
|
* @OA\Parameter(name="page", in="query", @OA\Schema(type="string"), required=false, description="页码"),
|
|
* @OA\Parameter(name="sort_name", in="query", @OA\Schema(type="string"), required=false, description="排序字段名字"),
|
|
* @OA\Parameter(name="sort_type", in="query", @OA\Schema(type="string"), required=false, description="排序类型"),
|
|
* @OA\Response(
|
|
* response="200",
|
|
* description="暂无"
|
|
* )
|
|
* )
|
|
*/
|
|
public function index()
|
|
{
|
|
$all = \request()->all();
|
|
$list = Book::with('cover')->where(function ($query) use ($all) {
|
|
if (isset($all['keyword'])) {
|
|
$query->where(function ($q) use ($all) {
|
|
$q->where('title', 'like', '%' . $all['keyword'] . '%')
|
|
->orWhere('author', 'like', '%' . $all['keyword'] . '%')
|
|
->orWhere('isbn', 'like', '%' . $all['keyword'] . '%');
|
|
});
|
|
}
|
|
if (isset($all['category'])) {
|
|
$query->where('category', 'like', '%' . $all['category'] . '%');
|
|
}
|
|
})->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc')
|
|
->paginate($all['page_size'] ?? 20);
|
|
return $this->success($list);
|
|
}
|
|
|
|
/**
|
|
* @OA\Get(
|
|
* path="/api/mobile/book/detail",
|
|
* tags={"小程序-图书"},
|
|
* summary="图书详情",
|
|
* @OA\Parameter(name="id", in="query", @OA\Schema(type="integer"), required=true, description="id"),
|
|
* @OA\Response(
|
|
* response=200,
|
|
* description="操作成功"
|
|
* )
|
|
* )
|
|
*/
|
|
public function detail()
|
|
{
|
|
$all = \request()->all();
|
|
$messages = [
|
|
'id.required' => 'Id必填',
|
|
];
|
|
$validator = Validator::make($all, [
|
|
'id' => 'required'
|
|
], $messages);
|
|
if ($validator->fails()) {
|
|
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
|
|
}
|
|
$book = Book::with('cover')->find($all['id']);
|
|
return $this->success($book);
|
|
}
|
|
}
|