最近距离,我的收藏

This commit is contained in:
2025-12-12 15:31:44 +08:00
parent f5ea63a883
commit b1eb0468c6
4 changed files with 129 additions and 3 deletions
+43 -1
View File
@@ -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());
}
}
@@ -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;
}
+64 -1
View File
@@ -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
{
// 提取后缀
+1
View File
@@ -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']);