where('status', 'normal') ->where('verify', 'passed'); if ($type) { $query->where('type', $type); } if ($categoryId) { $query->where('category_id', $categoryId); } $all = $query->order('weigh', 'desc')->select(); // 课程按 part(relation/parenting)过滤 if ($part) { $all = array_values(array_filter($all, function ($item) use ($part) { $extra = json_decode($item['extra'], true) ?: []; return isset($extra['part']) && $extra['part'] === $part; })); } // 阶段优先排序(不足则回退全量,避免列表空置) if ($phase) { $matched = []; $rest = []; foreach ($all as $item) { $phases = array_filter(explode(',', $item['phase'])); if (in_array('all', $phases) || in_array($phase, $phases)) { $matched[] = $item; } else { $rest[] = $item; } } $all = (count($matched) >= 3 || !$rest) ? $matched : array_merge($matched, $rest); } $total = count($all); $slice = array_slice($all, ($page - 1) * $limit, $limit); $member = MemberService::status($userId); $purchased = self::purchasedIds($userId); $list = []; foreach ($slice as $item) { $list[] = self::format($item, $member, false, $purchased); } return [ 'list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit, 'has_more' => $page * $limit < $total, 'phase' => $phase, ]; } /** * 内容详情 */ public static function detail($userId, $id) { $item = Db::name('content')->where('id', (int)$id)->where('status', 'normal')->find(); if (!$item) { return null; } $member = MemberService::status($userId); return self::format($item, $member, true, self::purchasedIds($userId)); } /** * 用户已购买的课程 ID */ public static function purchasedIds($userId) { return Db::name('course_order') ->where('user_id', $userId) ->where('pay_status', 'paid') ->column('content_id'); } /** * 是否已购买指定课程 */ public static function hasPurchased($userId, $contentId) { return Db::name('course_order') ->where('user_id', $userId) ->where('content_id', $contentId) ->where('pay_status', 'paid') ->count() > 0; } /** * 统一格式化(含会员锁态与免责声明) */ public static function format($item, $member, $withBody = false, $purchased = []) { $ownsCourse = in_array($item['id'], array_map('intval', (array)$purchased), true); $locked = !$ownsCourse && in_array($item['access'], ['member', 'pay'], true) && !$member['is_member']; if (in_array($item['access'], ['pay'], true) && !$ownsCourse && !$locked) { // 单课付费(非会员)也需要购买 $locked = true; } $extra = $item['extra'] ? json_decode($item['extra'], true) : []; $data = [ 'id' => (int)$item['id'], 'type' => $item['type'], 'category_id' => (int)$item['category_id'], 'title' => $item['title'], 'summary' => $item['summary'], 'emoji' => $item['cover_emoji'], 'media_type' => $item['media_type'], 'duration' => (int)$item['duration'], 'duration_text' => $item['duration'] ? floor($item['duration'] / 60) . ' 分钟' : '', 'access' => $item['access'], 'locked' => $locked, 'extra' => $extra, 'lock_tip' => $locked ? '该内容为会员专享,开通会员即可查看' : '', ]; if ($withBody) { $data['body'] = $locked ? mb_substr($item['body'], 0, 80) . '…' : $item['body']; $data['disclaimer'] = in_array($item['type'], ['advice', 'knowledge', 'emotion', 'guide'], true) ? '以上内容仅供参考,不能替代医疗诊断。' : ''; } return $data; } }