feat: 补齐 M1–M9 全部 47 个功能点并打通前后端
后端(新增 7 控制器 + 2 服务 + 17 张表) - Prepare:排卵预测、BBT(双向体温识别)、试纸识别(规则引擎+额度)、备孕任务、同房指导 - Family:邀请码绑定(≤5)、话术库、爱心积分、私密留言、家庭健康视图 - Health:按周期阶段输出饮食/运动/睡眠/痛经四类建议 - Relation:关系测评、引导卡、冲突工具、课程下单、双人任务(联动记忆)、纪念日 - Parenting:宝宝档案、成长记录、月龄活动匹配、共育任务、育儿情绪、疫苗儿保排期 - Consult:咨询师(资质/定价/时段)、预约下单/取消、量表测评(4 套)、危机干预转介 - Setting:自定义提醒 CRUD、数据备份快照与恢复(保留 5 份) - Mood 增补急救包;Home 提醒合并自定义提醒 - 服务层:ContentService(统一内容/锁态/购买态)、AssessmentService(计分分级+危机识别) 前端(新增 8 页面) - 备孕、家人共护、健康建议、两性关系、亲子教育、心理咨询、测评执行页、情绪急救包 - 首页快捷入口扩至 8 个模块;我的页新增提醒中心与数据备份 - utils/api.js 扩至 84 个接口 数据(可重复执行脚本) - 03_modules.sql:fa_reminder/bbt/lh_test/task/family_bind/care_message/point_log/memory/ assessment(+record)/course_order/baby/growth_record/vaccine/counselor/consult_order/user_backup - 04_module_seed.sql:话术6/引导卡6/冲突3/急救4/同房3/课程6/活动6/育儿情绪4/知识7、 4 套量表、17 项疫苗儿保、3 位咨询师 验证(全部通过) - 接口全链路 82 项断言(M1–M9,含高风险测评危机干预、备份恢复) - 契约校验 84 接口 0 偏差;Vue SFC 19/19、SCSS 18/18 编译通过 - 修复 3 个真实缺陷:action 内调用 action 导致响应被劫持、coTasks 返回数组、 咨询时段跨年错误
这个提交包含在:
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 量表测评服务(M7 关系测评 / M9 心理测评 共用)
|
||||
*
|
||||
* 题目为文本数组,答案统一 0-4 五级计分;分级阈值由 scoring 字段配置。
|
||||
* 危机干预:命中高风险或用「自我伤害」类题目高分时返回转介信息。
|
||||
*/
|
||||
class AssessmentService
|
||||
{
|
||||
/** 危机干预提示(合规文案,必须与人工审核口径一致) */
|
||||
public static function crisis()
|
||||
{
|
||||
return [
|
||||
'title' => '请优先照顾当下的安全',
|
||||
'text' => '如果出现伤害自己或他人的念头,请立即联系身边的人陪伴,并拨打心理援助热线:12356(心理援助)或 120(急救)。',
|
||||
'hotlines' => [
|
||||
['name' => '全国心理援助热线', 'phone' => '12356'],
|
||||
['name' => '北京心理危机研究与干预中心', 'phone' => '010-82951332'],
|
||||
],
|
||||
'action' => '如需专业帮助,可预约在线心理咨询',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 量表列表
|
||||
*/
|
||||
public static function lists($scope = '', $userId = 0)
|
||||
{
|
||||
$query = Db::name('assessment')->where('status', 'normal');
|
||||
if ($scope) {
|
||||
$query->where('scope', $scope);
|
||||
}
|
||||
$list = $query->order('weigh', 'desc')->select();
|
||||
$member = $userId ? MemberService::status($userId) : ['is_member' => false];
|
||||
|
||||
$result = [];
|
||||
foreach ($list as $item) {
|
||||
$questions = json_decode($item['questions'], true) ?: [];
|
||||
$last = $userId ? Db::name('assessment_record')
|
||||
->where('user_id', $userId)
|
||||
->where('assessment_id', $item['id'])
|
||||
->order('id', 'desc')
|
||||
->find() : null;
|
||||
|
||||
$result[] = [
|
||||
'id' => (int)$item['id'],
|
||||
'code' => $item['code'],
|
||||
'title' => $item['title'],
|
||||
'intro' => $item['intro'],
|
||||
'scope' => $item['scope'],
|
||||
'level' => $item['level'],
|
||||
'access' => $item['access'],
|
||||
'locked' => $item['access'] === 'member' && !$member['is_member'],
|
||||
'count' => count($questions),
|
||||
'last_score' => $last ? (float)$last['score'] : null,
|
||||
'last_result' => $last ? $last['result'] : '',
|
||||
'last_date' => $last ? date('Y-m-d', (int)$last['createtime']) : '',
|
||||
];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 量表详情(题目与选项)
|
||||
*/
|
||||
public static function detail($id)
|
||||
{
|
||||
$item = Db::name('assessment')->where('id', (int)$id)->where('status', 'normal')->find();
|
||||
if (!$item) {
|
||||
return null;
|
||||
}
|
||||
return [
|
||||
'id' => (int)$item['id'],
|
||||
'code' => $item['code'],
|
||||
'title' => $item['title'],
|
||||
'intro' => $item['intro'],
|
||||
'scope' => $item['scope'],
|
||||
'questions' => json_decode($item['questions'], true) ?: [],
|
||||
'options' => self::options(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function options()
|
||||
{
|
||||
return [
|
||||
['text' => '没有', 'value' => 0],
|
||||
['text' => '偶尔', 'value' => 1],
|
||||
['text' => '有时', 'value' => 2],
|
||||
['text' => '经常', 'value' => 3],
|
||||
['text' => '总是', 'value' => 4],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交作答
|
||||
*
|
||||
* @param int $userId
|
||||
* @param int $assessmentId
|
||||
* @param array $answers 与题目顺序一致的 0-4 分值
|
||||
* @return array
|
||||
*/
|
||||
public static function submit($userId, $assessmentId, $answers)
|
||||
{
|
||||
$item = Db::name('assessment')->where('id', (int)$assessmentId)->where('status', 'normal')->find();
|
||||
if (!$item) {
|
||||
return ['error' => '量表不存在'];
|
||||
}
|
||||
|
||||
$questions = json_decode($item['questions'], true) ?: [];
|
||||
$answers = is_array($answers) ? array_values($answers) : [];
|
||||
if (count($answers) !== count($questions)) {
|
||||
return ['error' => '请完成全部题目'];
|
||||
}
|
||||
|
||||
$clean = [];
|
||||
$score = 0;
|
||||
foreach ($answers as $value) {
|
||||
$v = min(4, max(0, (int)$value));
|
||||
$clean[] = $v;
|
||||
$score += $v;
|
||||
}
|
||||
|
||||
$scoring = json_decode($item['scoring'], true) ?: [];
|
||||
$levels = isset($scoring['levels']) ? $scoring['levels'] : [];
|
||||
$max = isset($scoring['max']) ? (int)$scoring['max'] : count($questions) * 4;
|
||||
|
||||
$resultKey = 'low';
|
||||
$resultText = '';
|
||||
foreach ($levels as $level) {
|
||||
if ($score < (int)$level['lt']) {
|
||||
$resultKey = $level['key'];
|
||||
$resultText = $level['text'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$resultText) {
|
||||
$last = end($levels);
|
||||
if ($last) {
|
||||
$resultKey = $last['key'];
|
||||
$resultText = $last['text'];
|
||||
}
|
||||
}
|
||||
|
||||
// 危机识别:产后筛查最后一题(自伤念头)或总分达到满分
|
||||
$risk = 0;
|
||||
$crisisQ = ['pdp' => 4];
|
||||
if (isset($crisisQ[$item['code']]) && isset($clean[$crisisQ[$item['code']]]) && $clean[$crisisQ[$item['code']]] >= 2) {
|
||||
$risk = 1;
|
||||
}
|
||||
if ($score >= $max) {
|
||||
$risk = 1;
|
||||
}
|
||||
|
||||
$advice = [
|
||||
'测评结果不是诊断,仅用于自我觉察。',
|
||||
$resultText,
|
||||
];
|
||||
if ($item['code'] === 'pms') {
|
||||
$advice[] = '建议连续记录 2-3 个周期的情绪与症状,观察规律。';
|
||||
} elseif ($item['code'] === 'prepare_anxiety') {
|
||||
$advice[] = '可以和伴侣约定每天 15 分钟「不谈备孕」的时间。';
|
||||
} elseif ($item['code'] === 'relation') {
|
||||
$advice[] = '试试本周安排一次不被打断的 30 分钟对话。';
|
||||
} elseif ($item['code'] === 'pdp') {
|
||||
$advice[] = '把感受告诉家人,让他们参与照护;必要时就医评估。';
|
||||
}
|
||||
|
||||
$id = Db::name('assessment_record')->insertGetId([
|
||||
'user_id' => $userId,
|
||||
'assessment_id' => (int)$item['id'],
|
||||
'answers' => json_encode($clean),
|
||||
'score' => $score,
|
||||
'result' => $resultKey,
|
||||
'report' => $resultText,
|
||||
'risk_flag' => $risk,
|
||||
'createtime' => time(),
|
||||
]);
|
||||
|
||||
return [
|
||||
'id' => (int)$id,
|
||||
'title' => $item['title'],
|
||||
'score' => $score,
|
||||
'max' => $max,
|
||||
'result' => $resultKey,
|
||||
'result_text' => $resultText,
|
||||
'advice' => $advice,
|
||||
'risk_flag' => $risk,
|
||||
'crisis' => $risk ? self::crisis() : null,
|
||||
'disclaimer' => '测评结果仅供参考,不能替代专业诊断。',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 内容服务(情绪 / 健康建议 / 话术 / 引导卡 / 课程 / 亲子活动 / 知识库 共用)
|
||||
*
|
||||
* 统一处理:类型筛选、阶段匹配、会员锁态、分页。
|
||||
*/
|
||||
class ContentService
|
||||
{
|
||||
/**
|
||||
* 内容列表
|
||||
*
|
||||
* @param int $userId
|
||||
* @param array $params type/phase/category_id/page/limit/part
|
||||
* @return array
|
||||
*/
|
||||
public static function lists($userId, $params = [])
|
||||
{
|
||||
$type = isset($params['type']) ? $params['type'] : '';
|
||||
$phase = isset($params['phase']) ? $params['phase'] : '';
|
||||
$categoryId = isset($params['category_id']) ? (int)$params['category_id'] : 0;
|
||||
$part = isset($params['part']) ? $params['part'] : '';
|
||||
$page = max(1, isset($params['page']) ? (int)$params['page'] : 1);
|
||||
$limit = min(50, max(1, isset($params['limit']) ? (int)$params['limit'] : 10));
|
||||
|
||||
$query = Db::name('content')
|
||||
->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;
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户