Imchat.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_info ui','ui.user_id='.$this->name.'.from_user_id')
  46. ->where([$this->name.'.to_user_id'=> $userId])
  47. ->where(function($query) use($param){
  48. $keyword = isset($param['keyword'])? $param['keyword'] : '';
  49. if($keyword){
  50. $query->where('from_user_name','like',"%{$keyword}%");
  51. }
  52. })
  53. ->field($this->name.".*, u.user_type,ui.admission_year")
  54. ->group($this->name.'.from_user_id')
  55. ->order($this->name.'.sendtime','desc')
  56. ->paginate($listRows);
  57. return $this->setListDataFromApi($list, $param);
  58. }
  59. /**
  60. * 设置展示的数据 api模块
  61. * @param $info
  62. * @return mixed
  63. */
  64. private function setListDataFromApi($info, $params=[])
  65. {
  66. return $this->setListData($info, function ($data) use ($params){
  67. // 统计最新未读消息
  68. $dataType = isset($params['data_type'])? $params['data_type'] : '';
  69. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  70. $fromUserId = isset($data['from_user_id'])? $data['from_user_id'] : 0;
  71. if($dataType == 'msg'){
  72. $data['unread_num'] = $userId && $fromUserId? Imchat::getUnreadCount($userId, $fromUserId) : 0;
  73. }
  74. // 整理数据 api模块
  75. $this->setDataFromApi($data);
  76. // 隐藏冗余的字段
  77. $this->hidden(array_merge($this->hidden, ['']));
  78. });
  79. }
  80. /**
  81. * 整理数据 api模块
  82. * @param $info
  83. * @return mixed
  84. */
  85. private function setDataFromApi($info)
  86. {
  87. return $this->setData($info, function ($data) {
  88. // 头像
  89. $data['to_user_avatar'] = isset($data['to_user_avatar']) && $data['to_user_avatar']? getPreview($data['to_user_avatar']) : '';
  90. $data['from_user_avatar'] = isset($data['from_user_avatar']) && $data['from_user_avatar']? getPreview($data['from_user_avatar']) : '';
  91. //
  92. if(isset($data['user_type'])){
  93. $userType = isset($data['user_type'])? $data['user_type'] : 0;
  94. $admissionYear = isset($data['admission_year'])? $data['admission_year'] : '';
  95. $data['user_type_text'] = $userType == 3? '招生老师' : ($admissionYear? $admissionYear.'级' : '学生');
  96. }
  97. $sendTime = $data['sendtime']? strtotime($data['sendtime']) : 0;
  98. $data['sendtime_text'] = $sendTime? getTimeText($sendTime) : '';
  99. });
  100. }
  101. /**
  102. * 获取未读消息数量
  103. * @param $userId
  104. * @return int
  105. */
  106. public static function getUnreadCount($userId, $fromUserId)
  107. {
  108. return self::where(['from_user_id'=> $fromUserId,'to_user_id'=> $userId,'is_push'=>1])->count('id');
  109. }
  110. }