| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- // +----------------------------------------------------------------------
- // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: thinkphp <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\api\model;
- use app\common\model\Imchat as ImchatModel;
- /**
- * 聊天消息模型
- * Class Imchat
- * @package app\api\model
- */
- class Imchat extends ImchatModel
- {
- /**
- * 获取列表
- * @param array $param 查询条件
- * @param int $listRows 分页数量
- * @return mixed|\think\model\Collection|\think\Paginator
- * @throws \think\db\exception\DbException
- */
- public function getList(array $param = [], int $listRows = 15)
- {
- // 整理查询参数
- $params = array_merge($param, []);
- // 获取商品列表
- $list = parent::getList($params, $listRows);
- if ($list->isEmpty()) {
- return $list;
- }
- // 整理列表数据并返回
- return $this->setListDataFromApi($list);
- }
- public function getListByUser(int $userId, array $param = [], int $listRows = 15)
- {
- $param = array_merge($param, ['user_id'=> $userId,'data_type'=>'msg']);
- $list = $this->alias($this->name)
- ->leftJoin('user u','u.user_id='.$this->name.'.from_user_id')
- ->leftJoin('user_info ui','ui.user_id='.$this->name.'.from_user_id')
- ->where([$this->name.'.to_user_id'=> $userId])
- ->where(function($query) use($param){
- $keyword = isset($param['keyword'])? $param['keyword'] : '';
- if($keyword){
- $query->where('from_user_name','like',"%{$keyword}%");
- }
- })
- ->field($this->name.".*, u.user_type,ui.admission_year")
- ->group($this->name.'.from_user_id')
- ->order($this->name.'.sendtime','desc')
- ->paginate($listRows);
- return $this->setListDataFromApi($list, $param);
- }
- /**
- * 设置展示的数据 api模块
- * @param $info
- * @return mixed
- */
- private function setListDataFromApi($info, $params=[])
- {
- return $this->setListData($info, function ($data) use ($params){
- // 统计最新未读消息
- $dataType = isset($params['data_type'])? $params['data_type'] : '';
- $userId = isset($params['user_id'])? $params['user_id'] : 0;
- $fromUserId = isset($data['from_user_id'])? $data['from_user_id'] : 0;
- if($dataType == 'msg'){
- $data['unread_num'] = $userId && $fromUserId? Imchat::getUnreadCount($userId, $fromUserId) : 0;
- }
- // 整理数据 api模块
- $this->setDataFromApi($data);
- // 隐藏冗余的字段
- $this->hidden(array_merge($this->hidden, ['']));
- });
- }
- /**
- * 整理数据 api模块
- * @param $info
- * @return mixed
- */
- private function setDataFromApi($info)
- {
- return $this->setData($info, function ($data) {
- // 头像
- $data['to_user_avatar'] = isset($data['to_user_avatar']) && $data['to_user_avatar']? getPreview($data['to_user_avatar']) : '';
- $data['from_user_avatar'] = isset($data['from_user_avatar']) && $data['from_user_avatar']? getPreview($data['from_user_avatar']) : '';
- //
- if(isset($data['user_type'])){
- $userType = isset($data['user_type'])? $data['user_type'] : 0;
- $admissionYear = isset($data['admission_year'])? $data['admission_year'] : '';
- $data['user_type_text'] = $userType == 3? '招生老师' : ($admissionYear? $admissionYear.'级' : '学生');
- }
- $sendTime = $data['sendtime']? strtotime($data['sendtime']) : 0;
- $data['sendtime_text'] = $sendTime? getTimeText($sendTime) : '';
- });
- }
- /**
- * 获取未读消息数量
- * @param $userId
- * @return int
- */
- public static function getUnreadCount($userId, $fromUserId)
- {
- return self::where(['from_user_id'=> $fromUserId,'to_user_id'=> $userId,'is_push'=>1])->count('id');
- }
- }
|