204 lines
8.2 KiB
PHP
204 lines
8.2 KiB
PHP
<?php
|
|
|
|
namespace app\app\controller;
|
|
|
|
|
|
use think\Db;
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
|
|
|
|
class Xinlv extends Base{
|
|
protected $db_name = [
|
|
'xinlv'=>'app_heart_rate_log',
|
|
];
|
|
|
|
################################################################接口################################################################
|
|
################################################################接口################################################################
|
|
################################################################接口################################################################
|
|
public function heart_rate_save_data(){
|
|
try {
|
|
$data = input('post.');
|
|
if(!array_key_exists('token', $data)){
|
|
return $this->msg(10001,'token is miss');
|
|
}
|
|
if(!array_key_exists('height', $data)){
|
|
return $this->msg(10001,'height is miss');
|
|
}
|
|
if(!array_key_exists('weight', $data)){
|
|
return $this->msg(10001,'weight is miss');
|
|
}
|
|
if(!array_key_exists('heart_rate_data', $data)){
|
|
return $this->msg(10001,'heart_rate_data is miss');
|
|
}
|
|
if(!array_key_exists('aud_id', $data)){
|
|
return $this->msg(10001,'aud_id is miss');
|
|
}
|
|
if(!array_key_exists('adc', $data)){
|
|
return $this->msg(10001,'adc is miss');
|
|
}
|
|
|
|
if(!$this->verify_data_is_ok($data['token'],'str')){
|
|
return $this->msg(10005,'token type is error');
|
|
}
|
|
if(!$this->verify_data_is_ok($data['aud_id'],'intnum')){
|
|
return $this->msg(10005,'aud_id type is error');
|
|
}
|
|
if($data['height'] == ''){
|
|
$data['height'] = 0;
|
|
}
|
|
if($data['weight'] == ''){
|
|
$data['weight'] = 0;
|
|
}
|
|
if(!is_array($data['heart_rate_data'])){
|
|
return $this->msg(10005,'heart_rate_data type is error');
|
|
}
|
|
if(!$this->verify_data_is_ok($data['adc'],'num')){
|
|
$data['adc'] = 0;
|
|
}
|
|
$return_data = $this->heart_rate_save_data_action($data);
|
|
// 成功
|
|
// $this->record_api_log($data, null, $return_data);
|
|
return $return_data;
|
|
} catch (\Exception $e) {
|
|
// 捕获异常
|
|
$logContent["flie"] = $e->getFile();
|
|
$logContent["line"] = $e->getLine();
|
|
$logContent['all_content'] = "异常信息:\n";
|
|
$logContent['all_content'] .= "消息: " . $e->getMessage() . "\n";
|
|
$logContent['all_content'] .= "代码: " . $e->getCode() . "\n";
|
|
$logContent['all_content'] .= "文件: " . $e->getFile() . "\n";
|
|
$logContent['all_content'] .= "行号: " . $e->getLine() . "\n";
|
|
$logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n";
|
|
$this->record_api_log($data, $logContent, null);
|
|
return $this->msg(99999);
|
|
}
|
|
}
|
|
|
|
|
|
#################################################################action##############################################################################
|
|
#################################################################action##############################################################################
|
|
#################################################################action##############################################################################
|
|
|
|
public function heart_rate_save_data_action($data){
|
|
// dump($data);
|
|
// die;
|
|
foreach ($data['heart_rate_data'] as $key => $value) {
|
|
if(!$this->verify_data_is_ok($value,'num')){
|
|
$data['heart_rate_data'][$key] = 0;
|
|
}
|
|
}
|
|
$heart_rate_data = implode(',',$data['heart_rate_data']);
|
|
$data['height'] = $this->getNumber($data['height']);
|
|
$data['weight'] = $this->getNumber($data['weight']);
|
|
// dump($data);
|
|
// die;
|
|
$result = Db::table($this->db_name['xinlv'])->insert([
|
|
'token'=>$data['token'],
|
|
'aud_id'=>$data['aud_id'],
|
|
'height'=>$data['height'],
|
|
'weight'=>$data['weight'],
|
|
'heart_rate'=>$heart_rate_data,
|
|
'adc'=>$data['adc'],
|
|
'create_time'=>date('Y-m-d H:i:s')
|
|
]);
|
|
if($result){
|
|
return $this->msg([]);
|
|
}else{
|
|
return $this->msg(10002);
|
|
}
|
|
}
|
|
|
|
|
|
public function heart_rate_details($data){
|
|
// 加 bcadd(,,20)
|
|
// 减 bcsub(,,20)
|
|
// 乘 bcmul(,,20)
|
|
// 除 bcdiv(,,20)
|
|
$data_map = [
|
|
['min_val'=>'0','max_val'=>'60','text'=>'过缓','color'=>'#D0873A'],
|
|
['min_val'=>'60','max_val'=>'100','text'=>'正常','color'=>'#40A980'],
|
|
['min_val'=>'100','max_val'=>'220','text'=>'过速','color'=>'#DF4841']
|
|
];
|
|
$heart_rate_v = 0;
|
|
$data = explode(',',$data);
|
|
foreach ($data as $key => $value) {
|
|
$heart_rate_v = bcadd($heart_rate_v,$value,20);
|
|
}
|
|
$heart_rate_v = bcdiv($heart_rate_v,count($data),0);
|
|
$result = [
|
|
"name"=>"心率",
|
|
"value"=>$heart_rate_v,
|
|
"unit"=>"",
|
|
"standard"=>"",
|
|
"color"=>"",
|
|
"list"=>$data_map,
|
|
"key_name"=>"heart_rate_data",
|
|
"desc"=>"心率变化与心脏疾病密切相关,请随时关注心率变化情况,尤其是运动时期。",
|
|
"offset"=>"0",
|
|
"standard_val"=>"",
|
|
"difference_val"=>"",
|
|
];
|
|
foreach ($data_map as $key => $value) {
|
|
if($heart_rate_v > $value['min_val'] && $heart_rate_v <= $value['max_val']){
|
|
$result['standard'] = $value['text'];
|
|
$result['color'] = $value['color'];
|
|
|
|
// 计算数值与区间最小值差多少
|
|
$offset_min = bcsub($heart_rate_v,$value['min_val'],20);
|
|
// 计算出区间是多少
|
|
$offset_max = bcsub($value['max_val'],$value['min_val'],20);
|
|
// 算出来在区间占比多少
|
|
$offset = bcdiv($offset_min,$offset_max,20);
|
|
if($key = 2){
|
|
$offset = bcmul(0.34,$offset,20);
|
|
$offset = bcadd($offset,0.66,2);
|
|
}else if($key = 1){
|
|
$offset = bcmul(0.33,$offset,20);
|
|
$offset = bcadd($offset,0.33,2);
|
|
}else{
|
|
$offset = bcmul(0.33,$offset,20);
|
|
}
|
|
$result['offset'] = bcmul($offset,100,0);
|
|
}
|
|
}
|
|
|
|
if($heart_rate_v > $data_map[2]['max_val']){
|
|
$result['standard'] = '危险';
|
|
$result['color'] = '#DF4841';
|
|
$result['offset'] = '100';
|
|
}
|
|
$result['curve_chart'] = [
|
|
'title' => '心率图',
|
|
'key' => 'heart_rate_data',
|
|
'line' => [
|
|
'categories' => [
|
|
|
|
],
|
|
'series' => [
|
|
[
|
|
'color'=>'#D0873A',
|
|
'data'=>[],
|
|
]
|
|
],
|
|
],
|
|
];
|
|
foreach ($data as $key => $value) {
|
|
$result['curve_chart']['line']['categories'][] = '第'.($key+1).'次';
|
|
$result['curve_chart']['line']['series'][0]['data'][] = $value;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
|
|
#################################################################工具##############################################################################
|
|
#################################################################工具##############################################################################
|
|
#################################################################工具##############################################################################
|
|
|
|
public function getNumber($str) {
|
|
// 匹配所有数字(包括小数)
|
|
preg_match('/\d+\.?\d*/', (string)$str, $matches);
|
|
return isset($matches[0]) ? (float)$matches[0] : 0;
|
|
}
|
|
|
|
|
|
} |