hyperf-chat/app/Service/ContactsService.php

77 lines
2.0 KiB
PHP
Raw Normal View History

2021-01-28 20:07:14 +08:00
<?php
/**
* This is my open source code, please do not use it for commercial applications.
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
*
* @author Yuandong<837215079@qq.com>
* @link https://github.com/gzydong/hyperf-chat
*/
namespace App\Service;
use App\Model\User;
2022-01-16 10:29:16 +08:00
use App\Model\Contact;
2021-01-28 20:07:14 +08:00
use App\Traits\PagingTrait;
/**
* ContactsService
* 注:联系人服务层
*
* @package App\Service
*/
class ContactsService extends BaseService
{
use PagingTrait;
/**
* 删除联系人
*
2021-04-20 16:30:57 +08:00
* @param int $user_id 用户ID
2021-01-28 20:07:14 +08:00
* @param int $friend_id 好友ID
* @return bool
*/
2021-07-07 19:43:09 +08:00
public function delete(int $user_id, int $friend_id): bool
2021-01-28 20:07:14 +08:00
{
2022-01-16 10:29:16 +08:00
$res = (bool)Contact::where('user_id', $user_id)->where('friend_id', $friend_id)->where('status', 1)->update([
2021-07-07 19:43:09 +08:00
'status' => 0,
2021-01-28 20:07:14 +08:00
'updated_at' => date('Y-m-d H:i:s')
]);
2021-07-07 19:43:09 +08:00
if ($res) redis()->del("good_friends:{$user_id}_{$friend_id}");
2021-07-08 23:44:43 +08:00
2021-07-07 19:43:09 +08:00
return $res;
2021-01-28 20:07:14 +08:00
}
/**
* 编辑联系人备注
*
2021-04-20 16:30:57 +08:00
* @param int $user_id 用户ID
2021-07-06 23:44:17 +08:00
* @param int $friend_id 好友ID
* @param string $remark 好友备注名称
2021-01-28 20:07:14 +08:00
* @return bool
*/
2021-07-07 19:43:09 +08:00
public function editRemark(int $user_id, int $friend_id, string $remark): bool
2021-01-28 20:07:14 +08:00
{
2022-01-16 10:29:16 +08:00
return (bool)Contact::where('user_id', $user_id)->where('friend_id', $friend_id)->update([
2021-07-07 19:43:09 +08:00
'remark' => $remark,
'updated_at' => date('Y-m-d H:i:s')
]);
2021-01-28 20:07:14 +08:00
}
/**
* 搜索联系人
*
* @param string $mobile 用户手机号/登录账号
* @return array
*/
public function findContact(string $mobile): array
{
$user = User::where('mobile', $mobile)->first(['id', 'nickname', 'mobile', 'avatar', 'gender']);
return $user ? $user->toArray() : [];
}
}