SchoolPhysicalExamination/application/KitchenScale/controller/app/Cookbook.php

979 lines
44 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\KitchenScale\controller\app;
use think\Db;
class Cookbook extends Base{
// protected $token_time = 2592000;//30天的秒数
protected $default_head_pic = 'http://tc.pcxbc.com/tsf/head_pic.png';
protected $reedaw_db_msg = [
'zhanghao'=>'app_account_number',//账号表
'juese'=>'app_user_data',//角色表
];
protected $kitchenscale_db_msg = [
'cookbook'=>'app_user_cookbook',//菜谱表
'cookbook_label'=>'app_user_cookbook_label',//菜谱标签表
'uploadimg'=>'app_user_upload_img',//素材表
'followlist'=>'app_user_follow_list',//关注列表
'collect_list'=>'app_user_collect_list',//收藏列表
'foodlist1'=>'app_food_type_one',//食材列表3
'foodlist2'=>'app_food_type_two',//食材列表3
'foodlist3'=>'app_food_type_three',//食材列表3
'user_kcal_log'=>'app_user_kcal_log',//食材列表3
];
// 加 bcadd(,,20)
// 减 bcsub(,,20)
// 乘 bcmul(,,20)
// 除 bcdiv(,,20)
################################################################接口################################################################
################################################################接口################################################################
################################################################接口################################################################
// 添加菜谱(OK)
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');
}
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');
}
$return_data = $this->add_cookbook_action($data);
return $return_data;
// } catch (\Exception $e) {
// // 捕获异常
// $logContent["file"] = $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 find_by_cook_label($data=['token'=>'caadd1be045a65f3','cook_label'=>2,'page'=>'1']){
// 尝试捕获异常
// try {
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('token', $data)){
return $this->msg(10001,'token is miss');
}
if(!array_key_exists('cook_label', $data)){
return $this->msg(10001,'cook_label 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['cook_label'],'intnum')){
return $this->msg(10005,'cook_label type is error');
}
if(!$this->verify_data_is_ok($data['page'],'intnum')){
return $this->msg(10005,'page type is error');
}
$return_data = $this->find_by_cook_label_action($data);
return $return_data;
// } catch (\Exception $e) {
// // 捕获异常
// $logContent["file"] = $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 find_by_food($data=['token'=>'caadd1be045a65f3','food_name'=>'猪肉','page'=>'1']){
// 尝试捕获异常
// try {
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('token', $data)){
return $this->msg(10001,'token is miss');
}
if(!array_key_exists('food_name', $data)){
return $this->msg(10001,'food_name 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['food_name'],'str')){
return $this->msg(10005,'food_name type is error');
}
if(!$this->verify_data_is_ok($data['page'],'intnum')){
return $this->msg(10005,'page type is error');
}
$return_data = $this->find_by_food_action($data);
return $return_data;
// } catch (\Exception $e) {
// // 捕获异常
// $logContent["file"] = $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 cookbook_details($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','cookbook_id'=>'21']){
// 尝试捕获异常
// 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('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_details_action($data);
return $return_data;
// } catch (\Exception $e) {
// // 捕获异常
// $logContent["file"] = $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 cookbook_follow($data=['token'=>'caadd1be045a65f3','being_followed'=>'caadd1be045a65f30b92aa805f1de54a']){
// 尝试捕获异常
// 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('being_followed', $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['being_followed'],'str')){
return $this->msg(10005,'cookbook_id type is error');
}
$return_data = $this->cookbook_follow_action($data);
return $return_data;
// } catch (\Exception $e) {
// // 捕获异常
// $logContent["file"] = $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 cookbook_like($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','cookbook_id'=>'12']){
// 尝试捕获异常
// 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('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;
// } catch (\Exception $e) {
// // 捕获异常
// $logContent["file"] = $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 food_count_kcal($data=['food_name'=>'鸡肉','food_weight'=>456.37]){
// 尝试捕获异常
// try {
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('food_name', $data)){
return $this->msg(10001,'food_name is miss');
}
if(!array_key_exists('food_weight', $data)){
return $this->msg(10001,'food_weight is miss');
}
if(!$this->verify_data_is_ok($data['food_name'],'str')){
return $this->msg(10005,'food_name type is error');
}
if(!$this->verify_data_is_ok($data['food_weight'],'num')){
return $this->msg(10005,'food_weight type is error');
}
$return_data = $this->food_count_kcal_action($data);
return $return_data;
// } catch (\Exception $e) {
// // 捕获异常
// $logContent["file"] = $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 find_food($data=['food_name'=>'鸡肉']){
// 尝试捕获异常
// try {
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('food_name', $data)){
return $this->msg(10001,'food_name is miss');
}
if(!$this->verify_data_is_ok($data['food_name'],'str')){
return $this->msg(10005,'food_name type is error');
}
$return_data = $this->find_food_action($data);
return $return_data;
// } catch (\Exception $e) {
// // 捕获异常
// $logContent["file"] = $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_food_list($data=['token'=>'caadd1be045a65f30b92aa805f1de54a']){
// 尝试捕获异常
// try {
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_food_list_action($data);
return $return_data;
// } catch (\Exception $e) {
// // 捕获异常
// $logContent["file"] = $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' => '系统错误']);
// }
}
// 获取所有食谱label
public function get_cookbook_label_list($data=['token'=>'caadd1be045a65f30b92aa805f1de54a']){
// 尝试捕获异常
// try {
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;
// } catch (\Exception $e) {
// // 捕获异常
// $logContent["file"] = $e->getFile();
// $logContent["line"] = $e->getLine();
// $logContent['all_content'] = "异常信息:\n";
// $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n";
// $logContent['all_content'] .= "代码: " . $e->getCode() . "\n";
// $logContent['all_content'] .= "文件: " . $e->getFile() . "\n";
// $logContent['all_content'] .= "行号: " . $e->getLine() . "\n";
// $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n";
// // 记录日志
// // $this->record_api_log($data, $logContent, null);
// return json(['status' => 'error', 'message' => '系统错误']);
// }
}
#######################################################################action#######################################################################
#######################################################################action#######################################################################
#######################################################################action#######################################################################
public function add_cookbook_action($data){
// 获取账号下信息以及用户信息
$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,'账号信息错误');
}
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) {
if (!array_key_exists('name', $value) || !array_key_exists('weight', $value)) {
return $this->msg(10001,'食材缺少名称或者重量');
}
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,'食材重量格式错误,需整数数字');
}
if(!in_array($value['name'], $food_type)){
array_push($food_type, $value['name']);
}
}
// 检验一下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();
// 处理食材卡路里start
$kcal_data = $this->count_calorie($data['food_list'],$data['step_list']);
$data['food_list'] =$kcal_data;
// 处理食材卡路里end
$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']),
'food_type'=>implode(',', $food_type),
'cook_label'=>$data['cook_label'],
'create_time'=>date('Y-m-d H:i:s')
];
// dump($insert_data);
// die;
$cook_book_result = $cfc->table($this->kitchenscale_db_msg['cookbook'])->insert($insert_data);
if($cook_book_result){
return $this->msg([]);
}else{
return $this->msg(10002);
}
}
public function find_by_cook_label_action($data){
// 获取账号下信息以及用户信息
$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,'账号信息错误');
}
$page_now = $data['page'];
$page_total = $data['page'];
$page_num = 20;
$cook_label = $data['cook_label'];
$cfc = Db::connect('cfc_db');
$content_num = $cfc->table($this->kitchenscale_db_msg['cookbook'])
->where(['cook_label'=>$cook_label])
->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.cook_label'=>$cook_label])
->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();
// 获取用户收藏列表
$my_collect_list = $cfc->table($this->kitchenscale_db_msg['collect_list'])
->where(['token'=>$data['token']])
->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']);
}
return $this->msg([
'page_now'=>$page_now,
'page_total'=>$page_total,
'content_list'=>$content_list
]);
}
public function find_by_food_action($data){
// 获取账号下信息以及用户信息
$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,'账号信息错误');
}
$page_now = $data['page'];
$page_total = $data['page'];
$page_num = 20;
$food_name = $data['food_name'];
$cfc = Db::connect('cfc_db');
$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();
// 获取用户收藏列表
$my_collect_list = $cfc->table($this->kitchenscale_db_msg['collect_list'])
->where(['token'=>$data['token']])
->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']);
}
return $this->msg([
'page_now'=>$page_now,
'page_total'=>$page_total,
'content_list'=>$content_list
]);
}
public function cookbook_details_action($data){
// 获取账号下信息以及用户信息
$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,'账号信息错误');
}
$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){
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_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);
}
}
}
}
$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'];
}
// 设置菜谱图片
foreach ($cookbook_data['step_data'] as $key => $value) {
$cookbook_data['step_data'][$key]['pic_list_ls'] = [];
foreach ($value['pic_list'] as $k => $v) {
if(array_key_exists($v, $cookbook_img_data)){
array_push($cookbook_data['step_data'][$key]['pic_list_ls'],$cookbook_img_data[$v]);
}else{
array_push($cookbook_data['step_data'][$key]['pic_list_ls'],'https://tc.pcxbc.com/kitchenscale_all/diule.jpg');
}
}
$cookbook_data['step_data'][$key]['pic_list'] = $cookbook_data['step_data'][$key]['pic_list_ls'];
unset($cookbook_data['step_data'][$key]['pic_list_ls']);
$cookbook_data['step_data'][$key]['step_num'] = "步骤".($key+1).'/'.$cookbook_data_step_data_count;
}
if(array_key_exists($cookbook_data['cover'], $cookbook_img_data)){
$cookbook_data['cover'] = $cookbook_img_data[$cookbook_data['cover']];
}else{
$cookbook_data['cover'] = 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg';
}
// // 处理关注跟收藏信息
// if($data['token'] == $cookbook_data['create_user_token']){
// // 如果查询跟作者一致
// $cookbook_data['follow_status'] = 'myself';
// $cookbook_data['collect_status'] = 'myself';
// }else{
// $follow_data = $cfc->table($this->kitchenscale_db_msg['followlist'])
// ->where([
// 'follow_user_token'=>$data['token'],
// 'being_follow_user_token'=>$cookbook_data['create_user_token'],
// ])
// ->find();
// if($follow_data){
// if($follow_data['is_del'] == 0){
// // 如果有结果并且没被删过
// $cookbook_data['follow_status'] = 'yes';
// }else{
// // 如果有结果被删过
// $cookbook_data['follow_status'] = 'no';
// }
// }else{
// // 如果没结果
// $cookbook_data['follow_status'] = 'no';
// }
$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';
}
}else{
// 如果没结果
$cookbook_data['collect_status'] = 'no';
}
// }
// 添加阅读量
// $read_num = $cfc->table($this->kitchenscale_db_msg['cookbook'])->where(['id'=>$data['cookbook_id']])->setInc('read_it');
// if($read_num){
// $cookbook_data['read_it'] = $cookbook_data['read_it']+1;
// }
// unset($cookbook_data['create_user_token']);
unset($cookbook_data['create_time']);
unset($cookbook_data['cook_label']);
unset($cookbook_data['food_type']);
unset($cookbook_data['ROW_NUMBER']);
return $this->msg($cookbook_data);
}
public function cookbook_follow_action($data){
// 获取账号下信息以及用户信息
$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,'账号信息错误');
}
// dump($data);
$cfc = Db::connect('cfc_db');
// $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']){
// 如果查询跟作者一致
return $this->msg(10003,'不能关注自己');
}
$follow_data = $cfc->table($this->kitchenscale_db_msg['followlist'])
->where([
'follow_user_token'=>$data['token'],
'being_follow_user_token'=>$data['being_followed'],
])
->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'])
->where(['id'=>$follow_data['id']])
->update(['is_del'=>$follow_data_state]);
}else{
$follow_result = $cfc->table($this->kitchenscale_db_msg['followlist'])
->insert([
'follow_user_token'=>$data['token'],
'being_follow_user_token'=>$data['being_followed'],
'create_time'=>date('Y-m-d H:i:s')
]);
}
if($follow_result){
return $this->msg([]);
}else{
return $this->msg(10002,'操作失败');
}
}
public function cookbook_like_action($data){
// 获取账号下信息以及用户信息
$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,'账号信息错误');
}
$cfc = Db::connect('cfc_db');
$cookbook_data = $cfc->table($this->kitchenscale_db_msg['cookbook'])->where(['id'=>$data['cookbook_id']])->field('id,create_user_token,likes_num')->find();
if(!$cookbook_data){
return $this->msg(10004,'未找到菜谱');
}
// return $this->msg(10002,'是这里');
// if($data['token'] == $cookbook_data['create_user_token']){
// // 如果查询跟作者一致
// return $this->msg(10002,'不能收藏自己');
// }
$like_data = $cfc->table($this->kitchenscale_db_msg['collect_list'])->where(['token'=>$data['token'],'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{
$cfc->table($this->kitchenscale_db_msg['collect_list'])
->where(['id'=>$like_data['id']])
->update(['is_del'=>$like_data_state]);
if($like_data_state == 0){
$cfc->table($this->kitchenscale_db_msg['cookbook'])->where(['id'=>$data['cookbook_id']])->setInc('likes_num');
}else{
$cfc->table($this->kitchenscale_db_msg['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{
$cfc->table($this->kitchenscale_db_msg['collect_list'])
->insert([
'token'=>$data['token'],
'cookbook_id'=>$data['cookbook_id'],
'create_time'=>date('Y-m-d H:i:s')
]);
$cfc->table($this->kitchenscale_db_msg['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 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){
return $this->msg(10004,'未登记的食材');
}
$weight = bcdiv($data['food_weight'],100,2);
$kcal = bcmul($weight,$food_data['kcal'],2);
return $this->msg(['value'=>$kcal,'unit'=>'kcal']);
}
public function find_food_action($data){
$cfc = Db::connect('cfc_db');
$food_data = $cfc->query("select f3.id,f3.name,f3.kcal,f2.id as up_one_level_id,f1.id as up_two_level_id
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
where f3.is_del=0 AND f3.name like '%".$data['food_name']."%'
");
if(count($food_data)>0){
return $this->msg($food_data);
}else{
return $this->msg(10004,'未找到该食材');
}
}
public function get_food_list_action($data){
$cfc = Db::connect('cfc_db');
// 获取食材分类列表start
$foodlist1 = $cfc->table($this->kitchenscale_db_msg['foodlist1'])->where("is_del = 0")->field('id,name')->select();
$foodlist2 = $cfc->table($this->kitchenscale_db_msg['foodlist2'])->where("is_del = 0")->field('id,name,one_id')->select();
$foodlist3 = $cfc->table($this->kitchenscale_db_msg['foodlist3'])->where("is_del = 0")->field('id,name,two_id,kcal,unit')->select();
// dump($foodlist3);
foreach ($foodlist1 as $key => $value) {
unset($foodlist1[$key]['ROW_NUMBER']);
$foodlist1[$key]['list'] = [];
foreach ($foodlist2 as $k => $v) {
$foodlist2[$k]['list'] = [];
foreach ($foodlist3 as $k3 => $v3) {
if($v3['two_id'] == $v['id']){
unset($foodlist3[$k3]['ROW_NUMBER']);
$foodlist3[$k3]['one_id'] = $v['one_id'];
array_push($foodlist2[$k]['list'],$foodlist3[$k3]);
// unset($foodlist3[$k3]);
}
}
if($v['one_id'] == $value['id']){
unset($foodlist2[$k]['ROW_NUMBER']);
array_push($foodlist1[$key]['list'],$foodlist2[$k]);
// unset($foodlist2[$k]);
}
}
}
return $this->msg($foodlist1);
}
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]);
}
return $this->msg($cook_label);
// 获取菜谱分类标签end
}
#######################################################################小工具#######################################################################
#######################################################################小工具#######################################################################
#######################################################################小工具#######################################################################
// 处理食材的卡路里
public function count_calorie($data,$step){
$foot_name = array_column($data, 'name');
$foot_name = array_unique($foot_name);
$cfc = Db::connect('cfc_db');
$foot_kcal = $cfc->table($this->kitchenscale_db_msg['foodlist3'])->where("name in ('".implode("','", $foot_name)."')")->field("name,kcal")->select();
$foot_kcal2 = [];
foreach ($foot_kcal as $key => $value) {
$foot_kcal2[$value['name']] = $value['kcal'];
}
foreach ($data as $key => $value) {
if(array_key_exists($value['name'], $foot_kcal2)){
$data[$key]['kcal'] = $this->count_calorie_action($value['weight'],$foot_kcal2[$value['name']]).'kcal';
}else{
$data[$key]['kcal'] = '0kcal';
}
$data[$key]['weight'] = $data[$key]['weight'].'g';
}
// foreach ($step as $key => $value) {
// foreach ($value['foot_list'] as $k => $v) {
// if(array_key_exists($v['name'], $foot_kcal2)){
// $step[$key]['foot_list'][$k]['kcal'] = $this->count_calorie_action($v['weight'],$foot_kcal2[$v['name']]).'kcal';
// }else{
// $step[$key]['foot_list'][$k]['kcal'] = '0kcal';
// }
// $step[$key]['foot_list'][$k]['weight'] = $step[$key]['foot_list'][$k]['weight'].'g';
// }
// }
return [$data];
}
// 加 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);
}
}