Imchat.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\api\model;
  13. use app\common\model\Imchat as ImchatModel;
  14. /**
  15. * 聊天消息模型
  16. * Class Imchat
  17. * @package app\api\model
  18. */
  19. class Imchat extends ImchatModel
  20. {
  21. /**
  22. * 获取列表
  23. * @param array $param 查询条件
  24. * @param int $listRows 分页数量
  25. * @return mixed|\think\model\Collection|\think\Paginator
  26. * @throws \think\db\exception\DbException
  27. */
  28. public function getList(array $param = [], int $listRows = 15, $field='')
  29. {
  30. // 整理查询参数
  31. $params = array_merge($param, []);
  32. // 获取列表
  33. $list = parent::getList($params, $listRows,'id,from_user_id,to_user_id,message,chat_key,sendtime,is_push');
  34. if ($list->isEmpty()) {
  35. return $list;
  36. }
  37. // 整理列表数据并返回
  38. return $this->setListDataFromApi($list, $params);
  39. }
  40. /**
  41. * @param int $userId
  42. * @param array $param
  43. * @param int $listRows
  44. * @return mixed
  45. */
  46. public function getListByUser(int $userId, array $param = [], int $listRows = 15)
  47. {
  48. $param = array_merge($param, ['user_id'=> $userId,'data_type'=>'msg']);
  49. $list = $this->alias($this->name)
  50. ->leftJoin('user u','u.user_id='.$this->name.'.from_user_id')
  51. ->leftJoin('user u1','u.user_id='.$this->name.'.to_user_id')
  52. ->where(function($query) use($userId){
  53. $query->where($this->name.'.from_user_id','=', $userId)
  54. ->whereOr($this->name.'.to_user_id','=', $userId);
  55. })
  56. ->where(function($query) use($param){
  57. $keyword = isset($param['keyword'])? $param['keyword'] : '';
  58. if($keyword){
  59. $query->where('u.nick_name','like',"%{$keyword}%")
  60. ->whereOr('u1.nick_name','like',"%{$keyword}%");
  61. }
  62. })
  63. ->field($this->name.".*")
  64. ->group($this->name.'.chat_key')
  65. ->order($this->name.'.sendtime desc,'.$this->name.'.is_push desc')
  66. ->paginate($listRows);
  67. return $this->setListDataFromApi($list, $param);
  68. }
  69. /**
  70. * 设置展示的数据 api模块
  71. * @param $info
  72. * @return mixed
  73. */
  74. private function setListDataFromApi($info, $params=[])
  75. {
  76. return $this->setListData($info, function ($data) use ($params){
  77. // 处理消息已读
  78. $fromUserId = isset($params['from_user_id'])? $params['from_user_id'] : 0;
  79. if($fromUserId){
  80. if($data['is_push'] == 1 && $data['to_user_id'] == $fromUserId){
  81. ImchatModel::where(['id'=> $data['id']])->save(['is_push'=> 0]);
  82. $data['is_push'] = 0;
  83. }
  84. }
  85. // 处理最新消息
  86. if(isset($params['data_type'])){
  87. $info = self::getNewMessageByChat($data['chat_key']);
  88. $data['message'] = isset($info['message'])? $info['message'] : '';
  89. $data['is_push'] = isset($info['is_push'])? $info['is_push'] : 0;
  90. $data['sendtime'] = isset($info['sendtime'])? $info['sendtime'] : 0;
  91. // 统计最新未读消息
  92. $dataType = isset($params['data_type'])? $params['data_type'] : '';
  93. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  94. $fromUserId = isset($data['from_user_id'])? $data['from_user_id'] : 0;
  95. $toUserId = isset($data['to_user_id'])? $data['to_user_id'] : 0;
  96. $data['unread_num'] = 0;
  97. $data['user_type'] = 1;
  98. if($dataType == 'msg'){
  99. $data['unread_num'] = $userId && $fromUserId && $userId != $fromUserId? Imchat::getUnreadCount($userId, $fromUserId) : 0;
  100. }
  101. // 收到消息
  102. if($userId == $toUserId)
  103. {
  104. $data['msg_type'] = 1;
  105. $data['show_user_id'] = $fromUserId;
  106. $info = User::cacheDetail($fromUserId);
  107. $data['nick_name'] = $info['nick_name'];
  108. $data['avatar_url'] = $info['avatar_url'];
  109. $data['user_type'] = $info['user_type'];
  110. $data['user_school_id'] = isset($info['info']['school_id'])? $info['info']['school_id'] : 0;
  111. $admissionYear = isset($info['info']['admission_year'])? $info['info']['admission_year'] : '';
  112. $data['user_type_text'] = $data['user_type'] == 3? '招生老师' : ($admissionYear? $admissionYear.'级' : '学生');
  113. }
  114. // 发送消息
  115. if($userId == $fromUserId)
  116. {
  117. $data['msg_type'] = 2;
  118. $data['show_user_id'] = $toUserId;
  119. $info = User::cacheDetail($toUserId);
  120. $info['avatar'] = isset($info['avatar'])? $info['avatar'] : [];
  121. $info['info'] = isset($info['info'])? $info['info'] : [];
  122. $data['nick_name'] = $info['nick_name'];
  123. $data['avatar_url'] = $info['avatar_url'];
  124. $data['user_type'] = $info['user_type'];
  125. $data['user_school_id'] = isset($info['info']['school_id'])? $info['info']['school_id'] : 0;
  126. $admissionYear = isset($info['info']['admission_year'])? $info['info']['admission_year'] : '';
  127. $data['user_type_text'] = $data['user_type'] == 3? '招生老师' : ($admissionYear? $admissionYear.'级' : '学生');
  128. }
  129. // 用户学校
  130. if($data['user_type'] == 3){
  131. $data['user_school_name'] = $data['user_school_id']? School::getSchoolField($data['user_school_id']) : '';
  132. }else{
  133. $data['user_school_name'] = $data['user_school_id']? SourceShool::getSchoolField($data['user_school_id']) : '';
  134. }
  135. }
  136. // 整理数据 api模块
  137. $this->setDataFromApi($data);
  138. // 隐藏冗余的字段
  139. $this->hidden(array_merge($this->hidden, []));
  140. });
  141. }
  142. /**
  143. * 整理数据 api模块
  144. * @param $info
  145. * @return mixed
  146. */
  147. private function setDataFromApi($info)
  148. {
  149. return $this->setData($info, function ($data) {
  150. $sendTime = $data['sendtime']? intval($data['sendtime']) : 0;
  151. $data['sendtime_text'] = $sendTime? getTimeText($sendTime) : '';
  152. });
  153. }
  154. /**
  155. * 获取最新消息
  156. * @param $chatKey
  157. * @return Imchat|array|mixed|\think\Model|null
  158. * @throws \think\db\exception\DataNotFoundException
  159. * @throws \think\db\exception\DbException
  160. * @throws \think\db\exception\ModelNotFoundException
  161. */
  162. public static function getNewMessageByChat($chatKey)
  163. {
  164. return self::where(['chat_key'=> $chatKey])
  165. ->order('sendtime desc')
  166. ->field('message,from_user_id,to_user_id,sendtime,is_push')
  167. ->find();
  168. }
  169. /**
  170. * 获取未读消息数量
  171. * @param $userId
  172. * @return int
  173. */
  174. public static function getUnreadCount($userId, $fromUserId)
  175. {
  176. return self::where(['from_user_id'=> $fromUserId,'to_user_id'=> $userId,'is_push'=>1])->count('id');
  177. }
  178. }