model = new InstitutionModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 获取院校列表 * @param $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { $query = $this->getQuery($params); $model = clone $query; $counts = [ 'count' => $model->count('id'), ]; $list = $query->select(['*']) ->orderBy('sort', 'desc') ->orderBy('id', 'desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { foreach ($list['data'] as &$item) { // 格式化时间 $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : ''; $item['update_time'] = $item['update_time'] ? datetime($item['update_time'], 'Y-m-d H:i:s') : ''; // 处理封面图片URL if (isset($item['thumb']) && $item['thumb']) { $item['thumb'] = get_image_url($item['thumb']); } // 格式化状态 $item['status_text'] = $item['status'] == 1 ? '正常' : '不显示'; // 格式化链接类型 $item['link_type_text'] = $item['link_type'] == 1 ? 'Web链接' : '小程序链接'; } } return [ 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'counts' => $counts, 'list' => isset($list['data']) ? $list['data'] : [] ]; } /** * 查询院校 * @param $params * @return \Illuminate\Database\Eloquent\Builder */ public function getQuery($params) { $where = ['mark' => 1]; $status = isset($params['status']) ? $params['status'] : 0; $linkType = isset($params['link_type']) ? $params['link_type'] : 0; $type = isset($params['type']) ? $params['type'] : 0; if ($status > 0) { $where['status'] = $status; } if ($linkType > 0) { $where['link_type'] = $linkType; } if ($type > 0) { $where['type'] = $type==3? 3 : 1; } return $this->model->where($where) ->where(function ($query) use ($params) { $keyword = isset($params['keyword']) ? $params['keyword'] : ''; if ($keyword) { $query->where(function ($q) use ($keyword) { $q->where('name', 'like', "%{$keyword}%") ->orWhere('code', 'like', "%{$keyword}%"); }); } // 处理日期区间 $date = isset($params['date']) ? $params['date'] : []; $start = isset($date[0]) ? $date[0] : ''; $end = isset($date[1]) ? $date[1] : ''; if ($start) { $query->where('create_time', '>=', strtotime($start)); } if ($end) { $query->where('create_time', '<=', strtotime($end)); } }); } /** * 添加或编辑院校 * @return array */ public function edit() { $data = request()->all(); // 验证必填字段 if (empty($data['name'])) { return message('院校名称不能为空', false); } if (empty($data['code'])) { return message('院校代码不能为空', false); } $data['link_url'] = isset($data['link_url'])? $data['link_url'] : ''; $data['mp_appid'] = isset($data['mp_appid'])? $data['mp_appid'] : ''; // 检查院校代码是否重复 // $existId = $this->checkExists('code', $data['code'], 'id', 0); // if ($existId && $existId != ($data['id'] ?? 0)) { // return message('院校代码已存在', false); // } // 处理封面图片路径(前端传递的是完整 URL,需要转换为相对路径) if (!empty($data['thumb'])) { $data['thumb'] = get_image_path($data['thumb']); } // 直接传递给 parent::edit,确保包含 id 字段用于判断新增/编辑 return parent::edit($data); } /** * 获取记录详情 * @return array */ public function info() { // 记录ID $id = request()->input("id", 0); $info = []; if ($id) { // 查询记录 $record = $this->model->where('id', $id)->where('mark', 1)->first(); if ($record) { $info = $record->toArray(); // 处理封面图片URL(确保返回完整URL) if (isset($info['thumb']) && $info['thumb']) { $info['thumb'] = get_image_url($info['thumb']); } } } return message(MESSAGE_OK, true, $info); } /** * 删除院校 * @return array */ public function delete() { // 设置日志标题 ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除院校信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']); ActionLogModel::record(); // 删除七天之前标记软删除的数据 $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete(); return parent::delete(); } /** * 设置状态 * @return array */ public function status() { return parent::status(); } /** * 院校选项 * @return array */ public function options() { // 获取参数 $param = request()->all(); // 关键词 $keyword = getter($param, "keyword"); $datas = $this->model ->where(function ($query) { $query->where(['status' => 1, 'mark' => 1]); }) ->where(function ($query) use ($keyword) { if ($keyword) { $query->where('name', 'like', "%{$keyword}%"); } }) ->select(['id', 'name', 'code']) ->orderBy('sort', 'desc') ->orderBy('id', 'desc') ->get(); return $datas ? $datas->toArray() : []; } /** * 设置排序 * @return array */ public function sort() { $data = request()->all(); if (!$data['id']) { return message('记录ID不能为空', false); } if (!isset($data['sort'])) { return message('排序值不能为空', false); } $error = ''; $item = [ 'id' => $data['id'], 'sort' => $data['sort'] ]; $rowId = $this->model->edit($item, $error); if (!$rowId) { return message($error, false); } return message(); } }