'app_account_number',//账号表 'juese'=>'app_user_data',//角色表 ]; protected $kitchenscale_db_msg = [ 'cookbook'=>'app_user_cookbook',//菜谱表 'uploadimg'=>'app_user_upload_img',//素材表 'followlist'=>'app_user_follow_list',//关注列表 'collect_list'=>'app_user_collect_list',//收藏列表 'foodlist3'=>'app_z_national_standard_food_type_3',//食材列表3 'foodlist4'=>'app_z_national_standard_food_type_4',//食材列表3 'eat_log'=>'app_user_kcal_log',//食材列表3 'user'=>'app_user_data',//banner ]; // 加 bcadd(,,20) // 减 bcsub(,,20) // 乘 bcmul(,,20) // 除 bcdiv(,,20) ################################################################接口################################################################ ################################################################接口################################################################ ################################################################接口################################################################ // 添加每日摄入记录 public function add_intake_food($data=['token'=>'3e5876042361c8cb42bd48c46918f737','aud_id'=>6,'meals_type'=>'早餐','food_list'=>[['id'=>2778,'name'=>"啤酒(X)",'weight'=>'150','unit'=>'g']]]){ // 尝试捕获异常 try { if(count(input('post.')) > 0){ $data = input('post.'); } if(!array_key_exists('token', $data)){ return $this->msg(10001,'token is miss'); } if(!array_key_exists('aud_id', $data)){ return $this->msg(10001,'aud_id is miss'); } if(!array_key_exists('meals_type', $data)){ return $this->msg(10001,'meals_type is miss'); } if(!array_key_exists('food_list', $data)){ return $this->msg(10001,'food_list 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'); } $return_data = $this->add_intake_food_action($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 json(['status' => 'error', 'message' => '系统错误']); } } // 获取记食器板块内容 public function get_countfoot_content(){ // 尝试捕获异常 // try { if(count(input('post.')) > 0){ $data = input('post.'); } if(!array_key_exists('token', $data)){ return $this->msg(10001,'token is miss'); } if(!array_key_exists('aud_id', $data)){ return $this->msg(10001,'aud_id is miss'); } if(!array_key_exists('time', $data)){ return $this->msg(10001,'time 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(!$this->verify_data_is_ok($data['time'],'datetime')){ return $this->msg(10005,'time type is error'); } $return_data = $this->get_countfoot_content_action($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 json(['status' => 'error', 'message' => '系统错误']); // } } // 获取记食器记录 public function get_log_list($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>1,'s_time'=>'2025-03-15','e_time'=>'2025-03-16']){ // 尝试捕获异常 try { if(count(input('post.')) > 0){ $data = input('post.'); } if(!array_key_exists('token', $data)){ return $this->msg(10001,'token is miss'); } if(!array_key_exists('aud_id', $data)){ return $this->msg(10001,'aud_id is miss'); } if(!array_key_exists('s_time', $data)){ return $this->msg(10001,'s_time is miss'); } if(!array_key_exists('e_time', $data)){ return $this->msg(10001,'e_time 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(!$this->verify_data_is_ok($data['s_time'],'datetime')){ return $this->msg(10005,'s_time type is error'); } if(!$this->verify_data_is_ok($data['e_time'],'datetime')){ return $this->msg(10005,'e_time type is error'); } $return_data = $this->get_log_list_action($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 json(['status' => 'error', 'message' => '系统错误']); } } // 计食器板块-设置内容 public function set_up_content($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>61]){ // 尝试捕获异常 try { if(count(input('post.')) > 0){ $data = input('post.'); } if(!array_key_exists('token', $data)){ return $this->msg(10001,'token is miss'); } if(!array_key_exists('aud_id', $data)){ return $this->msg(10001,'aud_id 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'); } $return_data = $this->set_up_content_action($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 json(['status' => 'error', 'message' => '系统错误']); } } // 设置用户的卡路里 public function set_user_kcal($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>61,'set_kcal'=>2000]){ // 尝试捕获异常 try { if(count(input('post.')) > 0){ $data = input('post.'); } if(!array_key_exists('token', $data)){ return $this->msg(10001,'token is miss'); } if(!array_key_exists('aud_id', $data)){ return $this->msg(10001,'aud_id is miss'); } if(!array_key_exists('set_kcal', $data)){ return $this->msg(10001,'set_kcal 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(!$this->verify_data_is_ok($data['set_kcal'],'num')){ return $this->msg(10005,'set_kcal type is error'); } $return_data = $this->set_user_kcal_action($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 json(['status' => 'error', 'message' => '系统错误']); } } public function del_user_eat_log($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>6,'eat_log_id'=>160]){ // 尝试捕获异常 try { if(count(input('post.')) > 0){ $data = input('post.'); } if(!array_key_exists('token', $data)){ return $this->msg(10001,'token is miss'); } if(!array_key_exists('aud_id', $data)){ return $this->msg(10001,'aud_id is miss'); } if(!array_key_exists('eat_log_id', $data)){ return $this->msg(10001,'eat_log_id 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(!$this->verify_data_is_ok($data['eat_log_id'],'num')){ return $this->msg(10005,'eat_log_id type is error'); } $return_data = $this->del_user_eat_log_action($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 json(['status' => 'error', 'message' => '系统错误']); } } #######################################################################action####################################################################### #######################################################################action####################################################################### #######################################################################action####################################################################### // 加 bcadd(,,20) // 减 bcsub(,,20) // 乘 bcmul(,,20) // 除 bcdiv(,,20) public function add_intake_food_action($data){ $cfc = Db::connect('cfc_db'); $user_data = $cfc->table($this->kitchenscale_db_msg['user']) ->where(["id"=>$data['aud_id']]) ->field('weight,height,gender,age,is_use_set_kcal,set_kcal') ->find(); if(!$user_data){ return $this->msg(10003); } // 统计食物的id $food_id_arr = []; foreach ($data['food_list'] as $key => $value) { if(!array_key_exists('id', $value)){ return $this->msg(10001,'id is miss'); } if(!array_key_exists('weight', $value)){ return $this->msg(10001,'weight is miss'); } if(!$this->verify_data_is_ok($value['id'],'intnum')){ return $this->msg(10005,'id type is error'); } if(!$this->verify_data_is_ok($value['weight'],'num')){ return $this->msg(10005,'weight type is error'); } foreach ($data['food_list'][$key] as $k => $v) { // dump($k); if(!in_array($k,['id','weight','unit'])){ unset($data['food_list'][$key][$k]); } } array_push($food_id_arr,$value['id']); } $food_content = $cfc->table($this->kitchenscale_db_msg['foodlist3']) ->where("id in (".implode(',',$food_id_arr).")") ->field('id,food_name as name,Calorie_val as kcal,Carbohydrate_val as carbohydrate,Protein_val as protein,Fat_val as fat') ->select(); // 整理食物信息 $food_content_arr = []; foreach ($food_content as $key => $value) { $food_content_arr[$value['id']] = $value; } $create_time = date('Y-m-d H:i:s'); foreach ($data['food_list'] as $key => $value) { //获取每100g食物的比例 $proportion_num = bcdiv($value['weight'],100,2); if(array_key_exists($value['id'], $food_content_arr)){ $data['food_list'][$key]['kcal_val'] = bcmul($food_content_arr[$value['id']]['kcal'],$proportion_num,2); $data['food_list'][$key]['carbohydrate_val'] = bcmul($food_content_arr[$value['id']]['carbohydrate'],$proportion_num,2); $data['food_list'][$key]['protein_val'] = bcmul($food_content_arr[$value['id']]['protein'],$proportion_num,2); $data['food_list'][$key]['fat_val'] = bcmul($food_content_arr[$value['id']]['fat'],$proportion_num,2); $data['food_list'][$key]['food_name'] = $food_content_arr[$value['id']]['name']; $data['food_list'][$key]['aud_id'] = $data['aud_id']; $data['food_list'][$key]['meals_type'] = $data['meals_type']; $data['food_list'][$key]['create_time'] = $create_time; $data['food_list'][$key]['food_id'] = $value['id']; unset($data['food_list'][$key]['id']); }else{ unset($data['food_list'][$key]); } } // dump($data['food_list']); // die; // 数据库数据字段:id,aud_id,meals_type,food_id,food_name,weight,kcal_val,carbohydrate_val,protein_val,fat_val,create_time // 启动事务 Db::startTrans(); try{ $result = $cfc->table($this->kitchenscale_db_msg['eat_log'])->insertAll($data['food_list']); if ($result !== count($data['food_list'])) { Db::rollback(); return $this->msg(10002,'添加数据错误'); } else { Db::commit(); return $this->msg([]); } } catch (\Exception $e) { // 回滚事务 Db::rollback(); return $this->msg(10002); } // dump($data); // dump($food_id_arr); // dump($food_content); // die; } public function get_countfoot_content_action($data){ $cfc = Db::connect('cfc_db'); $user_data = $cfc->table($this->kitchenscale_db_msg['user']) ->where(["id"=>$data['aud_id']]) ->field('weight,height,gender,age,birthday,is_use_set_kcal,set_kcal') ->find(); if(!$user_data){ return $this->msg(10003); } if($user_data['birthday']){ $user_data['age_num'] = $this->calculate_age($user_data['birthday']); }else{ $user_data['age_num'] = $user_data['age']; } $nutrition_data = $this->count_user_nutrition_all($user_data); if($user_data['is_use_set_kcal'] == 1){ // 加 bcadd(,,20) // 减 bcsub(,,20) // 乘 bcmul(,,20) // 除 bcdiv(,,20) $proportion = bcdiv($user_data['set_kcal'],$nutrition_data['kcal'],20); $nutrition_data['kcal'] = $user_data['set_kcal']; $nutrition_data['carbohydrate'] = bcmul($nutrition_data['carbohydrate'],$proportion,2); $nutrition_data['protein'] = bcmul($nutrition_data['protein'],$proportion,2); $nutrition_data['fat'] = bcmul($nutrition_data['fat'],$proportion,2); } // 查询用户今日摄入食物 $food_content = $cfc->table($this->kitchenscale_db_msg['eat_log']) ->alias('a') ->join('app_z_national_standard_food_type_3 b','a.food_id = b.id','LEFT') ->where("a.is_del = 0 AND a.aud_id = " . $data['aud_id'] . " AND CAST(a.create_time AS DATE) = CAST('" . $data['time'] . "' AS DATE)") ->field('a.meals_type,a.food_name,a.weight,a.kcal_val,a.carbohydrate_val,a.protein_val,a.fat_val,a.id,\'https://tc.pcxbc.com\' + b.pic_url as pic_url,a.food_id') ->select(); if(count($food_content) > 0){ $food_content = $this->calculate_nutrients($food_content); } $date = date('Y-m-d H:i:s'); $return_data = [ 'suggestion'=>[ 'kcal'=>$nutrition_data['kcal'], 'carbohydrate'=>$nutrition_data['carbohydrate'], 'protein'=>$nutrition_data['protein'], 'fat'=>$nutrition_data['fat'], ], 'today_intake'=>[ 'kcal'=>0, 'carbohydrate'=>0, 'protein'=>0, 'fat'=>0, ], 'remaining_kcal'=>$nutrition_data['kcal'],//剩下可摄入卡路里 'list'=>[ [ 'name'=>'早餐', 'val'=>0, 'unit'=>'kcal', 'list'=>[], ], [ 'name'=>'午餐', 'val'=>0, 'unit'=>'kcal', 'list'=>[], ], [ 'name'=>'晚餐', 'val'=>0, 'unit'=>'kcal', 'list'=>[], ], [ 'name'=>'早加餐', 'val'=>0, 'unit'=>'kcal', 'list'=>[], ], [ 'name'=>'午加餐', 'val'=>0, 'unit'=>'kcal', 'list'=>[], ], [ 'name'=>'晚加餐', 'val'=>0, 'unit'=>'kcal', 'list'=>[], ], ], 'date'=>date('Y-m-d'), ]; if(count($food_content) <= 0){ $return_data['list'] = []; return $this->msg($return_data); } foreach ($food_content as $key => $value) { $return_data['today_intake']['kcal'] = bcadd($return_data['today_intake']['kcal'],$value['kcal_val'],2); $return_data['today_intake']['carbohydrate'] = bcadd($return_data['today_intake']['carbohydrate'],$value['carbohydrate_val'],2); $return_data['today_intake']['protein'] = bcadd($return_data['today_intake']['protein'],$value['protein_val'],2); $return_data['today_intake']['fat'] = bcadd($return_data['today_intake']['fat'],$value['fat_val'],2); if($value['meals_type'] == '早餐'){ $return_data['list'][0]['val'] = bcadd($return_data['list'][0]['val'],$value['kcal_val'],2); array_push($return_data['list'][0]['list'],[ 'name'=>$value['food_name'], 'weight'=>$value['weight'].'克', 'id'=>$value['id'], 'pic_url'=>$value['pic_url'], // 'val'=>$value['kcal_val'].'kcal', 'val'=>$value['kcal_val'], 'nutrients_four' => $value['nutrients_four'], 'nutrients_list' => $value['nutrients_list'] ]); }else if($value['meals_type'] == '午餐'){ $return_data['list'][1]['val'] = bcadd($return_data['list'][1]['val'],$value['kcal_val'],2); array_push($return_data['list'][1]['list'],[ 'name'=>$value['food_name'], 'weight'=>$value['weight'].'克', 'id'=>$value['id'], 'pic_url'=>$value['pic_url'], // 'val'=>$value['kcal_val'].'kcal', 'val'=>$value['kcal_val'], 'nutrients_four' => $value['nutrients_four'], 'nutrients_list' => $value['nutrients_list'] ]); }else if($value['meals_type'] == '晚餐'){ $return_data['list'][2]['val'] = bcadd($return_data['list'][2]['val'],$value['kcal_val'],2); array_push($return_data['list'][2]['list'],[ 'name'=>$value['food_name'], 'weight'=>$value['weight'].'克', 'id'=>$value['id'], 'pic_url'=>$value['pic_url'], // 'val'=>$value['kcal_val'].'kcal', 'val'=>$value['kcal_val'], 'nutrients_four' => $value['nutrients_four'], 'nutrients_list' => $value['nutrients_list'] ]); }else if($value['meals_type'] == '早加餐'){ $return_data['list'][3]['val'] = bcadd($return_data['list'][3]['val'],$value['kcal_val'],2); array_push($return_data['list'][3]['list'],[ 'name'=>$value['food_name'], 'weight'=>$value['weight'].'克', 'id'=>$value['id'], 'pic_url'=>$value['pic_url'], // 'val'=>$value['kcal_val'].'kcal', 'val'=>$value['kcal_val'], 'nutrients_four' => $value['nutrients_four'], 'nutrients_list' => $value['nutrients_list'] ]); }else if($value['meals_type'] == '午加餐'){ $return_data['list'][4]['val'] = bcadd($return_data['list'][4]['val'],$value['kcal_val'],2); array_push($return_data['list'][4]['list'],[ 'name'=>$value['food_name'], 'weight'=>$value['weight'].'克', 'id'=>$value['id'], 'pic_url'=>$value['pic_url'], // 'val'=>$value['kcal_val'].'kcal', 'val'=>$value['kcal_val'], 'nutrients_four' => $value['nutrients_four'], 'nutrients_list' => $value['nutrients_list'] ]); }else if($value['meals_type'] == '晚加餐'){ $return_data['list'][5]['val'] = bcadd($return_data['list'][5]['val'],$value['kcal_val'],2); array_push($return_data['list'][5]['list'],[ 'name'=>$value['food_name'], 'weight'=>$value['weight'].'克', 'id'=>$value['id'], 'pic_url'=>$value['pic_url'], // 'val'=>$value['kcal_val'].'kcal', 'val'=>$value['kcal_val'], 'nutrients_four' => $value['nutrients_four'], 'nutrients_list' => $value['nutrients_list'] ]); }else{ } } foreach ($return_data['list'] as $key => $value) { if(count($value['list']) <= 0){ unset($return_data['list'][$key]); } } $return_data['list'] = array_values($return_data['list']); $return_data['remaining_kcal'] = bcsub($return_data['suggestion']['kcal'],$return_data['today_intake']['kcal'],2)>=0?bcsub($return_data['suggestion']['kcal'],$return_data['today_intake']['kcal'],2):0; return $this->msg($return_data); } public function get_log_list_action($data){ $cfc = Db::connect('cfc_db'); $user_data = $cfc->table($this->kitchenscale_db_msg['user']) ->where(["id"=>$data['aud_id']]) ->field('weight,height,gender,age,is_use_set_kcal,set_kcal') ->find(); if(!$user_data){ return $this->msg(10003); } // dump($user_data); // 计算年龄 $user_data['age_num'] = $user_data['age']; // 计算推荐营养 $nutrition_data = $this->count_user_nutrition_all($user_data); if($user_data['is_use_set_kcal'] == 1){ $nutrition_data['kcal'] = $user_data['set_kcal']; } // 计算取值范围 // $time_arr = $this->calculateDateRange($data['page']); // $content_num = $cfc->query("SELECT COUNT(DISTINCT CONVERT(varchar, create_time, 23)) as total // FROM ".$this->kitchenscale_db_msg['eat_log']." // WHERE aud_id = ".$data['aud_id'].""); // $page_total = ceil($content_num[0]['total']/$this->page_num); $food_content = $cfc->table($this->kitchenscale_db_msg['eat_log']) ->field("CONVERT(varchar, create_time, 23) as create_time, SUM(kcal_val) as kcal_val") ->where(['aud_id'=>$data['aud_id'],'create_time'=>['>=',$data['s_time'].' 00:00:00'],'create_time'=>['<=',$data['e_time'].' 23:59:59']]) ->group('CONVERT(varchar, create_time, 23)') // ->page($data['page'],$this->page_num) ->order('create_time DESC') ->select(); // dump($food_content); // die; $user_log = []; foreach ($food_content as $key => $value) { $time_data = substr($value['create_time'], 0, 10); if(array_key_exists($time_data, $user_log)){ $user_log[$time_data] += $value['kcal_val']; }else{ $user_log[$time_data] = $value['kcal_val']; } } $return_data = []; foreach ($user_log as $key => $value) { if(bcdiv($value,$nutrition_data['kcal'],2) < 0.9){ $bz['text'] = '不达标'; $bz['color'] = '#F0AD4E'; }else if(bcdiv($value,$nutrition_data['kcal'],2) >= 0.9 && bcdiv($value,$nutrition_data['kcal'],2) < 1.1){ $bz['text'] = '达标'; $bz['color'] = '#4CD964'; }else{ $bz['text'] = '超标'; $bz['color'] = '#FF0000'; } array_push($return_data,['time'=>$key,'val'=>$value,'unit'=>'kcal','describe'=>$bz['text'],'color'=>$bz['color']]); } // dump($return_data); return $this->msg([ // 'page_now'=>$data['page'], // 'page_total'=>$page_total, 'content_list'=>$return_data ]); } public function set_up_content_action($data){ $cfc = Db::connect('cfc_db'); $user_data = $cfc->table($this->kitchenscale_db_msg['user']) ->where(["id"=>$data['aud_id']]) ->field('weight,height,gender,age,is_use_set_kcal,set_kcal') ->find(); if(!$user_data){ return $this->msg(10003); } // 计算年龄 $user_data['age_num'] = $user_data['age']; // 计算推荐营养 $nutrition_data = $this->count_user_nutrition_all($user_data); if($user_data['is_use_set_kcal'] == 1){ $nutrition_data['kcal'] = $user_data['set_kcal']; } $nutrition_describe = [ [ '对于一个孩子(2-18岁)(没有特殊健康问题),身体处于快速生长发育阶段,需要充足的营养支持,尤其是蛋白质和健康脂肪。通常建议的三大营养素比例为:', '1、碳水化合物:45%-65% 的总热量', '提供能量,支持孩子的日常活动和生长发育。', '优先选择复合碳水化合物(如全谷物、蔬菜、水果),避免精制糖和高糖零食。', '2、蛋白质:10%-30% 的总热量', '支持肌肉、骨骼和器官的发育。', '建议摄入优质蛋白质来源,如瘦肉、鱼类、鸡蛋、豆类和乳制品。', '蛋白质需求较高,尤其是青春期孩子。', '3、脂肪:25%-35% 的总热量', '提供能量,并支持大脑发育(尤其是Omega-3脂肪酸)。', '优先选择健康脂肪,如鱼类、坚果、种子、橄榄油和牛油果。', '避免反式脂肪和过多的饱和脂肪。', '注意事项', '如果你有特定的健康目标(如增高,减重),可以进一步调整比例,并咨询营养师或医生以获得个性化建议。', ], [ '对于一个正常成年人(没有特殊健康问题或特定健身目标),通常建议的三大营养素比例为:', '1、碳水化合物:45%-65% 的总热量', '主要功能是提供能量', '建议选择复合碳水化合物(如全谷物、蔬菜、豆类),而不是精制糖。', '对于普通人,碳水化合物占总热量的 50%-55% 是一个常见的推荐值。', '2、蛋白质:10%-35% 的总热量', '用于维持肌肉、修复组织和支持免疫功能。', '普通人每日蛋白质摄入量建议为 0.8-1.2克/公斤体重。', '对于活动量较大或健身人群,蛋白质比例可以提高到 20%-30%。', '3、脂肪:20%-35% 的总热量', '提供能量、支持细胞功能并帮助吸收脂溶性维生素。', '建议以 不饱和脂肪(如橄榄油、坚果、鱼类)为主,减少饱和脂肪和反式脂肪的摄入。', '脂肪占总热量的 20%-30% 是常见的推荐值。', '以上是根据世界卫生组织(WHO)和其他健康机构的建议制定的。具体比例可以根据个人的活动水平、健康状况和目标进行微调。', '注意事项', '活动水平:活动量大的人可能需要更多的碳水化合物来提供能量,而健身人群可能需要更多的蛋白质。', '健康状况:例如,糖尿病患者可能需要控制碳水化合物比例,而心血管疾病患者可能需要减少脂肪摄入。', '个体差异:每个人的代谢和需求不同,建议根据个人情况调整。', '如果你有特定的健康目标(如减脂、增肌或控制慢性病),可以进一步调整比例,并咨询营养师或医生以获得个性化建议。', ], [ '对于一个老人(65岁以上)(没有特殊健康问题),身体的代谢率下降,肌肉量减少,可能面临营养不良或慢性病风险,因此需要调整营养比例。通常建议的三大营养素比例为:', '1、碳水化合物:45%-65% 的总热量', '提供能量,但应选择低血糖指数(GI)的食物,如全谷物、蔬菜和豆类,以控制血糖水平。', '避免精制糖和高糖食物,尤其是糖尿病患者。', '帮助维持肌肉质量,预防肌肉流失(少肌症)。', '建议摄入优质蛋白质,如鱼类、瘦肉、鸡蛋、豆类和乳制品。', '蛋白质需求较高,尤其是活动量较大的老人。', '3、脂肪:20%-35% 的总热量', '提供能量,并支持细胞功能和脂溶性维生素的吸收。', '优先选择不饱和脂肪,如橄榄油、坚果、种子和鱼类。', '减少饱和脂肪和反式脂肪的摄入,以降低心血管疾病风险。', '注意事项', '如果你有特定的健康目标(控制慢性病),可以进一步调整比例,并咨询营养师或医生以获得个性化建议。', ] ]; $return_data = [ 'kcal'=>[ 'title'=>"建议摄入卡路里", 'suggestion_kcal_val'=>$nutrition_data['kcal'], 'suggestion_kcal_unit'=>"千卡", 'suggestion_kcal_range_val'=>"建议您在:".bcmul($nutrition_data['bmr'],1.2,2)."~".bcmul($nutrition_data['bmr'],1.9,2)."千卡范围内调整,修改时请以医生建议为准!", 'describe'=>[ '基础代谢率(BMR):是指人体在清醒而又极端安静的状态下,不受肌肉活动、环境温度、食物及精神紧张等影响时的能量代谢率。', '每日总能量消耗(TDEE):', 'TDEE是BMR乘以活动系数,活动系数根据日常活动水平确定', '久坐(很少或没有运动):BMR × 1.2', '轻度活动(每周1-3天轻度运动):BMR × 1.375', '中度活动(每周3-5天中度运动):BMR × 1.55', '高度活动(每周6-7天高强度运动):BMR × 1.725', '极高活动(体力劳动或每天高强度训练):BMR × 1.9', '您的BMR为'.$nutrition_data['bmr'].',建议根据您的日常生活水平,调整合适的TDEE', ], ], 'nutrition'=>[ 'title'=>"建议三大营养比例", 'list'=>[ [ 'name'=>'碳水化合物', 'icon'=>'icon-tanshuihuahewu', 'proportion'=>'50%', 'val'=>$nutrition_data['carbohydrate'], 'unit'=>'克' ], [ 'name'=>'蛋白质', 'icon'=>'icon-Sm-danbaizhi', 'proportion'=>'20%', 'val'=>$nutrition_data['protein'], 'unit'=>'克' ], [ 'name'=>'脂肪', 'icon'=>'icon-w_fat_normal', 'proportion'=>'30%', 'val'=>$nutrition_data['fat'], 'unit'=>'克' ] ], 'describe'=>[] ] ]; if($user_data['age_num'] >= 2 && $user_data['age_num'] <= 18){ $return_data['nutrition']['describe'] = $nutrition_describe[0]; }else if($user_data['age_num'] > 18 && $user_data['age_num'] < 65){ $return_data['nutrition']['describe'] = $nutrition_describe[1]; }else if($user_data['age_num'] >= 65){ $return_data['nutrition']['describe'] = $nutrition_describe[2]; }else{ } return $this->msg($return_data); } public function set_user_kcal_action($data){ $cfc = Db::connect('cfc_db'); $user_data = $cfc->table($this->kitchenscale_db_msg['user']) ->where(["id"=>$data['aud_id']]) ->field('weight,height,gender,age,is_use_set_kcal,set_kcal') ->find(); if(!$user_data){ return $this->msg(10003); } $result = $cfc->table($this->kitchenscale_db_msg['user']) ->where(["id"=>$data['aud_id']]) ->update([ 'is_use_set_kcal'=>1, 'set_kcal'=>$data['set_kcal'] ]); if($result){ return $this->msg([]); }else{ return $this->msg(10002); } } public function del_user_eat_log_action($data){ $cfc = Db::connect('cfc_db'); $user_data = $cfc->table($this->kitchenscale_db_msg['eat_log']) ->where(["id"=>$data['eat_log_id']]) ->update(['is_del'=>1]); if($user_data){ return $this->msg([]); }else{ return $this->msg(10002); } } #######################################################################工具####################################################################### #######################################################################工具####################################################################### #######################################################################工具####################################################################### // public function count_user_nutrition_all($data){ // // 计算基础代谢率(BMR) // if($data['gender'] == 1){ // // 男性:BMR = 10 × 体重(kg) + 6.25 × 身高(cm) - 5 × 年龄(岁) + 5 // $bmr = bcmul(10,$data['weight'],20); // $bmr = bcadd($bmr,bcmul(6.25,$data['height'],20),20); // $bmr = bcsub($bmr,bcmul(5,$data['age_num'],20),20); // $bmr = bcadd($bmr,5,2); // }else if($data['gender'] == 2){ // // 女性:BMR = 10 × 体重(kg) + 6.25 × 身高(cm) - 5 × 年龄(岁) - 161 // $bmr = bcmul(10,$data['weight'],20); // $bmr = bcadd($bmr,bcmul(6.25,$data['height'],20),20); // $bmr = bcsub($bmr,bcmul(5,$data['age_num'],20),20); // $bmr = bcsub($bmr,161,2); // }else{ // return $this->msg(10003,'性别未知'); // } // // 每日总能量消耗(TDEE) // // 久坐(很少或没有运动):BMR × 1.2 // // 轻度活动(每周1-3天轻度运动):BMR × 1.375 // // 中度活动(每周3-5天中度运动):BMR × 1.55 // // 高度活动(每周6-7天高强度运动):BMR × 1.725 // // 极高活动(体力劳动或每天高强度训练):BMR × 1.9 // $tdee = bcmul($bmr,1.55,2); // // 碳水化合物:通常占总热量的45-65% // // 蛋白质:通常占总热量的10-35% // // 脂肪:通常占总热量的20-35% // // 孩子&成年人:碳水化合物50%,蛋白质20%,脂肪30%。 // // 老人:碳水化合物50%,蛋白质25%,脂肪25%。 // // 建议每日摄入量计算: // // 1.碳水化合物(克): (TDEE × 碳水化合物比例) / 4 // // 2.蛋白质(克):(TDEE × 蛋白质比例) / 4 // // 3.脂肪(克): (TDEE × 脂肪比例) / 9 // $carbohydrate = bcdiv(bcmul($tdee,0.5,20),4,2); // if($data['age_num'] < 65){ // $protein = bcdiv(bcmul($tdee,0.2,20),4,2); // $fat = bcdiv(bcmul($tdee,0.3,20),9,2); // }else{ // $protein = bcdiv(bcmul($tdee,0.25,20),4,2); // $fat =bcdiv(bcmul($tdee,0.25,20),9,2); // } // return ['kcal'=>$tdee,'carbohydrate'=>$carbohydrate,'protein'=>$protein,'fat'=>$fat,'bmr'=>$bmr]; // } public function calculateDateRange($n) { // 获取当前日期和时间 $today = new \DateTime(); // 计算结束时间:往前推 (10 * (n - 1)) 天,时间设为 23:59:59 $endInterval = new \DateInterval('P' . (10 * ($n - 1)) . 'D'); $endTime = clone $today; $endTime->sub($endInterval)->setTime(23, 59, 59); // 计算开始时间:往前推 (10 * n) 天,时间设为 00:00:00 $startInterval = new \DateInterval('P' . (10 * $n) . 'D'); $startTime = clone $today; $startTime->sub($startInterval)->setTime(0, 0, 0); return [ 's_time' => $startTime->format('Y-m-d H:i:s'), 'e_time' => $endTime->format('Y-m-d H:i:s'), ]; } // 计算营养物质 public function calculate_nutrients($data){ // dump($data); $food_id_arr = []; for ($i=0; $i < count($data); $i++) { $food_id_arr[] = $data[$i]['food_id']; } $cfc = Db::connect('cfc_db'); $nutrients_list = $cfc->table($this->kitchenscale_db_msg['foodlist4']) ->where("father_id in ('".implode("','",$food_id_arr)."')") // ->field() ->select(); $nutrients_arr = ['VitaminA','VitaminB1','VitaminB2','VitaminB6','VitaminB12','VitaminD','VitaminK','Niacin','VitaminC','VitaminE','FolicAcid','Biotin','PantothenicAcid','TotalCholine','Ca','Phosphorus','Kalium','Mg','Na','Fe','Zn','Se','Cu','Mn','Iodine']; // dump($nutrients_list); // 加 bcadd(,,20) // 减 bcsub(,,20) // 乘 bcmul(,,20) // 除 bcdiv(,,20) for ($i=0; $i < count($data); $i++) { $data[$i]['nutrients_four'][] = [ 'name' => '卡路里', 'unit' => 'kcal', 'color' => '', 'value' => $data[$i]['kcal_val'], 'proportion' => 0, ]; $data[$i]['nutrients_four'][] = [ 'name' => '蛋白质', 'unit' => 'g', 'color' => '#5180D8', 'value' => $data[$i]['protein_val'], 'proportion' => bcmul(bcdiv($data[$i]['protein_val'],bcadd($data[$i]['protein_val'],bcadd($data[$i]['fat_val'],$data[$i]['carbohydrate_val'],20),20),2),100,0), ]; $data[$i]['nutrients_four'][] = [ 'name' => '脂肪', 'unit' => 'g', 'color' => '#ED7886', 'value' => $data[$i]['fat_val'], 'proportion' => bcmul(bcdiv($data[$i]['fat_val'],bcadd($data[$i]['protein_val'],bcadd($data[$i]['fat_val'],$data[$i]['carbohydrate_val'],20),20),2),100,0), ]; $data[$i]['nutrients_four'][] = [ 'name' => '碳水化合物', 'unit' => 'g', 'color' => '#FFB169', 'value' => $data[$i]['carbohydrate_val'], 'proportion' => bcmul(bcdiv($data[$i]['carbohydrate_val'],bcadd($data[$i]['protein_val'],bcadd($data[$i]['fat_val'],$data[$i]['carbohydrate_val'],20),20),2),100,0), ]; $data[$i]['nutrients_list'][] = [ 'name' => 'Calorie', 'name_ch' => '卡路里', 'unit' => 'kcal', 'value' => $data[$i]['kcal_val'], 'type' => 1, 'type_name' => '能量及宏量营养素', 'color' => '#C4FFE0', ]; $data[$i]['nutrients_list'][] = [ 'name' => 'Protein', 'name_ch' => '蛋白质', 'unit' => 'g', 'value' => $data[$i]['protein_val'], 'type' => 1, 'type_name' => '能量及宏量营养素', 'color' => '#C4FFE0', ]; $data[$i]['nutrients_list'][] = [ 'name' => 'Fat', 'name_ch' => '脂肪', 'unit' => 'g', 'value' => $data[$i]['fat_val'], 'type' => 1, 'type_name' => '能量及宏量营养素', 'color' => '#C4FFE0', ]; $data[$i]['nutrients_list'][] = [ 'name' => 'Carbohydrate', 'name_ch' => '碳水化合物', 'unit' => 'g', 'value' => $data[$i]['carbohydrate_val'], 'type' => 1, 'type_name' => '能量及宏量营养素', 'color' => '#C4FFE0', ]; foreach ($nutrients_list as $key => $value) { if($value['father_id'] == $data[$i]['food_id']){ if(in_array($value['name'],$nutrients_arr)){ $data[$i]['nutrients_list'][] = [ 'name' => $value['name'], 'name_ch' => $value['name_ch'], 'unit' => $value['unit'], 'value' => bcmul($value['value'],bcdiv($data[$i]['weight'],100,20),2), 'type' => $value['type'], 'type_name' => $value['type'] == 1?'能量及宏量营养素':($value['type'] == 2?'维生素':($value['type'] == 3?'矿物质':'')), 'color' => $value['type'] == 1?'#C4FFE0':($value['type'] == 2?'#FFEFB7':($value['type'] == 3?'#7DA8E0':'')), ]; } } } } return $data; } }