School.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model;
  13. use app\common\library\helper;
  14. use app\common\model\Region;
  15. use app\common\model\School as SchoolModel;
  16. /**
  17. * 学校模型类
  18. * Class School
  19. * @package app\api\model
  20. */
  21. class School extends SchoolModel
  22. {
  23. protected $globalScope = [''];
  24. /**
  25. * 隐藏字段
  26. * @var array
  27. */
  28. protected $hidden = [
  29. 'update_time'
  30. ];
  31. /**
  32. * 获取列表
  33. * @param array $param 查询条件
  34. * @param int $listRows 分页数量
  35. * @return mixed|\think\model\Collection|\think\Paginator
  36. * @throws \think\db\exception\DbException
  37. */
  38. public function getList(array $param = [], int $listRows = 15)
  39. {
  40. // 整理查询参数
  41. $params = array_merge($param, ['audit_status' => 1]);
  42. // 获取商品列表
  43. $list = parent::getList($params, $listRows);
  44. if ($list->isEmpty()) {
  45. return $list;
  46. }
  47. // 隐藏冗余的字段
  48. $list->hidden(array_merge($this->hidden, ['albums']));
  49. // 整理列表数据并返回
  50. return $this->setListDataFromApi($list);
  51. }
  52. /**
  53. * 设置展示的数据 api模块
  54. * @param $info
  55. * @return mixed
  56. */
  57. private function setListDataFromApi($info)
  58. {
  59. $specialityModel = new SchoolSpeciality;
  60. return $this->setListData($info, function ($data) use($specialityModel) {
  61. $data['speciality'] = $specialityModel->getHots($data['id']);
  62. // 整理数据 api模块
  63. $this->setDataFromApi($data);
  64. });
  65. }
  66. /**
  67. * 整理数据 api模块
  68. * @param $info
  69. * @return mixed
  70. */
  71. private function setDataFromApi($info)
  72. {
  73. return $this->setData($info, function ($data) {
  74. // logo封面
  75. $data['logo'] = $data['logo']? getPreview($data['logo']) : '';
  76. $data['labels'] = $data['labels']? explode(',', $data['labels']) : [];
  77. // 图片列表
  78. $data['albums'] = helper::getArrayColumn($data['albums'], 'album_url');
  79. // 地区
  80. $data['province_name'] = $data['province_id']? Region::getNameById($data['province_id']) : '';
  81. $data['city_name'] = $data['city_id']? Region::getNameById($data['city_id']) : '';
  82. $data['region_name'] = $data['region_id']? Region::getNameById($data['region_id']) : '';
  83. // 分类等级
  84. $data['education_levels_text'] = isset($this->levelTypes[$data['education_levels']])? $this->levelTypes[$data['education_levels']] : '无';
  85. // 距离
  86. if(!is_null($data['distance'])){
  87. $data['distance'] = $data['distance']? ($data['distance']<1000? "{$data['distance']}m" : ($data['distance']/1000).'km') :'';
  88. }
  89. // 浏览数
  90. if(!is_null($data['views'])){
  91. $data['views'] = $data['views']? ($data['views']<10000? "{$data['views']}" : round($data['views']/10000,1).'w') :'';
  92. }
  93. });
  94. }
  95. /**
  96. * 获取选项列表
  97. * @param array $param 查询条件
  98. * @param int $listRows 分页数量
  99. * @return mixed|\think\model\Collection|\think\Paginator
  100. * @throws \think\db\exception\DbException
  101. */
  102. public function getOptionList(array $param = [], string $field='')
  103. {
  104. return $this->where(['audit_status'=> 1])
  105. ->where(function ($query) use ($param){
  106. $keyword = isset($param['keyword'])? trim($param['keyword']) : '';
  107. if($keyword){
  108. $query->where('school_name','like',"%{$keyword}%");
  109. }
  110. })
  111. ->field($field?:'id,school_name,type,level,hot_order')
  112. ->order('hot_order desc,views desc')
  113. ->select();
  114. }
  115. /**
  116. * 获取学校信息
  117. * @param $where
  118. * @param array $with
  119. * @return static|array|false|null
  120. */
  121. public static function detail($where, array $with = [])
  122. {
  123. $filter = [];
  124. if (is_array($where)) {
  125. $filter = array_merge($filter, $where);
  126. } else {
  127. $filter['id'] = (int)$where;
  128. }
  129. $data = static::get($filter, $with);
  130. $data->hidden(['book_fields']);
  131. if($data['logo']){
  132. $data['logo'] = getPreview($data['logo']);
  133. }
  134. return $data;
  135. }
  136. /**
  137. * 获取用户所属招生学校
  138. * @param $userId
  139. * @param string $field
  140. * @return mixed
  141. */
  142. public static function getUserSchool($userId, $field=''){
  143. $model = new SchoolModel;
  144. return $model->userSchool($userId, $field??'id, school_name');
  145. }
  146. }