quanwei
17 hours ago ad8477d3ee82a3fffd5de4cd60a237c9ee6b1fb7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
 
namespace app\api\model\plus\release;
 
use app\common\model\plus\release\Chat as ChatModel;
use app\common\model\plus\release\ChatRelation as ChatRelationModel;
use app\api\model\user\User as UserModel;
 
/**
 * 消息模型类
 */
class Chat extends ChatModel
{
 
    /**
     * 隐藏字段
     */
    protected $hidden = [
        'app_id',
        'update_time'
    ];
 
    //消息列表
    public function myList($user)
    {
        $ChatRelation = new ChatRelation();
        $list = $ChatRelation
            ->where(['user_id' => $user['user_id']])
            ->whereOr(['other_user_id' => $user['user_id']])
            ->order('update_time desc')
            ->select();
 
        // 过滤掉无效的关系记录(即user_id和other_user_id相同的记录)
        $validList = [];
        foreach ($list as $value) {
            if ($value['user_id'] != $value['other_user_id']) {
                $validList[] = $value;
            }
        }
 
        foreach ($validList as $key => &$value) {
            $where['relation_id'] = $value['relation_id'];
            // 确定显示的用户ID:应该是与自己不同的另一个用户
            if($value['user_id'] == $user['user_id']){
                // 当前记录是从当前用户角度创建的
                $where['user_id'] = $other_user_id = $value['other_user_id'];
            } else {
                // 当前记录是从对方角度创建的,对方是user_id
                $where['user_id'] = $other_user_id = $value['user_id'];
            }
 
            // 获取未读消息数量
            $where['status'] = 0;
            $value['num'] = $this->where($where)->count();
            unset($where['status']);
 
            // 获取最新消息
            $value['newMessage'] = $this->where($where)->order('chat_id desc')->field('content,create_time')->find();
 
            // 获取对方用户信息
            $value['user'] = UserModel::detail($other_user_id);
        }
        return $validList;
    }
 
    //获取聊天信息
    public function getMessage($data, $user)
    {
        $relation_id = ChatRelationModel::getRelationId($user['user_id'],$data['you_user_id']);   
        $list = $this->where("relation_id","=",$relation_id)
            ->with(['user'])
            ->order('chat_id desc')
            ->paginate($data);
        $where['relation_id'] = $relation_id;
        $where['user_id'] = $data['you_user_id'];    
        $this->where($where)->update(['status' => 1]);
        return $list;
    }
 
    //获取消息条数
    public function mCount($user)
    {
        $num = 0;
        if ($user) {
            $num = $this->where('user_id', '=', $user['user_id'])->whereOr('user_id', '=', $user['user_id'])->count();
        }
        return $num;
    }
 
    //获取用户信息
    public function getInfo($user_id)
    {
        $userInfo = UserModel::detail($user_id);
        $data['avatarUrl'] = $userInfo['avatarUrl'];
        return $data;
    }
 
    public static function getNoReadCount($user_id){
        return self::where('user_id', '=', $user_id)->whereOr('user_id', '=', $user_id)
            //->where('status', '=', 0)
            ->count();
    }
 
}