You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

584 lines
20 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\API\H5;
use App\Http\Controllers\API\AspNetZhuanController;
use App\Http\Controllers\API\XCXApiController;
use App\Http\Controllers\Controller;
use App\Services\ConfigService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
public function hunjianBySFZ(Request $request)
{
$sfz = $request->post('sfz');
$res = false;
if(isset($sfz)){
$dnet = new AspNetZhuanController();
$res = $dnet->GetPersonInfo([
"zjh" => $sfz,
"action" => "2",
"ghzid"=>""
]);
return \Yz::Return(true,"查询完成",['info'=>$res]);
}else{
return \Yz::echoError1('身份证不能为空');
}
}
//获取建档信息
public function GetPersonJianDangInfo(Request $request)
{
$sfz = $request->post('sfz');
$tel = $request->post('tel');
$dnet = new AspNetZhuanController();
$data=[];
if(isset($sfz)) {
$data=[
"zjh" => $sfz,
"action" => "2",
"ghzid" => ""
];
}
if(isset($tel)) {
$data=[
"sjh" => $tel,
"action" => "3",
"ghzid" => ""
];
}
$res = $dnet->GetPersonInfo2($data);
return \Yz::Return(true,"查询完成",['info'=>$res]);
}
public function hunjian(Request $request)
{
$person_id = $request->post('person_id');
$user_info = DB::table('web_user_person')->where(['id' => $person_id])->first();
$res = false;
if (!!$user_info) {
$dnet = new AspNetZhuanController();
$res = $dnet->GetPersonInfo([
"ghzid" => $user_info->ghzid,
"action" => "1"
]);
}
$id_type_map = [
'身份证' => 0,
'居民身份证' => 0,
'驾驶证' => 1,
'外国护照' => 2,
'护照' => 2,
'港澳居民来往内地通行证' => 3,
'港澳台身份证' => 3,
'港澳通行' => 3,
'外国人永久居留身份证' => 4,
'其他' => 5,
'暂未获取' => 5,
];
$person_info = [
'姓名' => ''
];
if (!!$res && count($res) != 0) {
$res = $res[0];
$id_type = $res['ID_Type_name'] ?? '居民身份证';
$id_type_show = 0;
if (isset($id_type_map[$id_type])) {
$id_type_show = $id_type_map[$id_type];
}
$address = $res['Address'] ?? '';
// $address = '海南省海口市龙华区11111111';
$address_array = [];
if (!!$address) {
$address_data_str = file_get_contents(public_path('assets/address.json'));
$address_data = json_decode($address_data_str, true);
foreach ($address_data as $ak => $av) {
if (strstr($address, $av['name']) !== false) {
$address_array[] = [
'value' => $av['code'],
'text' => $av['name']
];
foreach ($av['children'] as $aak => $aav) {
if (strstr($address, $aav['name']) !== false) {
$address_array[] = [
'value' => $aav['code'],
'text' => $aav['name']
];
foreach ($aav['children'] as $aaak => $aaav) {
if (strstr($address, $aaav['name']) !== false) {
$address_array[] = [
'value' => $aaav['code'],
'text' => $aaav['name']
];
break;
}
}
if (count($address_array) == 2) {
$address_array[] = [
'value' => $aav['children'][0]['code'],
'text' => $aav['children'][0]['name'],
];
}
break;
}
}
if (count($address_array) == 1) {
$address_array[] = [
'value' => $av['children'][0]['code'],
'text' => $av['children'][0]['name'],
];
$address_array[] = [
'value' => $av['children'][0]['children'][0]['code'],
'text' => $av['children'][0]['children'][0]['name'],
];
}
break;
}
}
}
$id_number = $res['ID_No'] ?? '';
$birthplace = '';
if ($id_type_show == 0 && mb_strlen($id_number, 'utf-8') == 18) {
$sheng = substr($id_number, 0, 2);
$shi = substr($id_number, 0, 4);
$xian = substr($id_number, 0, 6);
$address_data_str = file_get_contents(public_path('assets/address.json'));
$address_data = json_decode($address_data_str, true);
foreach ($address_data as $ak => $av) {
if ($av['code'] == $sheng) {
$birthplace = $birthplace . $av['name'];
foreach ($av['children'] as $aak => $aav) {
if ($aav['code'] == $shi) {
$birthplace = $birthplace . $aav['name'];
foreach ($aav['children'] as $aaak => $aaav) {
if ($aaav['code'] == $xian) {
$birthplace = $birthplace . $aaav['name'];
if (count($address_array) == 0) {
$address_array = [
[
'value' => $av['code'],
'text' => $av['name'],
],
[
'value' => $aav['code'],
'text' => $aav['name']
],
[
'value' => $aaav['code'],
'text' => $aaav['name']
],
];
}
break;
}
}
break;
}
}
break;
}
}
}
if (count($address_array) == 0) {
$address_array = [
[
'value' => '11',
'text' => '北京市'
],
[
'value' => '1101',
'text' => '市辖区'
],
[
'value' => '110101',
'text' => '东城区'
],
];
}
$person_info = [
'国籍' => $res['MZ'] == '外国血统' ? '外国' : '中国',
'证件类型' => $id_type_show,
'证件号' => $res['ID_No'] ?? '',
'姓名' => $res['Patname'] ?? '',
'生日' => $res['Patbdate'] ? date('Y-m-d', strtotime($res['Patbdate'])) : date('Y-m-d'),
'民族' => $res['MZ'] ?? '',
'现地址省市区' => $address_array,
'现地址' => $address,
// '详细地址省市区' => $address_array,
// '详细地址' => $address,
'户籍地址省市区' => $address_array,
'户籍地址' => $address,
'工作单位' => "",
'文化程度' => "",
'职业' => "",
'出生地' => $birthplace,
'配偶姓名' => "",
'配偶证件类型' => 0,
'配偶证件号' => "",
'血缘关系' => "",
// '邮政编码' => "",
'手机号码' => $res['Mobile'] ?? '',
];
}
//查询日志是否曾经录入过婚检
$hunjianLog=DB::table("questions_log")->where(['personid'=>$person_id,'q_type'=>3])->orderBy('id','desc')->first();
if(!!$hunjianLog){
$person_info=json_decode($hunjianLog->content,true);
unset($person_info['详细地址省市区']);
unset($person_info['详细地址']);
unset($person_info['邮政编码']);
}
return \Yz::Return(true, '获取成功', [
'info' => $person_info
]);
}
// 获取配置更新时间
public function info(Request $request)
{
$openid = $request->post('openid');
if (!isset($openid)) return \Yz::echoError1("openid不能为空");
$user = DB::table('web_users')->where(['openid' => $openid])->first();
$userid = false;
if (!$user) {
$userid = DB::table('web_users')->insertGetId(['openid' => $openid]);
} else {
$userid = $user->id;
}
if ($userid) {
if (self::UpdatePersonList($openid)) { //调用更新就诊人方法
//查询默认就诊人
$person_list = DB::table('web_user_person')->where(['user_id' => $userid, 'is_del' => 0])->get();
$default_person = DB::table('web_user_person')->where(['user_id' => $userid, 'is_default' => 1, 'is_del' => 0])->first();
$count = count($person_list);
if ($count > 0 and !$default_person) {
DB::table('web_user_person')->where(['id' => $person_list[0]->id])->update(['is_default' => 1]);
$default_person = $person_list[0];
}
if(!$default_person) return \Yz::echoError1("获取默认体检人信息失败");
$info = [
'name' => isset($default_person->name) ? $default_person->name : null,
'sex' => isset($default_person->sex) ? $default_person->sex : null,
'phone' => isset($default_person->phone) ? $default_person->phone:null,
'id_number' => isset($default_person->id_number) ? $default_person->id_number:null,
'birthday' => isset($default_person->birthday) ? $default_person->birthday : null,
'count' => $count,
'openid' => $openid,
'person_id' => $default_person->id,
'married' => $default_person->married
];
return \Yz::Return(true, '获取成功', [
'info' => $info
]);
}
}
}
//获取名下体检人列表
public function GetPersonList()
{
$openid = request('openid');
$user = DB::table('web_users')->where(['openid' => $openid, 'status' => 1, 'is_del' => 0])->first();
if (!$user) return \Yz::echoError1('用户不存在');
$persons = DB::table('web_user_person')->where(['user_id' => $user->id, 'is_del' => 0])->get();
return \Yz::Return(true, "查询完成", ['list' => $persons]);
}
//切换默认就诊人
public function SetDefaultPerson()
{
$openid = request('openid');
$person_id = request('person_id');
$user = DB::table('web_users')->where(['openid' => $openid, 'status' => 1, 'is_del' => 0])->first();
if (!$user) return \Yz::echoError1('用户不存在');
DB::table('web_user_person')->where(['user_id' => $user->id, 'is_del' => 0, 'is_default' => 1])->update([
'is_default' => 2
]);
$u = DB::table('web_user_person')->where(['id' => $person_id, 'user_id' => $user->id, 'is_del' => 0])->update([
'is_default' => 1
]);
if ($u) {
return \Yz::Return(true, "操作完成", []);
} else {
return \Yz::echoError1('操作失败');
}
}
//获取就诊人基本信息 积分、预存款等
public function GetPersonInfo()
{
$openid = request('openid');
$person_id = request('person_id');
$user = DB::table('web_users')->where(['openid' => $openid, 'status' => 1, 'is_del' => 0])->first();
if (!$user) return \Yz::echoError1('用户不存在');
$person = DB::table('web_user_person')->where(['user_id' => $user->id, 'is_del' => 0, 'is_default' => 1])->first();
if (!$person) return \Yz::echoError1('查询就就诊人信息出错');
//调用his接口查询用户积分和预存款
$integral = 90;
$save_money = 150;
$coupon_count = 2; //优惠券数量
$person->integral = $integral;
$person->save_money = $save_money;
$person->coupon_count = $coupon_count;
return \Yz::Return(true, "查询完成", ['person_info' => $person]);
}
public function tttt()
{
$XCX = new XCXApiController();
$data = [
'wxid' => 'oosgJj-SVIxTrm_g1p213tsSHK5g'
];
$res = $XCX::Post('就诊人列表', $data);
$ApiPersonList = $res['data'];
//dd($ApiPersonList[0]);
$XCX::XCXDecode($ApiPersonList[0]['idNumber']);
}
//更新用户列表,调用远程小程序接口
public function UpdatePersonList($openid)
{
$env = config('app.globals.Env');
if ($env == 'pro') { //如果是正式环境
$XCX = new XCXApiController();
$data = [
'wxid' => $openid
];
$res = $XCX::Post('就诊人列表', $data);
$ApiPersonList = $res['data'];
// dd($ApiPersonList);
}
if ($env == 'dev') { //如果是开发环境
$ApiPersonList = [
[
'ghzid' => 'ghz11',
'idNumber' => '15210219920524154X',
'name' => '周京京',
'phone' => '11111111111',
'sex' => '1',
'birthday' => '1989-01-01',
'patientType' => '0',
'marriage' => '1',
],
[
'ghzid' => 'ghz22',
'idNumber' => '460026199002190013',
'name' => '测试2',
'phone' => '222222222',
'sex' => '1',
'birthday' => '1990-01-01',
'patientType' => '1',
'marriage' => '1',
],
[
'ghzid' => 'ghz22555',
'idNumber' => '411329199901052356',
'name' => '信息科1',
'phone' => '222222222',
'sex' => '1',
'birthday' => '1990-01-01',
'patientType' => '1',
'marriage' => '1',
]
];
}
$user = DB::table('web_users')->where(['openid' => $openid, 'status' => 1, 'is_del' => 0])->first();
if (!$user) return \Yz::echoError1('用户不存在');
//库里存在的用户ghzid数组
$db_person_ghzids = DB::table('web_user_person')->where(['user_id' => $user->id, 'is_del' => 0])->pluck('ghzid')->toArray();
//接口返回的用户ghzids数组
$api_person_ghzids = [];
foreach ($ApiPersonList as $apiperson) {
$api_person_ghzids[] = $apiperson['ghzid'];
}
// 取交集
$intersection = array_intersect($db_person_ghzids, $api_person_ghzids);
// 仅存在于 数据库里 中的ghzid
$onlyInDb = array_diff($db_person_ghzids, $api_person_ghzids);
// 仅存在于 api接口 中的ghzid
$onlyInApi = array_diff($api_person_ghzids, $db_person_ghzids);
$success_count = 0;
foreach ($ApiPersonList as $apiperson) {
$marriage = 0;
if ($apiperson['marriage'] == '未婚') {
$marriage = 2;
}
if ($apiperson['marriage'] == '已婚') {
$marriage = 1;
}
$personInfo = [
'ghzid' => $apiperson['ghzid'],
'id_number' => $env == 'dev' ? $apiperson['idNumber'] : $XCX::XCXDecode($apiperson['idNumber']),
'name' => $apiperson['name'],
'birthday' => $apiperson['birthday'],
'sex' => $apiperson['sex'],
'phone' => $env == 'dev' ? $apiperson['phone'] : $XCX::XCXDecode($apiperson['phone']),
'married' => $marriage,
'user_id' => $user->id,
'patient_type' => $apiperson['patientType'],
'updated_at' => date('Y-m-d H:i:s')
];
if (in_array($apiperson['ghzid'], $intersection)) { //双方都有的,更新
$u = DB::table('web_user_person')->where(['user_id' => $user->id, 'ghzid' => $apiperson['ghzid']])->update($personInfo);
if ($u) $success_count += $u;
}
if (in_array($apiperson['ghzid'], $onlyInApi)) { //小程序新增的 添加
$i = DB::table('web_user_person')->insert($personInfo);
if ($i) $success_count++;
}
}
if (count($onlyInDb) > 0) { //小程序不存在的用户 ,数据进行删除
$d = DB::table('web_user_person')->where(['user_id' => $user->id])->whereIn('ghzid', $onlyInDb)->update([
'is_del' => 1
]);
if ($d) $success_count++;
}
//设置默认体检人
// $default=DB::table('web_user_person')->where(['user_id' => $user->id,'is_del'=>0,'is_default'=>1])->get();
// $p_list=DB::table('web_user_person')->where(['user_id' => $user->id,'is_del'=>0])->get();
// if(count($p_list)>0 and count($default)===0){
// DB::table('web_user_person')->where(['id'=>$p_list[0]->id])->update(['is_default'=>1]);
// }
return true;
}
public function checkChildCare(Request $request)
{
$person_id = $request->post('person_id');
if (!$person_id) return \Yz::echoError1('person_id 不能为空');
$person = DB::table('web_user_person')->where(['id' => $person_id, 'is_del' => 0])->first();
if (!$person) return \Yz::echoError1('就诊人不存在');
// 获取出生日期,为空则从身份证号提取
$birthday = $person->birthday;
if (!$birthday && $person->id_number) {
$id_number = $person->id_number;
$len = strlen($id_number);
if ($len == 18) {
$birth = substr($id_number, 6, 8);
$birthday = substr($birth, 0, 4) . '-' . substr($birth, 4, 2) . '-' . substr($birth, 6, 2);
} elseif ($len == 15) {
$birth = substr($id_number, 6, 6);
$birthday = '19' . substr($birth, 0, 2) . '-' . substr($birth, 2, 2) . '-' . substr($birth, 4, 2);
}
}
if (!$birthday) return \Yz::echoError1('无法获取出生日期');
// 计算周岁年龄
$birth = new \DateTime($birthday);
$now = new \DateTime();
$age = $now->diff($birth)->y;
// 读取儿保科配置
$config = DB::table('configs')->where(['label' => '儿保科配置'])->first();
if (!$config) return \Yz::echoError1('儿保科配置未设置');
$configJson = json_decode($config->value, true);
if (!$configJson || !isset($configJson['age']) || !isset($configJson['check'])) {
return \Yz::echoError1('儿保科配置格式异常');
}
// status 灰度控制0=白名单模式1=全量模式
$status = $configJson['status'] ?? '1';
if ($status == '0') {
$testConfig = DB::table('configs')->where(['label' => '首页测试按钮'])->first();
$idList = $testConfig ? json_decode($testConfig->value, true) : [];
$inWhitelist = false;
if (is_array($idList)) {
if ($person->id_number) {
// 有身份证号,直接判断
$inWhitelist = in_array($person->id_number, $idList);
} else {
// 身份证号为空(新生儿),查同 user_id 下其他就诊人
$siblings = DB::table('web_user_person')
->where(['user_id' => $person->user_id, 'is_del' => 0])
->where('id', '!=', $person->id)
->whereNotNull('id_number')
->where('id_number', '!=', '')
->get();
foreach ($siblings as $sibling) {
if (in_array($sibling->id_number, $idList)) {
$inWhitelist = true;
break;
}
}
}
}
if (!$inWhitelist) {
return \Yz::Return(true, '成功', ['check' => '0', 'path' => '']);
}
}
$configAge = (int)$configJson['age'];
$check = $configJson['check'];
$path = $age < $configAge ? ($configJson['path'] ?? '') : '';
return \Yz::Return(true, '成功', [
'check' => $check,
'path' => $path
]);
}
public function checkTestButton(Request $request)
{
$person_id = $request->post('person_id');
if (!$person_id) return \Yz::echoError1('person_id 不能为空');
$person = DB::table('web_user_person')->where(['id' => $person_id, 'is_del' => 0])->first();
if (!$person) return \Yz::echoError1('就诊人不存在');
// 读取首页测试按钮配置
$config = DB::table('configs')->where(['label' => '首页测试按钮'])->first();
if (!$config) return \Yz::Return(true, '成功', ['check' => '0']);
$idList = json_decode($config->value, true);
if (!is_array($idList)) return \Yz::Return(true, '成功', ['check' => '0']);
$inWhitelist = false;
if ($person->id_number) {
// 有身份证号,直接判断
$inWhitelist = in_array($person->id_number, $idList);
} else {
// 身份证号为空(新生儿),查同 user_id 下其他就诊人
$siblings = DB::table('web_user_person')
->where(['user_id' => $person->user_id, 'is_del' => 0])
->where('id', '!=', $person->id)
->whereNotNull('id_number')
->where('id_number', '!=', '')
->get();
foreach ($siblings as $sibling) {
if (in_array($sibling->id_number, $idList)) {
$inWhitelist = true;
break;
}
}
}
$check = $inWhitelist ? '1' : '0';
return \Yz::Return(true, '成功', ['check' => $check]);
}
}