diff --git a/application/KitchenScale/controller/app/Cookbook.php b/application/KitchenScale/controller/app/Cookbook.php index 40db4d3..2ec1575 100644 --- a/application/KitchenScale/controller/app/Cookbook.php +++ b/application/KitchenScale/controller/app/Cookbook.php @@ -18,6 +18,7 @@ class Cookbook extends Base{ 'uploadimg'=>'app_user_upload_img',//素材表 'followlist'=>'app_user_follow_list',//关注列表 'collect_list'=>'app_user_collect_list',//收藏列表 + 'foodlist3'=>'app_food_type_three',//食材列表3 ]; // 加 bcadd(,,20) @@ -28,7 +29,7 @@ class Cookbook extends Base{ ################################################################接口################################################################ ################################################################接口################################################################ - // 添加菜谱 + // 添加菜谱(OK) public function add_cookbook(){ // 尝试捕获异常 // try { @@ -130,7 +131,6 @@ class Cookbook extends Base{ // return json(['status' => 'error', 'message' => '系统错误']); // } } - // 根据食材详细查找列表(OK) public function find_by_food($data=['token'=>'caadd1be045a65f3','food_name'=>'猪肉','page'=>'1']){ // 尝试捕获异常 @@ -173,9 +173,8 @@ class Cookbook extends Base{ // return json(['status' => 'error', 'message' => '系统错误']); // } } - - // 查询食谱的详情 - public function cookbook_details($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','cookbook_id'=>'12']){ + // 查询食谱的详情(OK) + public function cookbook_details($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','cookbook_id'=>'17']){ // 尝试捕获异常 // try { if(count(input('post.')) > 0){ @@ -210,9 +209,8 @@ class Cookbook extends Base{ // return json(['status' => 'error', 'message' => '系统错误']); // } } - - // 关注菜谱 - public function cookbook_follow($data=['token'=>'caadd1be045a65f3','cookbook_id'=>'12']){ + // 关注行为(OK) + public function cookbook_follow($data=['token'=>'caadd1be045a65f3','being_followed'=>'caadd1be045a65f30b92aa805f1de54a']){ // 尝试捕获异常 // try { if(count(input('post.')) > 0){ @@ -221,13 +219,13 @@ class Cookbook extends Base{ if(!array_key_exists('token', $data)){ return $this->msg(10001,'token is miss'); } - if(!array_key_exists('cookbook_id', $data)){ + 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['cookbook_id'],'intnum')){ + 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); @@ -247,8 +245,8 @@ class Cookbook extends Base{ // return json(['status' => 'error', 'message' => '系统错误']); // } } - // 收藏菜谱 - public function cookbook_like($data=['token'=>'caadd1be045a65f3','cookbook_id'=>'12']){ + // 点赞收藏菜谱(OK) + public function cookbook_like($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','cookbook_id'=>'12']){ // 尝试捕获异常 // try { if(count(input('post.')) > 0){ @@ -283,9 +281,8 @@ class Cookbook extends Base{ // return json(['status' => 'error', 'message' => '系统错误']); // } } - - // 计算根据菜谱称重 - public function cookbook_count_weight($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','cookbook_id'=>'12','food_weight'=>[]]){ + // 计算当前食材重量的卡路里 + public function food_count_kcal($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','food_name'=>'鸡肉','food_weight'=>456.37]){ // 尝试捕获异常 // try { if(count(input('post.')) > 0){ @@ -294,8 +291,8 @@ class Cookbook extends Base{ if(!array_key_exists('token', $data)){ return $this->msg(10001,'token is miss'); } - if(!array_key_exists('cookbook_id', $data)){ - return $this->msg(10001,'cookbook_id is miss'); + if(!array_key_exists('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'); @@ -303,11 +300,14 @@ class Cookbook extends Base{ 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')){ + if(!$this->verify_data_is_ok($data['food_name'],'str')){ + return $this->msg(10005,'cook_label type is error'); + } + if(!$this->verify_data_is_ok($data['food_weight'],'num')){ return $this->msg(10005,'cook_label type is error'); } - $return_data = $this->cookbook_count_weight_action($data); + $return_data = $this->food_count_kcal_action($data); return $return_data; // } catch (\Exception $e) { // // 捕获异常 @@ -324,7 +324,8 @@ class Cookbook extends Base{ // return json(['status' => 'error', 'message' => '系统错误']); // } } - + #######################################################################action####################################################################### + #######################################################################action####################################################################### #######################################################################action####################################################################### public function add_cookbook_action($data){ @@ -341,7 +342,7 @@ class Cookbook extends Base{ return $this->msg(10005,'食材缺少名称或者重量'); } if(!$this->verify_data_is_ok($value['name'],'str')){ - return $this->msg(10005,'食材名称格式错误,需字符串'); + return $this->msg(10005,'食材名称格式错误'); } if(!$this->verify_data_is_ok($value['weight'],'intnum')){ return $this->msg(10005,'食材重量格式错误,需整数数字'); @@ -350,7 +351,6 @@ class Cookbook extends Base{ } // 检验一下step_list是否合规(步骤列表) foreach ($data['step_list'] as $key => $value) { - // if (!array_key_exists('description', $value) || !array_key_exists('foot_list', $value) || !array_key_exists('pic_list', $value)) { if (!array_key_exists('description', $value) || !array_key_exists('pic_list', $value)) { return $this->msg(10005,'步骤缺少描述或者图片'); } @@ -363,17 +363,6 @@ class Cookbook extends Base{ return $this->msg(10005,'步骤中图片ID错误,需整数数字'); } } - // foreach ($value['foot_list'] as $k => $v) { - // if (!array_key_exists('name', $v) || !array_key_exists('weight', $v)) { - // return $this->msg(10005,'步骤食材缺少名称或者重量'); - // } - // if(!$this->verify_data_is_ok($v['name'],'str')){ - // return $this->msg(10005,'步骤食材名称格式错误,需字符串'); - // } - // if(!$this->verify_data_is_ok($v['weight'],'intnum')){ - // return $this->msg(10005,'步骤食材重量格式错误,需整数数字'); - // } - // } } $cfc = Db::connect('cfc_db'); @@ -381,14 +370,9 @@ class Cookbook extends Base{ $user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->field('id,token,nickname,head_pic')->find(); // 处理食材卡路里start - foreach ($data['food_list'] as $key => $value) { - $data['food_list'][$key]['kcal'] = '0kcal'; - } - // foreach ($data['step_list'] as $key => $value) { - // foreach ($value['foot_list'] as $k => $v) { - // $data['step_list'][$key]['foot_list'][$k]['kcal'] = '0kcal'; - // } - // } + $kcal_data = $this->count_calorie($data['food_list'],$data['step_list']); + $data['food_list'] =$kcal_data[0]; + $data['step_list'] = $kcal_data[1]; // 处理食材卡路里end $insert_data = [ @@ -412,7 +396,6 @@ class Cookbook extends Base{ return $this->msg(10002); } } - public function find_by_cook_label_action($data){ $page_now = $data['page']; $page_total = $data['page']; @@ -457,7 +440,6 @@ class Cookbook extends Base{ 'content_list'=>$content_list ]); } - public function find_by_food_action($data){ $page_now = $data['page']; $page_total = $data['page']; @@ -502,12 +484,9 @@ class Cookbook extends Base{ 'content_list'=>$content_list ]); } - public function cookbook_details_action($data){ $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){ @@ -516,10 +495,6 @@ class Cookbook extends Base{ $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']); - - dump($cookbook_data_step_data_count); - dump($cookbook_data); - die; // 设置需要的图片id array_push($img_arr, $cookbook_data['cover']); foreach ($cookbook_data['step_data'] as $key => $value) { @@ -532,7 +507,6 @@ class Cookbook extends Base{ } } $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 = []; @@ -540,7 +514,6 @@ class Cookbook extends Base{ 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'] = []; @@ -560,13 +533,11 @@ class Cookbook extends Base{ }else{ $cookbook_data['cover'] = 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg'; } - - // 处理关注跟收藏信息 if($data['token'] == $cookbook_data['create_user_token']){ // 如果查询跟作者一致 - $cookbook_data['follow_status'] = 3; - $cookbook_data['collect_status'] = 3; + $cookbook_data['follow_status'] = 'myself'; + $cookbook_data['collect_status'] = 'myself'; }else{ $follow_data = $cfc->table($this->kitchenscale_db_msg['followlist']) ->where([ @@ -577,14 +548,14 @@ class Cookbook extends Base{ if($follow_data){ if($follow_data['is_del'] == 0){ // 如果有结果并且没被删过 - $cookbook_data['follow_status'] = 1; + $cookbook_data['follow_status'] = 'yes'; }else{ // 如果有结果被删过 - $cookbook_data['follow_status'] = 0; + $cookbook_data['follow_status'] = 'no'; } }else{ // 如果没结果 - $cookbook_data['follow_status'] = 0; + $cookbook_data['follow_status'] = 'no'; } $collect_data = $cfc->table($this->kitchenscale_db_msg['collect_list']) @@ -596,52 +567,43 @@ class Cookbook extends Base{ if($collect_data){ if($collect_data['is_del'] == 0){ // 如果有结果并且没被删过 - $cookbook_data['collect_status'] = 1; + $cookbook_data['collect_status'] = 'yes'; }else{ // 如果有结果被删过 - $cookbook_data['collect_status'] = 0; + $cookbook_data['collect_status'] = 'no'; } }else{ // 如果没结果 - $cookbook_data['collect_status'] = 0; + $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_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); - // dump($cookbook_data); - } - - public function cookbook_follow_action($data){ // 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'] == $cookbook_data['create_user_token']){ + // $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(10002,'不能关注自己'); } $follow_data = $cfc->table($this->kitchenscale_db_msg['followlist']) ->where([ 'follow_user_token'=>$data['token'], - 'being_follow_user_token'=>$cookbook_data['create_user_token'], + 'being_follow_user_token'=>$data['being_followed'], ]) ->find(); $follow_data_state = 0; @@ -652,39 +614,33 @@ class Cookbook extends Base{ }else{ $follow_data_state = 0; } - $follow_result= $cfc->table($this->kitchenscale_db_msg['followlist']) - ->where([ - 'follow_user_token'=>$data['token'], - 'being_follow_user_token'=>$cookbook_data['create_user_token'], - ]) + ->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'=>$cookbook_data['create_user_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(10001,'操作失败'); } } - public function cookbook_like_action($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'] == $cookbook_data['create_user_token']){ - // 如果查询跟作者一致 - 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'], @@ -700,15 +656,11 @@ class Cookbook extends Base{ }else{ $like_data_state = 0; } - // 启动事务 Db::startTrans(); try{ $cfc->table($this->kitchenscale_db_msg['collect_list']) - ->where([ - 'token'=>$data['token'], - 'cookbook_id'=>$data['cookbook_id'], - ]) + ->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('like_it'); @@ -744,28 +696,69 @@ class Cookbook extends Base{ } } } - - public function cookbook_count_weight_action($data){ - $data['food_weight'] = [ - [ - 'food_name'=>'牛肉', - 'food_weight'=>'100', - 'food_kcal'=>'190', - ], - [ - 'food_name'=>'狗肉', - 'food_weight'=>'100', - 'food_kcal'=>'116', - ], - ]; - foreach ($variable as $key => $value) { - if(!array_key_exists('food_name', $data['food_weight']) || !array_key_exists('food_weight', $data['food_weight']) || !array_key_exists('food_kcal', $data['food_weight'])){ - return $this->msg(10001,'食材称重参数错误'); - } + 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(10002,'未登记的食材'); } + $weight = bcdiv($data['food_weight'],100,2); + $kcal = bcmul($weight,$food_data['kcal'],2); + return $this->msg(['kcal'=>$kcal.'kcal']); } + #######################################################################小工具####################################################################### + #######################################################################小工具####################################################################### + #######################################################################小工具####################################################################### + + // 处理食材的卡路里 + 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,$step]; + } + + + // 加 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); + } + } \ No newline at end of file diff --git a/application/app/controller/Login.php b/application/app/controller/Login.php index b3bda95..36e4cff 100644 --- a/application/app/controller/Login.php +++ b/application/app/controller/Login.php @@ -387,7 +387,7 @@ class Login extends Base{ $postData = array( 'phone' => $tel, // 'content' => '您好,欢迎使用Reedaw,您的手机验证码是:'.$code.',验证码三分钟内有效,若非本人操作,请忽略!' - 'content' => '【瑞达沃】您好,欢迎使用Reedaw,您的验证码是:'.$code.',验证码一分钟内有效,若非本人操作,请忽略本短信' + 'content' => '【Reedaw】您好,欢迎使用Reedaw,您的验证码是:'.$code.',验证码一分钟内有效,若非本人操作,请忽略本短信' ); $postData = json_encode($postData); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); @@ -403,6 +403,7 @@ class Login extends Base{ // 关闭cURL会话 curl_close($ch); // 处理响应 + if ($response) { return json_decode($response,true); } else { diff --git a/application/route.php b/application/route.php index af83289..9686583 100644 --- a/application/route.php +++ b/application/route.php @@ -447,7 +447,9 @@ Route::any('/testedition/kitchenscale/cookbook_follow', 'app/kitchenscale/testap // 收藏菜谱 Route::any('/kitchenscale/cookbook_like', 'app/kitchenscale/app.cookbook/cookbook_like'); Route::any('/testedition/kitchenscale/cookbook_like', 'app/kitchenscale/testapp.cookbook/cookbook_like'); - +// 获取当前食材重量卡路里 +Route::any('/kitchenscale/food_count_kcal', 'app/kitchenscale/app.cookbook/food_count_kcal'); +Route::any('/testedition/kitchenscale/food_count_kcal', 'app/kitchenscale/testapp.cookbook/food_count_kcal'); diff --git a/public/kitchenscale_all/ceshi.html b/public/kitchenscale_all/ceshi.html index 721b45f..54f74eb 100644 --- a/public/kitchenscale_all/ceshi.html +++ b/public/kitchenscale_all/ceshi.html @@ -230,6 +230,8 @@ console.log(pair[0] + ': ' + pair[1]); } + console.log(submit_data); + return $.ajax({ url: "http://wm.tcxbc.com/kitchenscale/add_cookbook", // 请求的url地址 contentType: false, diff --git a/public/kitchenscale_all/upload.html b/public/kitchenscale_all/upload.html index 0d909bb..fc057ef 100644 --- a/public/kitchenscale_all/upload.html +++ b/public/kitchenscale_all/upload.html @@ -29,7 +29,7 @@ width: 95vw; height: 100vw; background-color: #ccc; - position: absolute; + position: fixed; top: 20%; left: 0; right: 0; @@ -100,7 +100,7 @@