Ver código fonte

wesmiler 报恩寺项目提交

wesmiler 4 anos atrás
pai
commit
48a9b0dee4

+ 24 - 1
app/Http/Controllers/Api/v1/BuddhistController.php

@@ -58,10 +58,33 @@ class BuddhistController extends BaseController
     }
 
     /**
+     * 列表
+     * @return array
+     */
+    public function relaction(){
+        $params = request()->all();
+        return $this->service->getRelactionList($params);
+    }
+
+    /**
+     *
+     */
+    public function detail(){
+        $id = request()->get('id',0);
+        if($id<=0){
+            return message(1006, false);
+        }
+
+        return $this->service->getDetail($id);
+    }
+
+    /**
      * 获取章节内容
      */
     public function page(){
         $id = request()->get('id', 0);
-        return $this->pageService->getNext($id);
+        $bid = request()->get('bid', 0);
+        $this->service->updateVisit($this->userId);
+        return $this->pageService->getDetail($bid, $id);
     }
 }

+ 36 - 0
app/Services/BuddhistPagesService.php

@@ -11,6 +11,7 @@
 
 namespace App\Services;
 
+use App\Models\BuddhistModel;
 use App\Models\BuddhistPagesModel;
 
 /**
@@ -80,4 +81,39 @@ class BuddhistPagesService extends BaseService
     public function getCount($id){
         return $this->model::where(['bid'=> $id,'status'=> 1])->count('id');
     }
+
+    /**
+     * 获取详情
+     * @param $bid
+     * @param int $id
+     * @return array
+     */
+    public function getDetail($bid, $id=0){
+        $where = ['bid'=> $bid,'mark'=> 1,'status'=> 1];
+        if($id){
+            $where['id'] = $id;
+        }
+        $info = $this->model::where($where)
+            ->select(['id','title','description','content','create_time'])
+            ->orderBy('sort','asc')
+            ->orderBy('create_time','asc')
+            ->first();
+
+        $info = $info? $info->toArray() : [];
+        if($info){
+            $info['last'] = $this->model::where(['bid'=> $bid,'mark'=> 1,'status'=> 1])
+                ->where('id','<', $id)
+                ->orderBy('sort','desc')
+                ->orderBy('create_time','desc')
+                ->first();
+
+            $info['next'] = $this->model::where(['bid'=> $bid,'mark'=> 1,'status'=> 1])
+                ->where('id','>', $id)
+                ->orderBy('sort','desc')
+                ->orderBy('create_time','desc')
+                ->first();
+        }
+
+        return message(MESSAGE_OK, true, $info);
+    }
 }

+ 103 - 0
app/Services/BuddhistService.php

@@ -118,6 +118,11 @@ class BuddhistService extends BaseService
                 if ($cateId > 0) {
                     $query->where('a.cate_id', $cateId);
                 }
+
+                $isRecommand = isset($params['is_recommand']) ? intval($params['is_recommand']) : 0;
+                if ($isRecommand > 0) {
+                    $query->where('a.is_recommand', $isRecommand);
+                }
             })
             ->select(['a.id', 'a.cate_id', 'c.name as cate_name', 'a.title','a.is_recommand', 'a.view_num', 'a.thumb', 'a.status', 'a.create_time', 'a.update_time', 'a.description', 'a.sort','a.publish_at'])
             ->orderBy('a.update_time', 'desc')
@@ -183,4 +188,102 @@ class BuddhistService extends BaseService
 
         return parent::delete(); // TODO: Change the autogenerated stub
     }
+
+    /**
+     * 相关推荐列表
+     * @return array
+     * @since 2020/11/11
+     * @author wesmiler
+     */
+    public function getRelactionList($params)
+    {
+        $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
+        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
+        $id = isset($params['id']) ? intval($params['id']) : PERPAGE;
+
+        $info = $this->model::from('buddhists as a')
+            ->leftJoin('buddhist_cates as c', 'a.cate_id', '=', 'c.id')
+            ->where(['a.id'=> $id,'a.mark'=> 1,'a.status'=> 1])
+            ->select(['a.id','a.cate_id','a.type'])
+            ->first();
+
+        $dataList = $this->model::from('buddhists as a')
+            ->leftJoin('buddhist_cates as c', 'a.cate_id', '=', 'c.id')
+            ->where(function ($query) use ($params, $info) {
+                $query->where(['a.mark'=>1,'a.status'=> 1]);
+                $cateId = isset($info['cate_id']) ? intval($info['cate_id']) : 0;
+                if ($cateId > 0) {
+                    $query->where('a.cate_id', $cateId);
+                }
+
+            })
+            ->select(['a.id','a.title','a.thumb','a.cate_id','c.name as cate_name','a.view_num','a.description','a.is_recommand','a.publish_at','a.create_time'])
+            ->orderBy('a.sort', 'desc')
+            ->orderBy('a.update_time', 'desc')
+            ->paginate($pageSize);
+
+        $dataList = $dataList ? $dataList->toArray() : [];
+        if(empty($dataList)){
+            $dataList = $this->model::from('buddhists as a')
+                ->leftJoin('buddhist_cates as c', 'a.cate_id', '=', 'c.id')
+                ->where(['a.mark'=>1,'a.status'=> 1])
+                ->select(['a.id','a.title','a.thumb','a.cate_id','c.name as cate_name','a.view_num','a.description','a.is_recommand','a.publish_at','a.create_time'])
+                ->orderBy(\DB::raw('rand()'))
+                ->orderBy('a.update_time', 'desc')
+                ->paginate($pageSize);
+            $dataList = $dataList ? $dataList->toArray() : [];
+        }
+        if ($dataList) {
+            foreach ($dataList['data'] as &$item) {
+                $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
+                $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
+                $item['publish_at'] = $item['publish_at'] ? $item['publish_at'] : $item['create_at'];
+
+            }
+            unset($item);
+        }
+
+        return [
+            'code' => 0,
+            'success'=> true,
+            'msg' => '操作成功',
+            'count' => isset($dataList['total']) ? $dataList['total'] : 0,
+            'data' => isset($dataList['data']) ? $dataList['data'] : 0,
+        ];
+    }
+
+    /**
+     * 获取详情
+     * @param $id
+     */
+    public function getDetail($id, $userId=0){
+        $info = $this->model::from('buddhists as a')
+            ->leftJoin('buddhist_cates as c', 'a.cate_id', '=', 'c.id')
+            ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id])
+            ->select(['a.id','a.title','a.thumb','a.cate_id','c.name as cate_name','a.view_num','a.description','a.is_recommand','a.publish_at','a.create_time'])
+            ->first();
+        $info = $info? $info->toArray() : [];
+        if($info){
+            $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
+            $info['publish_at'] = $info['publish_at']? $info['publish_at'] : datetime( $info['create_time'],'Y-m-d H:i:s');
+
+            $pageNum = BuddhistPagesService::make()->getCount($info['id']);
+            $item['pageNum'] = intval($pageNum);
+        }
+        return $info;
+    }
+
+    /**
+     * 访问量
+     * @return mixed
+     */
+    public function updateVisit($userId=0){
+        $bid = request()->get('bid');
+        $cacheKey = "caches:buddhist:visit:{$userId}_{$bid}";
+        $check = RedisService::get($cacheKey);
+        if($bid && !$check){
+            RedisService::set($cacheKey, $bid, 3600);
+            return $this->model::where(['id'=> $bid])->increment('view_num', 1);
+        }
+    }
 }

+ 1 - 0
routes/api.php

@@ -206,6 +206,7 @@ Route::post('/donates/mecryList', [\App\Http\Controllers\Api\v1\DonatesControlle
 
 // 佛经
 Route::post('/buddhist/list', [\App\Http\Controllers\Api\v1\BuddhistController::class, 'index']);
+Route::post('/buddhist/detail', [\App\Http\Controllers\Api\v1\BuddhistController::class, 'detail']);
 Route::post('/buddhist/cates', [\App\Http\Controllers\Api\v1\BuddhistController::class, 'cates']);
 Route::post('/buddhist/page', [\App\Http\Controllers\Api\v1\BuddhistController::class, 'page']);