123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- /**
- * Created by PhpStorm
- * DateTime: 2022/10/2 13:32
- *
- * @description
- */
- namespace App\Http\Controllers\V1;
- use App\Models\Product;
- use App\Models\ProductCategory;
- use App\Models\StatProduct;
- use App\Models\StatProductDownload;
- use Dingo\Api\Http\Request;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Http\JsonResponse;
- class ProductController extends Controller
- {
- public function cate(Request $request): JsonResponse
- {
- $limit = $request->input('limit', 3);
- $keywords = $request->input('keywords', '');
- $page = request()->input('page',1);
- $offset = ($page - 1) * $limit;
- $lists = ProductCategory::where('is_opened', 1)
- ->when($keywords, function ($query, $keywords) {
- /* @var Builder $query*/
- return $query->where('name','like', "%$keywords%");
- })
- ->orderByDesc('sort')
- // ->limit($limit)
- // ->offset($offset)
- ->get();
- return $this->success($lists);
- }
- public function search(Request $request): JsonResponse
- {
- $limit = $request->input('limit');
- $keywords = $request->input('keywords', '');
- $page = request()->input('page',1);
- $cateId = request()->input('cate_id',0);
- $ids = request()->input('ids','');
- $offset = ($page - 1) * $limit;
- $ids = array_filter(explode(",",$ids));
- $lists = Product::withCount(['viewer'])->where('is_opened', 1)
- ->when($keywords, function ($query, $keywords) {
- /* @var Builder $query*/
- return $query->where('name','like', "%$keywords%");
- })
- ->when($cateId, function ($query, $cateId) {
- /* @var Builder $query*/
- return $query->where('cate_id', $cateId);
- })->when($ids, function ($query, $ids) {
- /* @var Builder $query*/
- return $query->whereIn('id',$ids);
- })
- ->orderByDesc('viewer_count')
- ->orderByDesc('sort')
- ->limit($limit)
- ->offset($offset)
- ->get();
- return $this->success($lists);
- }
- public function detail($id): JsonResponse
- {
- $info = Product::with(['specs' => function($query) {
- $query->select(['id','product_id','name','specs'])
- ->where('is_opened', 1)
- ->orderByDesc('sort');
- }])
- ->where('id',$id)
- ->first();
- $info->files = $info;
- return $this->success($info);
- }
- public function viewer($id): JsonResponse
- {
- $stat = new StatProduct();
- $stat->product_id = $id;
- $stat->user_id = \user()->id;
- $stat->save();
- return $this->success();
- }
- public function download($id): JsonResponse
- {
- $stat = new StatProductDownload();
- $stat->product_id = $id;
- $stat->user_id = \user()->id;
- $stat->save();
- return $this->success();
- }
- }
|