| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- // +----------------------------------------------------------------------
- // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: thinkphp <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\api\model;
- use app\common\model\Imchat as ImchatModel;
- /**
- * 聊天消息模型
- * Class Imchat
- * @package app\api\model
- */
- class Imchat extends ImchatModel
- {
- /**
- * 获取列表
- * @param array $param 查询条件
- * @param int $listRows 分页数量
- * @return mixed|\think\model\Collection|\think\Paginator
- * @throws \think\db\exception\DbException
- */
- public function getList(array $param = [], int $listRows = 15, $field='')
- {
- // 整理查询参数
- $params = array_merge($param, []);
- // 获取列表
- $list = parent::getList($params, $listRows,'id,from_user_id,to_user_id,message,chat_key,sendtime,is_push');
- if ($list->isEmpty()) {
- return $list;
- }
- // 整理列表数据并返回
- return $this->setListDataFromApi($list, $params);
- }
- /**
- * @param int $userId
- * @param array $param
- * @param int $listRows
- * @return mixed
- */
- public function getListByUser(int $userId, array $param = [], int $listRows = 15)
- {
- $param = array_merge($param, ['user_id'=> $userId,'data_type'=>'msg']);
- $list = $this->alias($this->name)
- ->leftJoin('user u','u.user_id='.$this->name.'.from_user_id')
- ->leftJoin('user u1','u.user_id='.$this->name.'.to_user_id')
- ->where(function($query) use($userId){
- $query->where($this->name.'.from_user_id','=', $userId)
- ->whereOr($this->name.'.to_user_id','=', $userId);
- })
- ->where(function($query) use($param){
- $keyword = isset($param['keyword'])? $param['keyword'] : '';
- if($keyword){
- $query->where('u.nick_name','like',"%{$keyword}%")
- ->whereOr('u1.nick_name','like',"%{$keyword}%");
- }
- })
- ->field($this->name.".*")
- ->group($this->name.'.chat_key')
- ->order($this->name.'.sendtime desc,'.$this->name.'.is_push desc')
- ->paginate($listRows);
- return $this->setListDataFromApi($list, $param);
- }
- /**
- * 设置展示的数据 api模块
- * @param $info
- * @return mixed
- */
- private function setListDataFromApi($info, $params=[])
- {
- return $this->setListData($info, function ($data) use ($params){
- // 处理消息已读
- $fromUserId = isset($params['from_user_id'])? $params['from_user_id'] : 0;
- if($fromUserId){
- if($data['is_push'] == 1 && $data['to_user_id'] == $fromUserId){
- ImchatModel::where(['id'=> $data['id']])->save(['is_push'=> 0]);
- $data['is_push'] = 0;
- }
- }
- // 处理最新消息
- if(isset($params['data_type'])){
- $info = self::getNewMessageByChat($data['chat_key']);
- $data['message'] = isset($info['message'])? $info['message'] : '';
- $data['is_push'] = isset($info['is_push'])? $info['is_push'] : 0;
- $data['sendtime'] = isset($info['sendtime'])? $info['sendtime'] : 0;
- // 统计最新未读消息
- $dataType = isset($params['data_type'])? $params['data_type'] : '';
- $userId = isset($params['user_id'])? $params['user_id'] : 0;
- $fromUserId = isset($data['from_user_id'])? $data['from_user_id'] : 0;
- $toUserId = isset($data['to_user_id'])? $data['to_user_id'] : 0;
- $data['unread_num'] = 0;
- $data['user_type'] = 1;
- if($dataType == 'msg'){
- $data['unread_num'] = $userId && $fromUserId && $userId != $fromUserId? Imchat::getUnreadCount($userId, $fromUserId) : 0;
- }
- // 收到消息
- if($userId == $toUserId)
- {
- $data['msg_type'] = 1;
- $data['show_user_id'] = $fromUserId;
- $info = User::cacheDetail($fromUserId);
- $data['nick_name'] = $info['nick_name'];
- $data['avatar_url'] = $info['avatar_url'];
- $data['user_type'] = $info['user_type'];
- $data['user_school_id'] = isset($info['info']['school_id'])? $info['info']['school_id'] : 0;
- $admissionYear = isset($info['info']['admission_year'])? $info['info']['admission_year'] : '';
- $data['user_type_text'] = $data['user_type'] == 3? '招生老师' : ($admissionYear? $admissionYear.'级' : '学生');
- }
- // 发送消息
- if($userId == $fromUserId)
- {
- $data['msg_type'] = 2;
- $data['show_user_id'] = $toUserId;
- $info = User::cacheDetail($toUserId);
- $info['avatar'] = isset($info['avatar'])? $info['avatar'] : [];
- $info['info'] = isset($info['info'])? $info['info'] : [];
- $data['nick_name'] = $info['nick_name'];
- $data['avatar_url'] = $info['avatar_url'];
- $data['user_type'] = $info['user_type'];
- $data['user_school_id'] = isset($info['info']['school_id'])? $info['info']['school_id'] : 0;
- $admissionYear = isset($info['info']['admission_year'])? $info['info']['admission_year'] : '';
- $data['user_type_text'] = $data['user_type'] == 3? '招生老师' : ($admissionYear? $admissionYear.'级' : '学生');
- }
- // 用户学校
- if($data['user_type'] == 3){
- $data['user_school_name'] = $data['user_school_id']? School::getSchoolField($data['user_school_id']) : '';
- }else{
- $data['user_school_name'] = $data['user_school_id']? SourceShool::getSchoolField($data['user_school_id']) : '';
- }
- }
- // 整理数据 api模块
- $this->setDataFromApi($data);
- // 隐藏冗余的字段
- $this->hidden(array_merge($this->hidden, []));
- });
- }
- /**
- * 整理数据 api模块
- * @param $info
- * @return mixed
- */
- private function setDataFromApi($info)
- {
- return $this->setData($info, function ($data) {
- $sendTime = $data['sendtime']? intval($data['sendtime']) : 0;
- $data['sendtime_text'] = $sendTime? getTimeText($sendTime) : '';
- });
- }
- /**
- * 获取最新消息
- * @param $chatKey
- * @return Imchat|array|mixed|\think\Model|null
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getNewMessageByChat($chatKey)
- {
- return self::where(['chat_key'=> $chatKey])
- ->order('sendtime desc')
- ->field('message,from_user_id,to_user_id,sendtime,is_push')
- ->find();
- }
- /**
- * 获取未读消息数量
- * @param $userId
- * @return int
- */
- public static function getUnreadCount($userId, $fromUserId)
- {
- return self::where(['from_user_id'=> $fromUserId,'to_user_id'=> $userId,'is_push'=>1])->count('id');
- }
- }
|