model = new ExamTopicModel(); } public static function make() { if (!self::$instance) { self::$instance = new TopicService(); } return self::$instance; } public function customList($param, $pageSize = 15) { $query = $this->model->where('mark', 1); if (!empty($param['paper_id'])) { $query->where('paper_id', $param['paper_id']); } if (!empty($param['keyword'])) { $query->where(function ($q) use ($param) { $q->where('id', $param['keyword']) ->orWhere('topic_name', 'like', "%{$param['keyword']}%"); }); } $list = $query->orderBy('sort', 'desc')->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; // 要处理的字段 $fields = ['poster', 'topic_analysis', 'topic_name']; $items = isset($list['data']) ? $list['data'] : []; foreach ($items as &$item) { // 1-图片 2-文字 if ($item['answer_type'] == 1 && in_array($item['topic_type'], ['填空题', '简单题'])) { $fields[] = 'correct_answer'; } if (!empty($item['show_type']) && (int) $item['show_type'] === 2) { // 图片型 → 转为完整图片路径(支持多图) foreach ($fields as $field) { if (!empty($item[$field])) { $item[$field] = format_image_field($item[$field]); } } } else { // 文本型 → 保持原样(如果你想裁剪空格可以加 trim) foreach ($fields as $field) { if (!empty($item[$field])) { $item[$field] = trim($item[$field]); } } } } return [ 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'list' => $items ]; } /** * 排序更新 * @param array $param * - paper_id * - sort_data = [{id:xx, sort:xx}, ...] * @return array */ public function sort($param) { $paperId = $param['paper_id'] ?? 0; $sortData = $param['sort_data'] ?? []; if (empty($paperId) || empty($sortData)) { return message("参数错误", false); } DB::beginTransaction(); try { // 先更新前端传来的顺序 foreach ($sortData as $item) { $this->model ->where('id', $item['id']) ->where('paper_id', $paperId) ->update([ 'sort' => $item['sort'], 'update_time' => time() ]); } // 再统一重新编号,保证唯一性 + 倒序 $topics = $this->model ->where('paper_id', $paperId) ->orderByDesc('sort') ->get(); $newSort = count($topics); foreach ($topics as $topic) { $this->model ->where('id', $topic->id) ->update(['sort' => $newSort--]); } DB::commit(); return message("排序更新成功", true); } catch (\Exception $e) { DB::rollBack(); return message("排序更新失败:" . $e->getMessage(), false); } } /** * 编辑或新增视频集 */ public function edit() { $data = request()->all(); // 定义需要处理的字段 $fields = ['poster', 'topic_analysis', 'topic_name', 'correct_answer']; foreach ($fields as $field) { if (!empty($data[$field])) { // 如果 show_type == 2 → 处理为图片路径 if (!empty($data['show_type']) && (int) $data['show_type'] === 2) { if (is_array($data[$field])) { $data[$field] = array_map(function ($img) { return get_image_path(trim($img)); }, $data[$field]); $data[$field] = implode(',', $data[$field]); // 多图用逗号拼接 } else { $data[$field] = get_image_path(trim($data[$field])); } } else { // show_type != 2 → 文本,直接 trim if (is_array($data[$field])) { $data[$field] = implode(',', array_map('trim', $data[$field])); } else { $data[$field] = trim($data[$field]); } } } } $id = $data['id'] ?? 0; // 默认字段处理 $data['update_time'] = time(); if (!$id) { $data['create_time'] = time(); } return parent::edit($data); // 调用父类的 edit 方法 } }