2025-01-22 12:31:08 +08:00
< ? php
namespace app\KitchenScale\controller\app ;
use think\Db ;
class Cookbook extends Base {
2025-03-08 14:04:09 +08:00
2025-01-22 12:31:08 +08:00
// protected $token_time = 2592000;//30天的秒数
protected $default_head_pic = 'http://tc.pcxbc.com/tsf/head_pic.png' ;
2025-04-03 18:19:04 +08:00
protected $page_num = 10 ;
2025-01-22 12:31:08 +08:00
protected $reedaw_db_msg = [
'zhanghao' => 'app_account_number' , //账号表
'juese' => 'app_user_data' , //角色表
];
protected $kitchenscale_db_msg = [
'cookbook' => 'app_user_cookbook' , //菜谱表
2025-07-23 21:32:05 +08:00
'cookbook_food_relation' => 'app_user_cookbook_food_relation' , //菜谱表
2025-03-14 18:46:26 +08:00
'cookbook_label' => 'app_user_cookbook_label' , //菜谱标签表
2025-01-22 12:31:08 +08:00
'uploadimg' => 'app_user_upload_img' , //素材表
'followlist' => 'app_user_follow_list' , //关注列表
2025-03-07 11:42:26 +08:00
'collect_list' => 'app_user_collect_list' , //收藏列表
2025-07-23 21:32:05 +08:00
'foodlist1' => 'app_z_national_standard_food_type_1' , //食材列表3
'foodlist2' => 'app_z_national_standard_food_type_2' , //食材列表3
'foodlist3' => 'app_z_national_standard_food_type_3' , //食材列表3
'foodlist4' => 'app_z_national_standard_food_type_4' , //食材列表3
'nutrition' => 'app_user_cookbook_nutrition' , //能量
'vitamin' => 'app_user_cookbook_vitamin' , //维生素
'mineral' => 'app_user_cookbook_mineral' , //矿物质
2025-03-12 18:03:31 +08:00
'user_kcal_log' => 'app_user_kcal_log' , //食材列表3
2025-04-03 18:19:04 +08:00
'user' => 'app_user_data' , //banner
2025-01-22 12:31:08 +08:00
];
// 加 bcadd(,,20)
// 减 bcsub(,,20)
// 乘 bcmul(,,20)
// 除 bcdiv(,,20)
################################################################接口################################################################
################################################################接口################################################################
################################################################接口################################################################
2025-03-07 18:13:24 +08:00
// 添加菜谱(OK)
2025-01-22 12:31:08 +08:00
public function add_cookbook (){
// 尝试捕获异常
// 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 ( 'cover' , $data )){
return $this -> msg ( 10001 , 'cover is miss' );
}
if ( ! array_key_exists ( 'description' , $data )){
return $this -> msg ( 10001 , 'description is miss' );
}
if ( ! array_key_exists ( 'cook_label' , $data )){
return $this -> msg ( 10001 , 'cook_label is miss' );
}
if ( ! array_key_exists ( 'food_list' , $data )){
$data [ 'food_list' ] = [];
}
if ( ! array_key_exists ( 'step_list' , $data )){
$data [ 'step_list' ] = [];
}
if ( ! $this -> verify_data_is_ok ( $data [ 'token' ], 'str' )){
return $this -> msg ( 10005 , 'token type is error' );
}
if ( ! $this -> verify_data_is_ok ( $data [ 'cover' ], 'intnum' )){
return $this -> msg ( 10005 , 'cover type is error' );
}
if ( ! $this -> verify_data_is_ok ( $data [ 'description' ], 'str' )){
return $this -> msg ( 10005 , 'description type is error' );
}
2025-03-12 18:03:31 +08:00
if ( ! $this -> verify_data_is_ok ( $data [ 'cook_label' ], 'intnum' )){
return $this -> msg ( 10005 , 'cook_label type is error' );
}
2025-01-22 12:31:08 +08:00
if ( ! is_array ( $data [ 'food_list' ])) {
return $this -> msg ( 10005 , 'food_list type is error' );
}
if ( ! is_array ( $data [ 'step_list' ])) {
return $this -> msg ( 10005 , 'step_list type is error' );
}
$return_data = $this -> add_cookbook_action ( $data );
return $return_data ;
// } catch (\Exception $e) {
// // 捕获异常
2025-07-23 21:32:05 +08:00
// $logContent["flie"] = $e->getFile();
2025-01-22 12:31:08 +08:00
// $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";
// // 记录日志
2025-07-23 21:32:05 +08:00
// $this->record_api_log($data, $logContent, null);
2025-01-22 12:31:08 +08:00
// return json(['status' => 'error', 'message' => '系统错误']);
// }
}
2025-04-03 18:19:04 +08:00
// 修改菜谱(OK)
public function update_cookbook (){
2025-01-22 12:31:08 +08:00
// 尝试捕获异常
2025-07-23 21:32:05 +08:00
try {
2025-01-22 12:31:08 +08:00
if ( count ( input ( 'post.' )) > 0 ){
$data = input ( 'post.' );
}
if ( ! array_key_exists ( 'token' , $data )){
return $this -> msg ( 10001 , 'token is miss' );
}
2025-04-03 18:19:04 +08:00
if ( ! array_key_exists ( 'cover' , $data )){
return $this -> msg ( 10001 , 'cover is miss' );
}
if ( ! array_key_exists ( 'description' , $data )){
return $this -> msg ( 10001 , 'description is miss' );
}
2025-01-22 12:31:08 +08:00
if ( ! array_key_exists ( 'cook_label' , $data )){
return $this -> msg ( 10001 , 'cook_label is miss' );
}
2025-04-03 18:19:04 +08:00
if ( ! array_key_exists ( 'food_list' , $data )){
$data [ 'food_list' ] = [];
}
if ( ! array_key_exists ( 'step_list' , $data )){
$data [ 'step_list' ] = [];
}
if ( ! array_key_exists ( 'cookbook_id' , $data )){
return $this -> msg ( 10001 , 'cookbook_id is miss' );
2025-01-22 12:31:08 +08:00
}
2025-04-03 18:19:04 +08:00
2025-01-22 12:31:08 +08:00
if ( ! $this -> verify_data_is_ok ( $data [ 'token' ], 'str' )){
return $this -> msg ( 10005 , 'token type is error' );
}
2025-04-03 18:19:04 +08:00
if ( ! $this -> verify_data_is_ok ( $data [ 'cover' ], 'intnum' )){
return $this -> msg ( 10005 , 'cover type is error' );
}
if ( ! $this -> verify_data_is_ok ( $data [ 'description' ], 'str' )){
return $this -> msg ( 10005 , 'description type is error' );
}
if ( ! $this -> verify_data_is_ok ( $data [ 'cook_label' ], 'intnum' )){
return $this -> msg ( 10005 , 'cook_label type is error' );
}
if ( ! is_array ( $data [ 'food_list' ])) {
return $this -> msg ( 10005 , 'food_list type is error' );
}
if ( ! is_array ( $data [ 'step_list' ])) {
return $this -> msg ( 10005 , 'step_list type is error' );
}
if ( ! $this -> verify_data_is_ok ( $data [ 'cookbook_id' ], 'intnum' )){
return $this -> msg ( 10005 , 'cookbook_id type is error' );
}
$return_data = $this -> update_cookbook_action ( $data );
return $return_data ;
2025-07-23 21:32:05 +08:00
} 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' => '系统错误' ]);
}
2025-04-03 18:19:04 +08:00
}
// 根据菜谱标签查询列表(首页用)(OK)
public function find_by_cook_label ( $data = [ 'token' => '' , 'cook_label' => 4 , 'page' => '1' ]){
// 尝试捕获异常
2025-07-23 21:32:05 +08:00
try {
2025-04-03 18:19:04 +08:00
if ( count ( input ( 'post.' )) > 0 ){
$data = input ( 'post.' );
}
if ( ! array_key_exists ( 'cook_label' , $data )){
return $this -> msg ( 10001 , 'cook_label is miss' );
}
2025-07-23 21:32:05 +08:00
// if(!array_key_exists('page', $data)){
// return $this->msg(10001,'page is miss');
// }
2025-04-03 18:19:04 +08:00
2025-03-12 18:03:31 +08:00
if ( ! $this -> verify_data_is_ok ( $data [ 'cook_label' ], 'intnum' )){
2025-01-22 12:31:08 +08:00
return $this -> msg ( 10005 , 'cook_label type is error' );
}
2025-07-23 21:32:05 +08:00
// if(!$this->verify_data_is_ok($data['page'],'intnum')){
// return $this->msg(10005,'page type is error');
// }
2025-01-22 12:31:08 +08:00
$return_data = $this -> find_by_cook_label_action ( $data );
return $return_data ;
2025-07-23 21:32:05 +08:00
} 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' => '系统错误' ]);
}
2025-01-22 12:31:08 +08:00
}
2025-03-07 11:42:26 +08:00
// 根据食材详细查找列表(OK)
2025-07-23 21:32:05 +08:00
public function find_by_food ( $data = [ 'token' => 'caadd1be045a65f30b92aa805f1de54a' , 'food_name' => " 2724,2670 " , 'page' => '1' ]){
2025-01-22 12:31:08 +08:00
// 尝试捕获异常
2025-07-23 21:32:05 +08:00
try {
2025-01-22 12:31:08 +08:00
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 ( 'food_name' , $data )){
return $this -> msg ( 10001 , 'food_name is miss' );
}
2025-07-23 21:32:05 +08:00
// if(!array_key_exists('page', $data)){
// return $this->msg(10001,'page is miss');
// }
2025-01-22 12:31:08 +08:00
if ( ! $this -> verify_data_is_ok ( $data [ 'token' ], 'str' )){
return $this -> msg ( 10005 , 'token type is error' );
}
if ( ! $this -> verify_data_is_ok ( $data [ 'food_name' ], 'str' )){
2025-07-23 21:32:05 +08:00
return $this -> msg ( 10005 , 'tofood_nameken type is error' );
2025-01-22 12:31:08 +08:00
}
2025-07-23 21:32:05 +08:00
// if(!$this->verify_data_is_ok($data['page'],'intnum')){
// return $this->msg(10005,'page type is error');
// }
2025-01-22 12:31:08 +08:00
$return_data = $this -> find_by_food_action ( $data );
return $return_data ;
2025-07-23 21:32:05 +08:00
} 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' => '系统错误' ]);
}
2025-01-22 12:31:08 +08:00
}
2025-03-07 18:13:24 +08:00
// 查询食谱的详情(OK)
2025-07-23 21:32:05 +08:00
public function cookbook_details ( $data = [ 'token' => '' , 'cookbook_id' => '3594' ]){
2025-01-22 12:31:08 +08:00
// 尝试捕获异常
2025-07-23 21:32:05 +08:00
try {
2025-01-22 12:31:08 +08:00
if ( count ( input ( 'post.' )) > 0 ){
$data = input ( 'post.' );
}
if ( ! array_key_exists ( 'cookbook_id' , $data )){
return $this -> msg ( 10001 , 'cookbook_id is miss' );
}
if ( ! $this -> verify_data_is_ok ( $data [ 'cookbook_id' ], 'intnum' )){
return $this -> msg ( 10005 , 'cookbook_id type is error' );
}
$return_data = $this -> cookbook_details_action ( $data );
return $return_data ;
2025-07-23 21:32:05 +08:00
} 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' => '系统错误' ]);
}
2025-01-22 12:31:08 +08:00
}
2025-03-07 18:13:24 +08:00
// 关注行为(OK)
public function cookbook_follow ( $data = [ 'token' => 'caadd1be045a65f3' , 'being_followed' => 'caadd1be045a65f30b92aa805f1de54a' ]){
2025-01-22 12:31:08 +08:00
// 尝试捕获异常
2025-07-23 21:32:05 +08:00
try {
2025-01-22 12:31:08 +08:00
if ( count ( input ( 'post.' )) > 0 ){
$data = input ( 'post.' );
}
if ( ! array_key_exists ( 'token' , $data )){
return $this -> msg ( 10001 , 'token is miss' );
}
2025-03-07 18:13:24 +08:00
if ( ! array_key_exists ( 'being_followed' , $data )){
2025-01-22 12:31:08 +08:00
return $this -> msg ( 10001 , 'cookbook_id is miss' );
}
if ( ! $this -> verify_data_is_ok ( $data [ 'token' ], 'str' )){
return $this -> msg ( 10005 , 'token type is error' );
}
2025-03-07 18:13:24 +08:00
if ( ! $this -> verify_data_is_ok ( $data [ 'being_followed' ], 'str' )){
2025-01-22 12:31:08 +08:00
return $this -> msg ( 10005 , 'cookbook_id type is error' );
}
$return_data = $this -> cookbook_follow_action ( $data );
return $return_data ;
2025-07-23 21:32:05 +08:00
} 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' => '系统错误' ]);
}
2025-01-22 12:31:08 +08:00
}
2025-03-07 18:13:24 +08:00
// 点赞收藏菜谱(OK)
public function cookbook_like ( $data = [ 'token' => 'caadd1be045a65f30b92aa805f1de54a' , 'cookbook_id' => '12' ]){
2025-01-22 12:31:08 +08:00
// 尝试捕获异常
2025-07-23 21:32:05 +08:00
try {
2025-01-22 12:31:08 +08:00
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 ( 'cookbook_id' , $data )){
return $this -> msg ( 10001 , 'cookbook_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' );
}
$return_data = $this -> cookbook_like_action ( $data );
return $return_data ;
2025-07-23 21:32:05 +08:00
} 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' => '系统错误' ]);
}
2025-01-22 12:31:08 +08:00
}
2025-03-08 14:04:09 +08:00
// 计算当前食材重量的卡路里(OK)
2025-03-14 18:46:26 +08:00
public function food_count_kcal ( $data = [ 'food_name' => '鸡肉' , 'food_weight' => 456.37 ]){
2025-01-22 12:31:08 +08:00
// 尝试捕获异常
2025-07-23 21:32:05 +08:00
try {
2025-01-22 12:31:08 +08:00
if ( count ( input ( 'post.' )) > 0 ){
$data = input ( 'post.' );
}
2025-03-07 18:13:24 +08:00
if ( ! array_key_exists ( 'food_name' , $data )){
return $this -> msg ( 10001 , 'food_name is miss' );
2025-01-22 12:31:08 +08:00
}
if ( ! array_key_exists ( 'food_weight' , $data )){
return $this -> msg ( 10001 , 'food_weight is miss' );
}
2025-03-07 18:13:24 +08:00
if ( ! $this -> verify_data_is_ok ( $data [ 'food_name' ], 'str' )){
2025-03-08 14:04:09 +08:00
return $this -> msg ( 10005 , 'food_name type is error' );
2025-03-07 18:13:24 +08:00
}
if ( ! $this -> verify_data_is_ok ( $data [ 'food_weight' ], 'num' )){
2025-03-08 14:04:09 +08:00
return $this -> msg ( 10005 , 'food_weight type is error' );
2025-01-22 12:31:08 +08:00
}
2025-03-07 18:13:24 +08:00
$return_data = $this -> food_count_kcal_action ( $data );
2025-01-22 12:31:08 +08:00
return $return_data ;
2025-07-23 21:32:05 +08:00
} 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' => '系统错误' ]);
}
2025-01-22 12:31:08 +08:00
}
2025-03-12 18:03:31 +08:00
// 食材列表查询接口(OK)
2025-03-14 18:46:26 +08:00
public function find_food ( $data = [ 'food_name' => '鸡肉' ]){
2025-03-08 14:04:09 +08:00
// 尝试捕获异常
2025-07-23 21:32:05 +08:00
try {
2025-03-08 14:04:09 +08:00
if ( count ( input ( 'post.' )) > 0 ){
$data = input ( 'post.' );
}
2025-03-12 18:03:31 +08:00
if ( ! array_key_exists ( 'food_name' , $data )){
return $this -> msg ( 10001 , 'food_name is miss' );
2025-03-08 14:04:09 +08:00
}
2025-03-12 18:03:31 +08:00
if ( ! $this -> verify_data_is_ok ( $data [ 'food_name' ], 'str' )){
return $this -> msg ( 10005 , 'food_name type is error' );
}
2025-03-08 14:04:09 +08:00
2025-03-12 18:03:31 +08:00
$return_data = $this -> find_food_action ( $data );
2025-03-08 14:04:09 +08:00
return $return_data ;
2025-07-23 21:32:05 +08:00
} 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' => '系统错误' ]);
}
2025-03-08 14:04:09 +08:00
}
2025-03-14 18:46:26 +08:00
// 获取所有食材列表
2025-09-03 19:09:13 +08:00
public function get_food_list ( $data = [ 'food_level2_id' => '2' , 'search_data' => '' , 'page' => 1 ]){
2025-03-14 18:46:26 +08:00
// 尝试捕获异常
2025-07-23 21:32:05 +08:00
try {
2025-03-14 18:46:26 +08:00
if ( count ( input ( 'post.' )) > 0 ){
$data = input ( 'post.' );
}
2025-04-03 18:19:04 +08:00
if ( ! array_key_exists ( 'food_level2_id' , $data )){
return $this -> msg ( 10001 , 'food_level2_id is miss' );
2025-03-14 18:46:26 +08:00
}
2025-07-23 21:32:05 +08:00
// if(!array_key_exists('page', $data)){
// return $this->msg(10001,'page is miss');
// }
2025-04-03 18:19:04 +08:00
if ( ! $this -> verify_data_is_ok ( $data [ 'food_level2_id' ], 'intnum' )){
return $this -> msg ( 10005 , 'food_level2_id type is error' );
}
2025-07-23 21:32:05 +08:00
// if(!$this->verify_data_is_ok($data['page'],'intnum')){
// return $this->msg(10005,'page type is error');
// }
2025-03-14 18:46:26 +08:00
$return_data = $this -> get_food_list_action ( $data );
return $return_data ;
2025-07-23 21:32:05 +08:00
} 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' => '系统错误' ]);
}
2025-03-14 18:46:26 +08:00
}
// 获取所有食谱label
public function get_cookbook_label_list ( $data = [ 'token' => 'caadd1be045a65f30b92aa805f1de54a' ]){
// 尝试捕获异常
2025-07-23 21:32:05 +08:00
try {
2025-03-14 18:46:26 +08:00
if ( count ( input ( 'post.' )) > 0 ){
$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' );
}
$return_data = $this -> get_cookbook_label_list_action ( $data );
return $return_data ;
2025-07-23 21:32:05 +08:00
} catch ( \Exception $e ) {
// 捕获异常
$logContent [ " flie " ] = $e -> getFile ();
$logContent [ " line " ] = $e -> getLine ();
$logContent [ 'all_content' ] = " 异常信息: \n " ;
$logContent [ 'all_content' ] .= " 消息: " . $e -> getMessage () . " \n " ;
$logContent [ 'all_content' ] .= " 代码: " . $e -> getCode () . " \n " ;
$logContent [ 'all_content' ] .= " 文件: " . $e -> getFile () . " \n " ;
$logContent [ 'all_content' ] .= " 行号: " . $e -> getLine () . " \n " ;
$logContent [ 'all_content' ] .= " 跟踪信息: \n " . $e -> getTraceAsString () . " \n " ;
// 记录日志
$this -> record_api_log ( $data , $logContent , null );
return json ([ 'status' => 'error' , 'message' => '系统错误' ]);
}
}
// 获取查询页页面导航食材列表
public function get_search_food_page_list ( $data = [ 'token' => 'caadd1be045a65f30b92aa805f1de54a' ]){
// 尝试捕获异常
2025-08-02 23:03:04 +08:00
// try {
2025-07-23 21:32:05 +08:00
if ( count ( input ( 'post.' )) > 0 ){
$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' );
}
$return_data = $this -> get_search_food_page_list_action ( $data );
return $return_data ;
2025-08-02 23:03:04 +08:00
// } 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' => '系统错误']);
// }
2025-03-14 18:46:26 +08:00
}
2025-03-12 18:03:31 +08:00
2025-03-07 18:13:24 +08:00
#######################################################################action#######################################################################
#######################################################################action#######################################################################
2025-01-22 12:31:08 +08:00
#######################################################################action#######################################################################
public function add_cookbook_action ( $data ){
2025-07-23 21:32:05 +08:00
2025-03-12 18:03:31 +08:00
// 获取账号下信息以及用户信息
$user_data = Db :: table ( $this -> reedaw_db_msg [ 'zhanghao' ]) -> where ([ 'token' => $data [ 'token' ]]) -> field ( 'id,token,nickname,head_pic' ) -> find ();
if ( ! $user_data ){
2025-03-14 18:46:26 +08:00
return $this -> msg ( 20001 , '账号信息错误' );
2025-03-12 18:03:31 +08:00
}
2025-01-22 12:31:08 +08:00
if ( count ( $data [ 'food_list' ]) < 1 ){
2025-03-14 18:46:26 +08:00
return $this -> msg ( 10001 , '至少添加一个食物' );
2025-01-22 12:31:08 +08:00
}
if ( count ( $data [ 'step_list' ]) < 1 ){
2025-03-14 18:46:26 +08:00
return $this -> msg ( 10001 , '至少添加一个步骤' );
2025-01-22 12:31:08 +08:00
}
$food_type = [];
// 检验一下food_list是否合规( 食材列表)
foreach ( $data [ 'food_list' ] as $key => $value ) {
2025-07-23 21:32:05 +08:00
if ( ! array_key_exists ( 'id' , $value ) || ! array_key_exists ( 'weight' , $value )) {
return $this -> msg ( 10001 , '食材缺少id或者重量' );
2025-01-22 12:31:08 +08:00
}
if ( ! $this -> verify_data_is_ok ( $value [ 'name' ], 'str' )){
2025-03-07 18:13:24 +08:00
return $this -> msg ( 10005 , '食材名称格式错误' );
2025-01-22 12:31:08 +08:00
}
if ( ! $this -> verify_data_is_ok ( $value [ 'weight' ], 'intnum' )){
return $this -> msg ( 10005 , '食材重量格式错误,需整数数字' );
}
2025-07-23 21:32:05 +08:00
if ( ! in_array ( $value [ 'id' ], $food_type )){
array_push ( $food_type , $value [ 'id' ]);
2025-03-12 18:03:31 +08:00
}
2025-01-22 12:31:08 +08:00
}
// 检验一下step_list是否合规( 步骤列表)
foreach ( $data [ 'step_list' ] as $key => $value ) {
if ( ! array_key_exists ( 'description' , $value ) || ! array_key_exists ( 'pic_list' , $value )) {
2025-03-14 18:46:26 +08:00
return $this -> msg ( 10001 , '步骤缺少描述或者图片' );
2025-01-22 12:31:08 +08:00
}
if ( ! $this -> verify_data_is_ok ( $value [ 'description' ], 'str' )){
return $this -> msg ( 10005 , '步骤描述格式错误,需要正常字符' );
}
foreach ( $value [ 'pic_list' ] as $k => $v ) {
if ( ! $this -> verify_data_is_ok ( $v , 'intnum' )){
return $this -> msg ( 10005 , '步骤中图片ID错误,需整数数字' );
}
}
}
2025-07-23 21:32:05 +08:00
2025-01-22 12:31:08 +08:00
$cfc = Db :: connect ( 'cfc_db' );
// 获取账号下信息以及用户信息
$user_data = Db :: table ( $this -> reedaw_db_msg [ 'zhanghao' ]) -> where ([ 'token' => $data [ 'token' ]]) -> field ( 'id,token,nickname,head_pic' ) -> find ();
2025-07-23 21:32:05 +08:00
2025-01-22 12:31:08 +08:00
// 处理食材卡路里start
2025-03-07 18:13:24 +08:00
$kcal_data = $this -> count_calorie ( $data [ 'food_list' ], $data [ 'step_list' ]);
2025-03-12 18:03:31 +08:00
$data [ 'food_list' ] = $kcal_data ;
2025-01-22 12:31:08 +08:00
// 处理食材卡路里end
2025-04-03 18:19:04 +08:00
// dump($data['food_list']);
// die;
2025-01-22 12:31:08 +08:00
$insert_data = [
'title' => $data [ 'title' ],
'cover' => $data [ 'cover' ],
'create_user_token' => $user_data [ 'token' ],
'create_user_head_pic' => $user_data [ 'head_pic' ],
'create_user_nickname' => $user_data [ 'nickname' ],
'describe_data' => $data [ 'description' ],
'food_data' => json_encode ( $data [ 'food_list' ]),
'step_data' => json_encode ( $data [ 'step_list' ]),
2025-07-23 21:32:05 +08:00
// 'food_type'=>implode(',', $food_type),
2025-01-22 12:31:08 +08:00
'cook_label' => $data [ 'cook_label' ],
'create_time' => date ( 'Y-m-d H:i:s' )
];
2025-03-12 18:03:31 +08:00
// dump($insert_data);
// die;
2025-07-23 21:32:05 +08:00
// 启动事务
Db :: startTrans ();
try {
$cook_book_result = $cfc -> table ( $this -> kitchenscale_db_msg [ 'cookbook' ]) -> insertGetId ( $insert_data );
$food_type_insert = [];
foreach ( $food_type as $key => $value ) {
array_push ( $food_type_insert ,[ 'cookbook_id' => $cook_book_result , 'food_id' => $value ]);
}
$cfc -> table ( $this -> kitchenscale_db_msg [ 'cookbook_food_relation' ]) -> insertAll ( $food_type_insert );
// 提交事务
Db :: commit ();
2025-01-22 12:31:08 +08:00
return $this -> msg ([]);
2025-07-23 21:32:05 +08:00
} catch ( \Exception $e ) {
// 回滚事务
Db :: rollback ();
2025-01-22 12:31:08 +08:00
return $this -> msg ( 10002 );
}
2025-07-23 21:32:05 +08:00
// if($cook_book_result){
// return $this->msg([]);
// }else{
// return $this->msg(10002);
// }
2025-01-22 12:31:08 +08:00
}
2025-04-03 18:19:04 +08:00
public function update_cookbook_action ( $data ){
2025-03-14 18:46:26 +08:00
// 获取账号下信息以及用户信息
$user_data = Db :: table ( $this -> reedaw_db_msg [ 'zhanghao' ]) -> where ([ 'token' => $data [ 'token' ]]) -> field ( 'id,token,nickname,head_pic' ) -> find ();
if ( ! $user_data ){
return $this -> msg ( 20001 , '账号信息错误' );
}
2025-04-03 18:19:04 +08:00
if ( count ( $data [ 'food_list' ]) < 1 ){
return $this -> msg ( 10001 , '至少添加一个食物' );
}
if ( count ( $data [ 'step_list' ]) < 1 ){
return $this -> msg ( 10001 , '至少添加一个步骤' );
}
$food_type = [];
// 检验一下food_list是否合规( 食材列表)
foreach ( $data [ 'food_list' ] as $key => $value ) {
2025-07-23 21:32:05 +08:00
if ( ! array_key_exists ( 'id' , $value ) || ! array_key_exists ( 'weight' , $value )) {
return $this -> msg ( 10001 , '食材缺少id或者重量' );
2025-04-03 18:19:04 +08:00
}
if ( ! $this -> verify_data_is_ok ( $value [ 'name' ], 'str' )){
return $this -> msg ( 10005 , '食材名称格式错误' );
}
if ( ! $this -> verify_data_is_ok ( $value [ 'weight' ], 'intnum' )){
return $this -> msg ( 10005 , '食材重量格式错误,需整数数字' );
}
2025-07-23 21:32:05 +08:00
if ( ! in_array ( $value [ 'id' ], $food_type )){
array_push ( $food_type , $value [ 'id' ]);
2025-04-03 18:19:04 +08:00
}
}
// 检验一下step_list是否合规( 步骤列表)
foreach ( $data [ 'step_list' ] as $key => $value ) {
if ( ! array_key_exists ( 'description' , $value ) || ! array_key_exists ( 'pic_list' , $value )) {
return $this -> msg ( 10001 , '步骤缺少描述或者图片' );
}
if ( ! $this -> verify_data_is_ok ( $value [ 'description' ], 'str' )){
return $this -> msg ( 10005 , '步骤描述格式错误,需要正常字符' );
}
foreach ( $value [ 'pic_list' ] as $k => $v ) {
if ( ! $this -> verify_data_is_ok ( $v , 'intnum' )){
return $this -> msg ( 10005 , '步骤中图片ID错误,需整数数字' );
}
}
}
$cfc = Db :: connect ( 'cfc_db' );
// 获取账号下信息以及用户信息
// $user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->field('id,token,nickname,head_pic')->find();
// if(!$user_data){
// }
// 处理食材卡路里start
$kcal_data = $this -> count_calorie ( $data [ 'food_list' ], $data [ 'step_list' ]);
$data [ 'food_list' ] = $kcal_data ;
// 处理食材卡路里end
// dump($data['food_list']);
// die;
$insert_data = [
'title' => $data [ 'title' ],
'cover' => $data [ 'cover' ],
'create_user_token' => $user_data [ 'token' ],
'create_user_head_pic' => $user_data [ 'head_pic' ],
'create_user_nickname' => $user_data [ 'nickname' ],
'describe_data' => $data [ 'description' ],
'food_data' => json_encode ( $data [ 'food_list' ]),
'step_data' => json_encode ( $data [ 'step_list' ]),
2025-07-23 21:32:05 +08:00
// 'food_type'=>implode(',', $food_type),
2025-04-03 18:19:04 +08:00
'cook_label' => $data [ 'cook_label' ],
];
2025-07-23 21:32:05 +08:00
2025-04-03 18:19:04 +08:00
2025-07-23 21:32:05 +08:00
// 启动事务
Db :: startTrans ();
try {
$cook_book_result = $cfc -> table ( $this -> kitchenscale_db_msg [ 'cookbook' ]) -> where ([ 'id' => $data [ 'cookbook_id' ]]) -> update ( $insert_data );
$food_type_insert = [];
foreach ( $food_type as $key => $value ) {
array_push ( $food_type_insert ,[ 'cookbook_id' => $data [ 'cookbook_id' ], 'food_id' => $value ]);
}
$cfc -> table ( $this -> kitchenscale_db_msg [ 'cookbook_food_relation' ]) -> where ( 'cookbook_id' , $data [ 'cookbook_id' ]) -> delete ();
$cfc -> table ( $this -> kitchenscale_db_msg [ 'cookbook_food_relation' ]) -> insertAll ( $food_type_insert );
// 提交事务
Db :: commit ();
return $this -> msg ([]);
} catch ( \Exception $e ) {
// 回滚事务
Db :: rollback ();
return $this -> msg ( 10002 );
}
2025-04-03 18:19:04 +08:00
// dump($cook_book_result);
// die;
if ( $cook_book_result ){
return $this -> msg ([]);
} else {
return $this -> msg ( 10002 );
}
}
public function find_by_cook_label_action ( $data ){
2025-07-23 21:32:05 +08:00
$page_now = array_key_exists ( 'page' , $data ) ? $data [ 'page' ] : 1 ;
$page_total = $page_now ;
2025-01-22 12:31:08 +08:00
$page_num = 20 ;
$cook_label = $data [ 'cook_label' ];
$cfc = Db :: connect ( 'cfc_db' );
$content_num = $cfc -> table ( $this -> kitchenscale_db_msg [ 'cookbook' ])
2025-04-03 18:19:04 +08:00
-> where ([ 'cook_label' => $cook_label , 'is_del' => 0 ])
2025-01-22 12:31:08 +08:00
-> count ();
2025-03-07 11:42:26 +08:00
2025-01-22 12:31:08 +08:00
$page_total = ceil ( $content_num / $page_num );
$content_list = $cfc -> table ( $this -> kitchenscale_db_msg [ 'cookbook' ])
2025-04-03 18:19:04 +08:00
-> alias ( 'a' )
-> join ( $this -> kitchenscale_db_msg [ 'uploadimg' ] . ' b' , 'a.cover = b.id' , 'LEFT' )
-> where ([ 'a.cook_label' => $cook_label , 'a.is_del' => 0 ])
-> field ( 'a.id,a.title,b.pic_url as cover,a.create_user_head_pic,a.create_user_nickname,a.likes_num' )
2025-08-02 23:03:04 +08:00
// ->order('id DESC')
2025-01-22 12:31:08 +08:00
-> page ( " $page_now , $page_num " )
-> select ();
2025-04-03 18:19:04 +08:00
if ( array_key_exists ( 'token' , $data )){
if ( $data [ 'token' ] != '' ){
// 获取账号下信息以及用户信息
$user_data = Db :: table ( $this -> reedaw_db_msg [ 'zhanghao' ]) -> where ([ 'token' => $data [ 'token' ]]) -> field ( 'id,token,nickname,head_pic' ) -> find ();
if ( ! $user_data ){
return $this -> msg ( 20001 , '账号信息错误' );
}
// 获取用户收藏列表
$my_collect_list = $cfc -> table ( $this -> kitchenscale_db_msg [ 'collect_list' ])
-> where ([ 'token' => $data [ 'token' ], 'is_del' => 0 ])
-> column ( 'cookbook_id' );
// dump($my_collect_list);
// 处理菜谱收藏信息
foreach ( $content_list as $key => $value ) {
// if(in_array($value['id'],$my_collect_list)){
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' ]);
}
2025-01-22 12:31:08 +08:00
} else {
2025-04-03 18:19:04 +08:00
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' ]);
}
2025-01-22 12:31:08 +08:00
}
2025-04-03 18:19:04 +08:00
} 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' ]);
2025-01-22 12:31:08 +08:00
}
}
2025-04-03 18:19:04 +08:00
// dump($content_list);
// die;
2025-01-22 12:31:08 +08:00
return $this -> msg ([
'page_now' => $page_now ,
'page_total' => $page_total ,
'content_list' => $content_list
]);
}
public function find_by_food_action ( $data ){
2025-07-23 21:32:05 +08:00
2025-03-14 18:46:26 +08:00
// 获取账号下信息以及用户信息
$user_data = Db :: table ( $this -> reedaw_db_msg [ 'zhanghao' ]) -> where ([ 'token' => $data [ 'token' ]]) -> field ( 'id,token,nickname,head_pic' ) -> find ();
if ( ! $user_data ){
return $this -> msg ( 20001 , '账号信息错误' );
}
2025-07-23 21:32:05 +08:00
$page_now = array_key_exists ( 'page' , $data ) ? $data [ 'page' ] : 1 ;
$page_total = $page_now ;
2025-01-22 12:31:08 +08:00
$page_num = 20 ;
$food_name = $data [ 'food_name' ];
$cfc = Db :: connect ( 'cfc_db' );
2025-07-23 21:32:05 +08:00
// $food_name = implode(',',$food_name);
// dump($food_name);
// die;
$content_num = $cfc -> table ( $this -> kitchenscale_db_msg [ 'cookbook_food_relation' ])
-> where ( " food_id in ( $food_name ) " )
-> group ( 'cookbook_id' )
-> field ( 'count(distinct cookbook_id) as count' ) // 显式指定列名
-> select (); // 然后获取结果
// dump($content_num);
$page_total = ceil ( count ( $content_num ) / $page_num );
$content_list = $cfc -> table ( $this -> kitchenscale_db_msg [ 'cookbook_food_relation' ])
-> alias ( 'a' )
-> join ( $this -> kitchenscale_db_msg [ 'cookbook' ] . ' b' , 'a.cookbook_id = b.id' , 'LEFT' )
-> join ( $this -> kitchenscale_db_msg [ 'uploadimg' ] . ' c' , 'b.cover = c.id' , 'LEFT' )
-> where ( " a.food_id in ( $food_name ) " )
-> field ( 'b.id,b.title,c.pic_url as cover,b.create_user_head_pic,b.create_user_nickname,b.likes_num' )
-> group ( 'b.id,b.title,c.pic_url,b.create_user_head_pic,b.create_user_nickname,b.likes_num' )
2025-01-22 12:31:08 +08:00
-> page ( " $page_now , $page_num " )
-> select ();
2025-07-23 21:32:05 +08:00
// dump($food_name);
// dump($content_num);
// dump($page_total);
// dump($content_list);
// die;
// $content_num = $cfc->table($this->kitchenscale_db_msg['cookbook'])
// ->where("food_type like '%$food_name%'")
// ->count();
// $page_total = ceil($content_num/$page_num);
// $content_list = $cfc->table($this->kitchenscale_db_msg['cookbook'])
// ->alias('cookbook')
// ->join($this->kitchenscale_db_msg['uploadimg'].' uploadimg','cookbook.cover = uploadimg.id','LEFT')
// ->where("cookbook.food_type like '%$food_name%'")
// ->field('cookbook.id,cookbook.title,uploadimg.pic_url as cover,cookbook.create_user_head_pic,cookbook.create_user_nickname,cookbook.likes_num')
// ->page("$page_now,$page_num")
// ->select();
2025-01-22 12:31:08 +08:00
2025-03-07 11:42:26 +08:00
// 获取用户收藏列表
$my_collect_list = $cfc -> table ( $this -> kitchenscale_db_msg [ 'collect_list' ])
-> where ([ 'token' => $data [ 'token' ]])
-> column ( 'cookbook_id' );
// dump($my_collect_list);
// 处理菜谱收藏信息
2025-01-22 12:31:08 +08:00
foreach ( $content_list as $key => $value ) {
2025-03-07 11:42:26 +08:00
// if(in_array($value['id'],$my_collect_list)){
if ( array_key_exists ( $value [ 'id' ], $my_collect_list )){
2025-03-12 18:03:31 +08:00
$content_list [ $key ][ 'is_me_like_it' ] = 'yes' ;
2025-01-22 12:31:08 +08:00
} else {
2025-03-12 18:03:31 +08:00
$content_list [ $key ][ 'is_me_like_it' ] = 'no' ;
2025-01-22 12:31:08 +08:00
}
2025-03-07 11:42:26 +08:00
if ( $value [ 'cover' ] == null ){
$content_list [ $key ][ 'cover' ] = 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg' ;
2025-01-22 12:31:08 +08:00
}
2025-03-07 11:42:26 +08:00
unset ( $content_list [ $key ][ 'ROW_NUMBER' ]);
2025-01-22 12:31:08 +08:00
}
2025-07-23 21:32:05 +08:00
2025-01-22 12:31:08 +08:00
return $this -> msg ([
'page_now' => $page_now ,
'page_total' => $page_total ,
'content_list' => $content_list
]);
}
public function cookbook_details_action ( $data ){
2025-04-03 18:19:04 +08:00
2025-01-22 12:31:08 +08:00
$cfc = Db :: connect ( 'cfc_db' );
$img_arr = [];
// 查询菜谱详情
$cookbook_data = $cfc -> table ( $this -> kitchenscale_db_msg [ 'cookbook' ]) -> where ([ 'id' => $data [ 'cookbook_id' ]]) -> find ();
if ( ! $cookbook_data ){
2025-03-14 18:46:26 +08:00
return $this -> msg ( 10004 , '菜谱不存在' );
2025-01-22 12:31:08 +08:00
}
$cookbook_data [ 'food_data' ] = json_decode ( $cookbook_data [ 'food_data' ], true );
$cookbook_data [ 'step_data' ] = json_decode ( $cookbook_data [ 'step_data' ], true );
2025-04-03 18:19:04 +08:00
// $cookbook_data['food_type'] = explode(',',$cookbook_data['food_type']);
2025-01-22 12:31:08 +08:00
$cookbook_data_step_data_count = count ( $cookbook_data [ 'step_data' ]);
// 设置需要的图片id
array_push ( $img_arr , $cookbook_data [ 'cover' ]);
2025-04-03 18:19:04 +08:00
2025-01-22 12:31:08 +08:00
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 );
}
}
}
}
2025-04-03 18:19:04 +08:00
// dump($cookbook_data);
// die;
2025-01-22 12:31:08 +08:00
$img_arr = implode ( ',' , $img_arr );
// 查找需要的图片
$img_arr_data = $cfc -> table ( $this -> kitchenscale_db_msg [ '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' ];
}
2025-04-03 18:19:04 +08:00
2025-01-22 12:31:08 +08:00
// 设置菜谱图片
foreach ( $cookbook_data [ 'step_data' ] as $key => $value ) {
2025-04-03 18:19:04 +08:00
$cookbook_data [ 'step_data' ][ $key ][ 'step_num' ] = " 步骤 " . ( $key + 1 ) . '/' . $cookbook_data_step_data_count ;
$cookbook_data [ 'step_data' ][ $key ][ 'pic_url_list' ] = [];
2025-01-22 12:31:08 +08:00
foreach ( $value [ 'pic_list' ] as $k => $v ) {
if ( array_key_exists ( $v , $cookbook_img_data )){
2025-04-03 18:19:04 +08:00
array_push ( $cookbook_data [ 'step_data' ][ $key ][ 'pic_url_list' ], $cookbook_img_data [ $v ]);
2025-01-22 12:31:08 +08:00
} else {
2025-04-03 18:19:04 +08:00
array_push ( $cookbook_data [ 'step_data' ][ $key ][ 'pic_url_list' ], 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg' );
2025-01-22 12:31:08 +08:00
}
}
}
2025-04-03 18:19:04 +08:00
// $cookbook_data['cover_id'] = $cookbook_data['cover'];
2025-01-22 12:31:08 +08:00
if ( array_key_exists ( $cookbook_data [ 'cover' ], $cookbook_img_data )){
2025-04-03 18:19:04 +08:00
$cookbook_data [ 'cover_pic_url' ] = $cookbook_img_data [ $cookbook_data [ 'cover' ]];
2025-01-22 12:31:08 +08:00
} else {
2025-04-03 18:19:04 +08:00
$cookbook_data [ 'cover_pic_url' ] = 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg' ;
2025-01-22 12:31:08 +08:00
}
2025-04-03 18:19:04 +08:00
2025-01-22 12:31:08 +08:00
2025-04-03 18:19:04 +08:00
if ( array_key_exists ( $cookbook_data [ 'cover' ], $cookbook_img_data )){
if ( $data [ 'token' ] != '' ){
// 获取账号下信息以及用户信息
$user_data = Db :: table ( $this -> reedaw_db_msg [ 'zhanghao' ]) -> where ([ 'token' => $data [ 'token' ]]) -> field ( 'id,token,nickname,head_pic' ) -> find ();
if ( ! $user_data ){
return $this -> msg ( 20001 , '账号信息错误' );
}
$collect_data = $cfc -> table ( $this -> kitchenscale_db_msg [ 'collect_list' ])
-> where ([
'token' => $data [ 'token' ],
'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' ;
}
2025-01-22 12:31:08 +08:00
} else {
2025-04-03 18:19:04 +08:00
// 如果没结果
2025-03-07 18:13:24 +08:00
$cookbook_data [ 'collect_status' ] = 'no' ;
2025-01-22 12:31:08 +08:00
}
} else {
2025-03-07 18:13:24 +08:00
$cookbook_data [ 'collect_status' ] = 'no' ;
2025-01-22 12:31:08 +08:00
}
2025-04-03 18:19:04 +08:00
} else {
$cookbook_data [ 'collect_status' ] = 'no' ;
}
2025-07-23 21:32:05 +08:00
// 处理营养物质
$nutrition = $cfc -> table ( $this -> kitchenscale_db_msg [ '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_msg [ '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_msg [ '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' ],
]);
2025-08-02 23:03:04 +08:00
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' => [],
]);
}
2025-07-23 21:32:05 +08:00
2025-04-03 18:19:04 +08:00
$cookbook_data [ 'token' ] = $cookbook_data [ 'create_user_token' ];
$cookbook_data [ 'description' ] = $cookbook_data [ 'describe_data' ];
2025-07-23 21:32:05 +08:00
// $cookbook_data['food_list'] = $cookbook_data['food_data'];
2025-04-03 18:19:04 +08:00
$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' ]);
2025-01-22 12:31:08 +08:00
unset ( $cookbook_data [ 'create_time' ]);
unset ( $cookbook_data [ 'food_type' ]);
unset ( $cookbook_data [ 'ROW_NUMBER' ]);
2025-04-03 18:19:04 +08:00
unset ( $cookbook_data [ 'is_del' ]);
2025-01-22 12:31:08 +08:00
return $this -> msg ( $cookbook_data );
}
2025-08-02 23:03:04 +08:00
2025-01-22 12:31:08 +08:00
public function cookbook_follow_action ( $data ){
2025-03-14 18:46:26 +08:00
// 获取账号下信息以及用户信息
$user_data = Db :: table ( $this -> reedaw_db_msg [ 'zhanghao' ]) -> where ([ 'token' => $data [ 'token' ]]) -> field ( 'id,token,nickname,head_pic' ) -> find ();
if ( ! $user_data ){
return $this -> msg ( 20001 , '账号信息错误' );
}
2025-01-22 12:31:08 +08:00
// dump($data);
$cfc = Db :: connect ( 'cfc_db' );
2025-03-07 18:13:24 +08:00
// $cookbook_data = $cfc->table($this->kitchenscale_db_msg['cookbook'])->where(['id'=>$data['cookbook_id']])->field('id,create_user_token')->find();
// if(!$cookbook_data){
// return $this->msg(10002,'未找到菜谱');
// }
if ( $data [ 'token' ] == $data [ 'being_followed' ]){
2025-01-22 12:31:08 +08:00
// 如果查询跟作者一致
2025-03-14 18:46:26 +08:00
return $this -> msg ( 10003 , '不能关注自己' );
2025-01-22 12:31:08 +08:00
}
$follow_data = $cfc -> table ( $this -> kitchenscale_db_msg [ 'followlist' ])
-> where ([
'follow_user_token' => $data [ 'token' ],
2025-03-07 18:13:24 +08:00
'being_follow_user_token' => $data [ 'being_followed' ],
2025-01-22 12:31:08 +08:00
])
-> find ();
$follow_data_state = 0 ;
if ( $follow_data ){
if ( $follow_data [ 'is_del' ] == 0 ){
// 如果当前是关注状态
$follow_data_state = 1 ;
} else {
$follow_data_state = 0 ;
}
$follow_result = $cfc -> table ( $this -> kitchenscale_db_msg [ 'followlist' ])
2025-03-07 18:13:24 +08:00
-> where ([ 'id' => $follow_data [ 'id' ]])
2025-01-22 12:31:08 +08:00
-> update ([ 'is_del' => $follow_data_state ]);
} else {
$follow_result = $cfc -> table ( $this -> kitchenscale_db_msg [ 'followlist' ])
-> insert ([
'follow_user_token' => $data [ 'token' ],
2025-03-07 18:13:24 +08:00
'being_follow_user_token' => $data [ 'being_followed' ],
2025-01-22 12:31:08 +08:00
'create_time' => date ( 'Y-m-d H:i:s' )
]);
}
if ( $follow_result ){
return $this -> msg ([]);
} else {
2025-03-14 18:46:26 +08:00
return $this -> msg ( 10002 , '操作失败' );
2025-01-22 12:31:08 +08:00
}
}
public function cookbook_like_action ( $data ){
2025-03-12 18:03:31 +08:00
// 获取账号下信息以及用户信息
$user_data = Db :: table ( $this -> reedaw_db_msg [ 'zhanghao' ]) -> where ([ 'token' => $data [ 'token' ]]) -> field ( 'id,token,nickname,head_pic' ) -> find ();
if ( ! $user_data ){
2025-03-14 18:46:26 +08:00
return $this -> msg ( 20001 , '账号信息错误' );
2025-03-12 18:03:31 +08:00
}
2025-03-14 18:46:26 +08:00
2025-03-12 18:03:31 +08:00
2025-01-22 12:31:08 +08:00
$cfc = Db :: connect ( 'cfc_db' );
2025-03-12 18:03:31 +08:00
$cookbook_data = $cfc -> table ( $this -> kitchenscale_db_msg [ 'cookbook' ]) -> where ([ 'id' => $data [ 'cookbook_id' ]]) -> field ( 'id,create_user_token,likes_num' ) -> find ();
2025-01-22 12:31:08 +08:00
if ( ! $cookbook_data ){
2025-03-14 18:46:26 +08:00
return $this -> msg ( 10004 , '未找到菜谱' );
2025-01-22 12:31:08 +08:00
}
2025-03-12 18:03:31 +08:00
// return $this->msg(10002,'是这里');
2025-03-07 18:13:24 +08:00
// if($data['token'] == $cookbook_data['create_user_token']){
// // 如果查询跟作者一致
// return $this->msg(10002,'不能收藏自己');
// }
2025-03-12 18:03:31 +08:00
$like_data = $cfc -> table ( $this -> kitchenscale_db_msg [ 'collect_list' ]) -> where ([ 'token' => $data [ 'token' ], 'cookbook_id' => $data [ 'cookbook_id' ]]) -> find ();
2025-01-22 12:31:08 +08:00
$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 {
2025-03-07 11:42:26 +08:00
$cfc -> table ( $this -> kitchenscale_db_msg [ 'collect_list' ])
2025-03-07 18:13:24 +08:00
-> where ([ 'id' => $like_data [ 'id' ]])
2025-01-22 12:31:08 +08:00
-> update ([ 'is_del' => $like_data_state ]);
if ( $like_data_state == 0 ){
2025-03-12 18:03:31 +08:00
$cfc -> table ( $this -> kitchenscale_db_msg [ 'cookbook' ]) -> where ([ 'id' => $data [ 'cookbook_id' ]]) -> setInc ( 'likes_num' );
2025-01-22 12:31:08 +08:00
} else {
2025-03-12 18:03:31 +08:00
$cfc -> table ( $this -> kitchenscale_db_msg [ 'cookbook' ]) -> where ([ 'id' => $data [ 'cookbook_id' ]]) -> setDec ( 'likes_num' );
2025-01-22 12:31:08 +08:00
}
// 提交事务
Db :: commit ();
2025-03-12 18:03:31 +08:00
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
]);
2025-01-22 12:31:08 +08:00
} catch ( \Exception $e ) {
// 回滚事务
Db :: rollback ();
2025-03-14 18:46:26 +08:00
return $this -> msg ( 10002 , '操作失败.' );
2025-01-22 12:31:08 +08:00
}
} else {
// 启动事务
Db :: startTrans ();
try {
2025-03-07 11:42:26 +08:00
$cfc -> table ( $this -> kitchenscale_db_msg [ 'collect_list' ])
2025-01-22 12:31:08 +08:00
-> insert ([
'token' => $data [ 'token' ],
'cookbook_id' => $data [ 'cookbook_id' ],
'create_time' => date ( 'Y-m-d H:i:s' )
]);
2025-03-12 18:03:31 +08:00
$cfc -> table ( $this -> kitchenscale_db_msg [ 'cookbook' ]) -> where ([ 'id' => $data [ 'cookbook_id' ]]) -> setInc ( 'likes_num' );
2025-01-22 12:31:08 +08:00
// 提交事务
Db :: commit ();
2025-03-12 18:03:31 +08:00
return $this -> msg ([
'collect_status' => " yes " ,
'likes_num' => $cookbook_data [ 'likes_num' ] + 1
]);
2025-01-22 12:31:08 +08:00
} catch ( \Exception $e ) {
// 回滚事务
Db :: rollback ();
2025-03-14 18:46:26 +08:00
return $this -> msg ( 10002 , '操作失败' );
2025-01-22 12:31:08 +08:00
}
}
}
2025-03-07 18:13:24 +08:00
public function food_count_kcal_action ( $data ){
$cfc = Db :: connect ( 'cfc_db' );
$food_data = $cfc -> table ( $this -> kitchenscale_db_msg [ 'foodlist3' ]) -> where ([ 'name' => $data [ 'food_name' ]]) -> field ( " kcal " ) -> find ();
if ( ! $food_data ){
2025-03-14 18:46:26 +08:00
return $this -> msg ( 10004 , '未登记的食材' );
2025-01-22 12:31:08 +08:00
}
2025-03-07 18:13:24 +08:00
$weight = bcdiv ( $data [ 'food_weight' ], 100 , 2 );
$kcal = bcmul ( $weight , $food_data [ 'kcal' ], 2 );
2025-03-08 14:04:09 +08:00
return $this -> msg ([ 'value' => $kcal , 'unit' => 'kcal' ]);
}
2025-03-12 18:03:31 +08:00
public function find_food_action ( $data ){
2025-03-08 14:04:09 +08:00
$cfc = Db :: connect ( 'cfc_db' );
2025-07-23 21:32:05 +08:00
$food_data = $cfc -> query ( " select f3.id,f3.food_name as name,f3.pic_url,f3.Calorie_val as kcal,f3.weight_unit as unit,f2.id as up_one_level_id,f1.id as up_two_level_id
2025-03-12 18:03:31 +08:00
from " . $this->kitchenscale_db_msg ['foodlist3']. " as f3
LEFT JOIN " . $this->kitchenscale_db_msg ['foodlist2']. " as f2
on f3 . two_id = f2 . id
LEFT JOIN " . $this->kitchenscale_db_msg ['foodlist1']. " as f1
on f2 . one_id = f1 . id
2025-07-23 21:32:05 +08:00
where f3 . is_del = 0 AND f3 . food_name like '%".$data[' food_name ']."%'
2025-03-12 18:03:31 +08:00
" );
if ( count ( $food_data ) > 0 ){
2025-08-02 23:03:04 +08:00
for ( $i = 0 ; $i < count ( $food_data ); $i ++ ) {
if ( $food_data [ $i ][ 'pic_url' ] != '' ){
$food_data [ $i ][ 'pic_url' ] = 'https://tc.pcxbc.com/kitchenscale_all' . $food_data [ $i ][ 'pic_url' ];
}
}
2025-03-12 18:03:31 +08:00
return $this -> msg ( $food_data );
2025-03-08 14:04:09 +08:00
} else {
2025-03-14 18:46:26 +08:00
return $this -> msg ( 10004 , '未找到该食材' );
}
2025-08-02 23:03:04 +08:00
2025-03-14 18:46:26 +08:00
}
public function get_food_list_action ( $data ){
2025-07-23 21:32:05 +08:00
if ( ! array_key_exists ( 'page' , $data )){
$data [ 'page' ] = 1 ;
}
$cp_page_num = 30 ;
2025-03-14 18:46:26 +08:00
$cfc = Db :: connect ( 'cfc_db' );
2025-04-03 18:19:04 +08:00
$search_sql_str = " is_del = 0 AND two_id = " . $data [ 'food_level2_id' ];
if ( ! array_key_exists ( 'search_data' , $data )){
$data [ 'search_data' ] = " " ;
} else {
if ( $data [ 'search_data' ] === " " ){
$data [ 'search_data' ] = " " ;
} else {
2025-08-02 23:03:04 +08:00
$data [ 'search_data' ] = " AND food_name LIKE '% " . $data [ 'search_data' ] . " %' " ;
2025-03-14 18:46:26 +08:00
}
}
2025-04-03 18:19:04 +08:00
$search_sql_str = $search_sql_str . $data [ 'search_data' ];
2025-09-03 19:09:13 +08:00
2025-04-03 18:19:04 +08:00
$content_num = $cfc -> table ( $this -> kitchenscale_db_msg [ 'foodlist3' ])
-> where ( $search_sql_str )
-> count ();
$page_total = ceil ( $content_num / $cp_page_num );;
2025-09-03 19:09:13 +08:00
2025-04-03 18:19:04 +08:00
$collect_list = $cfc -> table ( $this -> kitchenscale_db_msg [ 'foodlist3' ])
-> where ( $search_sql_str )
2025-09-03 19:09:13 +08:00
-> field ( 'id,record_id,food_name as name,pic_url,Calorie_val as kcal,weight_unit as unit' )
2025-04-03 18:19:04 +08:00
-> page ( $data [ 'page' ], $cp_page_num )
-> select ();
2025-09-03 19:09:13 +08:00
$nutrients_list_id = [];
$nutrients_list_temporary = [];
2025-07-23 21:32:05 +08:00
foreach ( $collect_list as $key => $value ) {
$collect_list [ $key ][ 'pic_url' ] = " https://tc.pcxbc.com/kitchenscale_all " . $collect_list [ $key ][ 'pic_url' ];
2025-09-03 19:09:13 +08:00
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_msg [ 'foodlist4' ])
-> where ( " father_id in ( " . implode ( ',' , $nutrients_list_id ) . " ) " )
-> field ( " father_id,name,name_ch,unit,value,type " )
-> select ();
// dump($collect_list);
// die;
for ( $i = 0 ; $i < count ( $nutrients_list_data ); $i ++ ) {
if ( ! is_numeric ( $nutrients_list_data [ $i ][ 'value' ]) || $nutrients_list_data [ $i ][ 'value' ] == 0 ){
continue ;
} 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 ) {
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' ];
}
}
}
// 处理详情当中的环数据占比
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 ;
}
}
}
2025-07-23 21:32:05 +08:00
}
2025-04-03 18:19:04 +08:00
return $this -> msg ([
'page_now' => $data [ 'page' ],
'page_total' => $page_total ,
'content_list' => $collect_list
]);
2025-03-14 18:46:26 +08:00
}
public function get_cookbook_label_list_action (){
$cfc = Db :: connect ( 'cfc_db' );
// 获取菜谱分类标签start
$cook_label = $cfc -> table ( $this -> kitchenscale_db_msg [ 'cookbook_label' ])
-> where ( " is_del = 0 " )
-> field ( 'id,name' )
-> select ();
$temporary_label = [];
foreach ( $cook_label as $key => $value ) {
unset ( $cook_label [ $key ][ 'ROW_NUMBER' ]);
$cook_label [ $key ][ 'list' ] = [];
$temporary_label [ $value [ 'id' ]] = $key ;
}
$content_list = $cfc -> table ( $this -> kitchenscale_db_msg [ 'cookbook' ])
-> alias ( 'a' )
-> join ( $this -> kitchenscale_db_msg [ 'uploadimg' ] . ' b' , 'a.cover = b.id' , 'LEFT' )
-> where ([ 'a.is_del' => 0 ])
-> field ( 'a.id,a.cook_label,a.title,b.pic_url as cover' )
-> select ();
foreach ( $content_list as $key => $value ) {
unset ( $content_list [ $key ][ 'ROW_NUMBER' ]);
array_push ( $cook_label [ $temporary_label [ $value [ 'cook_label' ]]][ 'list' ], $content_list [ $key ]);
2025-03-08 14:04:09 +08:00
}
2025-03-14 18:46:26 +08:00
return $this -> msg ( $cook_label );
// 获取菜谱分类标签end
2025-01-22 12:31:08 +08:00
}
2025-07-23 21:32:05 +08:00
public function get_search_food_page_list_action ( $data ){
$cfc = Db :: connect ( 'cfc_db' );
// 获取菜谱分类标签start
// 蔬菜类id: 4 6 7
// 肉类id: 10 11 14
$data_list = $cfc -> table ( $this -> kitchenscale_db_msg [ 'foodlist3' ])
-> where ( 'two_id in (4,6,7,10,11,14) and is_popular = 1' )
2025-08-02 23:03:04 +08:00
-> field ( 'id,food_name,two_id' )
2025-07-23 21:32:05 +08:00
-> select ();
$result = [
'food' => [
'title' => '流行食材' ,
'list' => [
[
'title' => '蔬菜' ,
'list' => []
],
[
'title' => '肉类' ,
'list' => []
],
]
],
'log' => [],
];
foreach ( $data_list as $key => $value ) {
if ( $value [ 'two_id' ] == 4 || $value [ 'two_id' ] == 6 || $value [ 'two_id' ] == 7 ){
2025-08-02 23:03:04 +08:00
array_push ( $result [ 'food' ][ 'list' ][ 0 ][ 'list' ],[ 'id' => $value [ 'id' ], 'name' => $value [ 'food_name' ]]);
2025-07-23 21:32:05 +08:00
} else if ( $value [ 'two_id' ] == 10 || $value [ 'two_id' ] == 11 || $value [ 'two_id' ] == 14 ){
2025-08-02 23:03:04 +08:00
array_push ( $result [ 'food' ][ 'list' ][ 1 ][ 'list' ],[ 'id' => $value [ 'id' ], 'name' => $value [ 'food_name' ]]);
2025-07-23 21:32:05 +08:00
}
}
return $this -> msg ( $result );
// 获取菜谱分类标签end
}
2025-01-22 12:31:08 +08:00
2025-03-12 18:03:31 +08:00
2025-01-22 12:31:08 +08:00
2025-03-07 18:13:24 +08:00
#######################################################################小工具#######################################################################
#######################################################################小工具#######################################################################
#######################################################################小工具#######################################################################
// 处理食材的卡路里
public function count_calorie ( $data , $step ){
2025-04-03 18:19:04 +08:00
2025-07-23 21:32:05 +08:00
$foot_name = array_column ( $data , 'id' );
2025-03-07 18:13:24 +08:00
$foot_name = array_unique ( $foot_name );
$cfc = Db :: connect ( 'cfc_db' );
2025-07-23 21:32:05 +08:00
$foot_kcal = $cfc -> table ( $this -> kitchenscale_db_msg [ 'foodlist3' ]) -> where ( " id in (' " . implode ( " ',' " , $foot_name ) . " ') " ) -> field ( " id,food_name as name,Calorie_val as kcal " ) -> select ();
2025-03-07 18:13:24 +08:00
$foot_kcal2 = [];
foreach ( $foot_kcal as $key => $value ) {
2025-04-03 18:19:04 +08:00
$foot_kcal2 [ $value [ 'name' ]] = [ $value [ 'id' ], $value [ 'kcal' ]];
2025-03-07 18:13:24 +08:00
}
2025-04-03 18:19:04 +08:00
// dump($data);
// dump($foot_kcal2);
// die;
2025-03-07 18:13:24 +08:00
foreach ( $data as $key => $value ) {
if ( array_key_exists ( $value [ 'name' ], $foot_kcal2 )){
2025-04-03 18:19:04 +08:00
$data [ $key ][ 'id' ] = $foot_kcal2 [ $value [ 'name' ]][ 0 ];
$data [ $key ][ 'kcal' ] = $foot_kcal2 [ $value [ 'name' ]][ 1 ];
2025-03-07 18:13:24 +08:00
} else {
2025-04-03 18:19:04 +08:00
$data [ $key ][ 'kcal' ] = '0' ;
$data [ $key ][ 'id' ] = 0 ;
2025-03-07 18:13:24 +08:00
}
2025-04-03 18:19:04 +08:00
$data [ $key ][ 'unit' ] = 'g' ;
2025-03-07 18:13:24 +08:00
}
2025-04-03 18:19:04 +08:00
return $data ;
2025-03-07 18:13:24 +08:00
}
// 加 bcadd(,,20)
// 减 bcsub(,,20)
// 乘 bcmul(,,20)
// 除 bcdiv(,,20)
// 计算卡路里
public function count_calorie_action ( $weight , $kcal ){
$weight = bcdiv ( $weight , 100 , 2 );
return bcmul ( $weight , $kcal , 2 );
}
2025-01-22 12:31:08 +08:00
2025-04-03 18:19:04 +08:00
public function chuli_shuju (){
$cfc = Db :: connect ( 'cfc_db' );
$foot_kcal = $cfc -> table ( 'app_user_cookbook' ) -> field ( " food_data,id " ) -> select ();
$foot_name = [];
// dump($foot_kcal);
foreach ( $foot_kcal as $key => $value ) {
unset ( $foot_kcal [ $key ][ 'ROW_NUMBER' ]);
$foot_kcal [ $key ][ 'food_data' ] = json_decode ( $foot_kcal [ $key ][ 'food_data' ], true );
foreach ( $foot_kcal [ $key ][ 'food_data' ] as $k => $v ) {
$linshi_data = $v ;
// dump($linshi_data);
$foot_kcal [ $key ][ 'food_data' ][ $k ] = [];
$foot_kcal [ $key ][ 'food_data' ][ $k ][ 'name' ] = $linshi_data [ 'name' ];
$foot_kcal [ $key ][ 'food_data' ][ $k ][ 'weight' ] = $linshi_data [ 'weight' ];
$foot_kcal [ $key ][ 'food_data' ][ $k ][ 'unit' ] = 'g' ;
$foot_kcal [ $key ][ 'food_data' ][ $k ][ 'kcal' ] = rtrim ( $linshi_data [ 'kcal' ], " kcal " );
if ( ! in_array ( $linshi_data [ 'name' ], $foot_name )){
array_push ( $foot_name , $linshi_data [ 'name' ]);
}
}
// $foot_kcal[$key]['food_data'] = $foot_kcal[$key]['food_data'][0];
// $foot_kcal[$key]['food_data'] = json_encode($foot_kcal[$key]['food_data']);
}
$shicai = $cfc -> table ( $this -> kitchenscale_db_msg [ 'foodlist3' ]) -> where ( " name in (' " . implode ( " ',' " , $foot_name ) . " ') " ) -> field ( " id,name,kcal " ) -> select ();
$shicai2 = [];
foreach ( $shicai as $key => $value ) {
$shicai2 [ $value [ 'name' ]] = [ $value [ 'id' ], $value [ 'kcal' ]];
}
// dump($shicai2);
foreach ( $foot_kcal as $key => $value ) {
foreach ( $foot_kcal [ $key ][ 'food_data' ] as $k => $v ) {
$foot_kcal [ $key ][ 'food_data' ][ $k ][ 'kcal' ] = $shicai2 [ $v [ 'name' ]][ 1 ];
$foot_kcal [ $key ][ 'food_data' ][ $k ][ 'id' ] = $shicai2 [ $v [ 'name' ]][ 0 ];
}
// $foot_kcal[$key]['food_data'] = $foot_kcal[$key]['food_data'][0];
$foot_kcal [ $key ][ 'food_data' ] = json_encode ( $foot_kcal [ $key ][ 'food_data' ]);
}
// dump($foot_kcal);
// die;
foreach ( $foot_kcal as $key => $value ) {
$cfc -> table ( 'app_user_cookbook' ) -> where ([ 'id' => $value [ 'id' ]]) -> update ([ 'food_data' => $value [ 'food_data' ]]);
}
dump ( $foot_kcal );
die ;
}
2025-01-22 12:31:08 +08:00
}