From b1eb0468c678f75dabf6f9b921c25f38ead73dbf Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Fri, 12 Dec 2025 15:31:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=80=E8=BF=91=E8=B7=9D=E7=A6=BB=EF=BC=8C?= =?UTF-8?q?=E6=88=91=E7=9A=84=E6=94=B6=E8=97=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/controller/PostController.php | 44 ++++++++++++- .../controller/PostFavoriteController.php | 22 ++++++- app/common/service/PostService.php | 65 ++++++++++++++++++- config/route.php | 1 + 4 files changed, 129 insertions(+), 3 deletions(-) diff --git a/app/common/controller/PostController.php b/app/common/controller/PostController.php index 211bd0d..5de8a2f 100644 --- a/app/common/controller/PostController.php +++ b/app/common/controller/PostController.php @@ -28,6 +28,48 @@ class PostController extends BaseController { $this->service = new PostService(); } + /** + * 获取列表 + * + * @param Request $request 请求对象 + * @return mixed 返回分页数据及总数 + */ + public function nearby(Request $request) + { + try { + $raw = $this->service->serviceNearby($request); + $records = $this->transformData($raw, method_exists($this, 'indexFormat') + ? [$this, 'indexFormat'] + : null); +// $records = $data; + foreach ($records as &$record) { + $record = $this->indexFormat($record); + if (!isset($record['id'])) + break; + if(!empty($request->user) && !empty($request->user->uid)){ + $where = [ + 'post_id' => $record['id'], + 'user_id' => $request->user->uid + ]; + $record['is_like'] = PostLike::where($where)->count(); + $record['is_favorite'] = PostFavorite::where($where)->count(); + $record['is_comment'] = PostComment::where($where)->count(); + }else{ + $record['is_like'] = 0; + $record['is_favorite'] = 0; + $record['is_comment'] = 0; + } + $created_at = strtotime($record['created_at']); + $record['year'] = date('Y', $created_at); + $record['month'] = date('m', $created_at); + $record['day'] = date('d', $created_at); + } + $data['records'] = $records; + return $this->success($data); + } catch (\Throwable $e) { + return $this->error($e->getMessage()); + } + } /** * 获取列表 * @@ -66,7 +108,7 @@ class PostController extends BaseController $data['records'] = $records; return $this->success($data); } catch (\Throwable $e) { - return $this->error($e->getMessage().$e->getFile().$e->getLine()); + return $this->error($e->getMessage()); } } diff --git a/app/common/controller/PostFavoriteController.php b/app/common/controller/PostFavoriteController.php index 223d2ea..5d89388 100644 --- a/app/common/controller/PostFavoriteController.php +++ b/app/common/controller/PostFavoriteController.php @@ -4,6 +4,7 @@ namespace app\common\controller; use app\common\controller\BaseController; use app\common\model\Post; use app\common\model\PostMedia; +use app\common\model\Sku; use app\common\model\User; use app\common\service\OssService; use app\common\traits\crud\CrudControllerTrait; @@ -76,8 +77,27 @@ class PostFavoriteController extends BaseController $media['url_pull'] = $ossservice->generatePrivateUrl($media['url']); } } + $post['is_favorite'] = 1; - $data['post'] = $post; + if (isset($post['sku_id'])){ + $sku = Sku::where('id', $post['sku_id'])->first()->toArray(); + + if($sku){ + $sku['main_image_pull'] = $ossservice->generatePrivateUrl($sku['main_image']); + } + + if (!empty($sku['image'])){ + $sku['image'] = explode(',',$sku['image']); + foreach ($sku['image'] as $key => $image){ + $sku['image_pull'][$key] = $ossservice->generatePrivateUrl($image); + } + } + if (isset($sku['spec_json'])) + $sku['spec'] = json_decode($sku['spec_json'], true); + + $post['sku'] = $sku; + } + $post['post'] = $post; } return $data; } diff --git a/app/common/service/PostService.php b/app/common/service/PostService.php index 857f69b..f9bd463 100644 --- a/app/common/service/PostService.php +++ b/app/common/service/PostService.php @@ -11,6 +11,7 @@ use app\common\model\Post; use support\exception\BusinessException; use support\Redis; use support\Request; +use support\Db; class PostService { @@ -71,7 +72,12 @@ class PostService if (isset($p['keyword']) && $p['keyword']) { $config['where']['content'] = ['like', '%'.$p['keyword'].'%']; } - $config['where']['status'] = 1; + + if (isset($p['status']) && $p['status']) { + $config['where']['status'] = $p['status']; + }else{ + $config['where']['status'] = 1; + } if (isset($p['favorite']) && $p['favorite']) { $config['order']['favorite'] = 'desc'; @@ -182,6 +188,63 @@ class PostService return $data; } +// /** 分页列表 */ +// public function serviceNearby(Request $request, string $scene = 'index') +// { +// $result = $this->cqyProcess(ucfirst($scene), $request); +// if ($result === false) { +// $config = $this->buildQueryConfig($request, $scene); +// $limit = (int)$request->get('size', 15); +// $page = (int)$request->get('current', 1); +// $result = $this->logicList($config, $limit, $page); +// } +// return $this->postProcess(ucfirst($scene), $result); +// } + + /** 分页列表 */ + public function serviceNearby(Request $request, string $scene = 'nearby') + { + $lng = $request->get('lng'); + $lat = $request->get('lat'); + + if (!$lng || !$lat) { + throw new BusinessException('经纬度必传'); + } + + $sql = "SELECT + id, + user_id, + category_id, + sku_id, + location_name, + content, + latitude, + longitude, + created_at, + updated_at, + (6371000 * ACOS( + COS(RADIANS(?)) * COS(RADIANS(latitude)) * + COS(RADIANS(longitude) - RADIANS(?)) + + SIN(RADIANS(?)) * SIN(RADIANS(latitude)) + )) AS distance + FROM post + HAVING distance <= ? + ORDER BY distance + LIMIT 50"; + + // 根据场景设置半径 + $radius = $scene === 'nearby' ? 5000 : 20000; + +// $query = Db::select($sql, [ +// 'lat' => $lat, +// 'lng' => $lng, +// 'radius' => $radius +// ]); + $query = Db::select($sql, [$lat, $lng, $lat, $radius]); + $result =json_decode(json_encode($query), true); + return $result; + } + function getMediaTypeByUrl(string $url): int { // 提取后缀 diff --git a/config/route.php b/config/route.php index 242f8a8..8014a94 100644 --- a/config/route.php +++ b/config/route.php @@ -56,6 +56,7 @@ Route::group('/common', function () { // 帖子 Route::group('/post', function () { Route::any('/list', [app\common\controller\PostController::class, 'index']); + Route::any('/nearby', [app\common\controller\PostController::class, 'nearby']); Route::any('/show', [app\common\controller\PostController::class, 'show']); Route::any('/interaction', [app\common\controller\PostController::class, 'interaction']); Route::any('/store', [app\common\controller\PostController::class, 'store']);