优化代码

main
gzydong 2021-08-29 22:24:45 +08:00
parent 649a90ca0e
commit d07ae5203f
4 changed files with 55 additions and 42 deletions

View File

@ -64,7 +64,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
// 若开启单点登录,则主动关闭之前登录的连接
if ($isOnline) {
// TODO 预留
// todo 预留
}
// 绑定fd与用户关系

View File

@ -102,35 +102,6 @@ abstract class BaseRepository
return $this->toPaginate($model, $fields, $page, $size);
}
/**
* 通过 model 读取分页信息
*
* @param Builder $model 查询 Model
* @param array $fields 查询字段
* @param int $page 当前分页
* @param int $size 分页大小
* @return array
*/
public function toPaginate(Builder $model, array $fields = ['*'], int $page = 1, int $size = 15): array
{
$total = $model->count();
$data = [
'rows' => [],
'paginate' => [
'page' => $page,
'size' => $size,
'total' => $total,
]
];
if ($total > 0) {
$data['rows'] = $model->forPage($page, $size)->get($fields)->toArray();
}
return $data;
}
/**
* 根据条件更新数据
*
@ -176,6 +147,17 @@ abstract class BaseRepository
return $this->buildWhere($where)->update($data);
}
/**
* 删除数据
*
* @param array $where 删除的条件
* @return array
*/
final public function delete(array $where): array
{
return $this->buildWhere($where)->delete();
}
/**
* 打印查询 sql
*
@ -199,4 +181,33 @@ abstract class BaseRepository
{
return Db::select($query, $bindings, $useReadPdo);
}
/**
* 通过 model 读取分页信息
*
* @param Builder $model 查询 Model
* @param array $fields 查询字段
* @param int $page 当前分页
* @param int $size 分页大小
* @return array
*/
public function toPaginate(Builder $model, array $fields = ['*'], int $page = 1, int $size = 15): array
{
$total = $model->count();
$data = [
'rows' => [],
'paginate' => [
'page' => $page,
'size' => $size,
'total' => $total,
]
];
if ($total > 0) {
$data['rows'] = $model->forPage($page, $size)->get($fields)->toArray();
}
return $data;
}
}

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace App\Service;
@ -14,12 +15,13 @@ class TalkListService
/**
* 创建聊天列表记录
*
* @param int $user_id 用户ID
* @param int $receiver_id 接收者ID
* @param int $talk_type 创建类型[1:私聊;2:群聊;]
* @param int $user_id 用户ID
* @param int $receiver_id 接收者ID
* @param int $talk_type 创建类型[1:私聊;2:群聊;]
* @param bool $is_robot
* @return array
*/
public function create(int $user_id, int $receiver_id, int $talk_type, bool $is_robot = false)
public function create(int $user_id, int $receiver_id, int $talk_type, bool $is_robot = false): array
{
$result = TalkList::updateOrCreate([
'talk_type' => $talk_type,
@ -49,7 +51,7 @@ class TalkListService
* @param bool $is_top 是否置顶true: false:否)
* @return bool
*/
public function top(int $user_id, int $list_id, $is_top = true)
public function top(int $user_id, int $list_id, $is_top = true): bool
{
return (bool)TalkList::query()->where([
['id', '=', $list_id],
@ -67,7 +69,7 @@ class TalkListService
* @param int $list_id 会话列表ID
* @return bool
*/
public function delete(int $user_id, int $list_id)
public function delete(int $user_id, int $list_id): bool
{
return (bool)TalkList::query()->where([
['id', '=', $list_id],
@ -86,7 +88,7 @@ class TalkListService
* @param int $talk_type 对话类型
* @return bool
*/
public function deleteByType(int $user_id, int $receiver_id, int $talk_type)
public function deleteByType(int $user_id, int $receiver_id, int $talk_type): bool
{
return (bool)TalkList::query()->where([
['user_id', '=', $user_id],
@ -104,7 +106,7 @@ class TalkListService
* @param int $user_id 用户ID
* @return array
*/
public function getTalkList(int $user_id)
public function getTalkList(int $user_id): array
{
$filed = [
'list.id', 'list.talk_type', 'list.receiver_id', 'list.updated_at', 'list.is_disturb', 'list.is_top',
@ -168,7 +170,7 @@ class TalkListService
* @param int $is_disturb 是否免打扰[0:;1:;]
* @return boolean
*/
public function disturb(int $user_id, int $receiver_id, int $talk_type, int $is_disturb)
public function disturb(int $user_id, int $receiver_id, int $talk_type, int $is_disturb): bool
{
$result = TalkList::query()->where([
['user_id', '=', $user_id],
@ -194,7 +196,7 @@ class TalkListService
* @param int $talk_type 对话类型[1:私信;2:群聊;]
* @return bool
*/
public function isDisturb(int $user_id, int $receiver_id, int $talk_type)
public function isDisturb(int $user_id, int $receiver_id, int $talk_type): bool
{
return (bool)TalkList::query()->where([
['user_id', '=', $user_id],

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
use App\Helper\Hash;
use App\Helper\HashHelper;
use Hyperf\Database\Seeders\Seeder;
use App\Model\User;
use App\Model\Article\ArticleClass;
@ -27,7 +27,7 @@ class Initialize extends Seeder
for ($i = 0; $i < 9; $i++) {
$users[] = [
'mobile' => '1879827205' . $i,
'password' => Hash::make('admin123'),
'password' => HashHelper::make('admin123'),
'nickname' => 'test' . $i,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')