Imchat.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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)
  29. {
  30. // 整理查询参数
  31. $params = array_merge($param, []);
  32. // 获取商品列表
  33. $list = parent::getList($params, $listRows);
  34. if ($list->isEmpty()) {
  35. return $list;
  36. }
  37. // 整理列表数据并返回
  38. return $this->setListDataFromApi($list);
  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. $info = self::getNewMessageByChat($data['chat_key']);
  78. $data['message'] = isset($info['message'])? $info['message'] : '';
  79. $data['is_push'] = isset($info['is_push'])? $info['is_push'] : 0;
  80. $data['sendtime'] = isset($info['sendtime'])? $info['sendtime'] : 0;
  81. // 统计最新未读消息
  82. $dataType = isset($params['data_type'])? $params['data_type'] : '';
  83. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  84. $fromUserId = isset($data['from_user_id'])? $data['from_user_id'] : 0;
  85. $toUserId = isset($data['to_user_id'])? $data['to_user_id'] : 0;
  86. $data['unread_num'] = 0;
  87. $data['user_type'] = 1;
  88. if($dataType == 'msg'){
  89. $data['unread_num'] = $userId && $fromUserId && $userId != $fromUserId? Imchat::getUnreadCount($userId, $fromUserId) : 0;
  90. }
  91. // 收到消息
  92. if($userId == $toUserId)
  93. {
  94. $data['msg_type'] = 1;
  95. $data['show_user_id'] = $fromUserId;
  96. $info = User::cacheDetail($fromUserId);
  97. $data['nick_name'] = $info['nick_name'];
  98. $data['avatar_url'] = $info['avatar_url'];
  99. $data['user_type'] = $info['user_type'];
  100. $data['user_school_id'] = isset($info['info']['school_id'])? $info['info']['school_id'] : 0;
  101. $admissionYear = isset($info['info']['admission_year'])? $info['info']['admission_year'] : '';
  102. $data['user_type_text'] = $data['user_type'] == 3? '招生老师' : ($admissionYear? $admissionYear.'级' : '学生');
  103. }
  104. // 发送消息
  105. if($userId == $fromUserId)
  106. {
  107. $data['msg_type'] = 2;
  108. $data['show_user_id'] = $toUserId;
  109. $info = User::cacheDetail($toUserId);
  110. $info['avatar'] = isset($info['avatar'])? $info['avatar'] : [];
  111. $info['info'] = isset($info['info'])? $info['info'] : [];
  112. $data['nick_name'] = $info['nick_name'];
  113. $data['avatar_url'] = $info['avatar_url'];
  114. $data['user_type'] = $info['user_type'];
  115. $data['user_school_id'] = isset($info['info']['school_id'])? $info['info']['school_id'] : 0;
  116. $admissionYear = isset($info['info']['admission_year'])? $info['info']['admission_year'] : '';
  117. $data['user_type_text'] = $data['user_type'] == 3? '招生老师' : ($admissionYear? $admissionYear.'级' : '学生');
  118. }
  119. // 用户学校
  120. if($data['user_type'] == 3){
  121. $data['user_school_name'] = $data['user_school_id']? School::getSchoolField($data['user_school_id']) : '';
  122. }else{
  123. $data['user_school_name'] = $data['user_school_id']? SourceShool::getSchoolField($data['user_school_id']) : '';
  124. }
  125. // 整理数据 api模块
  126. $this->setDataFromApi($data);
  127. // 隐藏冗余的字段
  128. $this->hidden(array_merge($this->hidden, ['chat_key']));
  129. });
  130. }
  131. /**
  132. * 整理数据 api模块
  133. * @param $info
  134. * @return mixed
  135. */
  136. private function setDataFromApi($info)
  137. {
  138. return $this->setData($info, function ($data) {
  139. $sendTime = $data['sendtime']? intval($data['sendtime']) : 0;
  140. $data['sendtime_text'] = $sendTime? getTimeText($sendTime) : '';
  141. });
  142. }
  143. /**
  144. * 获取最新消息
  145. * @param $chatKey
  146. * @return Imchat|array|mixed|\think\Model|null
  147. * @throws \think\db\exception\DataNotFoundException
  148. * @throws \think\db\exception\DbException
  149. * @throws \think\db\exception\ModelNotFoundException
  150. */
  151. public static function getNewMessageByChat($chatKey)
  152. {
  153. return self::where(['chat_key'=> $chatKey])
  154. ->order('sendtime desc')
  155. ->field('message,from_user_id,to_user_id,sendtime,is_push')
  156. ->find();
  157. }
  158. /**
  159. * 获取未读消息数量
  160. * @param $userId
  161. * @return int
  162. */
  163. public static function getUnreadCount($userId, $fromUserId)
  164. {
  165. return self::where(['from_user_id'=> $fromUserId,'to_user_id'=> $userId,'is_push'=>1])->count('id');
  166. }
  167. }