403 lines
11 KiB
PHP
403 lines
11 KiB
PHP
<?php
|
||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||
|
||
|
||
/**
|
||
* @author luoxuancai 2017/7/13
|
||
*/
|
||
class Index extends ApiController {
|
||
|
||
const TEST_SUFFIX = '-Test';
|
||
protected $_is_test = false;
|
||
|
||
public function __construct()
|
||
{
|
||
header('Access-Control-Allow-Credentials: true');
|
||
parent::__construct();
|
||
$this->load->model('sample_model');
|
||
$this->load->model('report_model');
|
||
}
|
||
|
||
public function index_get()
|
||
{
|
||
if(empty($_GET['id'])){
|
||
$this->setError('请求错误');
|
||
}
|
||
$id = trim($_GET['id']);
|
||
$id = $this->getDeviceId($id);
|
||
//报告文件
|
||
$ret = $this->report_model->get_result($id,$this->_is_test);
|
||
if(empty($ret)){
|
||
$this->setError('报告未生成');
|
||
die;
|
||
}
|
||
|
||
$this->setSuccess($ret);
|
||
}
|
||
|
||
//获取犬类疾病的列表,需要挂钩报告编号,以便显示锁定与解锁的疾病
|
||
protected function disease_list($need_category,$need_detail)
|
||
{
|
||
/*
|
||
* 从库中读取
|
||
$params = [];
|
||
//区分单基因/多基因
|
||
if(!empty($_GET['gene_type'])){
|
||
$params['gene_type'] = $_GET['gene_type'];
|
||
}
|
||
|
||
$this->load->model('disease_model');
|
||
$list = $this->disease_model->listing($params, 0,50000,'base.*');
|
||
*/
|
||
|
||
/*
|
||
* 从素材分类
|
||
$material = $this->report_model->get_material();
|
||
$info = [];
|
||
$list = array_merge($material['犬类单基因疾病'],$material['犬类复杂疾病']);
|
||
//类别描述删除多余字符
|
||
$searchStrOne = explode('|','类遗传疾病|类疾病|系统遗传病|遗传病|遗传疾病|疾病');
|
||
$searchStrTwo = explode('|','类|系统类|系统');
|
||
foreach ($list as $key=>$val) {
|
||
if(empty($val['所属类别'])){
|
||
$name = '其他';
|
||
}else{
|
||
//删除多余的描述
|
||
$name = str_replace($searchStrOne,'',$val['所属类别']);
|
||
$name = str_replace($searchStrTwo,'',$name);
|
||
}
|
||
$info[$name][] = [
|
||
'name'=>$key,
|
||
'lock'=>$lock && !in_array($key,$lockDisease)
|
||
];
|
||
}
|
||
*/
|
||
|
||
if(empty($_GET['id'])){
|
||
$this->setError('请求错误');
|
||
}
|
||
|
||
$id = trim($_GET['id']);
|
||
$id = $this->getDeviceId($id);
|
||
|
||
//样品及套餐信息
|
||
$this->load->model('sample_model');
|
||
$this->load->model('package_model');
|
||
$sample = $this->sample_model->get($id,'device_id');
|
||
if(empty($sample)){
|
||
$this->setError('样品不存在');
|
||
die;
|
||
}
|
||
//报告文件
|
||
$json = $this->report_model->get_report_json($id,$sample['pet_species']);
|
||
|
||
//套餐设置成1,上线后删除
|
||
//$sample['package_id'] = 1;
|
||
$package = $this->package_model->get($sample['package_id']);
|
||
if(!$package){
|
||
return [];
|
||
}
|
||
$gene_type = !empty($_GET['gene_type'])?$_GET['gene_type']:0;
|
||
return $this->report_model->get_disease_list($gene_type,$sample,$json,$package,$need_category,$need_detail);
|
||
}
|
||
|
||
//疾病列表
|
||
public function disease_list_get(){
|
||
$ret = $this->disease_list(!empty($_GET['need_category']),false);
|
||
if(!empty($_GET['need_category'])){
|
||
//需要分类的疾病是单基因疾病,将解锁的放前面
|
||
foreach ($ret as $category_name => $list) {
|
||
$lock = [];
|
||
$unlock = [];
|
||
foreach ($list as $key=>$val) {
|
||
if($val['lock']){
|
||
$lock[] = $val;
|
||
}else{
|
||
$unlock[] = $val;
|
||
}
|
||
}
|
||
$ret[$category_name] = array_merge($unlock,$lock);
|
||
}
|
||
}
|
||
$this->setSuccess($ret);
|
||
}
|
||
|
||
//疾病列表
|
||
public function disease_get()
|
||
{
|
||
/*
|
||
$this->load->model('disease_model');
|
||
$row = $this->disease_model->get($_GET['disease_name'],'base.name');
|
||
*/
|
||
if(empty($_GET['disease_name'])){
|
||
$this->setError('请求错误');
|
||
}
|
||
$ret = [];
|
||
$material = $this->disease_list(false,true);
|
||
foreach ($material as $item) {
|
||
if($item['name'] == $_GET['disease_name']){
|
||
$ret = $item;
|
||
break;
|
||
}
|
||
}
|
||
//根据基因类型,做低风险描述调整,未检测出时没有描述
|
||
if(!$ret['risk']){
|
||
$ret['risk_desc'] = $ret['gene_type'] == 1?'未检测到致病突变':'低发病风险';
|
||
}
|
||
$this->setSuccess($ret);
|
||
}
|
||
|
||
//证书图片信息
|
||
public function cert_health_list_get()
|
||
{
|
||
//疾病描述
|
||
$this->load->model('Cat_disease_desc_model');
|
||
|
||
$disease = [];
|
||
$material = $this->disease_list(false,true);
|
||
foreach ($material as $item) {
|
||
$description = $this->Cat_disease_desc_model->breeding_single_detail($item['name']);
|
||
if(empty($description)){
|
||
continue;
|
||
}
|
||
//风险等级
|
||
$risk_tag = 'N/N';
|
||
if($item['risk']){
|
||
$risk_tag = $item['risk_level'] == 1?'N/F':'F/F';
|
||
}
|
||
$disease[] = [
|
||
//疾病名称
|
||
'name'=>$item['name'],
|
||
//疾病N/N
|
||
'risk_tag'=>$risk_tag,
|
||
//结果说明
|
||
'risk_desc'=>$description['检测结果'][$risk_tag],
|
||
//检测手段
|
||
'method'=>$description['检测手段'],
|
||
//基因型
|
||
'gene_type'=>$description['基因型'],
|
||
//N/N
|
||
'NN'=>$description['结果说明']['N/N'],
|
||
'NF'=>$description['结果说明']['N/F'],
|
||
'FF'=>$description['结果说明']['F/F'],
|
||
];
|
||
}
|
||
$this->success($disease);
|
||
}
|
||
|
||
//证书图片单张
|
||
public function cert_health_get()
|
||
{
|
||
|
||
if(empty($_GET['name'])){
|
||
$this->setError('请求错误');
|
||
}
|
||
$disease = [];
|
||
$material = $this->disease_list(false,true);
|
||
foreach ($material as $item) {
|
||
if($item['name'] == $_GET['name']){
|
||
$disease = $item;
|
||
break;
|
||
}
|
||
}
|
||
//根据基因类型,做低风险描述调整,未检测出时没有描述
|
||
if(!$disease['risk']){
|
||
$disease['risk_desc'] = $disease['gene_type'] == 1?'未检测到致病突变':'低发病风险';
|
||
}
|
||
|
||
|
||
//样本信息
|
||
$id = trim($_GET['id']);
|
||
$id = $this->getDeviceId($id);
|
||
|
||
//样品及套餐信息
|
||
$this->load->model('sample_model');
|
||
$this->load->model('package_model');
|
||
$sample = $this->sample_model->get($id,'device_id');
|
||
if(empty($sample)){
|
||
$this->setError('样品不存在');
|
||
die;
|
||
}
|
||
|
||
//疾病描述
|
||
$this->load->model('Cat_disease_desc_model');
|
||
$description = $this->Cat_disease_desc_model->breeding_single_detail($_GET['name']);
|
||
if(empty($description)){
|
||
die('empty');
|
||
}
|
||
|
||
|
||
//证书文件如果有就直接输出
|
||
$this->load->library('uploads');
|
||
$id = strtoupper($id);
|
||
$upload_path = $this->uploads->get_upload_path();
|
||
$cert_img_path = $upload_path.Uploads::DIR_PER_CERT_IMG.DIRECTORY_SEPARATOR.$id.DIRECTORY_SEPARATOR;
|
||
$cert_file_name = $cert_img_path.$_GET['name'].'.jpg';
|
||
if(file_exists($cert_file_name)){
|
||
//声明需要创建的图层的图片格式
|
||
header("Content-Type:image/jpg");
|
||
echo file_get_contents($cert_file_name);
|
||
die();
|
||
}
|
||
|
||
//风险等级
|
||
$risk_tag = 'N/N';
|
||
if($disease['risk']){
|
||
$risk_tag = $disease['risk_level'] == 1?'N/F':'F/F';
|
||
}
|
||
|
||
//图片
|
||
header("Content-type: image/png");
|
||
$file = APPPATH."uahpet/cert_template/health.png";//图片跟路径
|
||
$font = APPPATH."uahpet/font/SourceHanSansCN-Regular.otf";
|
||
$font_bold = APPPATH."uahpet/font/SourceHanSansCN-Bold.otf";
|
||
$img = imagecreatefrompng($file);
|
||
$blue = imagecolorallocate($img, 17, 182, 179);
|
||
$black = imagecolorallocate($img, 62, 62, 62);
|
||
|
||
//名字
|
||
$sting = $sample['pet_name'];
|
||
imagettftext($img, 15, 0, 288, 705, $black, $font, $sting);
|
||
|
||
//养育人
|
||
$sting = $sample['name'];
|
||
imagettftext($img, 15, 0, 600, 705, $black, $font, $sting);
|
||
|
||
//测试日期
|
||
$sting = date('Y-m-d',$sample['create_time']);
|
||
imagettftext($img, 15, 0, 970, 705, $black, $font, $sting);
|
||
|
||
//疾病名称
|
||
$sting = $_GET['name'];
|
||
imagettftext($img, 27, 0, 186, 965, $blue, $font_bold, $sting);
|
||
|
||
//疾病N/N
|
||
$sting = $risk_tag;
|
||
imagettftext($img, 27, 0, 638, 965, $blue, $font_bold, $sting);
|
||
|
||
//结果说明
|
||
$sting = $description['检测结果'][$risk_tag] ;
|
||
imagettftext($img, 15, 0, 736, 960, $black, $font, $sting);
|
||
|
||
/* 证书左侧段落 */
|
||
|
||
//检测手段
|
||
$sting = '检测手段:'.$description['检测手段'];
|
||
imagettftext($img, 13, 0, 188, 1030, $black, $font, $sting);
|
||
|
||
//检测手段解释
|
||
$sting = '基 因 型:';
|
||
imagettftext($img, 13, 0, 188, 1061, $black, $font, $sting);
|
||
|
||
//检测手段解释
|
||
$sting_arr = explode("\n",$description['基因型']);
|
||
//右侧,处理换行
|
||
foreach ($sting_arr as $key=>$val) {
|
||
imagettftext($img, 13, 0, 188+85, 1061+$key*25, $black, $font, $val);
|
||
}
|
||
|
||
/* 证书右侧段落 */
|
||
|
||
//N/N
|
||
$sting = 'N/N:'.$description['结果说明']['N/N'];
|
||
imagettftext($img, 13, 0, 644, 1030, $black, $font, $sting);
|
||
|
||
//N/F
|
||
//标题
|
||
$sting_arr = $this->cutImgString($description['结果说明']['N/F'],26);
|
||
$sting = 'N/F:';
|
||
imagettftext($img, 13, 0, 644, 1061, $black, $font, $sting);
|
||
//右侧,处理换行
|
||
foreach ($sting_arr as $key=>$val) {
|
||
imagettftext($img, 13, 0, 644+45, 1061+$key*25, $black, $font, $val);
|
||
}
|
||
|
||
//F/F
|
||
$sting = 'F/F:'.$description['结果说明']['F/F'];
|
||
imagettftext($img, 13, 0, 644, 1093+(count($sting_arr)-1)*16, $black, $font, $sting);
|
||
|
||
imagejpeg($img,$cert_file_name,75);
|
||
imagedestroy($img);
|
||
|
||
echo file_get_contents($cert_file_name);
|
||
exit;
|
||
}
|
||
|
||
public function cert_health_down_post()
|
||
{
|
||
//接口禁用
|
||
die('');
|
||
$data = $this->json_input();
|
||
if(empty($data) || empty($data['id'])){
|
||
$this->setError('请求错误');
|
||
}
|
||
|
||
//样本序列号
|
||
$device_id = trim($data['id']);
|
||
$device_id = $this->getDeviceId($device_id);
|
||
|
||
//接收血统证书,血统证书很难在服务器端生成
|
||
$base64_cert_lineage = trim($data['cert_lineage']);
|
||
|
||
$this->load->library('uploads');
|
||
$this->load->library('MakeZip');
|
||
|
||
//报告id
|
||
$device_id = strtoupper($device_id);
|
||
|
||
//图片
|
||
$pet_img = $this->uploads->save_cert_img_base64(
|
||
$base64_cert_lineage,
|
||
$device_id,
|
||
'健康证书'
|
||
);
|
||
|
||
if(!$pet_img){
|
||
$this->error('健康证书输出失败');
|
||
}
|
||
|
||
//压缩
|
||
$path = (Uploads::DIR).DIRECTORY_SEPARATOR;
|
||
$sourceDir = $path.(Uploads::DIR_PER_CERT_IMG).DIRECTORY_SEPARATOR.$device_id.DIRECTORY_SEPARATOR;
|
||
$targetFile = $path.(Uploads::DIR_PER_CERT_ZIP).DIRECTORY_SEPARATOR.uniqid().'.zip';
|
||
$res = $this->makezip->zip(FCPATH.$sourceDir,FCPATH.$targetFile);
|
||
if(!$res){
|
||
throw new Exception('压缩失败');
|
||
}
|
||
$this->success(['file'=>base_url().$targetFile]);
|
||
exit;
|
||
}
|
||
|
||
private function setError($err,$code = 0){
|
||
echo json_encode(['code'=>$code,'msg'=>$err]);
|
||
die;
|
||
}
|
||
private function setSuccess($data){
|
||
echo json_encode(['code'=>1,'msg'=>'','data'=>$data]);
|
||
die;
|
||
}
|
||
|
||
protected function getDeviceId($deviceID){
|
||
//是否预置了测试参数
|
||
$this->_is_test = substr($deviceID,-strlen(self::TEST_SUFFIX)) == self::TEST_SUFFIX;
|
||
if($this->_is_test){
|
||
return substr($deviceID,0,-strlen(self::TEST_SUFFIX));
|
||
}
|
||
return $deviceID;
|
||
}
|
||
|
||
//字数太长要折行,统计折行的行数
|
||
protected function cutImgString($str,$len){
|
||
if(mb_strlen($str)<$len){
|
||
return [$str];
|
||
}
|
||
$str_len = mb_strlen($str);
|
||
$count = ceil($str_len/$len);
|
||
$result = [];
|
||
for($x = 0; $x <= $count; $x++){
|
||
$result[] =mb_substr($str,$len*$x,$len);
|
||
}
|
||
return $result;
|
||
}
|
||
}
|