Imchat.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\common\model;
  13. use cores\BaseModel;
  14. use think\model\Collection;
  15. use think\Paginator;
  16. /**
  17. * 聊天消息模型
  18. * Class Imchat
  19. * @package app\common\model
  20. */
  21. class Imchat extends BaseModel
  22. {
  23. protected $globalScope = [''];
  24. // 定义表名
  25. protected $name = 'imchat';
  26. // 定义主键
  27. protected $pk = 'id';
  28. /**
  29. * 获取列表
  30. * @param array $param 查询条件
  31. * @param int $listRows 分页数量
  32. * @return mixed
  33. * @throws \think\db\exception\DbException
  34. */
  35. public function getList(array $param = [], int $listRows = 15)
  36. {
  37. // 筛选条件
  38. $query = $this->getQueryFilter($param);
  39. // 排序条件
  40. $sort = $this->setQuerySort($param);
  41. // 执行查询
  42. $list = $query->alias($this->name)
  43. ->order($sort)
  44. ->paginate($listRows);
  45. // 整理列表数据并返回
  46. return $list;
  47. }
  48. /**
  49. * 设置数据
  50. * @param Collection|Paginator $list 商品列表
  51. * @param callable|null $callback 回调函数
  52. * @return mixed
  53. */
  54. protected function setListData($list, callable $callback = null)
  55. {
  56. if ($list->isEmpty()) return $list;
  57. // 遍历商品列表整理数据
  58. foreach ($list as &$item) {
  59. $data = $this->setData($item, $callback);
  60. }
  61. return $list;
  62. }
  63. /**
  64. * 整理数据
  65. * @param Collection|static $info
  66. * @param callable|null $callback
  67. * @return mixed
  68. */
  69. protected function setData($info, callable $callback = null)
  70. {
  71. // 回调函数
  72. is_callable($callback) && call_user_func($callback, $info);
  73. return $info->hidden(array_merge($this->hidden, ['']));
  74. }
  75. /**
  76. * 检索查询条件
  77. * @param array $params
  78. * @return \think\db\BaseQuery
  79. */
  80. private function getQueryFilter(array $params)
  81. {
  82. $filter = [];
  83. // 实例化新查询对象
  84. $query = $this->getNewQuery();
  85. // 发送者
  86. !empty($params['from_user_id']) && $filter[] = ['from_user_id', '=', "{$params['from_user_id']}"];
  87. // 接受者
  88. !empty($params['to_user_id']) && $filter[] = ['to_user_id', '=', "{$params['to_user_id']}"];
  89. // 学校
  90. !empty($params['school_id']) && $filter[] = ['school_id', '=', "{$params['school_id']}"];
  91. // 咨询专业
  92. !empty($params['speciality_id']) && $filter[] = ['speciality_id', '=', "{$params['speciality_id']}"];
  93. // 是否已读
  94. !empty($params['is_push']) && $filter[] = ['is_push', '=', "{$params['is_push']}"];
  95. // 实例化新查询对象
  96. return $query->where($filter)->where(function($query) use ($params){
  97. // 关键词
  98. if(!empty($params['keyword'])){
  99. $query->where('school_name','like', "%{$params['keyword']}%");
  100. }
  101. });
  102. }
  103. /**
  104. * 检索排序条件
  105. * @param array $param
  106. * @return array|string[]
  107. */
  108. private function setQuerySort(array $param = [])
  109. {
  110. $params = $this->setQueryDefaultValue($param, [
  111. 'sortType' => 'all', // 排序类型
  112. 'sendtime' => false, // 排序 (true高到低 false低到高)
  113. ]);
  114. // 排序规则
  115. $sort = [];
  116. if ($params['sortType'] === 'all') {
  117. $sort = ['sendtime' => 'desc'];
  118. } elseif ($params['sortType'] === 'view') {
  119. $sort = ['id' => 'desc'];
  120. }
  121. return array_merge($sort, [$this->getPk() => 'desc']);
  122. }
  123. }