This commit is contained in:
Your Name
2026-05-29 09:28:38 +08:00
parent 7b46204454
commit 94e787ae80
11 changed files with 1834 additions and 0 deletions
@@ -0,0 +1,130 @@
<?php
namespace app\api\logic\tcm;
use app\adminapi\logic\tcm\DiagnosisLogic;
use app\common\model\user\User;
/**
* 日常血糖:手机号快捷建档(无完整就诊卡时)
*/
class DailyPhoneLogic
{
/** 小程序完整建档(edit_card */
public const CREATE_SOURCE_MNP = 'mnp';
/** 小程序手机号快捷建档(日常血糖) */
public const CREATE_SOURCE_MNP_DAILY = 'mnp_daily';
/**
* 用户 sex(1男2女) → 诊单 gender(1男0女)
*/
public static function mapUserSexToDiagnosisGender($sex): int
{
$sex = (int) $sex;
if ($sex === 2) {
return 0;
}
if ($sex === 1) {
return 1;
}
return 1;
}
/**
* 已绑定手机号则返回可用诊单;无卡时自动创建轻量档案
*
* @param array{sex?:int|null} $options sex: 用户表 1男2女,授权手机号时可传入
* @return array{ok:bool,need_mobile?:bool,error?:string,created?:bool,diagnosis_id?:int,patient_id?:int,patient_name?:string,gender?:int,age?:int,mobile?:string,create_source?:string}
*/
public static function ensureDailyContext(int $userId, array $options = []): array
{
if ($userId <= 0) {
return ['ok' => false, 'need_mobile' => false, 'error' => '请先登录'];
}
$user = User::where('id', $userId)
->field('id,nickname,real_name,mobile,sex,age')
->find();
if (!$user) {
return ['ok' => false, 'need_mobile' => false, 'error' => '用户不存在'];
}
$mobile = trim((string) ($user['mobile'] ?? ''));
if ($mobile === '') {
return ['ok' => false, 'need_mobile' => true, 'error' => '请先授权手机号'];
}
if (array_key_exists('sex', $options) && $options['sex'] !== null && $options['sex'] !== '') {
$sex = (int) $options['sex'];
if (in_array($sex, [1, 2], true) && (int) ($user['sex'] ?? 0) !== $sex) {
User::update(['id' => $userId, 'sex' => $sex]);
$user['sex'] = $sex;
}
}
$cardList = DiagnosisLogic::getCardList($userId);
if ($cardList === false) {
return [
'ok' => false,
'need_mobile' => false,
'error' => DiagnosisLogic::getError() ?: '获取就诊卡失败',
];
}
if (!empty($cardList)) {
$card = $cardList[0];
return [
'ok' => true,
'need_mobile' => false,
'created' => false,
'diagnosis_id' => (int) $card['id'],
'patient_id' => (int) $card['patient_id'],
'patient_name' => (string) ($card['patient_name'] ?? ''),
'gender' => (int) ($card['gender'] ?? 0),
'age' => (int) ($card['age'] ?? 0),
'mobile' => $mobile,
'create_source' => (string) ($card['create_source'] ?? ''),
];
}
$displayName = trim((string) (($user['real_name'] ?? '') ?: ($user['nickname'] ?? '')));
if ($displayName === '') {
$displayName = '用户' . substr($mobile, -4);
}
$gender = self::mapUserSexToDiagnosisGender($user['sex'] ?? 1);
$result = DiagnosisLogic::addCard([
'user_id' => $userId,
'patient_name' => $displayName,
'phone' => $mobile,
'gender' => $gender,
'age' => max(0, (int) ($user['age'] ?? 0)),
'diagnosis_date' => time(),
'create_source' => self::CREATE_SOURCE_MNP_DAILY,
'remark' => '小程序手机号快捷建档(日常血糖)',
]);
if ($result === false) {
return [
'ok' => false,
'need_mobile' => false,
'error' => DiagnosisLogic::getError() ?: '创建档案失败',
];
}
return [
'ok' => true,
'need_mobile' => false,
'created' => true,
'diagnosis_id' => (int) $result['id'],
'patient_id' => (int) $result['patient_id'],
'patient_name' => $displayName,
'gender' => $gender,
'age' => max(0, (int) ($user['age'] ?? 0)),
'mobile' => $mobile,
'create_source' => self::CREATE_SOURCE_MNP_DAILY,
];
}
}