hyperf-chat/app/Model/UsersChatList.php

150 lines
4.7 KiB
PHP
Raw Normal View History

2020-11-04 17:36:52 +08:00
<?php
declare (strict_types=1);
2020-11-14 18:10:28 +08:00
2020-11-04 17:36:52 +08:00
namespace App\Model;
/**
2020-11-14 18:10:28 +08:00
* 聊天列表组数据表模型
*
2021-04-20 16:30:57 +08:00
* @property int $id 聊天列表ID
* @property int $type 聊天类型[1:好友;2:群聊;]
* @property int $uid 用户ID
* @property int $friend_id 好友ID
* @property int $group_id 群组ID
* @property int $status 列表状态
* @property int $is_top 是否置顶
* @property int $not_disturb 是否消息免打扰
* @property string $created_at 创建时间
* @property string $updated_at 更新时间
2020-11-14 18:10:28 +08:00
* @package App\Model
2020-11-04 17:36:52 +08:00
*/
2020-11-05 17:40:51 +08:00
class UsersChatList extends BaseModel
2020-11-04 17:36:52 +08:00
{
protected $table = 'users_chat_list';
2020-11-21 19:53:01 +08:00
protected $fillable = [
'type',
'uid',
'friend_id',
'group_id',
'status',
'is_top',
'not_disturb',
'created_at',
'updated_at'
];
2020-11-04 17:36:52 +08:00
2020-11-05 17:40:51 +08:00
protected $casts = [
2021-04-20 16:30:57 +08:00
'id' => 'integer',
'type' => 'integer',
'uid' => 'integer',
'friend_id' => 'integer',
'group_id' => 'integer',
'status' => 'integer',
'is_top' => 'integer',
2020-11-05 17:40:51 +08:00
'not_disturb' => 'integer',
2021-04-20 16:30:57 +08:00
'created_at' => 'datetime',
'updated_at' => 'datetime'
2020-11-05 17:40:51 +08:00
];
2020-11-09 17:41:22 +08:00
/**
* 创建聊天列表记录
*
2021-04-20 16:30:57 +08:00
* @param int $user_id 用户ID
2020-11-09 17:41:22 +08:00
* @param int $receive_id 接收者ID
2021-04-20 16:30:57 +08:00
* @param int $type 创建类型 1:私聊 2:群聊
2020-11-09 17:41:22 +08:00
* @return array
*/
public static function addItem(int $user_id, int $receive_id, int $type)
{
2021-05-23 17:28:04 +08:00
$result = self::query()->where('uid', $user_id)->where('type', $type)->where($type == 1 ? 'friend_id' : 'group_id', $receive_id)->first();
2020-11-09 17:41:22 +08:00
if ($result) {
2021-04-20 16:30:57 +08:00
$result->status = 1;
2020-11-09 17:41:22 +08:00
$result->updated_at = date('Y-m-d H:i:s');
$result->save();
return [
2021-04-20 16:30:57 +08:00
'id' => $result->id,
'type' => $result->type,
2020-11-14 18:10:28 +08:00
'friend_id' => $result->friend_id,
2021-04-20 16:30:57 +08:00
'group_id' => $result->group_id,
2020-11-09 17:41:22 +08:00
];
}
2021-05-23 17:28:04 +08:00
if (!$result = self::query()->create([
2021-04-20 16:30:57 +08:00
'type' => $type,
'uid' => $user_id,
'status' => 1,
'friend_id' => $type == 1 ? $receive_id : 0,
'group_id' => $type == 2 ? $receive_id : 0,
2020-11-09 17:41:22 +08:00
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
])) {
return [];
}
return [
2021-04-20 16:30:57 +08:00
'id' => $result->id,
'type' => $result->type,
2020-11-14 18:10:28 +08:00
'friend_id' => $result->friend_id,
2021-04-20 16:30:57 +08:00
'group_id' => $result->group_id,
2020-11-09 17:41:22 +08:00
];
}
/**
* 聊天对话列表置顶操作
*
2021-04-20 16:30:57 +08:00
* @param int $user_id 用户ID
* @param int $list_id 对话列表ID
* @param bool $is_top 是否置顶true: false:否)
2020-11-09 17:41:22 +08:00
* @return bool
*/
public static function topItem(int $user_id, int $list_id, $is_top = true)
{
2021-05-23 17:28:04 +08:00
return (bool)self::query()->where('id', $list_id)->where('uid', $user_id)->update([
2021-04-20 16:30:57 +08:00
'is_top' => $is_top ? 1 : 0,
2020-12-03 16:22:55 +08:00
'updated_at' => date('Y-m-d H:i:s')
]);
2020-11-09 17:41:22 +08:00
}
/**
* 删除聊天列表
*
* @param int $user_id 用户ID
2021-04-20 16:30:57 +08:00
* @param int $id 聊天列表ID、好友ID或群聊ID
* @param int $type ID类型 1聊天列表ID 2:好友ID 3:群聊ID
2020-11-09 17:41:22 +08:00
* @return bool
*/
public static function delItem(int $user_id, int $id, $type = 1)
{
2020-12-03 16:22:55 +08:00
$data = ['status' => 0, 'updated_at' => date('Y-m-d H:i:s')];
2020-11-09 17:41:22 +08:00
if ($type == 1) {
2021-05-23 17:28:04 +08:00
return (bool)self::query()->where('id', $id)->where('uid', $user_id)->update($data);
2020-11-09 17:41:22 +08:00
} else if ($type == 2) {
2021-05-23 17:28:04 +08:00
return (bool)self::query()->where('uid', $user_id)->where('friend_id', $id)->update($data);
2020-11-09 17:41:22 +08:00
} else {
2021-05-23 17:28:04 +08:00
return (bool)self::query()->where('uid', $user_id)->where('group_id', $id)->update($data);
2020-11-09 17:41:22 +08:00
}
}
/**
* 设置消息免打扰
*
2021-04-20 16:30:57 +08:00
* @param int $user_id 用户ID
* @param int $receive_id 接收者ID
* @param int $type 接收者类型1:好友 2:群组)
2020-11-09 17:41:22 +08:00
* @param int $not_disturb 是否免打扰
* @return boolean
*/
public static function notDisturbItem(int $user_id, int $receive_id, int $type, int $not_disturb)
{
2021-05-23 17:28:04 +08:00
$result = self::query()->where('uid', $user_id)->where($type == 1 ? 'friend_id' : 'group_id', $receive_id)->where('status', 1)->first(['id', 'not_disturb']);
2020-11-09 17:41:22 +08:00
if (!$result || $not_disturb == $result->not_disturb) {
return false;
}
2021-05-23 17:28:04 +08:00
return (bool)self::query()->where('id', $result->id)->update(['not_disturb' => $not_disturb]);
2020-11-09 17:41:22 +08:00
}
2020-11-04 17:36:52 +08:00
}