'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 'tiaoma'=>'app_commodity_barcode',//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('food_list', $data)){ return $this->msg(10001,'food_list is miss'); } if(!array_key_exists('time', $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'); } if(!$this->verify_data_is_ok($data['time'],'datetime')){ return $this->msg(10005,'time 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,'page'=>1]){ // 尝试捕获异常 // 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('page', $data)){ return $this->msg(10001,'page 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['page'],'intnum')){ return $this->msg(10005,'page 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 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' => '系统错误']); } } public function del_user_eat_list_log(){ // 尝试捕获异常 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('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['time'],'datetime')){ return $this->msg(10005,'time type is error'); } $return_data = $this->del_user_eat_list_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' => '系统错误']); } } // 本次添加食材的营养统计 public function current_food_statistics(){ // 尝试捕获异常 try { $data = input('post.'); if(!array_key_exists('log_id', $data)){ return $this->msg(10001,'log_id is miss'); } if(is_array($data['log_id'])){ foreach ($data['log_id'] as $key => $value) { if(!$this->verify_data_is_ok($value,'intnum')){ return $this->msg(10005,'log_id type is error'); } } }else{ return $this->msg(10005,'log_id type is error'); } $return_data = $this->current_food_statistics_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(); // dump($user_data); if(!$user_data){ return $this->msg(10003); }else{ if(!$user_data['weight'] || !$user_data['height']){ return $this->msg(10008); } } if(count($data['food_list']) <= 0){ return $this->msg(10001,'未选择食材'); } // 统计食物的id $food_id_arr = []; $food_id_arr_product = []; 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(!array_key_exists('meals_type', $value)){ return $this->msg(10001,'meals_type 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'); // } if(!in_array($value['meals_type'],['早餐','午餐','晚餐','加餐'])){ return $this->msg(10005,'meals_type type is error'); } foreach ($data['food_list'][$key] as $k => $v) { if(!in_array($k,['id','weight','meals_type','unit'])){ unset($data['food_list'][$key][$k]); } } // 如果不是普通食材 if(array_key_exists('food_type',$value)){ array_push($food_id_arr_product,$value['id']); }else{ array_push($food_id_arr,$value['id']); } $data['food_list'][$key]['weight_g'] = $this->convertWeightToGrams($value['weight'],$value['unit']); } if(count($food_id_arr)>0){ $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(); if(count($food_content) <= 0){ return $this->msg(10004,'未找到对应食材'); } // 整理食物信息 $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_g'],100,20); if(array_key_exists($value['id'], $food_content_arr)){ $data['food_list'][$key]['kcal_val'] = bcmul($food_content_arr[$value['id']]['kcal'],$proportion_num,1); $data['food_list'][$key]['carbohydrate_val'] = bcmul($food_content_arr[$value['id']]['carbohydrate'],$proportion_num,1); $data['food_list'][$key]['protein_val'] = bcmul($food_content_arr[$value['id']]['protein'],$proportion_num,1); $data['food_list'][$key]['fat_val'] = bcmul($food_content_arr[$value['id']]['fat'],$proportion_num,1); $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'] = $value['meals_type']; $data['food_list'][$key]['create_time'] = $data['time'].date(' H:i:s'); $data['food_list'][$key]['food_id'] = $value['id']; $data['food_list'][$key]['type'] = 1; unset($data['food_list'][$key]['id']); unset($data['food_list'][$key]['weight_g']); }else{ unset($data['food_list'][$key]); } } } if(count($food_id_arr_product)>0){ $food_content_product = $cfc->table($this->kitchenscale_db_msg['tiaoma']) ->where("id in (".implode(',',$food_id_arr_product).")") ->field('id,name,Calorie as kcal,Carbohydrate as carbohydrate,Protein as protein,Fat as fat') ->select(); if(count($food_content_product) <= 0){ return $this->msg(10004,'未找到对应食材'); } // 整理食物信息 $food_content_arr_product = []; foreach ($food_content_product as $key => $value) { $food_content_arr_product[$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_g'],100,2); if(array_key_exists($value['id'], $food_content_arr_product)){ $data['food_list'][$key]['kcal_val'] = bcmul($food_content_arr_product[$value['id']]['kcal'],$proportion_num,2); $data['food_list'][$key]['carbohydrate_val'] = bcmul($food_content_arr_product[$value['id']]['carbohydrate'],$proportion_num,2); $data['food_list'][$key]['protein_val'] = bcmul($food_content_arr_product[$value['id']]['protein'],$proportion_num,2); $data['food_list'][$key]['fat_val'] = bcmul($food_content_arr_product[$value['id']]['fat'],$proportion_num,2); $data['food_list'][$key]['food_name'] = $food_content_arr_product[$value['id']]['name']; $data['food_list'][$key]['aud_id'] = $data['aud_id']; $data['food_list'][$key]['meals_type'] = $value['meals_type']; $data['food_list'][$key]['create_time'] = $data['time'].date(' H:i:s'); $data['food_list'][$key]['food_id'] = $value['id']; $data['food_list'][$key]['type'] = 2; unset($data['food_list'][$key]['id']); unset($data['food_list'][$key]['weight_g']); }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 // 启动事务 // return $this->msg($data['food_list']); Db::startTrans(); try{ if(count($data['food_list']) > 1){ $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([]); } }else{ $result = $cfc->table($this->kitchenscale_db_msg['eat_log'])->insertGetId($data['food_list'][0]);//新增1个 Db::commit(); return $this->msg(['id'=>$result]); } } 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'); $return_data = [ 'date'=>$data['time'], //时间 'suggestion'=>[ //建议 'kcal'=>0, //建议摄入卡路里量 'carbohydrate'=>0, //建议摄入碳水量 'protein'=>0, //建议摄入蛋白质量 'fat'=>0, //建议摄入脂肪量 ], 'today_intake'=>[ //今日已摄入 'kcal'=>0, //今日已摄入卡路里量 'carbohydrate'=>0, //今日已摄入碳水量 'protein'=>0, //今日已摄入蛋白质量 'fat'=>0, //今日已摄入脂肪量 ], 'remaining_kcal'=>0, //剩下可摄入卡路里量 'details'=>[ //当天营养元素能量占比 'carbohydrate'=>['name'=>'碳水','icon'=>'https://tc.pcxbc.com/kitchenscale_all/icon_carbohydrate.png','color'=>'#ED7886','val'=>0,'unit'=>'g','proportion'=>'0.00','rank_list'=>[['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank1.png','name'=>'','pic_url'=>'','weight'=>''],['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank2.png','name'=>'','pic_url'=>'','weight'=>''],['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank3.png','name'=>'','pic_url'=>'','weight'=>'']]], 'protein'=>['name'=>'蛋白质','icon'=>'https://tc.pcxbc.com/kitchenscale_all/icon_protein.png','color'=>'#FFB169','val'=>0,'unit'=>'g','proportion'=>'0.00','rank_list'=>[['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank1.png','name'=>'','pic_url'=>'','weight'=>''],['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank2.png','name'=>'','pic_url'=>'','weight'=>''],['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank3.png','name'=>'','pic_url'=>'','weight'=>'']]], 'fat'=>['name'=>'脂肪','icon'=>'https://tc.pcxbc.com/kitchenscale_all/icon_fat.png','color'=>'#3CB383','val'=>0,'unit'=>'g','proportion'=>'0.00','rank_list'=>[['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank1.png','name'=>'','pic_url'=>'','weight'=>''],['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank2.png','name'=>'','pic_url'=>'','weight'=>''],['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank3.png','name'=>'','pic_url'=>'','weight'=>'']]], ], 'trace_elements_all_day' => [ [ 'name' => 'VitaminA', 'name_ch' => '维生素A', 'unit' => 'μg RAE', 'value' => 0 ], [ 'name' => 'VitaminB1', 'name_ch' => '硫胺素', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'VitaminB2', 'name_ch' => '核黄素', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'VitaminB6', 'name_ch' => '维生素B6', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'VitaminB12', 'name_ch' => '维生素B12', 'unit' => 'μg', 'value' => 0 ], [ 'name' => 'VitaminD', 'name_ch' => '维生素D', 'unit' => 'μg', 'value' => 0 ], [ 'name' => 'VitaminK', 'name_ch' => '维生素K', 'unit' => 'μg', 'value' => 0 ], [ 'name' => 'Niacin', 'name_ch' => '烟酸', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'VitaminC', 'name_ch' => '维生素C', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'VitaminE', 'name_ch' => '维生素E', 'unit' => 'mg α-TE', 'value' => 0 ], [ 'name' => 'FolicAcid', 'name_ch' => '叶酸', 'unit' => 'μg', 'value' => 0 ], [ 'name' => 'Biotin', 'name_ch' => '生物素', 'unit' => 'μg', 'value' => 0 ], [ 'name' => 'PantothenicAcid', 'name_ch' => '泛酸', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'TotalCholine', 'name_ch' => '总胆碱', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Ca', 'name_ch' => '钙', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Phosphorus', 'name_ch' => '磷', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Kalium', 'name_ch' => '钾', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Mg', 'name_ch' => '镁', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Na', 'name_ch' => '钠', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Fe', 'name_ch' => '铁', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Zn', 'name_ch' => '锌', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Se', 'name_ch' => '硒', 'unit' => 'μg', 'value' => 0 ], [ 'name' => 'Cu', 'name_ch' => '铜', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Mn', 'name_ch' => '锰', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Iodine', 'name_ch' => '碘', 'unit' => 'μg', 'value' => 0 ] ], 'list'=>[ [ 'name'=>'早餐', 'val'=>0, 'unit'=>'kcal', 'color'=>'#0992B4', 'icon'=>'https://tc.pcxbc.com/kitchenscale_all/meal_1.png', 'icon_home'=>'/static/1.png', 'bgimg_home'=>'/static/2.png', 'kcal_proportion'=>0, "nutrients_four"=> [ [ 'name'=>'卡路里', 'unit'=>'kcal', 'color'=>'', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'碳水化合物', 'unit'=>'g', 'color'=>'#FFB169', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'蛋白质', 'unit'=>'g', 'color'=>'#5180D8', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'脂肪', 'unit'=>'g', 'color'=>'#ED7886', 'value'=>0, 'proportion'=>0, ], ], 'list'=>[], ], [ 'name'=>'午餐', 'val'=>0, 'unit'=>'kcal', 'color'=>'#4F9211', 'icon'=>'https://tc.pcxbc.com/kitchenscale_all/meal_2.png', 'icon_home'=>'/static/3.png', 'bgimg_home'=>'/static/4.png', 'kcal_proportion'=>0, "nutrients_four"=> [ [ 'name'=>'卡路里', 'unit'=>'kcal', 'color'=>'', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'蛋白质', 'unit'=>'g', 'color'=>'#5180D8', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'脂肪', 'unit'=>'g', 'color'=>'#ED7886', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'碳水化合物', 'unit'=>'g', 'color'=>'#FFB169', 'value'=>0, 'proportion'=>0, ], ], 'list'=>[], ], [ 'name'=>'晚餐', 'val'=>0, 'unit'=>'kcal', 'color'=>'#B354B0', 'icon'=>'https://tc.pcxbc.com/kitchenscale_all/meal_3.png', 'icon_home'=>'/static/5.png', 'bgimg_home'=>'/static/6.png', 'kcal_proportion'=>0, "nutrients_four"=> [ [ 'name'=>'卡路里', 'unit'=>'kcal', 'color'=>'', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'蛋白质', 'unit'=>'g', 'color'=>'#5180D8', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'脂肪', 'unit'=>'g', 'color'=>'#ED7886', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'碳水化合物', 'unit'=>'g', 'color'=>'#FFB169', 'value'=>0, 'proportion'=>0, ], ], 'list'=>[], ], [ 'name'=>'加餐', 'val'=>0, 'unit'=>'kcal', 'color'=>'#C08433', 'icon'=>'https://tc.pcxbc.com/kitchenscale_all/meal_4.png', 'icon_home'=>'/static/7.png', 'bgimg_home'=>'/static/8.png', 'kcal_proportion'=>0, "nutrients_four"=> [ [ 'name'=>'卡路里', 'unit'=>'kcal', 'color'=>'', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'蛋白质', 'unit'=>'g', 'color'=>'#5180D8', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'脂肪', 'unit'=>'g', 'color'=>'#ED7886', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'碳水化合物', 'unit'=>'g', 'color'=>'#FFB169', 'value'=>0, 'proportion'=>0, ], ], 'list'=>[], ], ], ]; if(!$data['aud_id']){//内部调用 return $this->msg($return_data); } $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') ->field('weight,height,gender,age,is_use_set_kcal,set_kcal,birthday,set_carbohydrate_v,set_carbohydrate_p,set_protein_v,set_protein_p,set_fat_v,set_fat_p,activity_level') ->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){ // $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); $nutrition_data['kcal'] = $user_data['set_kcal'] != 0?$user_data['set_kcal']:$nutrition_data['kcal']; $nutrition_data['carbohydrate'] = $user_data['set_carbohydrate_v'] != null?$user_data['set_carbohydrate_v']:$nutrition_data['carbohydrate']; $nutrition_data['protein'] = $user_data['set_protein_v'] != null?$user_data['set_protein_v']:$nutrition_data['protein']; $nutrition_data['fat'] = $user_data['set_fat_v'] != null?$user_data['set_fat_v']:$nutrition_data['fat']; $nutrition_data['carbohydrate_p'] = $user_data['set_carbohydrate_p'] != null?$user_data['set_carbohydrate_p']:bcmul($nutrition_data['carbohydrate_p'],100,2); $nutrition_data['protein_p'] = $user_data['set_protein_p'] != null?$user_data['set_protein_p']:bcmul($nutrition_data['protein_p'],100,2); $nutrition_data['fat_p'] = $user_data['set_fat_p'] != null?$user_data['set_fat_p']:bcmul($nutrition_data['fat_p'],100,2); }else{ $nutrition_data['carbohydrate_p'] = bcmul($nutrition_data['carbohydrate_p'],100,2); $nutrition_data['protein_p'] = bcmul($nutrition_data['protein_p'],100,2); $nutrition_data['fat_p'] = bcmul($nutrition_data['fat_p'],100,2); } $return_data = [ 'date'=>$data['time'], //时间 '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'], //剩下可摄入卡路里量 'details'=>[ //当天营养元素能量占比 'carbohydrate'=>['name'=>'碳水','icon'=>'https://tc.pcxbc.com/kitchenscale_all/icon_carbohydrate.png','color'=>'#ED7886','val'=>0,'unit'=>'g','proportion'=>'0.00','rank_list'=>[['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank1.png','name'=>'','pic_url'=>'','weight'=>''],['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank2.png','name'=>'','pic_url'=>'','weight'=>''],['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank3.png','name'=>'','pic_url'=>'','weight'=>'']]], 'protein'=>['name'=>'蛋白质','icon'=>'https://tc.pcxbc.com/kitchenscale_all/icon_protein.png','color'=>'#FFB169','val'=>0,'unit'=>'g','proportion'=>'0.00','rank_list'=>[['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank1.png','name'=>'','pic_url'=>'','weight'=>''],['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank2.png','name'=>'','pic_url'=>'','weight'=>''],['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank3.png','name'=>'','pic_url'=>'','weight'=>'']]], 'fat'=>['name'=>'脂肪','icon'=>'https://tc.pcxbc.com/kitchenscale_all/icon_fat.png','color'=>'#3CB383','val'=>0,'unit'=>'g','proportion'=>'0.00','rank_list'=>[['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank1.png','name'=>'','pic_url'=>'','weight'=>''],['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank2.png','name'=>'','pic_url'=>'','weight'=>''],['icon'=>'https://tc.pcxbc.com/kitchenscale_all/rank3.png','name'=>'','pic_url'=>'','weight'=>'']]], ], 'trace_elements_all_day' => [ [ 'name' => 'VitaminA', 'name_ch' => '维生素A', 'unit' => 'μg RAE', 'value' => 0 ], [ 'name' => 'VitaminB1', 'name_ch' => '硫胺素', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'VitaminB2', 'name_ch' => '核黄素', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'VitaminB6', 'name_ch' => '维生素B6', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'VitaminB12', 'name_ch' => '维生素B12', 'unit' => 'μg', 'value' => 0 ], [ 'name' => 'VitaminD', 'name_ch' => '维生素D', 'unit' => 'μg', 'value' => 0 ], [ 'name' => 'VitaminK', 'name_ch' => '维生素K', 'unit' => 'μg', 'value' => 0 ], [ 'name' => 'Niacin', 'name_ch' => '烟酸', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'VitaminC', 'name_ch' => '维生素C', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'VitaminE', 'name_ch' => '维生素E', 'unit' => 'mg α-TE', 'value' => 0 ], [ 'name' => 'FolicAcid', 'name_ch' => '叶酸', 'unit' => 'μg', 'value' => 0 ], [ 'name' => 'Biotin', 'name_ch' => '生物素', 'unit' => 'μg', 'value' => 0 ], [ 'name' => 'PantothenicAcid', 'name_ch' => '泛酸', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'TotalCholine', 'name_ch' => '总胆碱', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Ca', 'name_ch' => '钙', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Phosphorus', 'name_ch' => '磷', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Kalium', 'name_ch' => '钾', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Mg', 'name_ch' => '镁', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Na', 'name_ch' => '钠', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Fe', 'name_ch' => '铁', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Zn', 'name_ch' => '锌', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Se', 'name_ch' => '硒', 'unit' => 'μg', 'value' => 0 ], [ 'name' => 'Cu', 'name_ch' => '铜', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Mn', 'name_ch' => '锰', 'unit' => 'mg', 'value' => 0 ], [ 'name' => 'Iodine', 'name_ch' => '碘', 'unit' => 'μg', 'value' => 0 ] ], 'list'=>[ [ 'name'=>'早餐', 'val'=>0, 'unit'=>'kcal', 'color'=>'#0992B4', 'icon'=>'https://tc.pcxbc.com/kitchenscale_all/meal_1.png', 'icon_home'=>'/static/1.png', 'bgimg_home'=>'/static/2.png', 'kcal_proportion'=>0, "nutrients_four"=> [ [ 'name'=>'卡路里', 'unit'=>'kcal', 'color'=>'', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'碳水化合物', 'unit'=>'g', 'color'=>'#FFB169', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'蛋白质', 'unit'=>'g', 'color'=>'#5180D8', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'脂肪', 'unit'=>'g', 'color'=>'#ED7886', 'value'=>0, 'proportion'=>0, ], ], 'list'=>[], ], [ 'name'=>'午餐', 'val'=>0, 'unit'=>'kcal', 'color'=>'#4F9211', 'icon'=>'https://tc.pcxbc.com/kitchenscale_all/meal_2.png', 'icon_home'=>'/static/3.png', 'bgimg_home'=>'/static/4.png', 'kcal_proportion'=>0, "nutrients_four"=> [ [ 'name'=>'卡路里', 'unit'=>'kcal', 'color'=>'', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'蛋白质', 'unit'=>'g', 'color'=>'#5180D8', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'脂肪', 'unit'=>'g', 'color'=>'#ED7886', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'碳水化合物', 'unit'=>'g', 'color'=>'#FFB169', 'value'=>0, 'proportion'=>0, ], ], 'list'=>[], ], [ 'name'=>'晚餐', 'val'=>0, 'unit'=>'kcal', 'color'=>'#B354B0', 'icon'=>'https://tc.pcxbc.com/kitchenscale_all/meal_3.png', 'icon_home'=>'/static/5.png', 'bgimg_home'=>'/static/6.png', 'kcal_proportion'=>0, "nutrients_four"=> [ [ 'name'=>'卡路里', 'unit'=>'kcal', 'color'=>'', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'蛋白质', 'unit'=>'g', 'color'=>'#5180D8', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'脂肪', 'unit'=>'g', 'color'=>'#ED7886', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'碳水化合物', 'unit'=>'g', 'color'=>'#FFB169', 'value'=>0, 'proportion'=>0, ], ], 'list'=>[], ], [ 'name'=>'加餐', 'val'=>0, 'unit'=>'kcal', 'color'=>'#C08433', 'icon'=>'https://tc.pcxbc.com/kitchenscale_all/meal_4.png', 'icon_home'=>'/static/7.png', 'bgimg_home'=>'/static/8.png', 'kcal_proportion'=>0, "nutrients_four"=> [ [ 'name'=>'卡路里', 'unit'=>'kcal', 'color'=>'', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'蛋白质', 'unit'=>'g', 'color'=>'#5180D8', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'脂肪', 'unit'=>'g', 'color'=>'#ED7886', 'value'=>0, 'proportion'=>0, ], [ 'name'=>'碳水化合物', 'unit'=>'g', 'color'=>'#FFB169', 'value'=>0, 'proportion'=>0, ], ], 'list'=>[], ], ], ]; // 查询用户今日摄入食物 $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.unit,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,a.type') ->select(); $food_product = []; if(count($food_content) > 0){ //计算营养物质 // 提取出来商品食物的id并处理图片 foreach ($food_content as $key => $value) { if($value['type'] == 2){ $food_product[] = $value['food_id']; } } if(count($food_product) > 0){ $food_product_pic_url = $cfc->table($this->kitchenscale_db_msg['tiaoma']) ->where("id in (".implode(',',$food_product).")") ->field('id,pic_sp,name') ->select(); foreach ($food_content as $key => $value) { foreach ($food_product_pic_url as $k => $v) { if($value['type'] == 2){ if($v['id'] == $value['food_id']){ $food_content[$key]['pic_url'] = $v['pic_sp']; } } } } } // dump($food_content); // die; $food_content = $this->calculate_nutrients($food_content); return $this->msg($food_content); foreach ($food_content as $key => $value) { // dump($value['nutrients_four']); $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); $return_data['list'][0]['nutrients_four'][0]['value'] = bcadd($return_data['list'][0]['nutrients_four'][0]['value'],$value['kcal_val'],2); $return_data['list'][0]['nutrients_four'][1]['value'] = bcadd($return_data['list'][0]['nutrients_four'][1]['value'],$value['carbohydrate_val'],2); $return_data['list'][0]['nutrients_four'][2]['value'] = bcadd($return_data['list'][0]['nutrients_four'][2]['value'],$value['protein_val'],2); $return_data['list'][0]['nutrients_four'][3]['value'] = bcadd($return_data['list'][0]['nutrients_four'][3]['value'],$value['fat_val'],2); array_push($return_data['list'][0]['list'],[ 'name'=>$value['food_name'], 'weight'=>$value['weight'].' '.$value['unit'], 'id'=>$value['id'], 'pic_url'=>$value['pic_url'], '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); $return_data['list'][1]['nutrients_four'][0]['value'] = bcadd($return_data['list'][1]['nutrients_four'][0]['value'],$value['kcal_val'],2); $return_data['list'][1]['nutrients_four'][1]['value'] = bcadd($return_data['list'][1]['nutrients_four'][1]['value'],$value['carbohydrate_val'],2); $return_data['list'][1]['nutrients_four'][2]['value'] = bcadd($return_data['list'][1]['nutrients_four'][2]['value'],$value['protein_val'],2); $return_data['list'][1]['nutrients_four'][3]['value'] = bcadd($return_data['list'][1]['nutrients_four'][3]['value'],$value['fat_val'],2); array_push($return_data['list'][1]['list'],[ 'name'=>$value['food_name'], 'weight'=>$value['weight'].' '.$value['unit'], 'id'=>$value['id'], 'pic_url'=>$value['pic_url'], '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); $return_data['list'][2]['nutrients_four'][0]['value'] = bcadd($return_data['list'][2]['nutrients_four'][0]['value'],$value['kcal_val'],2); $return_data['list'][2]['nutrients_four'][1]['value'] = bcadd($return_data['list'][2]['nutrients_four'][1]['value'],$value['carbohydrate_val'],2); $return_data['list'][2]['nutrients_four'][2]['value'] = bcadd($return_data['list'][2]['nutrients_four'][2]['value'],$value['protein_val'],2); $return_data['list'][2]['nutrients_four'][3]['value'] = bcadd($return_data['list'][2]['nutrients_four'][3]['value'],$value['fat_val'],2); array_push($return_data['list'][2]['list'],[ 'name'=>$value['food_name'], 'weight'=>$value['weight'].' '.$value['unit'], 'id'=>$value['id'], 'pic_url'=>$value['pic_url'], 'val'=>$value['kcal_val'], 'nutrients_four' => $value['nutrients_four'], 'nutrients_list' => $value['nutrients_list'] ]); }else{ $return_data['list'][3]['val'] = bcadd($return_data['list'][3]['val'],$value['kcal_val'],2); $return_data['list'][3]['nutrients_four'][0]['value'] = bcadd($return_data['list'][3]['nutrients_four'][0]['value'],$value['kcal_val'],2); $return_data['list'][3]['nutrients_four'][1]['value'] = bcadd($return_data['list'][3]['nutrients_four'][1]['value'],$value['carbohydrate_val'],2); $return_data['list'][3]['nutrients_four'][2]['value'] = bcadd($return_data['list'][3]['nutrients_four'][2]['value'],$value['protein_val'],2); $return_data['list'][3]['nutrients_four'][3]['value'] = bcadd($return_data['list'][3]['nutrients_four'][3]['value'],$value['fat_val'],2); array_push($return_data['list'][3]['list'],[ 'name'=>$value['food_name'], 'weight'=>$value['weight'].' '.$value['unit'], 'id'=>$value['id'], 'pic_url'=>$value['pic_url'], 'val'=>$value['kcal_val'], 'nutrients_four' => $value['nutrients_four'], 'nutrients_list' => $value['nutrients_list'] ]); } } // dump($return_data['list']); // die; $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_data = $this->calculate_kcal_proportion($return_data); // 计算营养物质能量占比 $return_data = $this->calculate_energy_proportion($return_data); // 排序营养元素食物排行榜 $return_data = $this->energy_food_rank($return_data); // 微量元素处理全天 $return_data = $this->calculate_trace_elements($return_data); // 处理单餐营养占比 // return $this->msg($return_data); foreach ($return_data['list'] as $key => $value) { $all_yy_data_0 = bcadd($value['nutrients_four'][3]['value'],bcadd($value['nutrients_four'][1]['value'],$value['nutrients_four'][2]['value'],20),2); foreach ($value['nutrients_four'] as $k => $v) { if($k != 0){ if($all_yy_data_0 == 0){ $return_data['list'][$key]['nutrients_four'][$k]['proportion'] = 0; }else{ $return_data['list'][$key]['nutrients_four'][$k]['proportion'] = bcdiv($value['nutrients_four'][$k]['value'],$all_yy_data_0,2) >= 1?'100':bcmul(bcdiv($value['nutrients_four'][$k]['value'],$all_yy_data_0,2),100,2); } // if($value['name'] == '晚餐'){ // // dump($return_data['list'][$key]['nutrients_four'][$k]['proportion']); // } // dump($return_data['list'][$key]['nutrients_four'][$k]['proportion']); } } } // return $this->msg($return_data); } $nameMap = [ 'kcal' => ['卡路里','kcal','https://tc.pcxbc.com/kitchenscale_all/icon_kcal.png','#5180D8'], 'carbohydrate' => ['碳水','g','https://tc.pcxbc.com/kitchenscale_all/icon_carbohydrate.png','#ED7886'], 'protein' => ['蛋白质','g','https://tc.pcxbc.com/kitchenscale_all/icon_protein.png','#FFB169'], 'fat' => ['脂肪','g','https://tc.pcxbc.com/kitchenscale_all/icon_fat.png','#3CB383'], ]; $all_yy_data = bcadd($return_data['suggestion']['fat'],bcadd($return_data['suggestion']['carbohydrate'],$return_data['suggestion']['protein'],20),20); foreach ($return_data['suggestion'] as $key => $value) { $return_data['nutrients_four'][] = [ 'name'=>$nameMap[$key][0], 'unit'=>$nameMap[$key][1], 'suggestion'=>$value, 'today_intake'=>$return_data['today_intake'][$key], 'icon'=>$nameMap[$key][2], 'color'=>$nameMap[$key][3], 'proportion'=>bcdiv($return_data['today_intake'][$key],$value,2) >= 1?'100':bcmul(bcdiv($return_data['today_intake'][$key],$value,2),100,2), // 'proportion_fp'=>$key == 'kcal'?0:(bcdiv($return_data['suggestion'][$key],$all_yy_data,2))*100, 'proportion_fp'=>$key == 'kcal'?0:$nutrition_data[$key.'_p'], ]; } // dump($user_data); unset($return_data['suggestion']); unset($return_data['today_intake']); // return $return_data; 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,activity_level') ->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']; } $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']." and is_del = 0"); $page_total = ceil($content_num[0]['total']/$this->page_num); $food_content = $cfc->table($this->kitchenscale_db_msg['eat_log']) ->where(['aud_id'=>$data['aud_id'],'is_del'=>0]) ->group('CONVERT(varchar, create_time, 23)') ->page($data['page'],$this->page_num) ->order('create_time DESC') ->field("CONVERT(varchar, create_time, 23) as create_time, SUM(kcal_val) as kcal_val") ->select(); $return_data = []; foreach ($food_content as $key => $value) { if(bcdiv($value['kcal_val'],$nutrition_data['kcal'],2) < 0.9){ $bz['text'] = '不达标'; $bz['color'] = '#F0AD4E'; }else if(bcdiv($value['kcal_val'],$nutrition_data['kcal'],2) >= 0.9 && bcdiv($value['kcal_val'],$nutrition_data['kcal'],2) < 1.1){ $bz['text'] = '达标'; $bz['color'] = '#4CD964'; }else{ $bz['text'] = '超标'; $bz['color'] = '#FF0000'; } array_push($return_data,['time'=>$value['create_time'],'title'=>'摄入卡路里','val'=>$value['kcal_val'],'unit'=>'kcal','describe'=>$bz['text'],'color'=>$bz['color']]); } 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,birthday,set_carbohydrate_v,set_carbohydrate_p,set_protein_v,set_protein_p,set_fat_v,set_fat_p,activity_level') ->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){ $nutrition_data['kcal'] = $user_data['set_kcal'] != 0?$user_data['set_kcal']:$nutrition_data['kcal']; $nutrition_data['carbohydrate'] = $user_data['set_carbohydrate_v'] != null?$user_data['set_carbohydrate_v']:$nutrition_data['carbohydrate']; $nutrition_data['protein'] = $user_data['set_protein_v'] != null?$user_data['set_protein_v']:$nutrition_data['protein']; $nutrition_data['fat'] = $user_data['set_fat_v'] != null?$user_data['set_fat_v']:$nutrition_data['fat']; // $nutrition_data['carbohydrate_p'] = $user_data['set_carbohydrate_p'] != null?bcdiv($user_data['set_carbohydrate_p'],100,2):$nutrition_data['carbohydrate_p']; // $nutrition_data['protein_p'] = $user_data['set_protein_p'] != null?bcdiv($user_data['set_protein_p'],100,2):$nutrition_data['protein_p']; // $nutrition_data['fat_p'] = $user_data['set_fat_p'] != null?bcdiv($user_data['set_fat_p'],100,2):$nutrition_data['fat_p']; $nutrition_data['carbohydrate_p'] = $user_data['set_carbohydrate_p'] != null?$user_data['set_carbohydrate_p']:bcmul($nutrition_data['carbohydrate_p'],100,2); $nutrition_data['protein_p'] = $user_data['set_protein_p'] != null?$user_data['set_protein_p']:bcmul($nutrition_data['protein_p'],100,2); $nutrition_data['fat_p'] = $user_data['set_fat_p'] != null?$user_data['set_fat_p']:bcmul($nutrition_data['fat_p'],100,2); }else{ $nutrition_data['carbohydrate_p'] = bcmul($nutrition_data['carbohydrate_p'],100,2); $nutrition_data['protein_p'] = bcmul($nutrition_data['protein_p'],100,2); $nutrition_data['fat_p'] = bcmul($nutrition_data['fat_p'],100,2); } $nutrition_describe = [ [ // '对于一个孩子(2-18岁)(没有特殊健康问题),身体处于快速生长发育阶段,需要充足的营养支持,尤其是蛋白质和健康脂肪。通常建议的三大营养素比例为:', // '1、碳水化合物:45%-65% 的总热量', // '提供能量,支持孩子的日常活动和生长发育。', // '优先选择复合碳水化合物(如全谷物、蔬菜、水果),避免精制糖和高糖零食。', // '2、蛋白质:10%-30% 的总热量', // '支持肌肉、骨骼和器官的发育。', // '建议摄入优质蛋白质来源,如瘦肉、鱼类、鸡蛋、豆类和乳制品。', // '蛋白质需求较高,尤其是青春期孩子。', // '3、脂肪:25%-35% 的总热量', // '提供能量,并支持大脑发育(尤其是Omega-3脂肪酸)。', // '优先选择健康脂肪,如鱼类、坚果、种子、橄榄油和牛油果。', // '避免反式脂肪和过多的饱和脂肪。', // '注意事项', // '如果你有特定的健康目标(如增高,减重),可以进一步调整比例,并咨询营养师或医生以获得个性化建议。', '孩子(2-18岁)处于快速生长发育期,需充足营养,尤其蛋白质和健康脂肪。建议比例:', '1、碳水化合物:45%-65%总热量', '提供能量,优选全谷物、蔬果等复合碳水,避免高糖零食。', '2、蛋白质:10%-30%总热量', '支持肌肉、骨骼发育,推荐鱼、蛋、瘦肉、豆类等优质蛋白。', '3、脂肪:25%-35%总热量', '供能并促进大脑发育,选择坚果、鱼类、橄榄油等健康脂肪。', '注意事项:若有增高、减重等目标,可咨询专业人士调整。' ], [ // '对于一个正常成年人(没有特殊健康问题或特定健身目标),通常建议的三大营养素比例为:', // '1、碳水化合物:45%-65% 的总热量', // '主要功能是提供能量', // '建议选择复合碳水化合物(如全谷物、蔬菜、豆类),而不是精制糖。', // '对于普通人,碳水化合物占总热量的 50%-55% 是一个常见的推荐值。', // '2、蛋白质:10%-35% 的总热量', // '用于维持肌肉、修复组织和支持免疫功能。', // '普通人每日蛋白质摄入量建议为 0.8-1.2克/公斤体重。', // '对于活动量较大或健身人群,蛋白质比例可以提高到 20%-30%。', // '3、脂肪:20%-35% 的总热量', // '提供能量、支持细胞功能并帮助吸收脂溶性维生素。', // '建议以 不饱和脂肪(如橄榄油、坚果、鱼类)为主,减少饱和脂肪和反式脂肪的摄入。', // '脂肪占总热量的 20%-30% 是常见的推荐值。', // '以上是根据世界卫生组织(WHO)和其他健康机构的建议制定的。具体比例可以根据个人的活动水平、健康状况和目标进行微调。', // '注意事项', // '活动水平:活动量大的人可能需要更多的碳水化合物来提供能量,而健身人群可能需要更多的蛋白质。', // '健康状况:例如,糖尿病患者可能需要控制碳水化合物比例,而心血管疾病患者可能需要减少脂肪摄入。', // '个体差异:每个人的代谢和需求不同,建议根据个人情况调整。', // '如果你有特定的健康目标(如减脂、增肌或控制慢性病),可以进一步调整比例,并咨询营养师或医生以获得个性化建议。', '正常成年人(无特殊健康问题)建议营养比例:', '1、碳水化合物:45%-65%总热量', '主要供能,推荐全谷物、豆类等复合碳水。', '2、蛋白质:10%-35%总热量', '维持肌肉与免疫功能,日常建议0.8-1.2克/公斤体重。', '3、脂肪:20%-35%总热量', '支持细胞功能,以不饱和脂肪为主,减少饱和脂肪。', '注意事项:根据活动量、健康状况微调,特殊目标需个性化建议。' ], [ // '对于一个老人(65岁以上)(没有特殊健康问题),身体的代谢率下降,肌肉量减少,可能面临营养不良或慢性病风险,因此需要调整营养比例。通常建议的三大营养素比例为:', // '1、碳水化合物:45%-65% 的总热量', // '提供能量,但应选择低血糖指数(GI)的食物,如全谷物、蔬菜和豆类,以控制血糖水平。', // '避免精制糖和高糖食物,尤其是糖尿病患者。', // '帮助维持肌肉质量,预防肌肉流失(少肌症)。', // '建议摄入优质蛋白质,如鱼类、瘦肉、鸡蛋、豆类和乳制品。', // '蛋白质需求较高,尤其是活动量较大的老人。', // '3、脂肪:20%-35% 的总热量', // '提供能量,并支持细胞功能和脂溶性维生素的吸收。', // '优先选择不饱和脂肪,如橄榄油、坚果、种子和鱼类。', // '减少饱和脂肪和反式脂肪的摄入,以降低心血管疾病风险。', // '注意事项', // '如果你有特定的健康目标(控制慢性病),可以进一步调整比例,并咨询营养师或医生以获得个性化建议。', '老人(65岁以上)代谢减缓,需关注肌肉维持与慢性病预防。建议比例:', '1、碳水化合物:45%-65%总热量', '选择低GI食物如全谷物、蔬菜,控制血糖。', '2、蛋白质:10%-30%总热量', '预防肌肉流失,优选鱼、蛋、豆类等易消化蛋白。', '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'=>'碳水化合物', 'key_v'=>'carbohydrate', 'icon'=>'icon-tanshuihuahewu', 'proportion'=>$nutrition_data['carbohydrate_p'], 'val'=>$nutrition_data['carbohydrate'], 'unit'=>'克' ], [ 'name'=>'蛋白质', 'key_v'=>'protein', 'icon'=>'icon-Sm-danbaizhi', 'proportion'=>$nutrition_data['protein_p'], 'val'=>$nutrition_data['protein'], 'unit'=>'克' ], [ 'name'=>'脂肪', 'key_v'=>'fat', 'icon'=>'icon-w_fat_normal', 'proportion'=>$nutrition_data['fat_p'], '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 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 del_user_eat_list_log_action($data){ $cfc = Db::connect('cfc_db'); $user_data = $cfc->table($this->kitchenscale_db_msg['user'])->where(['token'=>$data['token']])->field('id,token')->find(); // dump($user_data); if(!$user_data){ return $this->msg(10003); } $result = $cfc->table($this->kitchenscale_db_msg['eat_log']) ->where("aud_id = ".$user_data['id']." AND LEFT(create_time,10) = '" .$data['time']. "'") ->update(['is_del'=>1]); if($result){ return $this->msg([]); }else{ return $this->msg(10002); } } public function current_food_statistics_action($data){ $id_arr = implode("','",$data['log_id']); $cfc = Db::connect('cfc_db'); $user_data = $cfc->table($this->kitchenscale_db_msg['user']) ->where(["token"=>$data['token']]) ->field('weight,height,gender,age,is_use_set_kcal,set_kcal,birthday,set_carbohydrate_v,set_carbohydrate_p,set_protein_v,set_protein_p,set_fat_v,set_fat_p,activity_level') ->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){ $nutrition_data['kcal'] = $user_data['set_kcal'] != 0?$user_data['set_kcal']:$nutrition_data['kcal']; $nutrition_data['carbohydrate'] = $user_data['set_carbohydrate_v'] != null?$user_data['set_carbohydrate_v']:$nutrition_data['carbohydrate']; $nutrition_data['protein'] = $user_data['set_protein_v'] != null?$user_data['set_protein_v']:$nutrition_data['protein']; $nutrition_data['fat'] = $user_data['set_fat_v'] != null?$user_data['set_fat_v']:$nutrition_data['fat']; $nutrition_data['carbohydrate_p'] = $user_data['set_carbohydrate_p'] != null?$user_data['set_carbohydrate_p']:bcmul($nutrition_data['carbohydrate_p'],100,2); $nutrition_data['protein_p'] = $user_data['set_protein_p'] != null?$user_data['set_protein_p']:bcmul($nutrition_data['protein_p'],100,2); $nutrition_data['fat_p'] = $user_data['set_fat_p'] != null?$user_data['set_fat_p']:bcmul($nutrition_data['fat_p'],100,2); }else{ $nutrition_data['carbohydrate_p'] = bcmul($nutrition_data['carbohydrate_p'],100,2); $nutrition_data['protein_p'] = bcmul($nutrition_data['protein_p'],100,2); $nutrition_data['fat_p'] = bcmul($nutrition_data['fat_p'],100,2); } $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'], //剩下可摄入卡路里量 ]; $eat_food_data = $cfc->table($this->kitchenscale_db_msg['eat_log'])->where("id in ('".$id_arr."')")->select(); // dump($eat_food_data); // die; if(count($eat_food_data) > 0){ //计算营养物质 // return $this->msg($food_content); foreach ($eat_food_data as $key => $value) { // dump($value['nutrients_four']); $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); // 处理各餐 } // 处理剩下可吃 $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; } $nameMap = [ 'kcal' => ['卡路里','kcal','https://tc.pcxbc.com/kitchenscale_all/icon_kcal.png','#5180D8'], 'carbohydrate' => ['碳水','g','https://tc.pcxbc.com/kitchenscale_all/icon_carbohydrate.png','#ED7886'], 'protein' => ['蛋白质','g','https://tc.pcxbc.com/kitchenscale_all/icon_protein.png','#FFB169'], 'fat' => ['脂肪','g','https://tc.pcxbc.com/kitchenscale_all/icon_fat.png','#3CB383'], ]; $all_yy_data = bcadd($return_data['suggestion']['fat'],bcadd($return_data['suggestion']['carbohydrate'],$return_data['suggestion']['protein'],20),20); foreach ($return_data['suggestion'] as $key => $value) { $return_data['nutrients_four'][] = [ 'name'=>$nameMap[$key][0], 'unit'=>$nameMap[$key][1], 'suggestion'=>$value, 'today_intake'=>$return_data['today_intake'][$key], 'icon'=>$nameMap[$key][2], 'color'=>$nameMap[$key][3], 'proportion'=>bcdiv($return_data['today_intake'][$key],$value,2) >= 1?'100':bcmul(bcdiv($return_data['today_intake'][$key],$value,2),100,2), // 'proportion_fp'=>$key == 'kcal'?0:(bcdiv($return_data['suggestion'][$key],$all_yy_data,2))*100, 'proportion_fp'=>$key == 'kcal'?0:$nutrition_data[$key.'_p'], ]; } // dump($user_data); unset($return_data['suggestion']); unset($return_data['today_intake']); return $this->msg($return_data); } #######################################################################工具####################################################################### #######################################################################工具####################################################################### #######################################################################工具####################################################################### // 计算每日每餐kcal占比 public function calculate_kcal_proportion($data){ $all_kcal = $data['today_intake']['kcal']; $arr_kcal = []; foreach ($data['list'] as $key => $value) { $arr_kcal[] = $value['val']; } // 计算占比 $percentages = []; if (bccomp($all_kcal, "0", 20) > 0) { $sum = "0"; // 计算前n-1个元素的占比 for ($i = 0; $i < count($arr_kcal) - 1; $i++) { $percentage = bcmul(bcdiv($arr_kcal[$i], $all_kcal, 20), "100", 20); $percentage = bcdiv($percentage, "1", 2); // 保留两位小数 $percentages[] = $percentage; $sum = bcadd($sum, $percentage, 2); } // 最后一个元素用100%减去前面总和,确保100% if($arr_kcal[count($arr_kcal)-1] != 0){ $lastPercentage = bcsub("100.00", $sum, 2); }else{ $lastPercentage = '0.00'; } $percentages[] = $lastPercentage; } else { // 如果总热量为0,所有占比都为0 $percentages = array_fill(0, count($arr_kcal), "0.00"); } // 将占比结果添加回原数据 foreach ($data['list'] as $key => &$value) { $value['kcal_proportion'] = $percentages[$key]; } return $data; } // 计算营养物质能量占比 public function calculate_energy_proportion($data){ // 获取今日摄入数据 $today_intake = $data['today_intake']; // dump($today_intake); // 总热量摄入 $total_kcal = $today_intake['kcal']; // 各营养素摄入量(克) $carb_intake = $today_intake['carbohydrate']; // 碳水 $protein_intake = $today_intake['protein']; // 蛋白质 $fat_intake = $today_intake['fat']; // 脂肪 // 供能系数 $carb_energy_coef = "4"; // 碳水:4 kcal/g $protein_energy_coef = "4"; // 蛋白质:4 kcal/g $fat_energy_coef = "9"; // 脂肪:9 kcal/g // 计算各营养素的能量贡献(kcal) $carb_energy = bcmul($carb_intake, $carb_energy_coef, 20); $protein_energy = bcmul($protein_intake, $protein_energy_coef, 20); $fat_energy = bcmul($fat_intake, $fat_energy_coef, 20); // 计算各营养素的能量占比(%) if (bccomp($total_kcal, "0", 20) > 0) { // 碳水能量占比 $carb_proportion = bcmul(bcdiv($carb_energy, $total_kcal, 20), "100", 20); $carb_proportion = bcdiv($carb_proportion, "1", 2); // 保留两位小数 // 蛋白质能量占比 $protein_proportion = bcmul(bcdiv($protein_energy, $total_kcal, 20), "100", 20); $protein_proportion = bcdiv($protein_proportion, "1", 2); // 脂肪能量占比 $fat_proportion = bcmul(bcdiv($fat_energy, $total_kcal, 20), "100", 20); $fat_proportion = bcdiv($fat_proportion, "1", 2); } else { $carb_proportion = $protein_proportion = $fat_proportion = "0.00"; } // 填充details数据 $data['details']['carbohydrate']['val'] = $carb_intake; $data['details']['carbohydrate']['proportion'] = $carb_proportion; $data['details']['protein']['val'] = $protein_intake; $data['details']['protein']['proportion'] = $protein_proportion; $data['details']['fat']['val'] = $fat_intake; $data['details']['fat']['proportion'] = $fat_proportion; return $data; } // 排序营养元素食物排行榜 public function energy_food_rank($data){ // 获取所有食物数据 $allFoods = []; // 遍历每餐数据,收集所有食物信息 foreach ($data['list'] as $meal) { if (!empty($meal['list'])) { foreach ($meal['list'] as $food) { // 获取食物的营养素数据 $nutrients_four = $food['nutrients_four']; $nutrientValues = []; // 将营养素数据转换为键值对,便于查询 foreach ($nutrients_four as $nutrient) { $nutrientValues[$nutrient['name']] = $nutrient['value']; } $allFoods[] = [ 'name' => $food['name'], 'pic_url' => $food['pic_url'], 'weight' => $food['weight'], 'carbohydrate' => isset($nutrientValues['碳水化合物']) ? $nutrientValues['碳水化合物'] : '0', 'protein' => isset($nutrientValues['蛋白质']) ? $nutrientValues['蛋白质'] : '0', 'fat' => isset($nutrientValues['脂肪']) ? $nutrientValues['脂肪'] : '0' ]; } } } // 如果没有食物数据,直接返回 if (empty($allFoods)) { return $data; } // 对每种营养素进行排序并取前三 $nutrientTypes = [ 'carbohydrate' => '碳水化合物', 'protein' => '蛋白质', 'fat' => '脂肪' ]; foreach ($nutrientTypes as $nutrientKey => $nutrientName) { // 使用BC函数进行精确比较排序 usort($allFoods, function($a, $b) use ($nutrientKey) { $valueA = $a[$nutrientKey]; $valueB = $b[$nutrientKey]; // 使用bccomp进行比较 $comparison = bccomp($valueA, $valueB, 20); if ($comparison == 0) { return 0; } // 返回-1表示a应该在b前面(降序排序) return ($comparison > 0) ? -1 : 1; }); // 取前三名 $topThree = array_slice($allFoods, 0, 3); // 填充rank_list $rankList = []; foreach ($topThree as $index => $food) { $rankList[] = [ 'icon' => 'https://tc.pcxbc.com/kitchenscale_all/rank'.($index+1).'.png', 'name' => $food['name'], 'pic_url' => $food['pic_url'], 'weight' => bcdiv($food[$nutrientKey], '1', 2) // 使用BC函数保留两位小数 ]; } // 如果不足三个,用空数据补全 $currentCount = count($rankList); for ($i = $currentCount; $i < 3; $i++) { $rankList[] = [ 'icon' => 'https://tc.pcxbc.com/kitchenscale_all/rank' . ($i + 1) . '.png', 'name' => '', 'pic_url' => '', 'weight' => '' ]; } // 更新到details中对应的营养素 $data['details'][$nutrientKey]['rank_list'] = $rankList; } return $data; } // 微量元素处理全天 public function calculate_trace_elements($data){ $traceElements = []; // 遍历所有餐次数据 foreach ($data['list'] as $meal) { if (!empty($meal['list'])) { foreach ($meal['list'] as $food) { if (!empty($food['nutrients_list'])) { foreach ($food['nutrients_list'] as $nutrient) { // 只处理维生素(type=2)和矿物质(type=3) if (in_array($nutrient['type'], [2, 3])) { $name = $nutrient['name']; $value = $nutrient['value']; // 初始化微量元素数据 if (!isset($traceElements[$name])) { $traceElements[$name] = [ 'name' => $nutrient['name'], 'name_ch' => isset($nutrient['name_ch']) ? $nutrient['name_ch'] : $nutrient['name'], 'unit' => $nutrient['unit'], 'value' => '0' ]; } // 使用BC数学函数精确累加 $traceElements[$name]['value'] = bcadd( $traceElements[$name]['value'], $value, 20 ); } } } } } } // 处理结果,保留两位小数 $formattedElements = []; foreach ($traceElements as $element) { // 使用BC函数格式化值为两位小数 $formattedValue = bcdiv($element['value'], '1', 2); // 如果值为0,跳过(可选,根据需求决定是否显示0值) if (bccomp($formattedValue, '0', 2) > 0) { $formattedElements[] = [ 'name' => $element['name'], 'name_ch' => $element['name_ch'], 'unit' => $element['unit'], 'value' => $formattedValue ]; } } // 按中文名称排序 usort($formattedElements, function($a, $b) { return strcmp($a['name_ch'], $b['name_ch']); }); $data['trace_elements_all_day'] = $formattedElements; return $data; } }