我帮你写好了,你把 namespace,类名、方法名 改一下就好
路由重写需要注意的几个点:
- 插件是否已启动
- 是否在插件内做的路由重写
<?php
namespace App\Plugins\Comment\src\Controller;
use App\CodeFec\Annotation\RouteRewrite;
use App\Plugins\Topic\src\Models\Topic;
use App\Plugins\User\src\Models\UsersCollection;
use Hyperf\RateLimit\Annotation\RateLimit;
class TestController
{
#[RouteRewrite(route:"/api/topic/star.topic",method:"POST")]
#[RateLimit(create:1, capacity:3)]
public function test(){
if(!auth()->check()){
return Json_Api(401,false,['msg' => '权限不足!']);
}
$topic_id = request()->input("topic_id");
if(!$topic_id){
return Json_Api(403,false,['msg' => '请求参数不足,缺少:topic_id']);
}
if(!Topic::query()->where("id",$topic_id)->exists()){
return Json_Api(403,false,['msg' => '要收藏的帖子不存在']);
}
if(UsersCollection::query()->where(['type' => 'topic','type_id' => $topic_id,'user_id' => auth()->id()])->exists()){
UsersCollection::query()->where(['type' => 'topic','type_id' => $topic_id,'user_id' => auth()->id()])->delete();
return Json_Api(201,true,['msg' => '取消收藏成功!']);
}
UsersCollection::query()->create([
'user_id' => auth()->id(),
'type' => 'topic',
'type_id' => $topic_id,
]);
return Json_Api(200,true,['msg'=>'已收藏']);
}
}