Imchat.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. public function getListByUser(int $userId, array $param = [], int $listRows = 15)
  41. {
  42. $param = array_merge($param, ['user_id'=> $userId,'data_type'=>'msg']);
  43. $list = $this->alias($this->name)
  44. ->leftJoin('user u','u.user_id='.$this->name.'.from_user_id')
  45. ->leftJoin('user u1','u.user_id='.$this->name.'.to_user_id')
  46. ->where(function($query) use($userId){
  47. $query->where($this->name.'.from_user_id','=', $userId)
  48. ->whereOr($this->name.'.to_user_id','=', $userId);
  49. })
  50. ->where(function($query) use($param){
  51. $keyword = isset($param['keyword'])? $param['keyword'] : '';
  52. if($keyword){
  53. $query->where('u.nick_name','like',"%{$keyword}%")
  54. ->whereOr('u1.nick_name','like',"%{$keyword}%");
  55. }
  56. })
  57. ->field($this->name.".*")
  58. ->group($this->name.'.chat_key')
  59. ->order($this->name.'.sendtime','desc')
  60. ->order($this->name.'.is_push desc,'.$this->name.'.sendtime','desc')
  61. ->paginate($listRows);
  62. return $this->setListDataFromApi($list, $param);
  63. }
  64. /**
  65. * 设置展示的数据 api模块
  66. * @param $info
  67. * @return mixed
  68. */
  69. private function setListDataFromApi($info, $params=[])
  70. {
  71. return $this->setListData($info, function ($data) use ($params){
  72. // 统计最新未读消息
  73. $dataType = isset($params['data_type'])? $params['data_type'] : '';
  74. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  75. $fromUserId = isset($data['from_user_id'])? $data['from_user_id'] : 0;
  76. $toUserId = isset($data['to_user_id'])? $data['to_user_id'] : 0;
  77. $data['unread_num'] = 0;
  78. $data['user_type'] = 1;
  79. if($dataType == 'msg'){
  80. $data['unread_num'] = $userId && $fromUserId && $userId != $fromUserId? Imchat::getUnreadCount($userId, $fromUserId) : 0;
  81. }
  82. // 收到消息
  83. if($userId == $toUserId)
  84. {
  85. $data['msg_type'] = 1;
  86. $data['show_user_id'] = $fromUserId;
  87. $info = User::cacheDetail($fromUserId);
  88. $data['nick_name'] = $info['nick_name'];
  89. $data['avatar_url'] = $info['avatar_url'];
  90. $data['user_type'] = $info['user_type'];
  91. $data['user_school_id'] = isset($info['info']['school_id'])? $info['info']['school_id'] : 0;
  92. $admissionYear = isset($info['info']['admission_year'])? $info['info']['admission_year'] : '';
  93. $data['user_type_text'] = $data['user_type'] == 3? '招生老师' : ($admissionYear? $admissionYear.'级' : '学生');
  94. }
  95. // 发送消息
  96. if($userId == $fromUserId)
  97. {
  98. $data['msg_type'] = 2;
  99. $data['show_user_id'] = $toUserId;
  100. $info = User::cacheDetail($toUserId);
  101. $info['avatar'] = isset($info['avatar'])? $info['avatar'] : [];
  102. $info['info'] = isset($info['info'])? $info['info'] : [];
  103. $data['nick_name'] = $info['nick_name'];
  104. $data['avatar_url'] = $info['avatar_url'];
  105. $data['user_type'] = $info['user_type'];
  106. $data['user_school_id'] = isset($info['info']['school_id'])? $info['info']['school_id'] : 0;
  107. $admissionYear = isset($info['info']['admission_year'])? $info['info']['admission_year'] : '';
  108. $admissionYear = isset($info['info']['admission_year'])? $info['info']['admission_year'] : '';
  109. $data['user_type_text'] = $data['user_type'] == 3? '招生老师' : ($admissionYear? $admissionYear.'级' : '学生');
  110. }
  111. // 用户学校
  112. if($data['user_type'] == 3){
  113. $data['user_school_name'] = $data['user_school_id']? School::getSchoolField($data['user_school_id']) : '';
  114. }else{
  115. $data['user_school_name'] = $data['user_school_id']? SourceShool::getSchoolField($data['user_school_id']) : '';
  116. }
  117. // 整理数据 api模块
  118. $this->setDataFromApi($data);
  119. // 隐藏冗余的字段
  120. $this->hidden(array_merge($this->hidden, ['chat_key','sendtime']));
  121. });
  122. }
  123. /**
  124. * 整理数据 api模块
  125. * @param $info
  126. * @return mixed
  127. */
  128. private function setDataFromApi($info)
  129. {
  130. return $this->setData($info, function ($data) {
  131. $sendTime = $data['sendtime']? intval($data['sendtime']) : 0;
  132. $data['sendtime_text'] = $sendTime? getTimeText($sendTime) : '';
  133. });
  134. }
  135. /**
  136. * 获取未读消息数量
  137. * @param $userId
  138. * @return int
  139. */
  140. public static function getUnreadCount($userId, $fromUserId)
  141. {
  142. return self::where(['from_user_id'=> $fromUserId,'to_user_id'=> $userId,'is_push'=>1])->count('id');
  143. }
  144. }