2025-12-08 17:37:34 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
namespace app\common\controller;
|
|
|
|
|
|
|
|
|
|
|
|
use app\common\controller\BaseController;
|
|
|
|
|
|
use app\common\model\Post;
|
|
|
|
|
|
use app\common\model\PostComment;
|
|
|
|
|
|
use app\common\model\PostFavorite;
|
|
|
|
|
|
use app\common\model\PostLike;
|
|
|
|
|
|
use app\common\model\PostMedia;
|
2025-12-10 14:59:41 +08:00
|
|
|
|
use app\common\model\Sku;
|
2025-12-08 17:37:34 +08:00
|
|
|
|
use app\common\model\User;
|
|
|
|
|
|
use app\common\service\OssService;
|
|
|
|
|
|
use app\common\traits\crud\CrudControllerTrait;
|
|
|
|
|
|
use app\common\service\PostService;
|
|
|
|
|
|
use support\Request;
|
|
|
|
|
|
|
|
|
|
|
|
class PostController extends BaseController
|
|
|
|
|
|
{
|
|
|
|
|
|
use CrudControllerTrait;
|
|
|
|
|
|
|
|
|
|
|
|
// protected $noNeedLogin = ['index'];
|
|
|
|
|
|
//
|
|
|
|
|
|
// protected $noNeedHeaderData = ['index'];
|
|
|
|
|
|
|
|
|
|
|
|
protected $service;
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->service = new PostService();
|
|
|
|
|
|
}
|
2025-12-12 15:31:44 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取列表
|
|
|
|
|
|
*
|
|
|
|
|
|
* @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());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-08 17:37:34 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取列表
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param Request $request 请求对象
|
|
|
|
|
|
* @return mixed 返回分页数据及总数
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function index(Request $request)
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
$raw = $this->service->serviceList($request);
|
|
|
|
|
|
$data = $this->transformData($raw, method_exists($this, 'indexFormat')
|
|
|
|
|
|
? [$this, 'indexFormat']
|
|
|
|
|
|
: null);
|
|
|
|
|
|
$records = $data['records'];
|
|
|
|
|
|
foreach ($records as &$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) {
|
2025-12-12 15:31:44 +08:00
|
|
|
|
return $this->error($e->getMessage());
|
2025-12-08 17:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 列表页数据格式化
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param array $data
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function indexFormat($data)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isset($data['created_at']))
|
|
|
|
|
|
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
|
|
|
|
|
if (isset($data['updated_at']))
|
|
|
|
|
|
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
|
|
|
|
|
$ossservice = new OssService();
|
|
|
|
|
|
if (isset($data['id'])){
|
|
|
|
|
|
$data['media'] = PostMedia::where('post_id', $data['id'])->get()->toArray();
|
2025-12-13 13:38:50 +08:00
|
|
|
|
foreach ($data['media'] as $key => $media) {
|
|
|
|
|
|
if(empty($media['url'])){
|
|
|
|
|
|
unset($data['media'][$key]);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
$data['media'][$key]['url_pull'] = $ossservice->generatePrivateUrl($media['url']);
|
2025-12-08 17:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (isset($data['user_id'])){
|
|
|
|
|
|
$data['user'] = User::where('id', $data['user_id'])->first();
|
|
|
|
|
|
unset($data['user']['password']);
|
|
|
|
|
|
$data['user']['avatar_pull'] = $data['user']['avatar'];
|
|
|
|
|
|
if(!str_contains($data['user']['avatar'], 'http') && !empty($data['user']['avatar'])){
|
|
|
|
|
|
$data['user']['avatar_pull'] = $ossservice->generatePrivateUrl($data['user']['avatar']);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-10 14:59:41 +08:00
|
|
|
|
|
|
|
|
|
|
if (isset($data['sku_id'])){
|
2025-12-12 13:38:15 +08:00
|
|
|
|
$sku = Sku::where('id', $data['sku_id'])->first()->toArray();
|
2025-12-10 14:59:41 +08:00
|
|
|
|
|
2026-01-12 09:51:35 +08:00
|
|
|
|
if($sku && !empty($sku['sku_id'])){
|
2025-12-10 14:59:41 +08:00
|
|
|
|
$sku['main_image_pull'] = $ossservice->generatePrivateUrl($sku['main_image']);
|
|
|
|
|
|
}
|
2025-12-11 14:17:24 +08:00
|
|
|
|
|
|
|
|
|
|
if (!empty($sku['image'])){
|
|
|
|
|
|
$sku['image'] = explode(',',$sku['image']);
|
|
|
|
|
|
foreach ($sku['image'] as $key => $image){
|
|
|
|
|
|
$sku['image_pull'][$key] = $ossservice->generatePrivateUrl($image);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-12 09:51:35 +08:00
|
|
|
|
if (!empty($sku['spec_json']))
|
2025-12-11 14:17:24 +08:00
|
|
|
|
$sku['spec'] = json_decode($sku['spec_json'], true);
|
|
|
|
|
|
|
2025-12-10 14:59:41 +08:00
|
|
|
|
$data['sku'] = $sku;
|
|
|
|
|
|
}
|
2025-12-08 17:37:34 +08:00
|
|
|
|
return $data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取评论,点赞,收藏数量
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param Request $request 请求对象
|
|
|
|
|
|
* @param int|string $id 资源 ID
|
|
|
|
|
|
* @return mixed 返回资源详情数据
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function interaction(Request $request)
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
$param = $request->all();
|
|
|
|
|
|
$data = Post::where('id', $param['id'])->select(['like','favorite','comment'])->first();
|
|
|
|
|
|
|
|
|
|
|
|
if(!empty($request->user) && !empty($request->user->uid)){
|
|
|
|
|
|
$where = [
|
|
|
|
|
|
'post_id' => $data['id'],
|
|
|
|
|
|
'user_id' => $request->user->uid
|
|
|
|
|
|
];
|
|
|
|
|
|
$data['is_like'] = PostLike::where($where)->count();
|
|
|
|
|
|
$data['is_favorite'] = PostFavorite::where($where)->count();
|
|
|
|
|
|
$data['is_comment'] = PostComment::where($where)->count();
|
|
|
|
|
|
}else{
|
|
|
|
|
|
$data['is_like'] = 0;
|
|
|
|
|
|
$data['is_favorite'] = 0;
|
|
|
|
|
|
$data['is_comment'] = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $this->success($data);
|
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
|
return $this->error($e->getMessage().$e->getFile().$e->getLine());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取详情
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param Request $request 请求对象
|
|
|
|
|
|
* @param int|string $id 资源 ID
|
|
|
|
|
|
* @return mixed 返回资源详情数据
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function show(Request $request)
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
$raw = $this->service->serviceShow($request);
|
|
|
|
|
|
$data = $this->transformData($raw, method_exists($this, 'showFormat')
|
|
|
|
|
|
? [$this, 'showFormat']
|
|
|
|
|
|
: null);
|
|
|
|
|
|
|
2025-12-22 11:05:01 +08:00
|
|
|
|
// if(!empty($request->user) && !empty($request->user->uid)){
|
|
|
|
|
|
// $where = [
|
|
|
|
|
|
// 'post_id' => $data['id'],
|
|
|
|
|
|
// 'user_id' => $request->user->uid
|
|
|
|
|
|
// ];
|
|
|
|
|
|
// $data['is_like'] = PostLike::where($where)->count();
|
|
|
|
|
|
// $data['is_favorite'] = PostFavorite::where($where)->count();
|
|
|
|
|
|
// $data['is_comment'] = PostComment::where($where)->count();
|
|
|
|
|
|
// }else{
|
|
|
|
|
|
// $data['is_like'] = 0;
|
|
|
|
|
|
// $data['is_favorite'] = 0;
|
|
|
|
|
|
// $data['is_comment'] = 0;
|
|
|
|
|
|
// }
|
2025-12-08 17:37:34 +08:00
|
|
|
|
|
|
|
|
|
|
return $this->success($data);
|
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
|
return $this->error($e->getMessage().$e->getFile().$e->getLine());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 列表页数据格式化
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param array $data
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function showFormat($data)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isset($data['created_at']))
|
|
|
|
|
|
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
|
|
|
|
|
if (isset($data['updated_at']))
|
|
|
|
|
|
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
|
|
|
|
|
$ossservice = new OssService();
|
|
|
|
|
|
if (isset($data['id'])){
|
|
|
|
|
|
$data['media'] = PostMedia::where('post_id', $data['id'])->get()->toArray();
|
|
|
|
|
|
foreach ($data['media'] as &$media) {
|
|
|
|
|
|
$media['url_pull'] = $ossservice->generatePrivateUrl($media['url']);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (isset($data['user_id'])){
|
|
|
|
|
|
$data['user'] = User::where('id', $data['user_id'])->first();
|
|
|
|
|
|
unset($data['user']['password']);
|
|
|
|
|
|
$data['user']['avatar_pull'] = $data['user']['avatar'];
|
|
|
|
|
|
if(!str_contains($data['user']['avatar'], 'http') && !empty($data['user']['avatar'])){
|
|
|
|
|
|
$data['user']['avatar_pull'] = $ossservice->generatePrivateUrl($data['user']['avatar']);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-22 11:05:01 +08:00
|
|
|
|
|
2025-12-12 09:53:02 +08:00
|
|
|
|
if (isset($data['sku_id'])){
|
2025-12-12 13:45:19 +08:00
|
|
|
|
$sku = Sku::where('id', $data['sku_id'])->first()->toArray();;
|
2025-12-12 09:53:02 +08:00
|
|
|
|
|
2026-01-12 09:53:59 +08:00
|
|
|
|
if($sku && !empty($sku['sku_id'])){
|
2025-12-12 09:53:02 +08:00
|
|
|
|
$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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-12 09:53:59 +08:00
|
|
|
|
if (!empty($sku['spec_json'])){
|
2025-12-12 09:53:02 +08:00
|
|
|
|
$sku['spec'] = json_decode($sku['spec_json'], true);
|
2025-12-22 10:40:03 +08:00
|
|
|
|
foreach ($sku['spec'] as $key => $spec){
|
2025-12-22 11:05:01 +08:00
|
|
|
|
if($spec['attr_name'] == '产品卖点'){
|
2026-01-12 09:51:35 +08:00
|
|
|
|
$spec['attr_value'] = str_replace([' ',','], ',', $spec['attr_value']);
|
2025-12-22 11:05:01 +08:00
|
|
|
|
$spec['attr_values'] = explode(',',$spec['attr_value']);
|
2026-01-12 09:51:35 +08:00
|
|
|
|
|
|
|
|
|
|
$spec['attr_values'] = array_filter($spec['attr_values']); //过滤空值
|
2025-12-22 11:05:01 +08:00
|
|
|
|
$sku['point'] = $spec;
|
2025-12-22 10:40:03 +08:00
|
|
|
|
unset($sku['spec'][$key]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-12 09:53:02 +08:00
|
|
|
|
|
|
|
|
|
|
$data['sku'] = $sku;
|
|
|
|
|
|
}
|
2025-12-08 17:37:34 +08:00
|
|
|
|
return $data;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|