Re:龙城男人|建议收藏帖子api返回改成201

发表:2年前 更新:2年前

@龙城男人

我帮你写好了,你把 namespace,类名、方法名 改一下就好

路由重写需要注意的几个点:

  1. 插件是否已启动
  2. 是否在插件内做的路由重写
<?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'=>'已收藏']);
	}
}
signature
保持理智
评论

无权限

请登录后评论

RunPod公众号

赞助商

联系我们

QQ群:798695907

邮箱:laravel@88.com