SchoolPhysicalExamination/application/NewReedaw/controller/app/Kitchenscale.php

2901 lines
137 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\NewReedaw\controller\app;
use think\Db;
use app\NewReedaw\controller\app\Guessyoulike;
class Kitchenscale extends Base{
protected $kitchenscale_db_name = [
'zhanghao'=>'app_account_number',
'juese'=>'app_user_data',
'eat_log'=>'app_user_kcal_log',//
'tiaoma'=>'app_commodity_barcode',//
'search_history'=>'app_user_search_history',//搜索历史表
'cookbook_label'=>'app_user_cookbook_label',//菜谱标签表
'foodlist3'=>'app_z_national_standard_food_type_3',//食材
'foodlist4'=>'app_z_national_standard_food_type_4',//食材营养物质
'collect_list'=>'app_user_collect_list',//点赞表
'cookbook'=>'app_user_cookbook',//菜谱表
'uploadimg'=>'app_user_upload_img',//图片素材表
'nutrition'=>'app_user_cookbook_nutrition',//能量
'vitamin'=>'app_user_cookbook_vitamin',//维生素
'mineral'=>'app_user_cookbook_mineral',//矿物质
];
// protected $skip_use_db_name = [
// '1'=>'app_card_skip_data',
// '2'=>'app_user_data',
// '3'=>'app_card_body_data',
// ];
protected $page_num = 10;
// 加 bcadd(,,20)
// 减 bcsub(,,20)
// 乘 bcmul(,,20)
// 除 bcdiv(,,20)
// 测试token=>'caadd1be045a65f30b92aa805f1de54a'
################################################################接口################################################################
################################################################接口################################################################
################################################################接口################################################################
################################################################首页
// // 根据用户获取他的饮食信息(首页)
// public function user_diet_content(){
// $data = input('post.');
// try {
// if(!array_key_exists('aud_id', $data) || !array_key_exists('token', $data)){
// return $this->msg(10001);
// }
// if(!$this->verify_data_is_ok($data['aud_id'],'intnum')){
// return $this->msg(10005,'aud_id type error');
// }
// if(!$this->verify_data_is_ok($data['token'],'str')){
// return $this->msg(10005,'r_time type error');
// }
// return $this->user_diet_content_action($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'] .= "接口: user_diet_content\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);
// }
// }
// 修改用户信息
public function update_user_msg(){
$data = input('post.');
// try {
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('nickname', $data)){
return $this->msg(10001,'nickname is miss');
}
if(!array_key_exists('gender', $data)){
return $this->msg(10001,'gender is miss');
}
if(!array_key_exists('birthday', $data)){
return $this->msg(10001,'birthday 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('activity_level', $data)){
return $this->msg(10001,'weight 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['nickname'],'str')){
return $this->msg(10005,'nickname type is error');
}
if(!$this->verify_data_is_ok($data['gender'],'intnum')){
return $this->msg(10005,'gender type is error');
}
if(!$this->verify_data_is_ok($data['birthday'],'datetime')){
return $this->msg(10005,'birthday type is error');
}
if(!$this->verify_data_is_ok($data['height'],'num')){
return $this->msg(10005,'height type is error');
}
if(!$this->verify_data_is_ok($data['weight'],'num')){
return $this->msg(10005,'weight type is error');
}
if(!$this->verify_data_is_ok($data['activity_level'],'num')){
return $this->msg(10005,'activity_level type is error');
}
return $this->update_user_msg_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'] .= "接口: update_user_msg\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);
// }
}
################################################################搜索&菜谱
// 搜索页面的默认信息数据
public function search_default_msg(){
try {
$data = input('post.');
$return_data = $this->search_default_msg_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'] .= "接口: search_default_msg\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);
}
}
// 搜索食材列表
public function get_food_list(){
// 尝试捕获异常
$data = input('post.');
try {
$return_data = $this->get_food_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'] .= "接口: get_food_list\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 search_column(){
$data = input('post.');
// try {
if(!array_key_exists('search_data', $data)){
return $this->msg(10001);
}
if(!array_key_exists('page', $data)){
return $this->msg(10001,'page is miss');
}
if(!array_key_exists('aud_id', $data)){
return $this->msg(10001,'aud_id is miss');
}
if(!$this->verify_data_is_ok($data['search_data'],'str')){
return $this->msg(10005);
}
if(!$this->verify_data_is_ok($data['page'],'intnum')){
return $this->msg(10005,'page 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->search_column_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'] .= "接口: (search_column)\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);
// }
}
// 查询食谱的详情(OK)
public function cookbook_details(){
// 尝试捕获异常
$data = input('post.');
try {
if(!array_key_exists('cookbook_id', $data)){
return $this->msg(10001,'cookbook_id is miss');
}
if(!array_key_exists('aud_id', $data)){
return $this->msg(10001,'aud_id is miss');
}
if(!$this->verify_data_is_ok($data['cookbook_id'],'intnum')){
return $this->msg(10005,'cookbook_id 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->cookbook_details_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'] .= "接口: (cookbook_details)\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' => '系统错误']);
}
}
// 点赞收藏菜谱(OK)
public function cookbook_like(){
// 尝试捕获异常
$data = input('post.');
try {
if(!array_key_exists('token', $data)){
return $this->msg(10001,'token is miss');
}
if(!array_key_exists('cookbook_id', $data)){
return $this->msg(10001,'cookbook_id 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['cookbook_id'],'intnum')){
return $this->msg(10005,'cookbook_id 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->cookbook_like_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'] .= "接口: (cookbook_like)\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_search_history(){
// 尝试捕获异常
$data = input('post.');
try {
if(!array_key_exists('token', $data)){
return $this->msg(10001,'token is miss');
}
if(!$this->verify_data_is_ok($data['token'],'str')){
return $this->msg(10005,'token type is error');
}
if(!array_key_exists('del_arr', $data)){
return $this->msg(10001,'del_arr is miss');
}
if(!$this->verify_data_is_ok($data['del_arr'],'str')){
return $this->msg(10005,'del_arr type is error');
}
$data['del_arr'] = strval($data['del_arr']);
$data['del_arr'] = trim($data['del_arr']);
// 判断是否是 "all"(不区分大小写)
if (strtolower($data['del_arr']) !== 'all' && preg_match('/^\d+(,\d+)*$/', $data['del_arr']) !== 1) {
return $this->msg(10005,'del_arr type is error');
}
$return_data = $this->del_search_history_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'] .= "接口: (del_search_history)\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(){
$data = input('post.');
// 尝试捕获异常
try {
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 $this->get_countfoot_content_action($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'] .= "接口: get_countfoot_content\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 add_intake_food(){
// 尝试捕获异常
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'] .= "接口: add_intake_food\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 = input('post.');
try {
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'] .= "接口: get_log_list\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_nutrition_proportion(){
// 尝试捕获异常
try {
$data = input('post.');
if(!array_key_exists('token', $data)){
return $this->msg(10001,'token is miss');
}
if(!$this->verify_data_is_ok($data['token'],'str')){
return $this->msg(10005,'token type is error');
}
if(!array_key_exists('set_kcal', $data)){
return $this->msg(10001,'set_kcal is miss');
}
if(!$this->verify_data_is_ok($data['set_kcal'],'num')){
return $this->msg(10005,'set_kcal type is error');
}
if(!array_key_exists('carbohydrate_v', $data)){
return $this->msg(10001,'carbohydrate_v is miss');
}
if(!$this->verify_data_is_ok($data['carbohydrate_v'],'num')){
return $this->msg(10005,'carbohydrate_v type is error');
}
if(!array_key_exists('carbohydrate_p', $data)){
return $this->msg(10001,'carbohydrate_p is miss');
}
if(!$this->verify_data_is_ok($data['carbohydrate_p'],'num')){
return $this->msg(10005,'carbohydrate_p type is error');
}
if(!array_key_exists('protein_v', $data)){
return $this->msg(10001,'protein_v is miss');
}
if(!$this->verify_data_is_ok($data['protein_v'],'num')){
return $this->msg(10005,'protein_v type is error');
}
if(!array_key_exists('protein_p', $data)){
return $this->msg(10001,'protein_p is miss');
}
if(!$this->verify_data_is_ok($data['protein_p'],'num')){
return $this->msg(10005,'protein_p type is error');
}
if(!array_key_exists('fat_v', $data)){
return $this->msg(10001,'fat_v is miss');
}
if(!$this->verify_data_is_ok($data['fat_v'],'num')){
return $this->msg(10005,'fat_v type is error');
}
if(!array_key_exists('fat_p', $data)){
return $this->msg(10001,'fat_p is miss');
}
if(!$this->verify_data_is_ok($data['fat_p'],'num')){
return $this->msg(10005,'fat_p type is error');
}
if(!array_key_exists('aud_id', $data)){
return $this->msg(10001,'aud_id is miss');
}
if(!$this->verify_data_is_ok($data['aud_id'],'num')){
return $this->msg(10005,'aud_id type is error');
}
$return_data = $this->set_nutrition_proportion_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'] .= "接口: (set_nutrition_proportion)\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 = input('post.');
try {
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'] .= "接口: (del_user_eat_log)\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(!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['time'],'datetime')){
return $this->msg(10005,'time 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->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 set_up_content(){
// 尝试捕获异常
$data = input('post.');
try {
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 current_food_statistics(){
// 尝试捕获异常
$data = input('post.');
try {
if(!array_key_exists('log_id', $data)){
return $this->msg(10001,'log_id is miss');
}
if(!array_key_exists('aud_id', $data)){
return $this->msg(10001,'aud_id is miss');
}
if(!$this->verify_data_is_ok($data['aud_id'],'intnum')){
return $this->msg(10005,'aud_id type is error');
}
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' => '系统错误']);
}
}
################################################################我的
// 获取用户收藏点赞列表(OK)
public function get_user_collect_list(){
try {
$data = input('post.');
if(!array_key_exists('token', $data)){
return $this->msg(10001,'token is miss');
}
if(!array_key_exists('page', $data)){
return $this->msg(10001,'page 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['page'],'intnum')){
return $this->msg(10005,'page 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->get_user_collect_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'] .= "接口: get_default_config\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################################################################
// public function user_diet_content_action($data){
// // 获取账号下信息以及用户信息
// $zhanghao_data = Db::table($this->kitchenscale_db_name['zhanghao'])->where(['token'=>$data['token']])->field('id,token,nickname,head_pic,tel,email')->find();
// if(!$zhanghao_data){
// return $this->msg(10004);
// }
// // 获取账号下信息以及用户信息start
// $user_all_data['aud_id'] = '';
// $user_all_data['token'] = $zhanghao_data['token'];
// $user_all_data['nickname'] = '';
// $user_all_data['head_pic'] = $zhanghao_data['head_pic'];
// $user_all_data['gender'] = '';
// $user_all_data['age'] = '';
// $user_all_data['height'] = '';
// $user_all_data['weight'] = '';
// $user_all_data['set_kcal'] = '';
// $user_all_data['is_use_set_kcal'] = '';
// $user_all_data['tel'] = $zhanghao_data['tel'];
// $user_all_data['email'] = $zhanghao_data['email'];
// $user_account = Db::table($this->kitchenscale_db_name['juese'])
// ->where(["id"=>$data['aud_id']])
// ->field('id as aud_id,nickname,head_pic,gender,height,weight,set_kcal,is_use_set_kcal,birthday,activity_level')
// ->find();
// if($user_account){
// if($user_account['set_kcal'] == '.00'){
// $user_account['set_kcal'] = 0;
// }
// $user_all_data['aud_id'] = $user_account['aud_id'];
// $user_all_data['nickname'] = $user_account['nickname'];
// $user_all_data['gender'] = $user_account['gender'];
// $user_all_data['age'] = $this->calculate_age($user_account['birthday']);
// $user_all_data['height'] = $user_account['height'];
// $user_all_data['weight'] = $user_account['weight'];
// $user_all_data['set_kcal'] = $user_account['set_kcal'];
// $user_all_data['is_use_set_kcal'] = $user_account['is_use_set_kcal'];
// $user_all_data['birthday'] = $user_account['birthday'];
// $user_all_data['activity_level'] = $user_account['activity_level'];
// if($user_all_data['gender'] == 2){
// if($user_all_data['age'] <= 18){
// $user_all_data['head_pic'] = "https://tc.pcxbc.com/kitchenscale_all/woman1.png";
// }else if($user_all_data['age'] <= 39 && $user_all_data['age'] > 18){
// $user_all_data['head_pic'] = "https://tc.pcxbc.com/kitchenscale_all/woman2.png";
// }else if($user_all_data['age'] <= 59 && $user_all_data['age'] > 39){
// $user_all_data['head_pic'] = "https://tc.pcxbc.com/kitchenscale_all/woman3.png";
// }else if($user_all_data['age'] >= 60){
// $user_all_data['head_pic'] = "https://tc.pcxbc.com/kitchenscale_all/woman4.png";
// }
// }else{
// if($user_all_data['age'] <= 18){
// $user_all_data['head_pic'] = "https://tc.pcxbc.com/kitchenscale_all/man1.png";
// }else if($user_all_data['age'] <= 39 && $user_all_data['age'] > 18){
// $user_all_data['head_pic'] = "https://tc.pcxbc.com/kitchenscale_all/man2.png";
// }else if($user_all_data['age'] <= 59 && $user_all_data['age'] > 39){
// $user_all_data['head_pic'] = "https://tc.pcxbc.com/kitchenscale_all/man3.png";
// }else if($user_all_data['age'] >= 60){
// $user_all_data['head_pic'] = "https://tc.pcxbc.com/kitchenscale_all/man4.png";
// }
// }
// $user_all_data['food_count'] = $this->get_countfoot_content_action(['token'=>$data['token'],'aud_id'=>$user_all_data['aud_id'],'time'=>date('Y-m-d')]);
// $user_all_data['food_count'] = ($user_all_data['food_count']->getData())['data'];
// // dump($user_all_data['food_count']);
// }else{
// $user_account['set_kcal'] = 0;
// $user_all_data['aud_id'] = null;
// $user_all_data['gender'] = 0;
// $user_all_data['age'] = false;
// $user_all_data['height'] = null;
// $user_all_data['weight'] = null;
// $user_all_data['set_kcal'] = 0;
// $user_all_data['is_use_set_kcal'] = 0;
// $user_all_data['birthday'] = null;
// $user_all_data['activity_level'] = null;
// $user_all_data['head_pic'] = "https://tc.pcxbc.com/kitchenscale_all/man1.png";
// $user_all_data['food_count'] = $this->get_countfoot_content_action(['token'=>$data['token'],'aud_id'=>$user_all_data['aud_id'],'time'=>date('Y-m-d')]);
// $user_all_data['food_count'] = ($user_all_data['food_count']->getData())['data'];
// }
// return $this->msg($user_all_data);
// }
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 = Db::table($this->kitchenscale_db_name['juese'])
->where(["id"=>$data['aud_id']])
->field('weight,height,gender,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);
}
$user_data['age_num'] = $this->calculate_age($user_data['birthday']);
$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'];
$return_data['suggestion']['carbohydrate'] = $nutrition_data['carbohydrate'];
$return_data['suggestion']['protein'] = $nutrition_data['protein'];
$return_data['suggestion']['fat'] = $nutrition_data['fat'];
$return_data['remaining_kcal'] = $nutrition_data['kcal'];
// 查询用户今日摄入食物
// $food_content = Db::table($this->kitchenscale_db_name['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();
// \'https://tc.pcxbc.com\' pic_url
$food_content = Db::table($this->kitchenscale_db_name['eat_log'])
// ->alias('a')
// ->join('app_z_national_standard_food_type_3 b','a.food_id = b.id','LEFT')
->where("is_del = 0 AND aud_id = " . $data['aud_id'] . " AND CAST(create_time AS DATE) = CAST('" . $data['time'] . "' AS DATE)")
->field('meals_type,food_name,weight,unit,kcal_val,carbohydrate_val,protein_val,fat_val,id,food_id,type')
->select();
// 处理食材图片
$food_id = [];
foreach ($food_content as $key => $value) {
$food_id[] = $value['food_id'];
}
if(count($food_id) > 0){
$food_ys_pic_url = $cfc->table('app_z_national_standard_food_type_3')
->where("id in (".implode(',',$food_id).")")
->field('id,\'https://tc.pcxbc.com\' + pic_url as pic_url')
->select();
foreach ($food_content as $key => $value) {
foreach ($food_ys_pic_url as $k => $v) {
if($value['food_id'] == $v['id']){
$food_content[$key]['pic_url'] = $v['pic_url'];
}
}
}
}else{
foreach ($food_content as $key => $value) {
$food_content[$key]['pic_url'] = '';
}
}
$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 = Db::table($this->kitchenscale_db_name['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 update_user_msg_action($data){
if($data['gender'] == 0){
return $this->msg(10005,'性别信息错误');
}
// 获取账号下信息以及用户信息
$user_data = Db::table($this->kitchenscale_db_name['zhanghao'])->where(['token'=>$data['token']])->count();
if($user_data<=0){
return $this->msg(10005,'账号信息错误');
}
// $cfc = Db::connect('cfc_db');
$is_user_true = Db::table($this->kitchenscale_db_name['juese'])->where(['id'=>$data['aud_id']])->count();
$user_msg['nickname'] = $data['nickname'];
$user_msg['head_pic'] = $data['gender'] == 1?'https://tc.pcxbc.com/tsf/1.png':'https://tc.pcxbc.com/tsf/2.png';
$user_msg['gender'] = $data['gender'];
$user_msg['birthday'] = $data['birthday'];
$user_msg['height'] = $data['height'];
$user_msg['weight'] = $data['weight'];
$user_msg['activity_level'] = $data['activity_level'];
if($is_user_true>0){
$user_msg['last_update_time'] = date('Y-m-d H:i:s');
$result = Db::table($this->kitchenscale_db_name['juese'])
->where(['id'=>$data['aud_id']])
->update($user_msg);
}else{
return $this->msg(10005,'角色信息错误');
}
if($result){
return $this->msg([]);
}else{
return $this->msg(10002);
}
}
public function search_default_msg_action($data){
$return_data = [
'search_history'=>['cookbook'=>[],'food'=>[]],
'search_guess'=>[],
];
// 如果有角色信息
if(array_key_exists('aud_id', $data)){
if(!$this->verify_data_is_ok($data['token'],'str')){
return $this->msg(10005);
}
// 获取账号下信息以及用户信息 start
$user = Db::table($this->kitchenscale_db_name['juese'])->where(["id"=>$data['aud_id']])->field('id,nickname')->find();
if($user){
// 处理搜索历史 start
$search_history_cookbook = Db::table($this->kitchenscale_db_name['search_history'])
->where(["user_id"=>$user['id'],'is_del'=>0,'type'=>'cookbook'])
->field('id,keyword,last_searched_at,type')
->order('last_searched_at desc')
->limit(10)
->select();
$search_history_food = Db::table($this->kitchenscale_db_name['search_history'])
->where(["user_id"=>$user['id'],'is_del'=>0,'type'=>'food'])
->field('id,keyword,last_searched_at,type')
->order('last_searched_at desc')
->limit(10)
->select();
// 去重
foreach ($search_history_cookbook as $key => $value) {
unset($search_history_cookbook[$key]['type']);
unset($search_history_cookbook[$key]['ROW_NUMBER']);
}
foreach ($search_history_food as $key => $value) {
unset($search_history_food[$key]['type']);
unset($search_history_food[$key]['ROW_NUMBER']);
}
$return_data['search_history']['cookbook'] = $search_history_cookbook;
$return_data['search_history']['food'] = $search_history_food;
// 处理搜索历史 end
}
}
// 处理猜你喜欢信息start
// 使用三元运算符判断$user是否存在
$user_id = isset($user) ? $user['id'] : 9999999;
$cnxh = new Guessyoulike;
$cookbook_data = $cnxh->getGuessYouLike($user_id,'cookbook');
// dump($cookbook_data);
// die;
$food_data = $cnxh->getGuessYouLike($user_id,'food');
// dump($return_data['search_history']);
// dump($cookbook_data);
// dump($food_data);
// die;
foreach ($cookbook_data as $key => $value) {
$return_data['search_guess']['cookbook'][] = ['title'=>$key,'list'=>$value];
}
foreach ($food_data as $key => $value) {
$return_data['search_guess']['food_data'][] = ['title'=>$key,'list'=>$value];
}
// 处理猜你喜欢信息end
// // 添加菜谱label start
// $cfc = Db::connect('cfc_db');
// $cookbook_label = $cfc->table($this->kitchenscale_db_name['cookbook_label'])->where(["is_del"=>0])->field('id,name')->select();
// $return_data['cookbook_label'] = $cookbook_label;
// // 添加菜谱label end
return $this->msg($return_data);
}
public function get_food_list_action($data){
if(!array_key_exists('page',$data)){
$data['page'] = 1;
}else{
if(!$this->verify_data_is_ok($data['page'],'intnum')){
$data['page'] = 1;
}
}
$cp_page_num = 15;
$cfc = Db::connect('cfc_db');
$search_sql_str = "is_del = 0";
if(!array_key_exists('search_data', $data)){
$data['search_data'] = "";
$ss = "";
}else{
if($data['search_data'] === ""){
$data['search_data'] = "";
$ss = "";
}else{
$ss = $data['search_data'];
$data['search_data'] = " AND food_name LIKE '%".str_replace('[', '[[]', $ss)."%'";
}
}
$search_sql_str = $search_sql_str.$data['search_data'];
// 计算总数
$count_sql = "SELECT COUNT(*) as total FROM {$this->kitchenscale_db_name['foodlist3']} WHERE {$search_sql_str}";
$count_result = $cfc->query($count_sql);
$content_num = $count_result[0]['total'] ?? 0;
$page_total = ceil($content_num/$cp_page_num);
// 计算分页偏移量
$page = max(1, intval($data['page']));
$offset = ($page - 1) * $cp_page_num;
// SQL Server分页查询 - 使用OFFSET FETCH语法SQL Server 2012及以上版本
$sql = "SELECT id, record_id, food_name as name, pic_url, Calorie_val as kcal, weight_unit as unit
FROM {$this->kitchenscale_db_name['foodlist3']}
WHERE {$search_sql_str}
ORDER BY id
OFFSET {$offset} ROWS
FETCH NEXT {$cp_page_num} ROWS ONLY";
// dump($count_sql);
$collect_list = $cfc->query($sql);
if(count($collect_list) > 0){
$nutrients_list_id = [];
$nutrients_list_temporary = [];
foreach ($collect_list as $key => $value) {
$collect_list[$key]['pic_url'] = "https://tc.pcxbc.com/kitchenscale_all".$collect_list[$key]['pic_url'];
array_push($nutrients_list_id,$collect_list[$key]['record_id']);
$nutrients_list_temporary[$collect_list[$key]['record_id']] = [];
if (!is_numeric($collect_list[$key]['kcal'])) {
$collect_list[$key]['kcal'] = 0;
}
}
// 处理营养物质
$nutrients_list_data = $cfc->table($this->kitchenscale_db_name['foodlist4'])
->where("father_id in (". implode(',',$nutrients_list_id) .")")
->field("father_id,name,name_ch,unit,value,type")
->select();
// dump($nutrients_list_id);
// die;
for ($i=0; $i < count($nutrients_list_data); $i++) {
if(!is_numeric($nutrients_list_data[$i]['value']) || $nutrients_list_data[$i]['value'] == 0){
array_push($nutrients_list_temporary[$nutrients_list_data[$i]['father_id']],[
'name'=>$nutrients_list_data[$i]['name'],
'name_ch'=>$nutrients_list_data[$i]['name_ch'],
'unit'=>$nutrients_list_data[$i]['unit'],
'value'=>0,
'type'=>$nutrients_list_data[$i]['type'],
'type_name'=>$nutrients_list_data[$i]['type'] == 1?'能量及宏量营养素':($nutrients_list_data[$i]['type'] == 2?'维生素':'矿物质'),
'color'=>$nutrients_list_data[$i]['type'] == 1?'#C4FFE0':($nutrients_list_data[$i]['type'] == 2?'#FFEFB7':'#7DA8E0')
]);
}else{
array_push($nutrients_list_temporary[$nutrients_list_data[$i]['father_id']],[
'name'=>$nutrients_list_data[$i]['name'],
'name_ch'=>$nutrients_list_data[$i]['name_ch'],
'unit'=>$nutrients_list_data[$i]['unit'],
'value'=>$nutrients_list_data[$i]['value'],
'type'=>$nutrients_list_data[$i]['type'],
'type_name'=>$nutrients_list_data[$i]['type'] == 1?'能量及宏量营养素':($nutrients_list_data[$i]['type'] == 2?'维生素':'矿物质'),
'color'=>$nutrients_list_data[$i]['type'] == 1?'#C4FFE0':($nutrients_list_data[$i]['type'] == 2?'#FFEFB7':'#7DA8E0')
]);
}
}
// dump($nutrients_list_temporary);
// die;
for ($i=0; $i < count($collect_list); $i++) {
$collect_list[$i]['nutrients_four'] = [];
$collect_list[$i]['nutrients_list'] = [];
$temporary_calculate = 0;
if(array_key_exists($collect_list[$i]['record_id'],$nutrients_list_temporary)){
$collect_list[$i]['nutrients_list'] = $nutrients_list_temporary[$collect_list[$i]['record_id']];
// 处理四大营养物质
foreach ($nutrients_list_temporary[$collect_list[$i]['record_id']] as $key => $value) {
// dump($value);
if($value['type'] == 1){
array_push($collect_list[$i]['nutrients_four'],[
'name'=>$value['name'] == 'Carbohydrate'?'碳水':$value['name_ch'],
'unit'=>$value['unit'],
'color'=>$value['name'] == 'Carbohydrate'?'#FFB169':($value['name'] == 'Fat'?'#ED7886':($value['name'] == 'Protein'?'#5180D8':'')),
'value'=>!is_numeric($value['value'])?0:$value['value'],
]);
// 如果不为卡路里
if($value['name'] != 'Calorie'){
$temporary_calculate = $temporary_calculate+$value['value'];
}
}
}
// dump($collect_list);
// 处理详情当中的环数据占比
foreach ($collect_list[$i]['nutrients_four'] as $k => $v) {
if($v['name'] != '卡路里'){
$collect_list[$i]['nutrients_four'][$k]['proportion'] = bcmul(bcdiv($v['value'],$temporary_calculate,2),100,0);
}else{
$collect_list[$i]['nutrients_four'][$k]['proportion'] = 0;
}
}
}
}
}
// 记录搜索历史
if(array_key_exists('aud_id',$data) && $data['aud_id'] != '' && $ss != ''){
$user_data = Db::table($this->kitchenscale_db_name['juese'])->where(['id'=>$data['aud_id']])->field('id,aan_id')->find();
if($user_data){
$this->add_search_history_action(['id'=>$user_data['id'],'search_data'=>$ss,'type'=>'food']);
}
}
return $this->msg([
'page_now'=>$data['page'],
'page_total'=>$page_total,
'content_list'=>$collect_list
]);
}
public function search_column_action($data){
// $cookbook = new Cookbook();
$cfc = Db::connect('cfc_db');
$page_now = array_key_exists('page',$data)?$data['page']:1;
$page_total = $page_now;
$page_num = 20;
// 获取菜谱信息
$content_num = $cfc->table($this->kitchenscale_db_name['cookbook'])
->where("is_del = 0 AND (title LIKE '%".$data['search_data']."%' OR describe_data LIKE '%".$data['search_data']."%')")
->count();
$page_total = ceil($content_num/$page_num);
$content_list = $cfc->table($this->kitchenscale_db_name['cookbook'])
->alias('cookbook')
->join($this->kitchenscale_db_name['uploadimg'].' uploadimg','cookbook.cover = uploadimg.id','LEFT')
->where("cookbook.is_del = 0 AND (cookbook.title LIKE '%".$data['search_data']."%' OR cookbook.describe_data LIKE '%".$data['search_data']."%')")
->field("cookbook.id,cookbook.title,cookbook.create_user_head_pic,cookbook.create_user_nickname,cookbook.likes_num,uploadimg.pic_url as cover")
->page("$page_now,$page_num")
->select();
if(count($content_list)<=0){
return $this->msg([]);
}
if(array_key_exists('aud_id',$data)){
if($data['aud_id'] != ''){
// 获取账号下信息以及用户信息
$user_data = Db::table($this->kitchenscale_db_name['juese'])->where(['id'=>$data['aud_id']])->field('id,aan_id,nickname,head_pic')->find();
if($user_data){
// 获取用户收藏列表
$my_collect_list = Db::table($this->kitchenscale_db_name['collect_list'])
->where(['aud_id'=>$data['aud_id']])
->column('cookbook_id');
// dump();
// 处理菜谱收藏信息
foreach ($content_list as $key => $value) {
if(array_key_exists($value['id'],$my_collect_list)){
$content_list[$key]['is_me_like_it'] = 'yes';
}else{
$content_list[$key]['is_me_like_it'] = 'no';
}
if($value['cover'] == null){
$content_list[$key]['cover'] = 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg';
}
unset($content_list[$key]['ROW_NUMBER']);
}
if($data['search_data'] != ''){
$this->add_search_history_action(['id'=>$user_data['id'],'search_data'=>$data['search_data'],'type'=>'cookbook']);
}
}else{
foreach ($content_list as $key => $value) {
$content_list[$key]['is_me_like_it'] = 'no';
if($value['cover'] == null){
$content_list[$key]['cover'] = 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg';
}
unset($content_list[$key]['ROW_NUMBER']);
}
}
}else{
foreach ($content_list as $key => $value) {
$content_list[$key]['is_me_like_it'] = 'no';
if($value['cover'] == null){
$content_list[$key]['cover'] = 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg';
}
unset($content_list[$key]['ROW_NUMBER']);
}
}
}else{
foreach ($content_list as $key => $value) {
$content_list[$key]['is_me_like_it'] = 'no';
if($value['cover'] == null){
$content_list[$key]['cover'] = 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg';
}
unset($content_list[$key]['ROW_NUMBER']);
}
}
return $this->msg([
'page_now'=>$page_now,
'page_total'=>$page_total,
'content_list'=>$content_list
]);
}
public function cookbook_details_action($data){
$cfc = Db::connect('cfc_db');
$img_arr = [];
// 查询菜谱详情
$cookbook_data = $cfc->table($this->kitchenscale_db_name['cookbook'])->where(['id'=>$data['cookbook_id']])->find();
if(!$cookbook_data){
return $this->msg(10004,'菜谱不存在');
}
$cookbook_data['food_data'] = json_decode($cookbook_data['food_data'],true);
$cookbook_data['step_data'] = json_decode($cookbook_data['step_data'],true);
// $cookbook_data['food_type'] = explode(',',$cookbook_data['food_type']);
$cookbook_data_step_data_count = count($cookbook_data['step_data']);
// 设置需要的图片id
array_push($img_arr, $cookbook_data['cover']);
foreach ($cookbook_data['step_data'] as $key => $value) {
if(count($value['pic_list']) > 0){
foreach ($value['pic_list'] as $k => $v) {
if(!in_array($v, $img_arr)){
array_push($img_arr, $v);
}
}
}
}
// dump($cookbook_data);
// die;
$img_arr = implode(',',$img_arr);
// 查找需要的图片
$img_arr_data = $cfc->table($this->kitchenscale_db_name['uploadimg'])->where("id in ($img_arr) AND is_del = 0")->field('id,pic_name,pic_url')->select();
$cookbook_img_data = [];
// 处理菜谱图片
foreach ($img_arr_data as $key => $value) {
$cookbook_img_data[$value['id']] = $value['pic_url'];
}
// 设置菜谱图片
foreach ($cookbook_data['step_data'] as $key => $value) {
$cookbook_data['step_data'][$key]['step_num'] = "步骤".($key+1).'/'.$cookbook_data_step_data_count;
$cookbook_data['step_data'][$key]['pic_url_list'] = [];
foreach ($value['pic_list'] as $k => $v) {
if(array_key_exists($v, $cookbook_img_data)){
array_push($cookbook_data['step_data'][$key]['pic_url_list'],$cookbook_img_data[$v]);
}else{
array_push($cookbook_data['step_data'][$key]['pic_url_list'],'https://tc.pcxbc.com/kitchenscale_all/diule.jpg');
}
}
}
// $cookbook_data['cover_id'] = $cookbook_data['cover'];
if(array_key_exists($cookbook_data['cover'], $cookbook_img_data)){
$cookbook_data['cover_pic_url'] = $cookbook_img_data[$cookbook_data['cover']];
}else{
$cookbook_data['cover_pic_url'] = 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg';
}
if(array_key_exists($cookbook_data['cover'], $cookbook_img_data)){
if($data['aud_id'] != ''){
// 获取账号下信息以及用户信息
$user_data = Db::table($this->kitchenscale_db_name['juese'])->where(['id'=>$data['aud_id']])->field('id,aan_id,nickname,head_pic')->find();
if(!$user_data){
return $this->msg(20001,'角色信息错误');
}
$collect_data = Db::table($this->kitchenscale_db_name['collect_list'])
->where([
'aud_id'=>$data['aud_id'],
'cookbook_id'=>$data['cookbook_id'],
])
->find();
if($collect_data){
if($collect_data['is_del'] == 0){
// 如果有结果并且没被删过
$cookbook_data['collect_status'] = 'yes';
}else{
// 如果有结果被删过
$cookbook_data['collect_status'] = 'no';
}
}else{
// 如果没结果
$cookbook_data['collect_status'] = 'no';
}
}else{
$cookbook_data['collect_status'] = 'no';
}
}else{
$cookbook_data['collect_status'] = 'no';
}
$nutrition = $cfc->table($this->kitchenscale_db_name['nutrition'])->where(['cookbook_id'=>$data['cookbook_id']])->field('name_en,name_ch as name,unit,value as weight,nrv')->select();
$vitamin = $cfc->table($this->kitchenscale_db_name['vitamin'])->where(['cookbook_id'=>$data['cookbook_id']])->field('name_en,name_ch as name,unit,value as weight,nrv')->select();
$mineral = $cfc->table($this->kitchenscale_db_name['mineral'])->where(['cookbook_id'=>$data['cookbook_id']])->field('name_en,name_ch as name,unit,value as weight,nrv')->select();
$cookbook_data['tags'] = [];
array_push($cookbook_data['tags'],[
'title'=>'所需食材',
'desc'=>'重量',
'list'=>$cookbook_data['food_data'],
]);
if(count($nutrition)>0){
array_push($cookbook_data['tags'],[
'title'=>'能量',
'desc'=>'含量',
'list'=>$nutrition,
]);
}else{
array_push($cookbook_data['tags'],[
'title'=>'',
'desc'=>'',
'list'=>[],
]);
}
if(count($vitamin)>0){
array_push($cookbook_data['tags'],[
'title'=>'维生素',
'desc'=>'含量',
'list'=>$vitamin,
]);
}else{
array_push($cookbook_data['tags'],[
'title'=>'',
'desc'=>'',
'list'=>[],
]);
}
if(count($mineral)>0){
array_push($cookbook_data['tags'],[
'title'=>'矿物质',
'desc'=>'含量',
'list'=>$mineral,
]);
}else{
array_push($cookbook_data['tags'],[
'title'=>'',
'desc'=>'',
'list'=>[],
]);
}
// 处理营养物质 end
$cookbook_data['token'] = $cookbook_data['create_user_token'];
$cookbook_data['description'] = $cookbook_data['describe_data'];
// $cookbook_data['food_list'] = $cookbook_data['food_data'];
$cookbook_data['step_list'] = $cookbook_data['step_data'];
unset($cookbook_data['create_user_token']);
unset($cookbook_data['describe_data']);
unset($cookbook_data['food_data']);
unset($cookbook_data['step_data']);
unset($cookbook_data['create_time']);
unset($cookbook_data['food_type']);
unset($cookbook_data['ROW_NUMBER']);
unset($cookbook_data['is_del']);
return $this->msg($cookbook_data);
}
public function cookbook_like_action($data){
// 获取账号下信息以及用户信息
$user_data = Db::table($this->kitchenscale_db_name['juese'])->where(['id'=>$data['aud_id']])->field('id,aan_id,nickname,head_pic')->find();
if(!$user_data){
return $this->msg(20001,'角色信息错误');
}
$cfc = Db::connect('cfc_db');
$cookbook_data = $cfc->table($this->kitchenscale_db_name['cookbook'])->where(['id'=>$data['cookbook_id']])->field('id,create_user_token,likes_num')->find();
if(!$cookbook_data){
return $this->msg(10004,'未找到菜谱');
}
$like_data = Db::table($this->kitchenscale_db_name['collect_list'])->where(['aud_id'=>$data['aud_id'],'cookbook_id'=>$data['cookbook_id']])->find();
$like_data_state = 0;
if($like_data){
if($like_data['is_del'] == 0){
// 如果当前是关注状态
$like_data_state = 1;
}else{
$like_data_state = 0;
}
// 启动事务
Db::startTrans();
try{
Db::table($this->kitchenscale_db_name['collect_list'])
->where(['id'=>$like_data['id']])
->update(['is_del'=>$like_data_state]);
if($like_data_state == 0){
$cfc->table($this->kitchenscale_db_name['cookbook'])->where(['id'=>$data['cookbook_id']])->setInc('likes_num');
}else{
$cfc->table($this->kitchenscale_db_name['cookbook'])->where(['id'=>$data['cookbook_id']])->setDec('likes_num');
}
// 提交事务
Db::commit();
if($like_data_state==0){
$likes_num = $cookbook_data['likes_num']+1;
}else{
$likes_num = $cookbook_data['likes_num']-1;
if($likes_num <= 0){
$likes_num = 0;
}
}
return $this->msg([
'collect_status'=>$like_data_state==0?'yes':'no',
'likes_num'=>$likes_num
]);
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return $this->msg(10002,'操作失败.');
}
}else{
// 启动事务
Db::startTrans();
try{
Db::table($this->kitchenscale_db_name['collect_list'])
->insert([
'aud_id'=>$data['aud_id'],
'cookbook_id'=>$data['cookbook_id'],
'create_time'=>date('Y-m-d H:i:s')
]);
$cfc->table($this->kitchenscale_db_name['cookbook'])->where(['id'=>$data['cookbook_id']])->setInc('likes_num');
// 提交事务
Db::commit();
return $this->msg([
'collect_status'=>"yes",
'likes_num'=>$cookbook_data['likes_num']+1
]);
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return $this->msg(10002,'操作失败');
}
}
}
public function add_intake_food_action($data){
$cfc = Db::connect('cfc_db');
$user_data = Db::table($this->kitchenscale_db_name['juese'])
->where(["id"=>$data['aud_id']])
->field('weight,height,gender,birthday,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(!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_name['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_name['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]);
}
}
}
Db::startTrans();
try{
if(count($data['food_list']) > 1){
$result = Db::table($this->kitchenscale_db_name['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 = Db::table($this->kitchenscale_db_name['eat_log'])->insertGetId($data['food_list'][0]);//新增1个
Db::commit();
return $this->msg(['id'=>$result]);
}
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return $this->msg(10002);
}
}
public function get_log_list_action($data){
// $cfc = Db::connect('cfc_db');
$user_data = Db::table($this->kitchenscale_db_name['juese'])
->where(["id"=>$data['aud_id']])
->field('weight,height,gender,birthday,is_use_set_kcal,set_kcal,activity_level')
->find();
if(!$user_data){
return $this->msg(10003);
}
// 计算年龄
$user_data['age_num'] = $this->calculate_age($user_data['birthday']);
// 计算推荐营养
$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 = Db::query("SELECT COUNT(DISTINCT CONVERT(varchar, create_time, 23)) as total
FROM ".$this->kitchenscale_db_name['eat_log']."
WHERE aud_id = ".$data['aud_id']." and is_del = 0");
$page_total = ceil($content_num[0]['total']/$this->page_num);
$food_content = Db::table($this->kitchenscale_db_name['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_nutrition_proportion_action($data){
// $cfc = Db::connect('cfc_db');
$result = Db::table($this->kitchenscale_db_name['juese'])
->where(['id'=>$data['aud_id']])
->update([
'is_use_set_kcal'=>1,
'set_kcal'=>$data['set_kcal'],
'set_carbohydrate_v'=>$data['carbohydrate_v'],
'set_carbohydrate_p'=>$data['carbohydrate_p'],
'set_protein_v'=>$data['protein_v'],
'set_protein_p'=>$data['protein_p'],
'set_fat_v'=>$data['fat_v'],
'set_fat_p'=>$data['fat_p'],
]);
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 = Db::table($this->kitchenscale_db_name['eat_log'])->where(['aud_id'=>$data['aud_id'],'id'=>$data['eat_log_id']])->find();
if($user_data){
$eat_log_data = Db::table($this->kitchenscale_db_name['eat_log'])
->where(["id"=>$data['eat_log_id']])
->update(['is_del'=>1]);
if($eat_log_data){
return $this->msg([]);
}else{
return $this->msg(10002);
}
}else{
return $this->msg(10004);
}
}
public function del_search_history_action($data){
// $cfc = Db::connect('cfc_db');
$user = Db::table($this->kitchenscale_db_name['juese'])->where(['id'=>$data['aud_id']])->field('id,aan_id')->find();
if(!$user){
return $this->msg(20001,'账号信息错误');
}
if($data['del_arr'] == 'all'){
$result = Db::table($this->kitchenscale_db_name['search_history'])
->where(['user_id'=>$user['id']])
->update(['is_del'=>1]);
}else{
$result = Db::table($this->kitchenscale_db_name['search_history'])
->where("user_id = ".$user['id']." AND id IN (".$data['del_arr'].")")
->update(['is_del'=>1]);
}
if($result){
return $this->msg([]);
}else{
return $this->msg(10002);
}
}
public function del_user_eat_list_log_action($data){
// $cfc = Db::connect('cfc_db');
$user_data = Db::table($this->kitchenscale_db_name['eat_log'])
->where("aud_id = ".$data['aud_id']." AND LEFT(create_time,10) = '".$data['time']."'")
->select();
// dump($user_data);
if(!$user_data){
return $this->msg(10003);
}else{
$result = Db::table($this->kitchenscale_db_name['eat_log'])
->where("aud_id = ".$data['aud_id']." AND LEFT(create_time,10) = '".$data['time']."'")
->update(['is_del'=>1]);
if($result){
return $this->msg([]);
}else{
return $this->msg(10002);
}
}
}
public function get_user_collect_list_action($data){
// 获取账号下信息以及用户信息
$user_data = Db::table($this->kitchenscale_db_name['juese'])->where(['id'=>$data['aud_id'],'is_del'=>0])->field('id,aan_id')->find();
if(!$user_data){
return $this->msg([
'page_now'=>$data['page'],
'page_total'=>0,
'content_list'=>[],
]);
}
$cookbook_like_count = Db::table($this->kitchenscale_db_name['collect_list'])->where(['aud_id'=>$data['aud_id'],'is_del'=>0])->count();
if($cookbook_like_count <= 0){
return $this->msg([
'page_now'=>$data['page'],
'page_total'=>0,
'content_list'=>[],
]);
}
$cookbook_like_data = Db::table($this->kitchenscale_db_name['collect_list'])->where(['aud_id'=>$data['aud_id'],'is_del'=>0])->page($data['page'],$this->page_num)->field('id,aud_id,cookbook_id')->select();
$page_total = ceil($cookbook_like_count/$this->page_num);
$cookbook_id = [];
foreach ($cookbook_like_data as $key => $value) {
$cookbook_id[] = $value['cookbook_id'];
}
$cfc = Db::connect('cfc_db');
$collect_list = $cfc->table($this->kitchenscale_db_name['cookbook'])
->alias('a')
->join($this->kitchenscale_db_name['uploadimg'].' c','a.cover = c.id','LEFT')
->where("a.id IN (".implode(',',$cookbook_id).") AND a.is_del = 0")
->field("a.id,a.title,a.cover as cover_id,c.pic_url as cover_url,a.likes_num,a.create_user_token,a.create_user_head_pic,a.create_user_nickname")
->page($data['page'],$this->page_num)
->select();
foreach ($collect_list as $key => $value) {
$collect_list[$key]['is_me_like_it'] = 'yes';
}
return $this->msg([
'page_now'=>$data['page'],
'page_total'=>$page_total,
'content_list'=>$collect_list
]);
}
public function set_up_content_action($data){
// $cfc = Db::connect('cfc_db');
$user_data = Db::table($this->kitchenscale_db_name['juese'])
->where(["id"=>$data['aud_id']])
->field('weight,height,gender,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);
}
// 计算年龄
$user_data['age_num'] = $this->calculate_age($user_data['birthday']);
// 计算推荐营养
$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 current_food_statistics_action($data){
$id_arr = implode("','",$data['log_id']);
$cfc = Db::connect('cfc_db');
$user_data = Db::table($this->kitchenscale_db_name['juese'])
->where(["id"=>$data['aud_id']])
->field('weight,height,gender,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);
}
$user_data['age_num'] = $this->calculate_age($user_data['birthday']);
$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 = Db::table($this->kitchenscale_db_name['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;
}
}