优化代码

main
gzydong 2021-05-23 17:28:04 +08:00
parent b65d427011
commit a836395976
6 changed files with 17 additions and 18 deletions

View File

@ -2,12 +2,11 @@
namespace App\Exception\Handler; namespace App\Exception\Handler;
use Qbhy\HyperfAuth\Exception\AuthException;
use Throwable; use Throwable;
use Hyperf\ExceptionHandler\ExceptionHandler; use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream; use Hyperf\HttpMessage\Stream\SwooleStream;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Phper666\JWTAuth\Exception\TokenValidException;
use Hyperf\WebSocketServer\Exception\WebSocketHandeShakeException;
/** /**
* Class JwtAuthExceptionHandler * Class JwtAuthExceptionHandler
@ -19,7 +18,7 @@ class JwtAuthExceptionHandler extends ExceptionHandler
public function handle(Throwable $throwable, ResponseInterface $response) public function handle(Throwable $throwable, ResponseInterface $response)
{ {
// 判断被捕获到的异常是希望被捕获的异常 // 判断被捕获到的异常是希望被捕获的异常
if ($throwable instanceof TokenValidException) { if ($throwable instanceof AuthException) {
// 格式化输出 // 格式化输出
$data = json_encode([ $data = json_encode([
'code' => $throwable->getCode(), 'code' => $throwable->getCode(),

View File

@ -29,7 +29,7 @@ class WebSocketExceptionHandler extends ExceptionHandler
public function handle(Throwable $throwable, ResponseInterface $response) public function handle(Throwable $throwable, ResponseInterface $response)
{ {
$stream = new SwooleStream((string)$throwable->getMessage()); $stream = new SwooleStream($throwable->getMessage());
return $response->withBody($stream); return $response->withBody($stream);
} }

View File

@ -70,7 +70,7 @@ class Group extends BaseModel
*/ */
public static function isManager(int $user_id, int $group_id, $leader = 2) public static function isManager(int $user_id, int $group_id, $leader = 2)
{ {
return self::where('id', $group_id)->where('creator_id', $user_id)->exists(); return self::query()->where('id', $group_id)->where('creator_id', $user_id)->exists();
} }
/** /**
@ -81,7 +81,7 @@ class Group extends BaseModel
*/ */
public static function isDismiss(int $group_id) public static function isDismiss(int $group_id)
{ {
return self::where('id', $group_id)->where('is_dismiss', 1)->exists(); return self::query()->where('id', $group_id)->where('is_dismiss', 1)->exists();
} }
/** /**

View File

@ -52,7 +52,7 @@ class GroupMember extends BaseModel
*/ */
public static function getGroupMemberIds(int $group_id) public static function getGroupMemberIds(int $group_id)
{ {
return self::where('group_id', $group_id)->where('is_quit', 0)->pluck('user_id')->toArray() ?? []; return self::query()->where('group_id', $group_id)->where('is_quit', 0)->pluck('user_id')->toArray() ?? [];
} }
/** /**
@ -64,7 +64,7 @@ class GroupMember extends BaseModel
*/ */
public static function visitCard(int $user_id, int $group_id) public static function visitCard(int $user_id, int $group_id)
{ {
return self::where('group_id', $group_id)->where('user_id', $user_id)->value('user_card') ?? ""; return self::query()->where('group_id', $group_id)->where('user_id', $user_id)->value('user_card') ?? "";
} }
/** /**
@ -75,6 +75,6 @@ class GroupMember extends BaseModel
*/ */
public static function getUserGroupIds(int $user_id) public static function getUserGroupIds(int $user_id)
{ {
return self::where('user_id', $user_id)->where('is_quit', 0)->pluck('group_id')->toArray() ?? []; return self::query()->where('user_id', $user_id)->where('is_quit', 0)->pluck('group_id')->toArray() ?? [];
} }
} }

View File

@ -58,7 +58,7 @@ class UsersChatList extends BaseModel
*/ */
public static function addItem(int $user_id, int $receive_id, int $type) public static function addItem(int $user_id, int $receive_id, int $type)
{ {
$result = self::where('uid', $user_id)->where('type', $type)->where($type == 1 ? 'friend_id' : 'group_id', $receive_id)->first(); $result = self::query()->where('uid', $user_id)->where('type', $type)->where($type == 1 ? 'friend_id' : 'group_id', $receive_id)->first();
if ($result) { if ($result) {
$result->status = 1; $result->status = 1;
$result->updated_at = date('Y-m-d H:i:s'); $result->updated_at = date('Y-m-d H:i:s');
@ -72,7 +72,7 @@ class UsersChatList extends BaseModel
]; ];
} }
if (!$result = self::create([ if (!$result = self::query()->create([
'type' => $type, 'type' => $type,
'uid' => $user_id, 'uid' => $user_id,
'status' => 1, 'status' => 1,
@ -102,7 +102,7 @@ class UsersChatList extends BaseModel
*/ */
public static function topItem(int $user_id, int $list_id, $is_top = true) public static function topItem(int $user_id, int $list_id, $is_top = true)
{ {
return (bool)self::where('id', $list_id)->where('uid', $user_id)->update([ return (bool)self::query()->where('id', $list_id)->where('uid', $user_id)->update([
'is_top' => $is_top ? 1 : 0, 'is_top' => $is_top ? 1 : 0,
'updated_at' => date('Y-m-d H:i:s') 'updated_at' => date('Y-m-d H:i:s')
]); ]);
@ -120,11 +120,11 @@ class UsersChatList extends BaseModel
{ {
$data = ['status' => 0, 'updated_at' => date('Y-m-d H:i:s')]; $data = ['status' => 0, 'updated_at' => date('Y-m-d H:i:s')];
if ($type == 1) { if ($type == 1) {
return (bool)self::where('id', $id)->where('uid', $user_id)->update($data); return (bool)self::query()->where('id', $id)->where('uid', $user_id)->update($data);
} else if ($type == 2) { } else if ($type == 2) {
return (bool)self::where('uid', $user_id)->where('friend_id', $id)->update($data); return (bool)self::query()->where('uid', $user_id)->where('friend_id', $id)->update($data);
} else { } else {
return (bool)self::where('uid', $user_id)->where('group_id', $id)->update($data); return (bool)self::query()->where('uid', $user_id)->where('group_id', $id)->update($data);
} }
} }
@ -139,11 +139,11 @@ class UsersChatList extends BaseModel
*/ */
public static function notDisturbItem(int $user_id, int $receive_id, int $type, int $not_disturb) public static function notDisturbItem(int $user_id, int $receive_id, int $type, int $not_disturb)
{ {
$result = self::where('uid', $user_id)->where($type == 1 ? 'friend_id' : 'group_id', $receive_id)->where('status', 1)->first(['id', 'not_disturb']); $result = self::query()->where('uid', $user_id)->where($type == 1 ? 'friend_id' : 'group_id', $receive_id)->where('status', 1)->first(['id', 'not_disturb']);
if (!$result || $not_disturb == $result->not_disturb) { if (!$result || $not_disturb == $result->not_disturb) {
return false; return false;
} }
return (bool)self::where('id', $result->id)->update(['not_disturb' => $not_disturb]); return (bool)self::query()->where('id', $result->id)->update(['not_disturb' => $not_disturb]);
} }
} }

View File

@ -92,7 +92,7 @@ SQL;
return true; return true;
} }
$isTrue = self::where('user1', $user_id1)->where('user2', $user_id2)->where('status', 1)->exists(); $isTrue = self::query()->where('user1', $user_id1)->where('user2', $user_id2)->where('status', 1)->exists();
if ($isTrue) { if ($isTrue) {
redis()->setex($cacheKey, 60 * 5, 1); redis()->setex($cacheKey, 60 * 5, 1);
} }