Message.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * User raingad
  4. * Date 2024/11/17 17:24
  5. */
  6. namespace app\manage\controller;
  7. use app\BaseController;
  8. use app\enterprise\model\{Message as MessageModel,User,Friend,Group};
  9. use think\facade\Db;
  10. class Message extends BaseController
  11. {
  12. protected $fileType = ['file', 'image','video','voice'];
  13. // 获取聊天记录
  14. public function index()
  15. {
  16. $param = $this->request->param();
  17. $user_id=$param['user_id'] ?? 0;
  18. $toContactId=$param['toContactId'] ?? 0;
  19. $is_group=($param['is_group'] ?? 0) ? $param['is_group']-1 : -1;
  20. $map = [ 'status' => 1];
  21. if($user_id){
  22. if(!$toContactId){
  23. return warning(lang('system.parameterError'));
  24. }
  25. $chat_identify=chat_identify($param['user_id'],$param['toContactId']);
  26. $map['chat_identify'] = $chat_identify;
  27. }
  28. if($is_group>=0){
  29. $map['is_group']=$is_group;
  30. }
  31. $type = isset($param['type']) ? $param['type'] : '';
  32. $where = [];
  33. if ($type && $type != "all") {
  34. $map['type'] = $type;
  35. } else {
  36. $where[] = ['type', 'not in', ['event','admin_notice','webrtc']];
  37. }
  38. $keywords = isset($param['keywords']) ? $param['keywords'] : '';
  39. if ($keywords && in_array($type, ['text', 'all'])) {
  40. $where[] = ['content', 'like', '%' . $keywords . '%'];
  41. $where[] = ['type', '=', 'text'];
  42. }
  43. $listRows = $param['limit'] ?: 20;
  44. $pageSize = $param['page'] ?: 1;
  45. $last_id = $param['last_id'] ?? 0;
  46. if($last_id){
  47. $where[]=['msg_id','<',$last_id];
  48. }
  49. $list = MessageModel::getList($map, $where, 'msg_id desc', $listRows, $pageSize);
  50. $data = $this->recombileMsg($list);
  51. return success('', $data, $list->total(),$list->currentPage());
  52. }
  53. protected function recombileMsg($list,$isPagination=true)
  54. {
  55. $data = [];
  56. if ($list) {
  57. $listData = $isPagination ? $list->toArray()['data'] : $list;
  58. $userList = User::matchUser($listData, true, 'from_user', 120);
  59. foreach ($listData as $k => $v) {
  60. $content = str_encipher($v['content'],false);
  61. $preview = '';
  62. $ext='';
  63. if (in_array($v['type'], $this->fileType)) {
  64. $content = getFileUrl($content);
  65. $preview = previewUrl($content);
  66. $ext=getExtUrl($content);
  67. }
  68. $fromUser = $userList[$v['from_user']];
  69. $toContactId=$v['is_group'] ==1 ? 'group-'.$v['to_user'] : $v['to_user'];
  70. $atList=($v['at'] ?? null) ? explode(',',$v['at']): [];
  71. if($v['is_group']==0){
  72. $toUser=User::where(['user_id'=>$v['to_user']])->field(User::$defaultField)->find() ?? [];
  73. if($toUser){
  74. $toUser=[
  75. 'name'=>$toUser['realname']
  76. ];
  77. }
  78. }else{
  79. $toUser=Group::where(['group_id'=>$v['to_user']])->find();
  80. if($toUser){
  81. $toUser=[
  82. 'name'=>$toUser['name']
  83. ];
  84. }
  85. }
  86. $data[] = [
  87. 'msg_id' => $v['msg_id'],
  88. 'id' => $v['id'],
  89. 'status' => "succeed",
  90. 'type' => $v['type'],
  91. 'sendTime' => $v['create_time'] * 1000,
  92. 'create_time' => is_string($v['create_time']) ? $v['create_time'] : date('Y-m-d H:i:s',$v['create_time']),
  93. 'content' => $content,
  94. 'preview' => $preview,
  95. 'download' => $v['file_id'] ? getMainHost().'/filedown/'.encryptIds($v['file_id']) : '',
  96. 'is_read' => $v['is_read'],
  97. 'is_group' => $v['is_group'],
  98. 'at' => $atList,
  99. 'toContactId' => $toContactId,
  100. 'from_user' => $v['from_user'],
  101. 'file_id' => $v['file_id'],
  102. 'file_cate' => $v['file_cate'],
  103. 'fileName' => $v['file_name'],
  104. 'fileSize' => $v['file_size'],
  105. 'fromUser' => $fromUser,
  106. 'toUser' => $toUser,
  107. 'extUrl'=>$ext,
  108. 'extends'=>is_string($v['extends'])?json_decode($v['extends'],true) : $v['extends']
  109. ];
  110. }
  111. }
  112. return $data;
  113. }
  114. // 获取某个联系人的好友列表
  115. public function getContacts(){
  116. $param = $this->request->param();
  117. $user_id=$param['user_id'] ?? 0;
  118. if(!$user_id){
  119. return warning(lang('system.parameterError'));
  120. }
  121. $config=$this->globalConfig;
  122. $listRows = $param['limit'] ?: 20;
  123. $pageSize = $param['page'] ?: 1;
  124. $keywords = $param['keywords'] ?: '';
  125. $where=[['status','=',1]];
  126. if($keywords){
  127. $where[] = ['realname', 'like', '%' . $keywords . '%'];
  128. }
  129. $hasConvo=$param['hasConvo'] ?? 0;
  130. if($hasConvo){
  131. // 查询最近的联系人
  132. $map1 = [['to_user', '=', $user_id], ['is_last', '=', 1], ['is_group', '=', 0]];
  133. $map2 = [['from_user', '=', $user_id], ['is_last', '=', 1], ['is_group', '=', 0]];
  134. $msgField = 'from_user,to_user,content as lastContent,create_time as lastSendTime,chat_identify,type,del_user';
  135. $lasMsgList = Db::name('message')
  136. ->field($msgField)
  137. ->whereOr([$map1, $map2])
  138. ->order('create_time desc')
  139. ->select();
  140. $ids1=\utils\Arr::arrayToString($lasMsgList,'from_user',false);
  141. $ids2=\utils\Arr::arrayToString($lasMsgList,'to_user',false);
  142. $ids=array_merge($ids1,$ids2);
  143. $userList = array_diff($ids, [$user_id]);
  144. $where[]=['user_id','in',$userList];
  145. }else{
  146. // 如果是社区模式,就只查询的好友,如果是企业模式,就查询所有用户
  147. if($config['sysInfo']['runMode']==1){
  148. $where[]=['user_id','<>',$user_id];
  149. }else{
  150. $friendList = Friend::getFriend(['create_user' => $user_id,'status'=>1]);
  151. $userList = array_keys($friendList);
  152. $where[]=['user_id','in',$userList];
  153. }
  154. }
  155. $list = User::where($where)->field(User::$defaultField)->paginate(['list_rows'=>$listRows,'page'=>$pageSize]);
  156. $data=[];
  157. if($list){
  158. $data=$list->toArray()['data'];
  159. foreach ($data as $k => $v) {
  160. $data[$k]['avatar'] = avatarUrl($v['avatar'], $v['realname'], $v['user_id'], 120);
  161. $data[$k]['id'] = $v['user_id'];
  162. }
  163. }
  164. return success('',$data,$list->total(),$list->currentPage());
  165. }
  166. // 消息处理
  167. public function dealMsg(){
  168. $param = $this->request->param();
  169. $id = $param['id'];
  170. $message = MessageModel::where(['id' => $id])->find();
  171. if ($message) {
  172. $dealType=$param['dealType'] ?? 0;
  173. $content=$message['content'] ?? '';
  174. if($dealType==1){
  175. MessageModel::where(['id' => $id])->delete();
  176. // 如果是最后一条消息,需要将上一条设置为最后一条
  177. if($message['is_last']){
  178. MessageModel::where(['chat_identify'=>$message['chat_identify']])->order('msg_id desc')->limit(1)->update(['is_last'=>1]);
  179. }
  180. $action='delMessage';
  181. }else{
  182. $content=str_encipher(lang('im.forbidMsg'),true);
  183. MessageModel::where(['id' => $id])->update(['content'=>$content,'type'=>'text']);
  184. $action='updateMessage';
  185. }
  186. $toContactId = $message['to_user'];
  187. if ($message['is_group'] == 1) {
  188. $toContactId = explode('-', $message['chat_identify'])[1];
  189. }
  190. $data=[
  191. 'id'=>$message['id'],
  192. 'type'=>"text",
  193. 'content'=>str_encipher($content,false),
  194. ];
  195. wsSendMsg($toContactId, $action, $data, $message['is_group']);
  196. return success('');
  197. } else {
  198. return warning(lang('im.exist'));
  199. }
  200. }
  201. }