91 lines
2.7 KiB
PHP
91 lines
2.7 KiB
PHP
<?php
|
|
namespace app\common\controller;
|
|
|
|
use app\common\controller\BaseController;
|
|
use app\common\model\PostComment;
|
|
use app\common\model\PostFavorite;
|
|
use app\common\model\PostLike;
|
|
use app\common\model\PostMedia;
|
|
use app\common\traits\crud\CrudControllerTrait;
|
|
use app\common\service\PostService;
|
|
use support\Request;
|
|
|
|
class PostController extends BaseController
|
|
{
|
|
use CrudControllerTrait;
|
|
|
|
protected $service;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->service = new PostService();
|
|
}
|
|
/**
|
|
* 获取列表
|
|
*
|
|
* @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;
|
|
var_dump($record['id']);
|
|
$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();
|
|
}
|
|
$data['records'] = $records;
|
|
return $this->success($data);
|
|
} catch (\Throwable $e) {
|
|
return $this->error($e->getMessage().$e->getFile().$e->getLine());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 列表页数据格式化
|
|
*
|
|
* @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']);
|
|
if (isset($data['id'])){
|
|
$data['media'] = PostMedia::where('post_id', $data['id'])->get();
|
|
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 列表页数据格式化
|
|
*
|
|
* @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']);
|
|
if (isset($data['id']))
|
|
$data['media'] = PostMedia::where('post_id', $data['id'])->get();
|
|
return $data;
|
|
}
|
|
} |