2021-07-07 08:54:05 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
2021-07-08 22:28:51 +08:00
|
|
|
use App\Cache\FriendApply;
|
2021-07-20 23:12:18 +08:00
|
|
|
use App\Constants\TalkEventConstant;
|
|
|
|
use App\Event\TalkEvent;
|
2021-07-07 19:43:09 +08:00
|
|
|
use App\Model\User;
|
2022-01-16 10:29:16 +08:00
|
|
|
use App\Model\Contact;
|
|
|
|
use App\Model\ContactApply;
|
2021-07-07 19:43:09 +08:00
|
|
|
use App\Traits\PagingTrait;
|
|
|
|
use Hyperf\DbConnection\Db;
|
2021-07-07 08:54:05 +08:00
|
|
|
|
|
|
|
class ContactApplyService
|
|
|
|
{
|
2021-07-07 19:43:09 +08:00
|
|
|
use PagingTrait;
|
|
|
|
|
2021-07-07 08:54:05 +08:00
|
|
|
/**
|
|
|
|
* 创建好友申请
|
|
|
|
*
|
2021-07-07 19:43:09 +08:00
|
|
|
* @param int $user_id 用户ID
|
2021-07-07 08:54:05 +08:00
|
|
|
* @param int $friend_id 朋友ID
|
|
|
|
* @param string $remark 申请备注
|
2021-07-08 22:28:51 +08:00
|
|
|
* @return bool
|
2021-07-07 08:54:05 +08:00
|
|
|
*/
|
|
|
|
public function create(int $user_id, int $friend_id, string $remark)
|
|
|
|
{
|
2022-01-16 10:29:16 +08:00
|
|
|
$result = ContactApply::where([
|
2021-07-08 22:28:51 +08:00
|
|
|
['user_id', '=', $user_id],
|
|
|
|
['friend_id', '=', $friend_id],
|
|
|
|
])->orderByDesc('id')->first();
|
2021-07-07 08:54:05 +08:00
|
|
|
|
2021-07-07 19:43:09 +08:00
|
|
|
if (!$result) {
|
2022-01-16 10:29:16 +08:00
|
|
|
$result = ContactApply::create([
|
2021-07-08 22:28:51 +08:00
|
|
|
'user_id' => $user_id,
|
|
|
|
'friend_id' => $friend_id,
|
|
|
|
'remark' => $remark,
|
|
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$result->remark = $remark;
|
|
|
|
$result->created_at = date('Y-m-d H:i:s');
|
|
|
|
$result->save();
|
2021-07-07 19:43:09 +08:00
|
|
|
}
|
|
|
|
|
2021-07-08 22:28:51 +08:00
|
|
|
// 好友申请未读消息数自增
|
|
|
|
FriendApply::getInstance()->incr($friend_id, 1);
|
|
|
|
|
|
|
|
// 判断对方是否在线。如果在线发送消息通知
|
2021-07-23 21:34:29 +08:00
|
|
|
$isOnline = di()->get(SocketClientService::class)->isOnlineAll($friend_id);
|
2021-07-08 22:28:51 +08:00
|
|
|
if ($isOnline) {
|
2021-07-20 23:12:18 +08:00
|
|
|
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_FRIEND_APPLY, [
|
2021-07-08 22:28:51 +08:00
|
|
|
'apply_id' => $result->id,
|
|
|
|
'type' => 1,
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2021-07-07 08:54:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 同意好友申请
|
2021-07-07 19:43:09 +08:00
|
|
|
*
|
|
|
|
* @param int $user_id 用户ID
|
|
|
|
* @param int $apply_id 申请记录ID
|
2021-07-07 08:54:05 +08:00
|
|
|
*/
|
2021-07-07 19:43:09 +08:00
|
|
|
public function accept(int $user_id, int $apply_id, string $remarks = '')
|
2021-07-07 08:54:05 +08:00
|
|
|
{
|
2022-01-16 10:29:16 +08:00
|
|
|
$info = ContactApply::where('id', $apply_id)->first();
|
2021-07-07 19:43:09 +08:00
|
|
|
if (!$info || $info->friend_id != $user_id) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-07 08:54:05 +08:00
|
|
|
|
2021-07-07 19:43:09 +08:00
|
|
|
Db::beginTransaction();
|
|
|
|
try {
|
2022-01-16 10:29:16 +08:00
|
|
|
Contact::updateOrCreate([
|
2021-07-07 19:43:09 +08:00
|
|
|
'user_id' => $info->user_id,
|
|
|
|
'friend_id' => $info->friend_id,
|
|
|
|
], [
|
|
|
|
'status' => 1,
|
|
|
|
'remark' => $remarks,
|
|
|
|
]);
|
|
|
|
|
2022-01-16 10:29:16 +08:00
|
|
|
Contact::updateOrCreate([
|
2021-07-07 19:43:09 +08:00
|
|
|
'user_id' => $info->friend_id,
|
|
|
|
'friend_id' => $info->user_id,
|
|
|
|
], [
|
|
|
|
'status' => 1,
|
|
|
|
'remark' => User::where('id', $info->user_id)->value('nickname'),
|
|
|
|
]);
|
|
|
|
|
2021-07-08 22:28:51 +08:00
|
|
|
$info->delete();
|
|
|
|
|
2021-07-07 19:43:09 +08:00
|
|
|
Db::commit();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Db::rollBack();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-08 22:28:51 +08:00
|
|
|
// 判断对方是否在线。如果在线发送消息通知
|
2021-07-23 21:34:29 +08:00
|
|
|
$isOnline = di()->get(SocketClientService::class)->isOnlineAll($info->user_id);
|
2021-07-08 22:28:51 +08:00
|
|
|
if ($isOnline) {
|
2021-07-20 23:12:18 +08:00
|
|
|
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_FRIEND_APPLY, [
|
2021-07-08 22:28:51 +08:00
|
|
|
'apply_id' => $apply_id,
|
|
|
|
'type' => 2,
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
2021-07-07 19:43:09 +08:00
|
|
|
return true;
|
2021-07-07 08:54:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 拒绝好友申请
|
2021-07-07 19:43:09 +08:00
|
|
|
*
|
|
|
|
* @param int $user_id 用户ID
|
|
|
|
* @param int $apply_id 申请记录ID
|
|
|
|
*/
|
|
|
|
public function decline(int $user_id, int $apply_id, string $reason = '')
|
|
|
|
{
|
2022-01-16 10:29:16 +08:00
|
|
|
$result = ContactApply::where('id', $apply_id)->where('friend_id', $user_id)->delete();
|
2021-07-07 19:43:09 +08:00
|
|
|
|
2021-07-08 22:28:51 +08:00
|
|
|
if (!$result) return false;
|
|
|
|
|
2021-07-28 23:42:21 +08:00
|
|
|
// todo 做聊天记录的推送
|
2021-07-08 22:28:51 +08:00
|
|
|
|
|
|
|
return true;
|
2021-07-07 19:43:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取联系人申请记录
|
|
|
|
*
|
|
|
|
* @param int $user_id 用户ID
|
|
|
|
* @param int $page 当前分页
|
|
|
|
* @param int $page_size 分页大小
|
|
|
|
* @return array
|
2021-07-07 08:54:05 +08:00
|
|
|
*/
|
2021-07-07 19:43:09 +08:00
|
|
|
public function getApplyRecords(int $user_id, $page = 1, $page_size = 30): array
|
2021-07-07 08:54:05 +08:00
|
|
|
{
|
2022-01-16 10:29:16 +08:00
|
|
|
$rowsSqlObj = ContactApply::select([
|
|
|
|
'contact_apply.id',
|
|
|
|
'contact_apply.remark',
|
2021-07-07 19:43:09 +08:00
|
|
|
'users.nickname',
|
|
|
|
'users.avatar',
|
|
|
|
'users.mobile',
|
2022-01-16 10:29:16 +08:00
|
|
|
'contact_apply.user_id',
|
|
|
|
'contact_apply.friend_id',
|
|
|
|
'contact_apply.created_at'
|
2021-07-07 19:43:09 +08:00
|
|
|
]);
|
|
|
|
|
2022-01-16 10:29:16 +08:00
|
|
|
$rowsSqlObj->leftJoin('users', 'users.id', '=', 'contact_apply.user_id');
|
|
|
|
$rowsSqlObj->where('contact_apply.friend_id', $user_id);
|
2021-07-07 19:43:09 +08:00
|
|
|
|
|
|
|
$count = $rowsSqlObj->count();
|
|
|
|
$rows = [];
|
|
|
|
if ($count > 0) {
|
2022-01-16 10:29:16 +08:00
|
|
|
$rows = $rowsSqlObj->orderBy('contact_apply.id', 'desc')->forPage($page, $page_size)->get()->toArray();
|
2021-07-07 19:43:09 +08:00
|
|
|
}
|
2021-07-07 08:54:05 +08:00
|
|
|
|
2021-07-07 19:43:09 +08:00
|
|
|
return $this->getPagingRows($rows, $count, $page, $page_size);
|
2021-07-07 08:54:05 +08:00
|
|
|
}
|
|
|
|
}
|