1、帖子搜索
2、收藏搜索 3、帖子、评论删除
This commit is contained in:
@@ -70,7 +70,8 @@ class PostCommentController extends BaseController
|
||||
public function delete(Request $request)
|
||||
{
|
||||
try {
|
||||
$count = $this->service->serviceDelete($request);
|
||||
$param = $request->all();
|
||||
$count = $this->service->serviceDelete($request,'mini');
|
||||
return $this->success([], "删除评论成功");
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage().$e->getLine().$e->getFile());
|
||||
|
||||
@@ -196,4 +196,20 @@ class PostController extends BaseController
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param Request $request 请求对象
|
||||
* @return mixed 返回删除操作结果
|
||||
*/
|
||||
public function destroy(Request $request)
|
||||
{
|
||||
try {
|
||||
$count = $this->service->serviceDelete($request, 'mini');
|
||||
return $this->success([], "成功删除了 {$count} 条");
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,8 @@ class PostFavoriteController extends BaseController
|
||||
public function self(Request $request)
|
||||
{
|
||||
try {
|
||||
$raw = $this->service->serviceSelfList($request);
|
||||
// $raw = $this->service->serviceSelfList($request);
|
||||
$raw = $this->service->serviceList($request);
|
||||
$data = $this->transformData($raw, method_exists($this, 'indexFormat')
|
||||
? [$this, 'indexFormat']
|
||||
: null);
|
||||
|
||||
@@ -40,7 +40,7 @@ class Post extends Model
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = ['id', 'user_id', 'category_id', 'content', 'location_name', 'latitude', 'longitude', 'status', 'sort', 'created_at', 'updated_at'];
|
||||
protected $fillable = ['id', 'user_id', 'category_id', 'content', 'location_name', 'latitude', 'longitude', 'status', 'sort', 'like', 'favorite', 'comment', 'browse', 'created_at', 'updated_at'];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
|
||||
@@ -46,4 +46,9 @@ class PostFavorite extends Model
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
public function post()
|
||||
{
|
||||
return $this->hasOne('app\common\model\Post', 'id', 'post_id');
|
||||
}
|
||||
}
|
||||
@@ -129,12 +129,10 @@ class PostCommentService
|
||||
$post->save();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 添加后置处理
|
||||
*/
|
||||
public function serviceDelete(Request $request)
|
||||
public function serviceDelete(Request $request,$scene = 'admin')
|
||||
{
|
||||
$param = $request->all();
|
||||
|
||||
@@ -146,6 +144,10 @@ class PostCommentService
|
||||
$where['id'] = $param['id'];
|
||||
$comment = PostComment::where($where)->first();
|
||||
|
||||
if($scene == 'mini' && $comment['user_id'] != $request->user->uid){
|
||||
throw new \Exception("只能删除本人评论");
|
||||
}
|
||||
|
||||
$post = Post::where(['id' => $comment['post_id']])->first();
|
||||
$post['comment'] = $post['comment']-1;
|
||||
$post->save();
|
||||
|
||||
@@ -46,6 +46,25 @@ class PostFavoriteService
|
||||
return $this->postProcess(ucfirst($scene), $result);
|
||||
}
|
||||
|
||||
public function complexQueryIndex(Request $request)
|
||||
{
|
||||
$param = $request->all();
|
||||
$limit = (int)$request->get('size', 15);
|
||||
$page = (int)$request->get('current', 1);
|
||||
$query = PostFavorite::with(['post'])
|
||||
->where('user_id',$request->user->uid)
|
||||
->whereHas('post', function ($q) use ($param) {
|
||||
// 固定过滤 status = 1
|
||||
$q->where('status', 1);
|
||||
|
||||
// 关键字过滤(如果存在)
|
||||
if (!empty($param['keyword'])) {
|
||||
$q->where('content', 'like', "%{$param['keyword']}%");
|
||||
}
|
||||
});
|
||||
return $query->paginate($limit, ['*'], 'page', $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造 CrudUtil 查询配置
|
||||
*/
|
||||
|
||||
@@ -67,9 +67,19 @@ class PostService
|
||||
'between', [$from, $to]
|
||||
];
|
||||
}
|
||||
if (isset($p['keyword']) && $p['keyword']) {
|
||||
$config['where']['content'] = ['like', '%'.$p['keyword'].'%'];
|
||||
}
|
||||
$config['where']['status'] = 1;
|
||||
$config['order']['sort'] = 'desc';
|
||||
$config['order']['id'] = 'desc';
|
||||
|
||||
if (isset($p['order']) && $p['order'] == 'favorite') {
|
||||
$config['order']['favorite'] = 'desc';
|
||||
} elseif (isset($p['order']) && $p['order'] == 'browse') {
|
||||
$config['order']['browse'] = 'desc';
|
||||
} else {
|
||||
$config['order']['id'] = 'desc';
|
||||
}
|
||||
break;
|
||||
case 'show':
|
||||
$config['where']['id'] = $p['id'];
|
||||
@@ -154,6 +164,20 @@ class PostService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加前置处理
|
||||
*/
|
||||
public function beforeServiceShow($data, Request $request)
|
||||
{
|
||||
$post = Post::find($data['id']);
|
||||
if(!$post){
|
||||
$post->browse = $post->browse+1;
|
||||
$post->save();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
function getMediaTypeByUrl(string $url): int
|
||||
{
|
||||
// 提取后缀
|
||||
@@ -172,5 +196,27 @@ class PostService
|
||||
}
|
||||
|
||||
return 0; // 未知类型
|
||||
} /**
|
||||
* 添加后置处理
|
||||
*/
|
||||
public function serviceDelete(Request $request,$scene = 'admin')
|
||||
{
|
||||
$param = $request->all();
|
||||
|
||||
if (empty($param['id'])) {
|
||||
throw new \Exception("参数错误");
|
||||
}
|
||||
|
||||
$where = [];
|
||||
$where['id'] = $param['id'];
|
||||
$post = Post::where($where)->first();
|
||||
|
||||
if($scene == 'mini' && $post['user_id'] != $request->user->uid){
|
||||
throw new \Exception("只能删除本人发布内容");
|
||||
}
|
||||
|
||||
$count = $post->delete();
|
||||
|
||||
return $count;
|
||||
}
|
||||
}
|
||||
@@ -59,6 +59,7 @@ Route::group('/common', function () {
|
||||
Route::any('/show', [app\common\controller\PostController::class, 'show']);
|
||||
Route::any('/interaction', [app\common\controller\PostController::class, 'interaction']);
|
||||
Route::any('/store', [app\admin\controller\PostController::class, 'store']);
|
||||
Route::any('/destroy', [app\common\controller\PostController::class, 'destroy']);
|
||||
|
||||
// 评论
|
||||
Route::group('/comment', function () {
|
||||
|
||||
Reference in New Issue
Block a user