/** * VIP 正式内容远程库 — 电子书 / 课程 / 课时 * 网络失败 → 包内 vipEbooks / vipCourse / vipLessonContent 兜底 */ import Taro from '@tarojs/taro' import { API_BASE, APP_TARGET, WEB_BASE } from '../utils/config' import { VIP_EBOOKS, type VipEbookId, type VipEbookPack, resolveVipEbookId, } from '../data/vipEbooks' import { VIP_COURSE_MODULES, type VipLesson, type VipModule, getVipLesson as getLocalLesson, } from '../data/vipCourse' import { VIP_LESSON_CONTENT } from '../data/vipLessonContent' export type VipCoursePack = { id: string title: string modules: VipModule[] } export type VipLessonDoc = { markdown: string videoUrl: string } type LibraryPayload = { ok?: boolean version?: number ebooks?: Record course?: VipCoursePack lessons?: Record } const CACHE_KEY = 'nomadro_vip_library_v1' const CACHE_TTL_MS = 1000 * 60 * 30 let memory: LibraryPayload | null = null function urls(path: string): string[] { if (APP_TARGET === 'h5') return [`/ebook-api${path}`, path] return [`${API_BASE}${path}`, `${WEB_BASE}${path}`] } function readCache(): LibraryPayload | null { try { const raw = Taro.getStorageSync(CACHE_KEY) if (!raw) return null const parsed = typeof raw === 'string' ? JSON.parse(raw) : raw if (Date.now() - Number(parsed.ts || 0) > CACHE_TTL_MS * 48) return null return parsed.data as LibraryPayload } catch { return null } } function writeCache(data: LibraryPayload) { try { Taro.setStorageSync(CACHE_KEY, JSON.stringify({ ts: Date.now(), data })) } catch { /* ignore */ } } async function fetchJson(path: string): Promise { for (const url of urls(path)) { try { const res = await Taro.request({ url, method: 'GET', timeout: 8000 }) if (res.statusCode >= 400) continue const body = res.data as LibraryPayload if (body && (body.ebooks || body.course || body.ok)) return body } catch { /* next */ } } return null } export async function loadVipLibrary(force = false): Promise<{ data: LibraryPayload fromOffline: boolean }> { if (!force && memory) return { data: memory, fromOffline: false } const cached = readCache() try { const remote = await fetchJson('/api/vip/library') if (remote && (remote.ebooks || remote.course)) { memory = remote writeCache(remote) return { data: remote, fromOffline: false } } } catch { /* fallthrough */ } if (cached) { memory = cached return { data: cached, fromOffline: true } } const bundle: LibraryPayload = { ok: true, ebooks: VIP_EBOOKS, course: { id: 'cloudphone', title: '云手机实战课', modules: VIP_COURSE_MODULES }, lessons: VIP_LESSON_CONTENT, } memory = bundle return { data: bundle, fromOffline: true } } export async function loadVipEbook(id?: string): Promise<{ book: VipEbookPack fromOffline: boolean }> { const bookId = resolveVipEbookId(id) const local = VIP_EBOOKS[bookId] try { const remote = await fetchJson(`/api/vip/ebooks/${bookId}`) const book = (remote as { ebook?: VipEbookPack } | null)?.ebook if (book && Array.isArray(book.sections) && book.sections.length) { return { book: { ...local, ...book, id: bookId as VipEbookId, headers: book.headers || local.headers, sections: book.sections, }, fromOffline: false, } } } catch { /* offline */ } const lib = await loadVipLibrary() const fromLib = lib.data.ebooks?.[bookId] if (fromLib?.sections?.length) { return { book: { ...local, ...fromLib, id: bookId as VipEbookId }, fromOffline: lib.fromOffline, } } return { book: local, fromOffline: true } } export async function loadVipCourse(): Promise<{ course: VipCoursePack fromOffline: boolean }> { try { const remote = await fetchJson('/api/vip/course') const course = (remote as { course?: VipCoursePack } | null)?.course if (course?.modules?.length) { return { course, fromOffline: false } } } catch { /* offline */ } const lib = await loadVipLibrary() if (lib.data.course?.modules?.length) { return { course: lib.data.course, fromOffline: lib.fromOffline } } return { course: { id: 'cloudphone', title: '云手机实战课', modules: VIP_COURSE_MODULES }, fromOffline: true, } } export async function loadVipLesson( moduleIndex: number, lessonIndex: number, ): Promise<{ lesson: VipLesson | null doc: VipLessonDoc modules: VipModule[] fromOffline: boolean }> { const { course, fromOffline } = await loadVipCourse() const modules = course.modules?.length ? course.modules : VIP_COURSE_MODULES const lesson = modules[moduleIndex]?.lessons?.[lessonIndex] || getLocalLesson(moduleIndex, lessonIndex) if (!lesson) { return { lesson: null, doc: { markdown: '', videoUrl: '' }, modules, fromOffline, } } const localDoc = VIP_LESSON_CONTENT[lesson.key] || { markdown: '', videoUrl: lesson.videoUrl || '', } try { const remote = await fetchJson(`/api/vip/lessons/${encodeURIComponent(lesson.key)}`) const body = remote as { lesson?: VipLessonDoc; meta?: VipLesson } | null if (body?.lesson || body?.meta) { return { lesson: { ...lesson, ...(body.meta || {}), key: lesson.key }, doc: { markdown: body.lesson?.markdown ?? localDoc.markdown, videoUrl: body.lesson?.videoUrl || body.meta?.videoUrl || lesson.videoUrl || localDoc.videoUrl, }, modules, fromOffline: false, } } } catch { /* offline */ } const lib = await loadVipLibrary() const fromLib = lib.data.lessons?.[lesson.key] return { lesson, doc: { markdown: fromLib?.markdown ?? localDoc.markdown, videoUrl: fromLib?.videoUrl || lesson.videoUrl || localDoc.videoUrl, }, modules, fromOffline: fromOffline || lib.fromOffline, } }