hyperf-chat/app/Service/Group/GroupNoticeService.php

92 lines
2.6 KiB
PHP
Raw Normal View History

2021-07-10 00:18:40 +08:00
<?php
2021-09-11 15:53:23 +08:00
declare(strict_types=1);
2021-07-10 00:18:40 +08:00
2021-07-28 23:08:07 +08:00
namespace App\Service\Group;
2021-07-10 00:18:40 +08:00
2021-07-28 23:08:07 +08:00
use Exception;
2021-07-10 00:18:40 +08:00
use App\Model\Group\GroupNotice;
2021-07-28 23:08:07 +08:00
use App\Service\BaseService;
2021-07-10 00:18:40 +08:00
2021-07-28 23:08:07 +08:00
class GroupNoticeService extends BaseService
2021-07-10 00:18:40 +08:00
{
public function create(int $user_id, array $params)
{
return GroupNotice::create([
'group_id' => $params['group_id'],
'creator_id' => $user_id,
'title' => $params['title'],
'content' => $params['content'],
'is_top' => $params['is_top'],
'is_confirm' => $params['is_confirm'],
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
]);
}
public function update(array $params)
{
return GroupNotice::where('id', $params['notice_id'])->update([
'title' => $params['title'],
'content' => $params['content'],
'is_top' => $params['is_top'],
'updated_at' => date('Y-m-d H:i:s')
]);
}
/**
* 删除群公告
*
* @param int $notice_id 公告ID
* @param int $user_id 用户ID
* @return bool
* @throws Exception
*/
2021-09-11 15:53:23 +08:00
public function delete(int $notice_id, int $user_id): bool
2021-07-10 00:18:40 +08:00
{
$notice = GroupNotice::where('id', $notice_id)->first();
2021-07-28 23:08:07 +08:00
if (!$notice) return false;
2021-07-10 00:18:40 +08:00
// 判断用户是否是管理员
2021-07-28 23:08:07 +08:00
if (!di()->get(GroupMemberService::class)->isAuth($notice->group_id, $user_id)) {
2021-07-10 00:18:40 +08:00
throw new Exception('非管理员,无法进行操作!', 403);
}
$notice->is_delete = 1;
$notice->deleted_at = date('Y-m-d H:i:s');
$notice->save();
return true;
}
/**
* 获取群公告列表
*
* @param int $group_id 群ID
* @return array
*/
2021-09-11 15:53:23 +08:00
public function lists(int $group_id): array
2021-07-10 00:18:40 +08:00
{
return GroupNotice::leftJoin('users', 'users.id', '=', 'group_notice.creator_id')
->where([
['group_notice.group_id', '=', $group_id],
['group_notice.is_delete', '=', 0]
])
->orderBy('group_notice.is_top', 'desc')
->orderBy('group_notice.updated_at', 'desc')
->get([
'group_notice.id',
'group_notice.creator_id',
'group_notice.title',
'group_notice.content',
'group_notice.is_top',
'group_notice.is_confirm',
'group_notice.confirm_users',
'group_notice.created_at',
'group_notice.updated_at',
'users.avatar',
'users.nickname',
])->toArray();
}
}