/** * 星环OPC中心 — 新手引导流程 * * 通过 3 个问题了解用户,生成个性化启动清单。 */ import type { OpcDatabase } from "../db/index.js"; /** 引导问题定义 */ export const ONBOARDING_QUESTIONS = [ { id: "stage", question: "你当前处于哪个阶段?", options: [ { value: "idea", label: "想法阶段 - 还在构思商业模式" }, { value: "preparing", label: "准备注册 - 已有明确方向,准备注册公司" }, { value: "registered", label: "已注册 - 公司已注册,准备开展业务" }, { value: "operating", label: "运营中 - 已经开始运营并产生收入" }, ], }, { id: "business_type", question: "你的主要业务类型是什么?", options: [ { value: "content", label: "内容创作 - 自媒体、课程、写作等" }, { value: "tech_service", label: "技术服务 - 开发、设计、咨询等" }, { value: "product_sales", label: "产品销售 - 电商、实物产品等" }, { value: "consulting", label: "咨询顾问 - 专业咨询、培训等" }, { value: "other", label: "其他" }, ], }, { id: "revenue_expectation", question: "你预计今年的收入目标是?", options: [ { value: "under_10w", label: "10万以下 - 小额变现或副业" }, { value: "10_50w", label: "10-50万 - 全职创业基础收入" }, { value: "50_100w", label: "50-100万 - 稳定业务收入" }, { value: "over_100w", label: "100万以上 - 规模化发展" }, ], }, ] as const; /** 引导状态 */ export type OnboardingState = { currentQuestionIndex: number; answers: Record; completed: boolean; checklist: OnboardingChecklistItem[]; }; /** 清单项 */ export type OnboardingChecklistItem = { id: string; title: string; description: string; completed: boolean; estimatedMinutes: number; }; /** 获取公司的引导状态 */ export function getOnboardingState(db: OpcDatabase, companyId: string): OnboardingState | null { const company = db.queryOne( "SELECT onboarding_data, onboarding_stage, onboarding_completed FROM opc_companies WHERE id = ?", companyId, ) as { onboarding_data: string; onboarding_stage: string; onboarding_completed: number } | null; if (!company) return null; try { const data = JSON.parse(company.onboarding_data || "{}") as Partial; return { currentQuestionIndex: data.currentQuestionIndex ?? 0, answers: data.answers ?? {}, completed: company.onboarding_completed === 1, checklist: data.checklist ?? [], }; } catch { return { currentQuestionIndex: 0, answers: {}, completed: false, checklist: [], }; } } /** 保存引导状态 */ export function saveOnboardingState( db: OpcDatabase, companyId: string, state: OnboardingState, ): void { db.execute( `UPDATE opc_companies SET onboarding_data = ?, onboarding_stage = ?, onboarding_completed = ?, updated_at = datetime('now') WHERE id = ?`, JSON.stringify(state), state.answers.stage ?? "", state.completed ? 1 : 0, companyId, ); } /** 回答问题 */ export function answerQuestion( db: OpcDatabase, companyId: string, questionId: string, answer: string, ): { success: boolean; message: string; nextQuestion?: typeof ONBOARDING_QUESTIONS[number] } { const state = getOnboardingState(db, companyId); if (!state) { return { success: false, message: "公司不存在" }; } if (state.completed) { return { success: false, message: "引导已完成,无需再次回答" }; } const currentQuestion = ONBOARDING_QUESTIONS[state.currentQuestionIndex]; if (!currentQuestion || currentQuestion.id !== questionId) { return { success: false, message: "问题 ID 不匹配" }; } // 验证答案是否有效 const validOption = currentQuestion.options.find(opt => opt.value === answer); if (!validOption) { return { success: false, message: "无效的选项" }; } // 保存答案 state.answers[questionId] = answer; state.currentQuestionIndex++; // 检查是否所有问题都已回答 if (state.currentQuestionIndex >= ONBOARDING_QUESTIONS.length) { // 生成清单 state.checklist = generateChecklist(state.answers); state.completed = false; // 清单未完成 } saveOnboardingState(db, companyId, state); const nextQuestion = ONBOARDING_QUESTIONS[state.currentQuestionIndex]; return { success: true, message: `已记录:${validOption.label}`, nextQuestion, }; } /** 生成个性化清单 */ export function generateChecklist(answers: Record): OnboardingChecklistItem[] { const stage = answers.stage ?? "idea"; const businessType = answers.business_type ?? "other"; const revenue = answers.revenue_expectation ?? "under_10w"; const checklist: OnboardingChecklistItem[] = []; // 基础任务(所有阶段) if (stage === "idea" || stage === "preparing") { checklist.push({ id: "register_company_info", title: "填写公司基本信息", description: "使用 opc_manage 工具注册你的公司信息(名称、行业、负责人等)", completed: false, estimatedMinutes: 5, }); } // OPB 商业画布 if (stage === "idea" || stage === "preparing") { checklist.push({ id: "complete_opb_canvas", title: "完成 OPB 商业画布", description: "梳理你的商业模式:目标客户、痛点、解决方案、收入模式等", completed: false, estimatedMinutes: 30, }); } // 配置 AI 团队 checklist.push({ id: "configure_ai_team", title: "配置 AI 员工团队", description: "根据业务需求启用财务、法务、运营等 AI 员工", completed: false, estimatedMinutes: 10, }); // 第一笔记账(已运营) if (stage === "operating") { checklist.push({ id: "first_transaction", title: "记录第一笔交易", description: "使用 opc_manage 添加收入或支出记录,开始财务管理", completed: false, estimatedMinutes: 5, }); } // 添加客户 if (businessType === "tech_service" || businessType === "consulting" || businessType === "content") { checklist.push({ id: "add_first_customer", title: "添加第一个客户", description: "使用 opc_manage 添加潜在客户或现有客户信息", completed: false, estimatedMinutes: 5, }); } // 创建合同模板(高收入目标) if (revenue === "50_100w" || revenue === "over_100w") { checklist.push({ id: "create_contract_template", title: "创建服务合同模板", description: "使用 opc_legal 创建标准化的服务合同模板", completed: false, estimatedMinutes: 20, }); } // 内容营销规划(内容创作) if (businessType === "content") { checklist.push({ id: "plan_content_strategy", title: "规划内容发布计划", description: "使用 opc_media 创建内容日历和发布计划", completed: false, estimatedMinutes: 15, }); } // 税务基础设置(已注册或运营中) if (stage === "registered" || stage === "operating") { checklist.push({ id: "setup_tax_basics", title: "了解税务基础", description: "查看税务日历,了解需要申报的税种和时间节点", completed: false, estimatedMinutes: 10, }); } // 项目管理(技术服务) if (businessType === "tech_service") { checklist.push({ id: "create_first_project", title: "创建第一个项目", description: "使用 opc_project 管理你的项目进度和预算", completed: false, estimatedMinutes: 10, }); } // 浏览 Dashboard checklist.push({ id: "explore_dashboard", title: "浏览运营看板", description: "访问管理后台,了解关键指标和运营状态", completed: false, estimatedMinutes: 5, }); return checklist; } /** 完成清单项 */ export function completeChecklistItem( db: OpcDatabase, companyId: string, itemId: string, ): { success: boolean; message: string; allCompleted?: boolean } { const state = getOnboardingState(db, companyId); if (!state) { return { success: false, message: "公司不存在" }; } const item = state.checklist.find(i => i.id === itemId); if (!item) { return { success: false, message: "清单项不存在" }; } if (item.completed) { return { success: false, message: "该项已完成" }; } // 标记为完成 item.completed = true; // 检查是否全部完成 const allCompleted = state.checklist.every(i => i.completed); if (allCompleted) { state.completed = true; } saveOnboardingState(db, companyId, state); return { success: true, message: `已完成:${item.title}`, allCompleted, }; } /** 获取当前问题 */ export function getCurrentQuestion( db: OpcDatabase, companyId: string, ): typeof ONBOARDING_QUESTIONS[number] | null { const state = getOnboardingState(db, companyId); if (!state || state.currentQuestionIndex >= ONBOARDING_QUESTIONS.length) { return null; } return ONBOARDING_QUESTIONS[state.currentQuestionIndex]; } /** 重置引导(用于重新开始) */ export function resetOnboarding(db: OpcDatabase, companyId: string): void { const initialState: OnboardingState = { currentQuestionIndex: 0, answers: {}, completed: false, checklist: [], }; saveOnboardingState(db, companyId, initialState); }