diff --git a/application/KitchenScale/controller/app/Base.php b/application/KitchenScale/controller/app/Base.php index 664871e..480f6cb 100644 --- a/application/KitchenScale/controller/app/Base.php +++ b/application/KitchenScale/controller/app/Base.php @@ -247,6 +247,45 @@ class Base extends Controller{ $return_result['result'] = $result; return $this->msg($return_result); } + public function pic_upload_one_action(){ + $temporary_data = []; + $cfc = Db::connect('cfc_db'); + $file = request()->file('image'); + $token = request()->param('token'); + if(!$token){ + return $this->msg(10001,'token is miss'); + } + if($file){ + $name = $file->getInfo()['name']; + // 使用 pathinfo() 函数获取文件名的扩展名 + $pathinfo = pathinfo($name); + $extension = strtolower($pathinfo['extension']); // 转换为小写以进行不区分大小写的比较 + $new_filename = time().$this->generateRandomString(). '.' . $extension; + $info = $file->validate(['size'=>$this->file_size,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'kitchenscale_all' . DS . 'user_upload',$new_filename); + if($info){ + $temporary_data = [ + 'user_token'=>$token, + 'pic_name'=>$new_filename, + 'pic_url'=>"https://tc.pcxbc.com/kitchenscale_all/user_upload/".$new_filename, + 'create_time'=>date('Y-m-d H:i:s'), + ]; + $pic_id = $cfc->table('app_user_upload_img')->insertGetId($temporary_data); + if($pic_id){ + $temporary_data['id'] = $pic_id; + unset($temporary_data['create_time']); + unset($temporary_data['user_token']); + return $this->msg($temporary_data); + }else{ + return $this->msg(10002); + } + }else{ + // 上传失败获取错误信息 + return $this->msg(10010,$file->getError()); + } + }else{ + return $this->msg(10001,'未选择图片'); + } + } public function pic_upload_action(){ $insert_data = []; $temporary_data = []; @@ -257,10 +296,10 @@ class Base extends Controller{ if(!$token){ return $this->msg(10001,'token is miss'); } - if(count($files)>5){ - return $this->msg(10001,'单次最多上传5张图片'); - } if($files){ + if(count($files)>5){ + return $this->msg(10001,'单次最多上传5张图片'); + } foreach($files as $file){ $name = $file->getInfo()['name']; // 使用 pathinfo() 函数获取文件名的扩展名 @@ -302,7 +341,7 @@ class Base extends Controller{ return $this->msg(10001,'未选择图片'); } } - + ####################################################图片选择上传end############################################################## public function msg($data,$str='',$result = []){ if(is_array($data)){ @@ -328,6 +367,7 @@ class Base extends Controller{ } return $randomString; } + public function ceshi(){ echo 'hello'; diff --git a/application/KitchenScale/controller/app/Cookbook.php b/application/KitchenScale/controller/app/Cookbook.php index da2c657..d2737ce 100644 --- a/application/KitchenScale/controller/app/Cookbook.php +++ b/application/KitchenScale/controller/app/Cookbook.php @@ -9,6 +9,7 @@ class Cookbook extends Base{ // protected $token_time = 2592000;//30天的秒数 protected $default_head_pic = 'http://tc.pcxbc.com/tsf/head_pic.png'; + protected $page_num = 10; protected $reedaw_db_msg = [ 'zhanghao'=>'app_account_number',//账号表 'juese'=>'app_user_data',//角色表 @@ -23,7 +24,7 @@ class Cookbook extends Base{ 'foodlist2'=>'app_food_type_two',//食材列表3 'foodlist3'=>'app_food_type_three',//食材列表3 'user_kcal_log'=>'app_user_kcal_log',//食材列表3 - + 'user'=>'app_user_data',//banner ]; // 加 bcadd(,,20) @@ -97,8 +98,8 @@ class Cookbook extends Base{ // return json(['status' => 'error', 'message' => '系统错误']); // } } - // 根据菜谱标签查询列表(首页用)(OK) - public function find_by_cook_label($data=['token'=>'caadd1be045a65f3','cook_label'=>2,'page'=>'1']){ + // 修改菜谱(OK) + public function update_cookbook(){ // 尝试捕获异常 // try { if(count(input('post.')) > 0){ @@ -107,15 +108,81 @@ class Cookbook extends Base{ 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(!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['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; + // } 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'=>'','cook_label'=>4,'page'=>'1']){ + // 尝试捕获异常 + // try { + if(count(input('post.')) > 0){ + $data = input('post.'); + } + 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'); } @@ -182,21 +249,15 @@ class Cookbook extends Base{ // } } // 查询食谱的详情(OK) - public function cookbook_details($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','cookbook_id'=>'21']){ + public function cookbook_details($data=['token'=>'','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'); } @@ -358,17 +419,23 @@ class Cookbook extends Base{ // } } // 获取所有食材列表 - public function get_food_list($data=['token'=>'caadd1be045a65f30b92aa805f1de54a']){ + public function get_food_list($data=['food_level2_id'=>'4','search_data'=>'','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_level2_id', $data)){ + return $this->msg(10001,'food_level2_id is miss'); } - if(!$this->verify_data_is_ok($data['token'],'str')){ - return $this->msg(10005,'token type is error'); + if(!array_key_exists('page', $data)){ + return $this->msg(10001,'page is miss'); + } + if(!$this->verify_data_is_ok($data['food_level2_id'],'intnum')){ + return $this->msg(10005,'food_level2_id type is error'); + } + if(!$this->verify_data_is_ok($data['page'],'intnum')){ + return $this->msg(10005,'page type is error'); } $return_data = $this->get_food_list_action($data); @@ -476,7 +543,8 @@ class Cookbook extends Base{ $data['food_list'] =$kcal_data; // 处理食材卡路里end - + // dump($data['food_list']); + // die; $insert_data = [ 'title'=>$data['title'], 'cover'=>$data['cover'], @@ -500,12 +568,89 @@ class Cookbook extends Base{ return $this->msg(10002); } } - public function find_by_cook_label_action($data){ + public function update_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(); + // 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']), + 'food_type'=>implode(',', $food_type), + 'cook_label'=>$data['cook_label'], + ]; + + $cook_book_result = $cfc->table($this->kitchenscale_db_msg['cookbook'])->where(['id'=>$data['cookbook_id']])->update($insert_data); + + // 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){ + $page_now = $data['page']; $page_total = $data['page']; $page_num = 20; @@ -513,36 +658,64 @@ class Cookbook extends Base{ $cfc = Db::connect('cfc_db'); $content_num = $cfc->table($this->kitchenscale_db_msg['cookbook']) - ->where(['cook_label'=>$cook_label]) + ->where(['cook_label'=>$cook_label,'is_del'=>0]) ->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') + ->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') + ->order('id DESC') ->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'; + + 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']); + } }else{ + foreach ($content_list as $key => $value) { + $content_list[$key]['is_me_like_it'] = 'no'; + if($value['cover'] == null){ + $content_list[$key]['cover'] = 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg'; + } + unset($content_list[$key]['ROW_NUMBER']); + } + } + }else{ + foreach ($content_list as $key => $value) { $content_list[$key]['is_me_like_it'] = 'no'; + if($value['cover'] == null){ + $content_list[$key]['cover'] = 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg'; + } + unset($content_list[$key]['ROW_NUMBER']); } - if($value['cover'] == null){ - $content_list[$key]['cover'] = 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg'; - } - unset($content_list[$key]['ROW_NUMBER']); } + // dump($content_list); + // die; return $this->msg([ 'page_now'=>$page_now, 'page_total'=>$page_total, @@ -599,11 +772,7 @@ class Cookbook extends Base{ ]); } 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 = []; // 查询菜谱详情 @@ -613,9 +782,11 @@ 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['food_type'] = explode(',',$cookbook_data['food_type']); $cookbook_data_step_data_count = count($cookbook_data['step_data']); // 设置需要的图片id array_push($img_arr, $cookbook_data['cover']); + foreach ($cookbook_data['step_data'] as $key => $value) { if(count($value['pic_list']) > 0){ foreach ($value['pic_list'] as $k => $v) { @@ -625,6 +796,8 @@ class Cookbook extends Base{ } } } + // dump($cookbook_data); + // die; $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(); @@ -633,79 +806,75 @@ 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'] = []; + $cookbook_data['step_data'][$key]['step_num'] = "步骤".($key+1).'/'.$cookbook_data_step_data_count; + $cookbook_data['step_data'][$key]['pic_url_list'] = []; foreach ($value['pic_list'] as $k => $v) { if(array_key_exists($v, $cookbook_img_data)){ - array_push($cookbook_data['step_data'][$key]['pic_list_ls'],$cookbook_img_data[$v]); + array_push($cookbook_data['step_data'][$key]['pic_url_list'],$cookbook_img_data[$v]); }else{ - array_push($cookbook_data['step_data'][$key]['pic_list_ls'],'https://tc.pcxbc.com/kitchenscale_all/diule.jpg'); + array_push($cookbook_data['step_data'][$key]['pic_url_list'],'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; } + + // $cookbook_data['cover_id'] = $cookbook_data['cover']; if(array_key_exists($cookbook_data['cover'], $cookbook_img_data)){ - $cookbook_data['cover'] = $cookbook_img_data[$cookbook_data['cover']]; + $cookbook_data['cover_pic_url'] = $cookbook_img_data[$cookbook_data['cover']]; + }else{ - $cookbook_data['cover'] = 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg'; + $cookbook_data['cover_pic_url'] = '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'; + 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'; + } }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']); + }else{ + $cookbook_data['collect_status'] = 'no'; + } + + + + $cookbook_data['token'] = $cookbook_data['create_user_token']; + $cookbook_data['description'] = $cookbook_data['describe_data']; + $cookbook_data['food_list'] = $cookbook_data['food_data']; + $cookbook_data['step_list'] = $cookbook_data['step_data']; + unset($cookbook_data['create_user_token']); + unset($cookbook_data['describe_data']); + unset($cookbook_data['food_data']); + unset($cookbook_data['step_data']); unset($cookbook_data['create_time']); - unset($cookbook_data['cook_label']); unset($cookbook_data['food_type']); unset($cookbook_data['ROW_NUMBER']); + unset($cookbook_data['is_del']); return $this->msg($cookbook_data); } public function cookbook_follow_action($data){ @@ -866,33 +1035,65 @@ class Cookbook extends Base{ } } public function get_food_list_action($data){ + $cp_page_num = 100; $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]); - } + // $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(); + + $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{ + $data['search_data'] = " AND name LIKE '%".$data['search_data']."%'"; } } - return $this->msg($foodlist1); + $search_sql_str = $search_sql_str.$data['search_data']; + + // dump($data); + // dump($search_sql_str); + $content_num = $cfc->table($this->kitchenscale_db_msg['foodlist3']) + // ->alias('a') + // ->join($this->kitchenscale_db_msg['foodlist2'].' b','a.two_id = b.id','LEFT') + // ->join($this->kitchenscale_db_msg['foodlist1'].' c','b.one_id = c.id','LEFT') + ->where($search_sql_str) + ->count(); + $page_total = ceil($content_num/$cp_page_num);; + $collect_list = $cfc->table($this->kitchenscale_db_msg['foodlist3']) + // ->alias('a') + // ->join($this->kitchenscale_db_msg['foodlist2'].' b','a.two_id = b.id','LEFT') + // ->join($this->kitchenscale_db_msg['foodlist1'].' c','b.one_id = c.id','LEFT') + ->where($search_sql_str) + ->field('id,name,kcal,unit') + ->page($data['page'],$cp_page_num) + ->select(); + // $return_data = []; + // $temporary_arr = []; + // foreach ($collect_list as $key => $value) { + // unset($collect_list[$key]['ROW_NUMBER']); + + // if(!array_key_exists($value['two_id'], $temporary_arr)){ + // $temporary_arr[$value['two_id']] = [ + // 'id'=>$value['two_id'], + // 'name'=>$value['two_name'], + // 'list'=>[['id'=>$value['id'],'name'=>$value['name'],'kcal'=>$value['kcal'],'unit'=>$value['unit']]], + // ]; + // }else{ + // array_push($temporary_arr[$value['two_id']]['list'],['id'=>$value['id'],'name'=>$value['name'],'kcal'=>$value['kcal'],'unit'=>$value['unit']]); + // } + // } + // foreach ($temporary_arr as $key => $value) { + // array_push($return_data,$value); + // } + return $this->msg([ + 'page_now'=>$data['page'], + 'page_total'=>$page_total, + 'content_list'=>$collect_list + ]); } public function get_cookbook_label_list_action(){ $cfc = Db::connect('cfc_db'); @@ -930,38 +1131,44 @@ class Cookbook extends Base{ // 处理食材的卡路里 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_kcal = $cfc->table($this->kitchenscale_db_msg['foodlist3'])->where("name in ('".implode("','", $foot_name)."')")->field("id,name,kcal")->select(); $foot_kcal2 = []; foreach ($foot_kcal as $key => $value) { - $foot_kcal2[$value['name']] = $value['kcal']; + $foot_kcal2[$value['name']] = [$value['id'],$value['kcal']]; } - + // dump($data); + // dump($foot_kcal2); + // die; 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'; + $data[$key]['id'] = $foot_kcal2[$value['name']][0]; + $data[$key]['kcal'] = $foot_kcal2[$value['name']][1]; }else{ - $data[$key]['kcal'] = '0kcal'; + $data[$key]['kcal'] = '0'; + $data[$key]['id'] = 0; } - $data[$key]['weight'] = $data[$key]['weight'].'g'; + $data[$key]['unit'] = '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'; + // 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'; + // $data[$key]['kcal'] = $this->count_calorie_action($value['weight'],$foot_kcal2[$value['name']]); + // }else{ + // // $data[$key]['kcal'] = '0kcal'; + // $data[$key]['kcal'] = '0'; // } - + // // $data[$key]['weight'] = $data[$key]['weight'].'g'; + // $data[$key]['unit'] = 'g'; + // $data[$key]['id'] = 'g'; // } - return [$data]; + + return $data; } @@ -976,4 +1183,58 @@ class Cookbook extends Base{ } + + 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; + } + + + + } \ No newline at end of file diff --git a/application/KitchenScale/controller/app/Countfood.php b/application/KitchenScale/controller/app/Countfood.php index 365d58a..c237c9f 100644 --- a/application/KitchenScale/controller/app/Countfood.php +++ b/application/KitchenScale/controller/app/Countfood.php @@ -4,9 +4,11 @@ namespace app\KitchenScale\controller\app; use think\Db; -class Login extends Base{ +class Countfood extends Base{ protected $default_head_pic = 'http://tc.pcxbc.com/tsf/head_pic.png'; + protected $page_num = 10; + protected $reedaw_db_msg = [ 'zhanghao'=>'app_account_number',//账号表 'juese'=>'app_user_data',//角色表 @@ -17,8 +19,8 @@ class Login extends Base{ 'followlist'=>'app_user_follow_list',//关注列表 'collect_list'=>'app_user_collect_list',//收藏列表 'foodlist3'=>'app_food_type_three',//食材列表3 - 'eat_log'=>'',//食材列表3 - + 'eat_log'=>'app_user_kcal_log',//食材列表3 + 'user'=>'app_user_data',//banner ]; // 加 bcadd(,,20) @@ -30,7 +32,7 @@ class Login extends Base{ ################################################################接口################################################################ // 添加每日摄入记录 - public function add_intake_food($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>1,'meals_type'=>'早餐','food_list'=>[['food_id'=>1,'weight'=>'500'],['food_id'=>1,'weight'=>'500']]]){ + public function add_intake_food($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>1,'meals_type'=>'午加餐','food_list'=>[['id'=>1,'weight'=>'150','unit'=>'g'],['id'=>2,'weight'=>'100','unit'=>'g']]]){ // 尝试捕获异常 // try { if(count(input('post.')) > 0){ @@ -42,6 +44,9 @@ class Login extends Base{ if(!array_key_exists('aud_id', $data)){ return $this->msg(10001,'aud_id is miss'); } + if(!array_key_exists('meals_type', $data)){ + return $this->msg(10001,'meals_type is miss'); + } if(!array_key_exists('food_list', $data)){ return $this->msg(10001,'food_list is miss'); } @@ -69,7 +74,7 @@ class Login extends Base{ // } } // 获取记食器板块内容 - public function get_countfoot_content($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>1,'time'=>'2025-03-17']){ + public function get_countfoot_content($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>61,'time'=>'2025-03-17']){ // 尝试捕获异常 // try { if(count(input('post.')) > 0){ @@ -112,7 +117,7 @@ class Login extends Base{ // } } // 获取记食器记录 - public function get_log_list($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>1,'page'=>1]){ + public function get_log_list($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>1,'s_time'=>'2025-03-15','e_time'=>'2025-03-16']){ // 尝试捕获异常 // try { if(count(input('post.')) > 0){ @@ -124,8 +129,11 @@ class Login extends Base{ if(!array_key_exists('aud_id', $data)){ return $this->msg(10001,'aud_id is miss'); } - if(!array_key_exists('page', $data)){ - return $this->msg(10001,'page is miss'); + if(!array_key_exists('s_time', $data)){ + return $this->msg(10001,'s_time is miss'); + } + if(!array_key_exists('e_time', $data)){ + return $this->msg(10001,'e_time is miss'); } if(!$this->verify_data_is_ok($data['token'],'str')){ return $this->msg(10005,'token type is error'); @@ -133,8 +141,11 @@ class Login extends Base{ if(!$this->verify_data_is_ok($data['aud_id'],'intnum')){ return $this->msg(10005,'aud_id type is error'); } - if(!$this->verify_data_is_ok($data['page'],'intnum')){ - return $this->msg(10005,'page type is error'); + if(!$this->verify_data_is_ok($data['s_time'],'datetime')){ + return $this->msg(10005,'s_time type is error'); + } + if(!$this->verify_data_is_ok($data['e_time'],'datetime')){ + return $this->msg(10005,'e_time type is error'); } $return_data = $this->get_log_list_action($data); @@ -154,7 +165,86 @@ class Login extends Base{ // return json(['status' => 'error', 'message' => '系统错误']); // } } - + // 计食器板块-设置内容 + public function set_up_content($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>61]){ + // 尝试捕获异常 + // try { + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('token', $data)){ + return $this->msg(10001,'token is miss'); + } + if(!array_key_exists('aud_id', $data)){ + return $this->msg(10001,'aud_id is miss'); + } + if(!$this->verify_data_is_ok($data['token'],'str')){ + return $this->msg(10005,'token type is error'); + } + if(!$this->verify_data_is_ok($data['aud_id'],'intnum')){ + return $this->msg(10005,'aud_id type is error'); + } + + $return_data = $this->set_up_content_action($data); + return $return_data; + // } catch (\Exception $e) { + // // 捕获异常 + // $logContent["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 set_user_kcal($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>61,'set_kcal'=>2000]){ + // 尝试捕获异常 + // try { + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('token', $data)){ + return $this->msg(10001,'token is miss'); + } + if(!array_key_exists('aud_id', $data)){ + return $this->msg(10001,'aud_id is miss'); + } + if(!array_key_exists('set_kcal', $data)){ + return $this->msg(10001,'set_kcal is miss'); + } + if(!$this->verify_data_is_ok($data['token'],'str')){ + return $this->msg(10005,'token type is error'); + } + if(!$this->verify_data_is_ok($data['aud_id'],'intnum')){ + return $this->msg(10005,'aud_id type is error'); + } + if(!$this->verify_data_is_ok($data['set_kcal'],'num')){ + return $this->msg(10005,'set_kcal type is error'); + } + + $return_data = $this->set_user_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' => '系统错误']); + // } + } #######################################################################action####################################################################### #######################################################################action####################################################################### #######################################################################action####################################################################### @@ -165,28 +255,42 @@ class Login extends Base{ public function add_intake_food_action($data){ $cfc = Db::connect('cfc_db'); + $user_data = $cfc->table($this->kitchenscale_db_msg['user']) + ->where(["id"=>$data['aud_id']]) + ->field('weight,height,gender,age,is_use_set_kcal,set_kcal') + ->find(); + if(!$user_data){ + return $this->msg(10003); + } // 统计食物的id $food_id_arr = []; + foreach ($data['food_list'] as $key => $value) { - if(!array_key_exists('food_id', $value)){ - return $this->msg(10001,'food_id is miss'); + if(!array_key_exists('id', $value)){ + return $this->msg(10001,'id is miss'); } if(!array_key_exists('weight', $value)){ return $this->msg(10001,'weight is miss'); } - if(!$this->verify_data_is_ok($value['food_id'],'intnum')){ - return $this->msg(10005,'food_id type is error'); + if(!$this->verify_data_is_ok($value['id'],'intnum')){ + return $this->msg(10005,'id type is error'); } if(!$this->verify_data_is_ok($value['weight'],'num')){ return $this->msg(10005,'weight type is error'); } + foreach ($data['food_list'][$key] as $k => $v) { + // dump($k); + if(!in_array($k,['id','weight','unit'])){ + unset($data['food_list'][$key][$k]); + } + } array_push($food_id_arr,$value['id']); } - $food_content = $cfc->table($this->kitchenscale_db_msg['foodlist3']) ->where("id in (".implode(',',$food_id_arr).")") ->field('id,name,kcal,carbohydrate,protein,fat') ->select(); + // 整理食物信息 $food_content_arr = []; foreach ($food_content as $key => $value) { @@ -201,22 +305,27 @@ class Login extends Base{ $data['food_list'][$key]['carbohydrate_val'] = bcmul($food_content_arr[$value['id']]['carbohydrate'],$proportion_num,2); $data['food_list'][$key]['protein_val'] = bcmul($food_content_arr[$value['id']]['protein'],$proportion_num,2); $data['food_list'][$key]['fat_val'] = bcmul($food_content_arr[$value['id']]['fat'],$proportion_num,2); + $data['food_list'][$key]['food_name'] = $food_content_arr[$value['id']]['name']; $data['food_list'][$key]['aud_id'] = $data['aud_id']; $data['food_list'][$key]['meals_type'] = $data['meals_type']; - $data['food_list'][$key]['food_name'] = $data['meals_type']; $data['food_list'][$key]['create_time'] = $create_time; + $data['food_list'][$key]['food_id'] = $value['id']; + unset($data['food_list'][$key]['id']); + }else{ unset($data['food_list'][$key]); } } + // dump($data['food_list']); + // die; // 数据库数据字段:id,aud_id,meals_type,food_id,food_name,weight,kcal_val,carbohydrate_val,protein_val,fat_val,create_time // 启动事务 Db::startTrans(); try{ - $result = $cfc->table($this->kitchenscale_db_msg['foodlist3'])->insertAll($data['food_list']); - if ($result !== count($data)) { + $result = $cfc->table($this->kitchenscale_db_msg['eat_log'])->insertAll($data['food_list']); + if ($result !== count($data['food_list'])) { Db::rollback(); - return $this->msg(10002); + return $this->msg(10001); } else { Db::commit(); return $this->msg([]); @@ -230,31 +339,34 @@ class Login extends Base{ public function get_countfoot_content_action($data){ $cfc = Db::connect('cfc_db'); - $user_data = Db::table($this->reedaw_db_msg['juese']) - ->where(["id"=>$data['aud_id'],'token'=>$data['token']]) - ->field('weight,height,gender,birthday') + $user_data = $cfc->table($this->kitchenscale_db_msg['user']) + ->where(["id"=>$data['aud_id']]) + ->field('weight,height,gender,age,is_use_set_kcal,set_kcal') ->find(); if(!$user_data){ return $this->msg(10003); } - $user_data['age_num'] = $this->calculate_age($user_data['birthday']); + $user_data['age_num'] = $user_data['age']; $nutrition_data = $this->count_user_nutrition_all($user_data); - + if($user_data['is_use_set_kcal'] == 1){ + $nutrition_data['kcal'] = $user_data['set_kcal']; + } + // 查询用户今日摄入食物 $food_content = $cfc->table($this->kitchenscale_db_msg['eat_log']) - // ->where(["aud_id"=>$data['aud_id'],'create_time'=>['=']]) - ->where("aud_id = ".$data['aud_id']." AND CAST(create_time AS DATE) = ".$data['time']."") + ->where("aud_id = " . $data['aud_id'] . " AND CAST(create_time AS DATE) = CAST('" . $data['time'] . "' AS DATE)") ->field('meals_type,food_name,weight,kcal_val,carbohydrate_val,protein_val,fat_val') ->select(); + $date = date('Y-m-d H:i:s'); $return_data = [ 'suggestion'=>[ - 'kcal'=>$nutrition_data['tdee'], - 'carbohydrate'=>$nutrition_data['carbohydrate'].'克', - 'protein'=>$nutrition_data['protein'].'克', - 'fat'=>$nutrition_data['fat'].'克', + 'kcal'=>$nutrition_data['kcal'], + 'carbohydrate'=>$nutrition_data['carbohydrate'], + 'protein'=>$nutrition_data['protein'], + 'fat'=>$nutrition_data['fat'], ], 'today_intake'=>[ 'kcal'=>0, @@ -262,7 +374,7 @@ class Login extends Base{ 'protein'=>0, 'fat'=>0, ], - 'remaining_kcal'=>$nutrition_data['tdee'],//剩下可摄入卡路里 + 'remaining_kcal'=>$nutrition_data['kcal'],//剩下可摄入卡路里 'list'=>[ [ 'name'=>'早餐', @@ -283,13 +395,19 @@ class Login extends Base{ 'list'=>[], ], [ - 'name'=>'加餐', + 'name'=>'早加餐', 'val'=>0, 'unit'=>'kcal', 'list'=>[], ], [ - 'name'=>'其他', + 'name'=>'午加餐', + 'val'=>0, + 'unit'=>'kcal', + 'list'=>[], + ], + [ + 'name'=>'晚加餐', 'val'=>0, 'unit'=>'kcal', 'list'=>[], @@ -298,79 +416,261 @@ class Login extends Base{ 'date'=>date('Y-m-d'), ]; if(count($food_content) <= 0){ + $return_data['list'] = []; return $this->msg($return_data); } foreach ($food_content as $key => $value) { - $return_data['today_intake']['kcal'] += $value['kcal_val']; - $return_data['today_intake']['carbohydrate'] += $value['carbohydrate_val']; - $return_data['today_intake']['protein'] += $value['protein_val']; - $return_data['today_intake']['fat'] += $value['fat_val']; + $return_data['today_intake']['kcal'] = bcadd($return_data['today_intake']['kcal'],$value['kcal_val'],2); + $return_data['today_intake']['carbohydrate'] = bcadd($return_data['today_intake']['carbohydrate'],$value['carbohydrate_val'],2); + $return_data['today_intake']['protein'] = bcadd($return_data['today_intake']['protein'],$value['protein_val'],2); + $return_data['today_intake']['fat'] = bcadd($return_data['today_intake']['fat'],$value['fat_val'],2); if($value['meals_type'] == '早餐'){ - $return_data['list'][0]['val'] += $value['kcal_val']; + $return_data['list'][0]['val'] = bcadd($return_data['list'][0]['val'],$value['kcal_val'],2); array_push($return_data['list'][0]['list'],['name'=>$value['food_name'],'weight'=>$value['weight'].'克','val'=>$value['kcal_val'].'kcal']); }else if($value['meals_type'] == '午餐'){ - $return_data['list'][1]['val'] += $value['kcal_val']; + $return_data['list'][1]['val'] = bcadd($return_data['list'][1]['val'],$value['kcal_val'],2); array_push($return_data['list'][1]['list'],['name'=>$value['food_name'],'weight'=>$value['weight'].'克','val'=>$value['kcal_val'].'kcal']); }else if($value['meals_type'] == '晚餐'){ - $return_data['list'][2]['val'] += $value['kcal_val']; + $return_data['list'][2]['val'] = bcadd($return_data['list'][2]['val'],$value['kcal_val'],2); array_push($return_data['list'][2]['list'],['name'=>$value['food_name'],'weight'=>$value['weight'].'克','val'=>$value['kcal_val'].'kcal']); - }else if($value['meals_type'] == '加餐'){ - $return_data['list'][3]['val'] += $value['kcal_val']; + }else if($value['meals_type'] == '早加餐'){ + $return_data['list'][3]['val'] = bcadd($return_data['list'][3]['val'],$value['kcal_val'],2); array_push($return_data['list'][3]['list'],['name'=>$value['food_name'],'weight'=>$value['weight'].'克','val'=>$value['kcal_val'].'kcal']); - }else{ - $return_data['list'][4]['val'] += $value['kcal_val']; + }else if($value['meals_type'] == '午加餐'){ + $return_data['list'][4]['val'] = bcadd($return_data['list'][4]['val'],$value['kcal_val'],2); array_push($return_data['list'][4]['list'],['name'=>$value['food_name'],'weight'=>$value['weight'].'克','val'=>$value['kcal_val'].'kcal']); + }else if($value['meals_type'] == '晚加餐'){ + $return_data['list'][5]['val'] = bcadd($return_data['list'][5]['val'],$value['kcal_val'],2); + array_push($return_data['list'][5]['list'],['name'=>$value['food_name'],'weight'=>$value['weight'].'克','val'=>$value['kcal_val'].'kcal']); + }else{ + } } - $return_data['today_intake'] = $return_data['suggestion']['kcal']-$return_data['today_intake']['kcal']>=0?$return_data['suggestion']['kcal']-$return_data['today_intake']['kcal']:0; - return $this->msg($return_data); - } + foreach ($return_data['list'] as $key => $value) { + if(count($value['list']) <= 0){ + unset($return_data['list'][$key]); + } + } + $return_data['list'] = array_values($return_data['list']); + $return_data['remaining_kcal'] = bcsub($return_data['suggestion']['kcal'],$return_data['today_intake']['kcal'],2)>=0?bcsub($return_data['suggestion']['kcal'],$return_data['today_intake']['kcal'],2):0; + return $this->msg($return_data); + + } public function get_log_list_action($data){ $cfc = Db::connect('cfc_db'); - $user_data = Db::table($this->reedaw_db_msg['juese']) - ->where(["id"=>$data['aud_id'],'token'=>$data['token']]) - ->field('weight,height,gender,birthday') + $user_data = $cfc->table($this->kitchenscale_db_msg['user']) + ->where(["id"=>$data['aud_id']]) + ->field('weight,height,gender,age,is_use_set_kcal,set_kcal') + ->find(); + if(!$user_data){ + return $this->msg(10003); + } + // dump($user_data); + // 计算年龄 + $user_data['age_num'] = $user_data['age']; + // 计算推荐营养 + $nutrition_data = $this->count_user_nutrition_all($user_data); + if($user_data['is_use_set_kcal'] == 1){ + $nutrition_data['kcal'] = $user_data['set_kcal']; + } + // 计算取值范围 + // $time_arr = $this->calculateDateRange($data['page']); + + // $content_num = $cfc->query("SELECT COUNT(DISTINCT CONVERT(varchar, create_time, 23)) as total + // FROM ".$this->kitchenscale_db_msg['eat_log']." + // WHERE aud_id = ".$data['aud_id'].""); + // $page_total = ceil($content_num[0]['total']/$this->page_num); + + $food_content = $cfc->table($this->kitchenscale_db_msg['eat_log']) + ->field("CONVERT(varchar, create_time, 23) as create_time, SUM(kcal_val) as kcal_val") + ->where(['aud_id'=>$data['aud_id'],'create_time'=>['>=',$data['s_time'].' 00:00:00'],'create_time'=>['<=',$data['e_time'].' 23:59:59']]) + ->group('CONVERT(varchar, create_time, 23)') + // ->page($data['page'],$this->page_num) + ->order('create_time DESC') + ->select(); + // dump($food_content); + // die; + $user_log = []; + foreach ($food_content as $key => $value) { + $time_data = substr($value['create_time'], 0, 10); + if(array_key_exists($time_data, $user_log)){ + $user_log[$time_data] += $value['kcal_val']; + }else{ + $user_log[$time_data] = $value['kcal_val']; + } + } + $return_data = []; + foreach ($user_log as $key => $value) { + if(bcdiv($value,$nutrition_data['kcal'],2) < 0.9){ + $bz['text'] = '不达标'; + $bz['color'] = '#F0AD4E'; + }else if(bcdiv($value,$nutrition_data['kcal'],2) >= 0.9 && bcdiv($value,$nutrition_data['kcal'],2) < 1.1){ + $bz['text'] = '达标'; + $bz['color'] = '#4CD964'; + }else{ + $bz['text'] = '超标'; + $bz['color'] = '#FF0000'; + } + array_push($return_data,['time'=>$key,'val'=>$value,'unit'=>'kcal','describe'=>$bz['text'],'color'=>$bz['color']]); + } + + // dump($return_data); + return $this->msg([ + // 'page_now'=>$data['page'], + // 'page_total'=>$page_total, + 'content_list'=>$return_data + ]); + } + public function set_up_content_action($data){ + $cfc = Db::connect('cfc_db'); + $user_data = $cfc->table($this->kitchenscale_db_msg['user']) + ->where(["id"=>$data['aud_id']]) + ->field('weight,height,gender,age,is_use_set_kcal,set_kcal') ->find(); if(!$user_data){ return $this->msg(10003); } // 计算年龄 - $user_data['age_num'] = $this->calculate_age($user_data['birthday']); + $user_data['age_num'] = $user_data['age']; // 计算推荐营养 $nutrition_data = $this->count_user_nutrition_all($user_data); - // 计算取值范围 - $time_arr = $this->calculateDateRange($data['page']); - - $food_content = $cfc->table($this->kitchenscale_db_msg['eat_log']) - ->where(['aud_id'=>$data['aud_id']]) - ->whereTime('create_time', 'between', [$time_arr['s_time'], $time_arr['e_time']]) - ->field('kcal_val,create_time') - ->order('create_time desc') - ->select(); - $user_log = []; - foreach ($food_content as $key => $value) { - if(array_key_exists($value['create_time'], $user_log)){ - $user_log[$value['create_time']] += $value['kcal_val']; - }else{ - $user_log[$value['create_time']] = $value['kcal_val']; - } + if($user_data['is_use_set_kcal'] == 1){ + $nutrition_data['kcal'] = $user_data['set_kcal']; } - $return_data = []; - foreach ($variable as $key => $value) { - if(bcdiv($value,$nutrition_data['kcal'],2) < 0.9){ - $bz['text'] = '不达标'; - $bz['color'] = '#F1D452'; - }else if(bcdiv($value,$nutrition_data['kcal'],2) >= 0.9 && bcdiv($value,$nutrition_data['kcal'],2) < 1.1){ - $bz['text'] = '达标'; - $bz['color'] = '#66AE00'; - }else{ - $bz['text'] = '超标'; - $bz['color'] = '#D7001A'; - } - array_push($return_data,['time'=>$key,'val'=>$value,'unit'=>'kcal','describe'=>$bz['text'],'color'=>$bz['color']]); + $nutrition_describe = [ + [ + '对于一个孩子(2-18岁)(没有特殊健康问题),身体处于快速生长发育阶段,需要充足的营养支持,尤其是蛋白质和健康脂肪。通常建议的三大营养素比例为:', + '1、碳水化合物:45%-65% 的总热量', + '提供能量,支持孩子的日常活动和生长发育。', + '优先选择复合碳水化合物(如全谷物、蔬菜、水果),避免精制糖和高糖零食。', + '2、蛋白质:10%-30% 的总热量', + '支持肌肉、骨骼和器官的发育。', + '建议摄入优质蛋白质来源,如瘦肉、鱼类、鸡蛋、豆类和乳制品。', + '蛋白质需求较高,尤其是青春期孩子。', + '3、脂肪:25%-35% 的总热量', + '提供能量,并支持大脑发育(尤其是Omega-3脂肪酸)。', + '优先选择健康脂肪,如鱼类、坚果、种子、橄榄油和牛油果。', + '避免反式脂肪和过多的饱和脂肪。', + '注意事项', + '如果你有特定的健康目标(如增高,减重),可以进一步调整比例,并咨询营养师或医生以获得个性化建议。', + ], + [ + '对于一个正常成年人(没有特殊健康问题或特定健身目标),通常建议的三大营养素比例为:', + '1、碳水化合物:45%-65% 的总热量', + '主要功能是提供能量', + '建议选择复合碳水化合物(如全谷物、蔬菜、豆类),而不是精制糖。', + '对于普通人,碳水化合物占总热量的 50%-55% 是一个常见的推荐值。', + '2、蛋白质:10%-35% 的总热量', + '用于维持肌肉、修复组织和支持免疫功能。', + '普通人每日蛋白质摄入量建议为 0.8-1.2克/公斤体重。', + '对于活动量较大或健身人群,蛋白质比例可以提高到 20%-30%。', + '3、脂肪:20%-35% 的总热量', + '提供能量、支持细胞功能并帮助吸收脂溶性维生素。', + '建议以 不饱和脂肪(如橄榄油、坚果、鱼类)为主,减少饱和脂肪和反式脂肪的摄入。', + '脂肪占总热量的 20%-30% 是常见的推荐值。', + '以上是根据世界卫生组织(WHO)和其他健康机构的建议制定的。具体比例可以根据个人的活动水平、健康状况和目标进行微调。', + '注意事项', + '活动水平:活动量大的人可能需要更多的碳水化合物来提供能量,而健身人群可能需要更多的蛋白质。', + '健康状况:例如,糖尿病患者可能需要控制碳水化合物比例,而心血管疾病患者可能需要减少脂肪摄入。', + '个体差异:每个人的代谢和需求不同,建议根据个人情况调整。', + '如果你有特定的健康目标(如减脂、增肌或控制慢性病),可以进一步调整比例,并咨询营养师或医生以获得个性化建议。', + ], + [ + '对于一个老人(65岁以上)(没有特殊健康问题),身体的代谢率下降,肌肉量减少,可能面临营养不良或慢性病风险,因此需要调整营养比例。通常建议的三大营养素比例为:', + '1、碳水化合物:45%-65% 的总热量', + '提供能量,但应选择低血糖指数(GI)的食物,如全谷物、蔬菜和豆类,以控制血糖水平。', + '避免精制糖和高糖食物,尤其是糖尿病患者。', + '帮助维持肌肉质量,预防肌肉流失(少肌症)。', + '建议摄入优质蛋白质,如鱼类、瘦肉、鸡蛋、豆类和乳制品。', + '蛋白质需求较高,尤其是活动量较大的老人。', + '3、脂肪:20%-35% 的总热量', + '提供能量,并支持细胞功能和脂溶性维生素的吸收。', + '优先选择不饱和脂肪,如橄榄油、坚果、种子和鱼类。', + '减少饱和脂肪和反式脂肪的摄入,以降低心血管疾病风险。', + '注意事项', + '如果你有特定的健康目标(控制慢性病),可以进一步调整比例,并咨询营养师或医生以获得个性化建议。', + ] + ]; + $return_data = [ + 'kcal'=>[ + 'title'=>"建议摄入卡路里", + 'suggestion_kcal_val'=>$nutrition_data['kcal'], + 'suggestion_kcal_unit'=>"千卡", + 'suggestion_kcal_range_val'=>"建议您在:".bcmul($nutrition_data['bmr'],1.2,2)."~".bcmul($nutrition_data['bmr'],1.9,2)."千卡范围内调整,修改时请以医生建议为准!", + 'describe'=>[ + '基础代谢率(BMR):是指人体在清醒而又极端安静的状态下,不受肌肉活动、环境温度、食物及精神紧张等影响时的能量代谢率。', + '每日总能量消耗(TDEE):', + 'TDEE是BMR乘以活动系数,活动系数根据日常活动水平确定', + '久坐(很少或没有运动):BMR × 1.2', + '轻度活动(每周1-3天轻度运动):BMR × 1.375', + '中度活动(每周3-5天中度运动):BMR × 1.55', + '高度活动(每周6-7天高强度运动):BMR × 1.725', + '极高活动(体力劳动或每天高强度训练):BMR × 1.9', + '您的BMR为'.$nutrition_data['bmr'].',建议根据您的日常生活水平,调整合适的TDEE', + ], + ], + 'nutrition'=>[ + 'title'=>"建议三大营养比例", + 'list'=>[ + [ + 'name'=>'碳水化合物', + 'icon'=>'icon-tanshuihuahewu', + 'proportion'=>'50%', + 'val'=>$nutrition_data['carbohydrate'], + 'unit'=>'克' + ], + [ + 'name'=>'蛋白质', + 'icon'=>'icon-Sm-danbaizhi', + 'proportion'=>'20%', + 'val'=>$nutrition_data['protein'], + 'unit'=>'克' + ], + [ + 'name'=>'脂肪', + 'icon'=>'icon-w_fat_normal', + 'proportion'=>'30%', + 'val'=>$nutrition_data['fat'], + 'unit'=>'克' + ] + ], + 'describe'=>[] + ] + ]; + if($user_data['age_num'] >= 2 && $user_data['age_num'] <= 18){ + $return_data['nutrition']['describe'] = $nutrition_describe[0]; + }else if($user_data['age_num'] > 18 && $user_data['age_num'] < 65){ + $return_data['nutrition']['describe'] = $nutrition_describe[1]; + }else if($user_data['age_num'] >= 65){ + $return_data['nutrition']['describe'] = $nutrition_describe[2]; + }else{ + } return $this->msg($return_data); + + } + public function set_user_kcal_action($data){ + $cfc = Db::connect('cfc_db'); + $user_data = $cfc->table($this->kitchenscale_db_msg['user']) + ->where(["id"=>$data['aud_id']]) + ->field('weight,height,gender,age,is_use_set_kcal,set_kcal') + ->find(); + if(!$user_data){ + return $this->msg(10003); + } + $result = $cfc->table($this->kitchenscale_db_msg['user']) + ->where(["id"=>$data['aud_id']]) + ->update([ + 'is_use_set_kcal'=>1, + 'set_kcal'=>$data['set_kcal'] + ]); + + if($result){ + return $this->msg([]); + }else{ + return $this->msg(10002); + } } #######################################################################工具####################################################################### @@ -384,13 +684,13 @@ class Login extends Base{ $bmr = bcmul(10,$data['weight'],20); $bmr = bcadd($bmr,bcmul(6.25,$data['height'],20),20); $bmr = bcsub($bmr,bcmul(5,$data['age_num'],20),20); - $bmr = bcadd($bmr,5,20); + $bmr = bcadd($bmr,5,2); }else if($data['gender'] == 2){ // 女性:BMR = 10 × 体重(kg) + 6.25 × 身高(cm) - 5 × 年龄(岁) - 161 $bmr = bcmul(10,$data['weight'],20); $bmr = bcadd($bmr,bcmul(6.25,$data['height'],20),20); $bmr = bcsub($bmr,bcmul(5,$data['age_num'],20),20); - $bmr = bcsub($bmr,161,20); + $bmr = bcsub($bmr,161,2); }else{ return $this->msg(10003,'性别未知'); } @@ -420,19 +720,19 @@ class Login extends Base{ $protein = bcdiv(bcmul($tdee,0.25,20),4,2); $fat =bcdiv(bcmul($tdee,0.25,20),9,2); } - return ['kcal'=>$tdee,'carbohydrate'=>$carbohydrate,'protein'=>$protein,'fat'=>$fat]; + return ['kcal'=>$tdee,'carbohydrate'=>$carbohydrate,'protein'=>$protein,'fat'=>$fat,'bmr'=>$bmr]; } public function calculateDateRange($n) { // 获取当前日期和时间 - $today = new DateTime(); + $today = new \DateTime(); // 计算结束时间:往前推 (10 * (n - 1)) 天,时间设为 23:59:59 - $endInterval = new DateInterval('P' . (10 * ($n - 1)) . 'D'); + $endInterval = new \DateInterval('P' . (10 * ($n - 1)) . 'D'); $endTime = clone $today; $endTime->sub($endInterval)->setTime(23, 59, 59); // 计算开始时间:往前推 (10 * n) 天,时间设为 00:00:00 - $startInterval = new DateInterval('P' . (10 * $n) . 'D'); + $startInterval = new \DateInterval('P' . (10 * $n) . 'D'); $startTime = clone $today; $startTime->sub($startInterval)->setTime(0, 0, 0); diff --git a/application/KitchenScale/controller/app/Index.php b/application/KitchenScale/controller/app/Index.php index dac3493..10ff122 100644 --- a/application/KitchenScale/controller/app/Index.php +++ b/application/KitchenScale/controller/app/Index.php @@ -3,6 +3,7 @@ namespace app\KitchenScale\controller\app; use think\Db; +// use app\KitchenScale\controller\app\Wechat;// 引入Wechat服务类 class Index extends Base{ @@ -23,6 +24,7 @@ class Index extends Base{ 'collect_list'=>'app_user_collect_list',//点赞表 'banner'=>'app_banner_data',//banner 'version'=>'app_version_log',//版本表 + 'user'=>'app_user_data',//banner ]; @@ -75,18 +77,96 @@ class Index extends Base{ } } + // 微信手机号快捷登录 + public function wechat_quick_login($data = ['code'=>'asdasdasd','encryptedData'=>'adsadasdasd','iv'=>'asdasdasdasd']){ + try { + // 你的业务逻辑 + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('code', $data)){ + // return $this->msg(10001,''); + return $this->msg(10001,'code is miss'); + } + if(!array_key_exists('encryptedData', $data)){ + return $this->msg(10001,'encryptedData is miss'); + } + if(!array_key_exists('iv', $data)){ + return $this->msg(10001,'iv is miss'); + } + // 校验参数 + if (empty($data['code'])) { + return $this->msg(10001,'code is miss.'); + } + if (empty($data['encryptedData'])) { + return $this->msg(10001,'encryptedData is miss.'); + } + if (empty($data['iv'])) { + return $this->msg(10001,'iv is miss.'); + } + + // 调用Wechat服务类处理微信登录逻辑 + $wechatService = new Wechat(); + // die; + $result = $wechatService->handleWechatLogin($data['code'], $data['encryptedData'], $data['iv']); + // dump($result); + // die; + if($result['code'] == 0){ + // return $this->msg($result['code'],$result['msg']); + + $user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['tel'=>$result['data']['phoneNumber'],'is_del'=>0])->find(); + + if($user_data){ + Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$user_data['token']])->update(['login_time'=>date('Y-m-d H:i:s')]); + $return_data = $this->msg(['token'=>$user_data['token'],'aan_id'=>$user_data['id']]); + }else{ + $set_data['password'] = ''; + $set_data['tel'] = $result['data']['phoneNumber']; + $set_data['head_pic'] = $this->default_head_pic; + $set_data['nickname'] = '用户'.$result['data']['phoneNumber']; + $set_data['create_time'] = date('Y-m-d H:i:s'); + $set_data['login_time'] = date('Y-m-d H:i:s'); + $set_data['token'] = md5($result['data']['phoneNumber'].$this->create_random_string(12).time()); + $set_user_result = Db::table($this->reedaw_db_msg['zhanghao'])->insertGetId($set_data); + if($set_user_result){ + $return_data = $this->msg(['token'=>$set_data['token'],'aan_id'=>$set_user_result],'登录成功'); + }else{ + $return_data = $this->msg(10002); + } + } + return $return_data; + }else{ + return $this->msg($result['code'],$result['msg']); + } + + } 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'] .= "方法: (wechat_quick_login)" . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + $this->record_api_log($data, $logContent, null); + return $this->msg(99999); + } + } + // 获取默认配置信息(包含:食材的分类列表,用户角色信息)(OK) - public function get_default_config($data = ['token'=>'caadd1be045a65f30b92aa805f1de54a']){ + public function get_default_config($data = ['token'=>'']){ // try { if(count(input('post.')) > 0){ $data = input('post.'); } - if(!array_key_exists('token', $data)){ - return $this->msg(10001); - } - if(!$this->verify_data_is_ok($data['token'],'str')){ - return $this->msg(10005); - } + // if(!array_key_exists('token', $data)){ + // return $this->msg(10001); + // } + // if(!$this->verify_data_is_ok($data['token'],'str')){ + // return $this->msg(10005); + // } $return_data = $this->get_default_config_action($data); return $return_data; // } catch (\Exception $e) { @@ -105,55 +185,19 @@ class Index extends Base{ // } } - // 获取首页信息(banner,金刚区,label_list)(OK) - public function get_homepage_information($data = ['token'=>'caadd1be045a65f30b92aa805f1de54a']){ - // try { - // 你的业务逻辑 - if(count(input('post.')) > 0){ - $data = input('post.'); - } - if(!array_key_exists('token', $data)){ - return $this->msg(10001); - } - if(!$this->verify_data_is_ok($data['token'],'str')){ - return $this->msg(10005); - } - $return_data = $this->get_homepage_information_action($data); - // 成功 - $this->record_api_log($data, null, $return_data); - return $return_data; - // } catch (\Exception $e) { - // // 捕获异常 - // $logContent["flie"] = $e->getFile(); - // $logContent["line"] = $e->getLine(); - // $logContent['all_content'] = "异常信息:\n"; - // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; - // $logContent['all_content'] .= "接口: (get_homepage_information)\n"; - // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; - // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; - // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; - // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; - // $this->record_api_log($data, $logContent, null); - // return $this->msg(99999); - // } - } - // 首页搜索接口(OK) - public function search_column($data = ['search_data'=>'鱼','token'=>'caadd1be045a65f30b92aa805f1de54a']){ + public function search_column($data = ['search_data'=>'鱼','token'=>'']){ // try { // 你的业务逻辑 if(count(input('post.')) > 0){ $data = input('post.'); } - if(!array_key_exists('search_data', $data) || !array_key_exists('token', $data)){ + if(!array_key_exists('search_data', $data)){ return $this->msg(10001); } if(!$this->verify_data_is_ok($data['search_data'],'str')){ return $this->msg(10005); } - if(!$this->verify_data_is_ok($data['token'],'str')){ - return $this->msg(10005); - } $return_data = $this->search_column_action($data); return $return_data; // } catch (\Exception $e) { @@ -177,45 +221,36 @@ class Index extends Base{ #######################################################################action####################################################################### public function get_default_config_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,'账号信息错误'); - } - $return_data = [ - // 'food_list'=>[], - // 'cook_label'=>[], - 'account'=>[], - ]; - + $return_data = [ + 'cook_label'=>[], + // 'account'=>[], + 'food_list'=>[], + 'banner'=>[], + 'jingang_region'=>[ + ['name'=>'菜谱分类','jump_url'=>'/pages/menu/menu','icon'=>'https://tc.pcxbc.com/kitchenscale_all/vajra1.png'], + ['name'=>'我的收藏','jump_url'=>'/pageTwo/me/mymenu','icon'=>'https://tc.pcxbc.com/kitchenscale_all/vajra2.png'], + ['name'=>'热量计算','jump_url'=>'/pages/count/count','icon'=>'https://tc.pcxbc.com/kitchenscale_all/vajra3.png'], + ['name'=>'健康食谱','jump_url'=>'/pages/menu/menu','icon'=>'https://tc.pcxbc.com/kitchenscale_all/vajra4.png'], + ], + ]; $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')->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_data['food_list'] = $foodlist1; + $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(); + // dump($foodlist3); + foreach ($foodlist1 as $key => $value) { + unset($foodlist1[$key]['ROW_NUMBER']); + $foodlist1[$key]['list'] = []; + foreach ($foodlist2 as $k => $v) { + if($v['one_id'] == $value['id']){ + unset($foodlist2[$k]['ROW_NUMBER']); + array_push($foodlist1[$key]['list'],$foodlist2[$k]); + // unset($foodlist2[$k]); + } + } + } + $return_data['food_list'] = $foodlist1; // // 获取食材分类列表end // 获取菜谱分类标签start $cook_label = $cfc->table($this->kitchenscale_db_msg['cookbook_label']) @@ -227,76 +262,56 @@ class Index extends Base{ } $return_data['cook_label'] = $cook_label; // 获取菜谱分类标签end - // 获取账号下信息以及用户信息start - $user_account = Db::table($this->reedaw_db_msg['zhanghao']) - ->alias('zhanghao') - ->join($this->reedaw_db_msg['juese'].' juese','zhanghao.id = juese.aan_id','LEFT') - ->where(["zhanghao.token"=>$data['token'],'juese.is_del'=>0]) - ->field('juese.id as aud_id,juese.nickname,juese.birthday,juese.gender,juese.grade,juese.head_pic') - ->select(); - $return_data['account'] = $user_account; - // 获取账号下信息以及用户信息end - return $this->msg($return_data); - } - public function get_homepage_information_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,'账号信息错误'); - } - $return_data = [ - 'banner'=>[], - 'jingang_region'=>[ - ['name'=>'菜谱分类','jump_url'=>'/xxx/xxx/xxx','icon'=>'/xxx/xxx/xxx'], - ['name'=>'我的收藏','jump_url'=>'/xxx/xxx/xxx','icon'=>'/xxx/xxx/xxx'], - ['name'=>'热量计算','jump_url'=>'/xxx/xxx/xxx','icon'=>'/xxx/xxx/xxx'], - ['name'=>'健康食谱','jump_url'=>'/xxx/xxx/xxx','icon'=>'/xxx/xxx/xxx'], - ], - ]; - $cfc = Db::connect('cfc_db'); - + // 获取首页信息start // 获取banner - $banner_list = $cfc->query("select b.id,b.title,b.cover,b.create_user_head_pic,b.create_user_nickname + $banner_list = $cfc->query("select b.id,b.title,b.cover,b.create_user_head_pic,b.create_user_nickname,c.pic_url from ".$this->kitchenscale_db_msg['banner']." as a - LEFT JOIN ".$this->kitchenscale_db_msg['cookbook']." as b - on a.cookbook_id = b.id - where a.is_del=0 + LEFT JOIN ".$this->kitchenscale_db_msg['cookbook']." as b on a.cookbook_id = b.id + LEFT JOIN ".$this->kitchenscale_db_msg['uploadimg']." as c on b.cover = c.id + where a.is_del=0 AND b.is_del=0 ORDER BY a.sort_num desc,a.id desc "); - $collect_list = $cfc->table($this->kitchenscale_db_msg['collect_list'])->where(['token'=>$data['token']])->column('cookbook_id'); - - - foreach ($banner_list as $key => $value) { - if(array_key_exists($value['id'],$collect_list)){ - $banner_list[$key]['is_me_like_it'] = 1; - }else{ - $banner_list[$key]['is_me_like_it'] = 0; + // dump($banner_list); + // die; + if($data['token'] != ''){ + // 获取账号下信息以及用户信息 + $user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->field('id,token,nickname,head_pic,tel,email')->find(); + if(!$user_data){ + return $this->msg(20001,'账号信息错误'); + } + if(!$this->verify_data_is_ok($data['token'],'str')){ + return $this->msg(10005); + } + // 获取账号下信息以及用户信息 + $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_list = $cfc->table($this->kitchenscale_db_msg['collect_list'])->where(['token'=>$data['token'],'is_del'=>0])->column('cookbook_id'); + foreach ($banner_list as $key => $value) { + if(array_key_exists($value['id'],$collect_list)){ + $banner_list[$key]['is_me_like_it'] = 'yes'; + }else{ + $banner_list[$key]['is_me_like_it'] = 'no'; + } + unset($banner_list[$key]['ROW_NUMBER']); + } + }else{ + foreach ($banner_list as $key => $value) { + $banner_list[$key]['is_me_like_it'] = 'no'; + unset($banner_list[$key]['ROW_NUMBER']); } - unset($banner_list[$key]['ROW_NUMBER']); } $return_data['banner'] = $banner_list; - - - // 获取菜谱列表 - // $label_list = $cfc->query("select id,name from ".$this->kitchenscale_db_msg['cookbook_label']." where is_del=0 ORDER BY id"); - // if(count($label_list) <= 0){ - // $return_data['label_list'] = []; - // return $this->msg($return_data); - // } - // $return_data['label_list'] = $label_list; + // return $this->msg($return_data); + // 获取首页信息end return $this->msg($return_data); - - } + public function search_column_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,'账号信息错误'); - } + // $cookbook = new Cookbook(); $cfc = Db::connect('cfc_db'); // 获取菜谱信息 @@ -309,25 +324,70 @@ class Index extends Base{ if(count($content_list)<=0){ return $this->msg([]); } - // 获取用户收藏列表 - $my_collect_list = $cfc->table($this->kitchenscale_db_msg['collect_list']) - ->where(['token'=>$data['token']]) - ->column('cookbook_id'); - // dump(); - // 处理菜谱收藏信息 - foreach ($content_list as $key => $value) { - if(array_key_exists($value['id'],$my_collect_list)){ - $content_list[$key]['is_me_like_it'] = 1; + 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']]) + ->column('cookbook_id'); + // dump(); + // 处理菜谱收藏信息 + foreach ($content_list as $key => $value) { + if(array_key_exists($value['id'],$my_collect_list)){ + $content_list[$key]['is_me_like_it'] = 'yes'; + }else{ + $content_list[$key]['is_me_like_it'] = 'no'; + } + if($value['cover'] == null){ + $content_list[$key]['cover'] = 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg'; + } + unset($content_list[$key]['ROW_NUMBER']); + } }else{ - $content_list[$key]['is_me_like_it'] = 0; + 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']); + } } - if($value['cover'] == null){ - $content_list[$key]['cover'] = 'https://tc.pcxbc.com/kitchenscale_all/diule.jpg'; + }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']); } - unset($content_list[$key]['ROW_NUMBER']); } - return $this->msg($content_list); } + + + + ######################################################################################################################################################################## + ######################################################################################################################################################################## + ######################################################################################################################################################################## + + + + + public function create_random_string($length = 12) + { + //创建随机字符 + $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + $str = ""; + for ($i = 0; $i < $length; $i++) { + $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); + } + return $str; + } + } \ No newline at end of file diff --git a/application/KitchenScale/controller/app/Usercenter.php b/application/KitchenScale/controller/app/Usercenter.php index dc211ca..60eea7a 100644 --- a/application/KitchenScale/controller/app/Usercenter.php +++ b/application/KitchenScale/controller/app/Usercenter.php @@ -23,6 +23,7 @@ class Usercenter extends Base{ 'foodlist3'=>'app_food_type_three',//食材列表3 'collect_list'=>'app_user_collect_list',//点赞表 'banner'=>'app_banner_data',//banner + 'user'=>'app_user_data',//banner ]; @@ -34,6 +35,95 @@ class Usercenter extends Base{ ################################################################接口################################################################ ################################################################接口################################################################ + // 获取角色信息 + public function get_user_msg($data = ['token'=>'caadd1be045a65f30b92aa805f1de54a']){ + // try { + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('token', $data)){ + return $this->msg(10001); + } + if(!$this->verify_data_is_ok($data['token'],'str')){ + return $this->msg(10005); + } + $return_data = $this->get_user_msg_action($data); + return $return_data; + // } catch (\Exception $e) { + // // 捕获异常 + // $logContent["flie"] = $e->getFile(); + // $logContent["line"] = $e->getLine(); + // $logContent['all_content'] = "异常信息:\n"; + // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + // $logContent['all_content'] .= "接口: (get_default_config)\n"; + // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // $this->record_api_log($data, $logContent, null); + // return $this->msg(99999); + // } + } + // 修改用户 + public function update_user_msg($data = ['token'=>'caadd1be045a65f30b92aa805f1de54a','nickname'=>'测试人员001','gender'=>'1','age'=>'18','height'=>'165.34','weight'=>'55.55']){ + // 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('nickname', $data)){ + return $this->msg(10001,'nickname is miss'); + } + if(!array_key_exists('gender', $data)){ + return $this->msg(10001,'gender is miss'); + } + if(!array_key_exists('age', $data)){ + return $this->msg(10001,'age is miss'); + } + if(!array_key_exists('height', $data)){ + return $this->msg(10001,'height is miss'); + } + if(!array_key_exists('weight', $data)){ + return $this->msg(10001,'weight is miss'); + } + if(!$this->verify_data_is_ok($data['token'],'str')){ + return $this->msg(10005,'token type is error'); + } + if(!$this->verify_data_is_ok($data['nickname'],'str')){ + return $this->msg(10005,'nickname type is error'); + } + if(!$this->verify_data_is_ok($data['gender'],'intnum')){ + return $this->msg(10005,'gender type is error'); + } + if(!$this->verify_data_is_ok($data['age'],'intnum')){ + return $this->msg(10005,'age type is error'); + } + if(!$this->verify_data_is_ok($data['height'],'num')){ + return $this->msg(10005,'height type is error'); + } + if(!$this->verify_data_is_ok($data['weight'],'num')){ + return $this->msg(10005,'weight type is error'); + } + $return_data = $this->update_user_msg_action($data); + return $return_data; + // } catch (\Exception $e) { + // // 捕获异常 + // $logContent["flie"] = $e->getFile(); + // $logContent["line"] = $e->getLine(); + // $logContent['all_content'] = "异常信息:\n"; + // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + // $logContent['all_content'] .= "接口: (get_default_config)\n"; + // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // $this->record_api_log($data, $logContent, null); + // return $this->msg(99999); + // } + } + // 获取用户收藏点赞列表(OK) public function get_user_collect_list($data = ['token'=>'caadd1be045a65f30b92aa805f1de54a','page'=>1,'search_data'=>'']){ // try { @@ -71,7 +161,7 @@ class Usercenter extends Base{ } // 我的菜谱 - public function get_my_cookbook($data = ['token'=>'caadd1be045a65f30b92aa805f1de54a','page'=>1]){ + public function get_my_cookbook($data = ['token'=>'caadd1be045a65f30b92aa805f1de54a','page'=>1,'search_data'=>'']){ // try { if(count(input('post.')) > 0){ $data = input('post.'); @@ -106,17 +196,171 @@ class Usercenter extends Base{ // } } + // 菜谱删除 + public function del_my_cookbook($data = ['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>1,'cookbook_id'=>'33']){ + // try { + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('token', $data)){ + return $this->msg(10001,'token is miss'); + } + if(!array_key_exists('aud_id', $data)){ + return $this->msg(10001,'aud_id is miss'); + } + if(!array_key_exists('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['aud_id'],'intnum')){ + return $this->msg(10005,'aud_id 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->del_my_cookbook_action($data); + return $return_data; + // } catch (\Exception $e) { + // // 捕获异常 + // $logContent["flie"] = $e->getFile(); + // $logContent["line"] = $e->getLine(); + // $logContent['all_content'] = "异常信息:\n"; + // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + // $logContent['all_content'] .= "接口: (get_default_config)\n"; + // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // $this->record_api_log($data, $logContent, null); + // return $this->msg(99999); + // } + } + #######################################################################action####################################################################### #######################################################################action####################################################################### #######################################################################action####################################################################### + + public function get_user_msg_action($data){ + // 获取账号下信息以及用户信息 + $user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->field('id,token,nickname,head_pic,tel,email')->find(); + if(!$user_data){ + return $this->msg(10003); + } + // $return_data = []; + // 获取账号下信息以及用户信息start + $user_all_data['aud_id'] = ''; + $user_all_data['token'] = $user_data['token']; + $user_all_data['nickname'] = $user_data['nickname']; + $user_all_data['head_pic'] = $user_data['head_pic']; + $user_all_data['gender'] = ''; + $user_all_data['age'] = ''; + $user_all_data['height'] = ''; + $user_all_data['weight'] = ''; + $user_all_data['set_kcal'] = ''; + $user_all_data['is_use_set_kcal'] = ''; + $user_all_data['tel'] = $user_data['tel']; + $user_all_data['email'] = $user_data['email']; + $cfc = Db::connect('cfc_db'); + $user_account = $cfc->table($this->kitchenscale_db_msg['user']) + ->where(["token"=>$data['token']]) + ->field('id as aud_id,token,nickname,head_pic,gender,age,height,weight,set_kcal,is_use_set_kcal') + ->find(); + if($user_account){ + if($user_account['set_kcal'] == '.00'){ + $user_account['set_kcal'] = 0; + } + $user_all_data['aud_id'] = $user_account['aud_id']; + $user_all_data['gender'] = $user_account['gender']; + $user_all_data['age'] = $user_account['age']; + $user_all_data['height'] = $user_account['height']; + $user_all_data['weight'] = $user_account['weight']; + $user_all_data['set_kcal'] = $user_account['set_kcal']; + $user_all_data['is_use_set_kcal'] = $user_account['is_use_set_kcal']; + }else{ + return $this->msg(10004); + } + + + // $return_data = $user_all_data; + return $this->msg($user_all_data); + // 获取账号下信息以及用户信息end + } + public function update_user_msg_action($data){ + if($data['gender'] == 0){ + return $this->msg(10005,'性别信息错误'); + } + // 获取账号下信息以及用户信息 + $user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->count(); + if($user_data<=0){ + return $this->msg(10005,'账号信息错误'); + } + + $cfc = Db::connect('cfc_db'); + + $is_user_true = $cfc->table($this->kitchenscale_db_msg['user'])->where(['token'=>$data['token']])->count(); + + $user_msg['nickname'] = $data['nickname']; + $user_msg['head_pic'] = $data['gender'] == 1?'https://tc.pcxbc.com/tsf/1.png':'https://tc.pcxbc.com/tsf/2.png'; + $user_msg['gender'] = $data['gender']; + $user_msg['age'] = $data['age']; + $user_msg['height'] = $data['height']; + $user_msg['weight'] = $data['weight']; + + // $cfc->startTrans(); + // Db::startTrans(); + // try { + // if($is_user_true>0){ + // $user_msg['update_time'] = date('Y-m-d H:i:s'); + // $result = $cfc->table($this->kitchenscale_db_msg['user']) + // ->where(['token'=>$data['token']]) + // ->update($user_msg); + // }else{ + // $user_msg['token'] = $data['token']; + // $result = $cfc->table($this->kitchenscale_db_msg['user']) + // ->insert($user_msg); + // } + // Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->update(['nickname'=>$user_msg['nickname']]); + // // 提交事务 + // $cfc->commit(); + // Db::commit(); + // return $this->msg([]); + // } catch (\Exception $e) { + // // 回滚事务 + // $cfc->rollback(); + // Db::rollback(); + // return $this->msg(10002); + // } + + + + if($is_user_true>0){ + $user_msg['update_time'] = date('Y-m-d H:i:s'); + $result = $cfc->table($this->kitchenscale_db_msg['user']) + ->where(['token'=>$data['token']]) + ->update($user_msg); + }else{ + $user_msg['token'] = $data['token']; + $result = $cfc->table($this->kitchenscale_db_msg['user']) + ->insert($user_msg); + } + Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->update(['nickname'=>$user_msg['nickname']]); + if($result){ + return $this->msg([]); + }else{ + return $this->msg(10002); + } + } public function get_user_collect_list_action($data){ // 获取账号下信息以及用户信息 $user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->count(); if($user_data<=0){ return $this->msg(10005,'账号信息错误'); } - $search_sql_str = "a.token = '".$data['token']."' AND a.is_del = 0"; + + $search_sql_str = "a.token = '".$data['token']."' AND a.is_del = 0 AND b.is_del = 0"; if(!array_key_exists('search_data', $data)){ $data['search_data'] = ""; }else{ @@ -146,6 +390,11 @@ class Usercenter extends Base{ ->field("b.id,b.title,b.cover as cover_id,c.pic_url as cover_url,b.likes_num,b.create_user_token,b.create_user_head_pic,b.create_user_nickname") ->page($data['page'],$this->page_num) ->select(); + + foreach ($collect_list as $key => $value) { + $collect_list[$key]['is_me_like_it'] = 'yes'; + + } return $this->msg([ 'page_now'=>$data['page'], @@ -160,26 +409,99 @@ class Usercenter extends Base{ if($user_data<=0){ return $this->msg(10005,'账号信息错误'); } + + $search_sql_str = "b.create_user_token = '".$data['token']."' AND b.is_del = 0"; + if(!array_key_exists('search_data', $data)){ + $data['search_data'] = ""; + }else{ + if($data['search_data'] === ""){ + $data['search_data'] = ""; + }else{ + $data['search_data'] = " AND (b.title LIKE '%".$data['search_data']."%' OR b.describe_data LIKE '%".$data['search_data']."%')"; + } + } + $search_sql_str = $search_sql_str.$data['search_data']; + $cfc = Db::connect('cfc_db'); $content_num = $cfc->table($this->kitchenscale_db_msg['cookbook']) ->alias('b') ->join($this->kitchenscale_db_msg['uploadimg'].' c','b.cover = c.id','LEFT') - ->where(['b.create_user_token'=>$data['token'],'b.is_del'=>0]) + ->where($search_sql_str) ->count(); $page_total = ceil($content_num/$this->page_num);; - $collect_list = $cfc->table($this->kitchenscale_db_msg['cookbook']) + $content_list = $cfc->table($this->kitchenscale_db_msg['cookbook']) ->alias('b') ->join($this->kitchenscale_db_msg['uploadimg'].' c','b.cover = c.id','LEFT') - ->where(['b.create_user_token'=>$data['token'],'b.is_del'=>0]) + ->where($search_sql_str) ->field("b.id,b.title,b.cover as cover_id,c.pic_url as cover_url,b.likes_num,b.create_user_token,b.create_user_head_pic,b.create_user_nickname") ->page($data['page'],$this->page_num) ->select(); + + // 获取用户收藏列表 + $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']); + } + + // foreach ($collect_list as $key => $value) { + // $collect_list[$key]['is_me_like_it'] = 'yes'; + + // } return $this->msg([ 'page_now'=>$data['page'], 'page_total'=>$page_total, - 'content_list'=>$collect_list + 'content_list'=>$content_list ]); } + public function del_my_cookbook_action($data){ + // 获取账号下信息以及用户信息 + $user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->count(); + if($user_data<=0){ + return $this->msg(10005,'账号信息错误'); + } + + $cfc = Db::connect('cfc_db'); + + $cookbook_data = $cfc->table($this->kitchenscale_db_msg['cookbook'])->where(['id'=>$data['cookbook_id'],'create_user_token'=>$data['token']])->find(); + + if($cookbook_data){ + + // 启动事务 + $cfc->startTrans(); + try{ + $cfc->table($this->kitchenscale_db_msg['cookbook'])->where(['id'=>$data['cookbook_id']])->update(['is_del'=>1]); + $result_banner = $cfc->table($this->kitchenscale_db_msg['banner'])->where(['cookbook_id'=>$cookbook_data['id']])->count(); + if($result_banner > 0){ + $cfc->table($this->kitchenscale_db_msg['banner'])->where(['cookbook_id'=>$cookbook_data['id']])->update(['is_del'=>1]); + } + // 提交事务 + $cfc->commit(); + return $this->msg([]); + } catch (\Exception $e) { + // 回滚事务 + $cfc->rollback(); + return $this->msg(10002); + } + }else{ + return $this->msg(10003); + } + + } + + } \ No newline at end of file diff --git a/application/KitchenScale/controller/app/Wechat.php b/application/KitchenScale/controller/app/Wechat.php new file mode 100644 index 0000000..c5ca3b5 --- /dev/null +++ b/application/KitchenScale/controller/app/Wechat.php @@ -0,0 +1,123 @@ +getSessionKey($code); + + if (empty($sessionData['openid']) || empty($sessionData['session_key'])) { + // throw new Exception('获取openid或session_key失败'); + // return false; + // return $this->msg(10001); + return ['code'=>10002,'msg'=>'获取openid或session_key失败']; + } + + // 2. 解密用户信息 + $userInfo = $this->decryptData($encryptedData, $iv, $sessionData['session_key']); + + if(array_key_exists('phoneNumber',$userInfo)){ + return ['code'=>0,'msg'=>'seccess','data'=>$userInfo]; + }else{ + return ['code'=>10002,'msg'=>'解密用户信息失败']; + } + // if (empty($userInfo['phoneNumber'])) { + // // throw new Exception('获取手机号失败'); + + // }else{ + + // } + + // // 3. 保存或更新用户信息 + // $user = User::where('openid', $sessionData['openid'])->find(); + // if (!$user) { + // $user = new User(); + // $user->openid = $sessionData['openid']; + // } + // $user->phone = $userInfo['phoneNumber']; + // $user->save(); + + // 返回成功信息 + // return ['code' => 0, 'msg' => '登录成功', 'data' => $user]; + // } catch (Exception $e) { + // // 返回错误信息 + // return ['code' => 500, 'msg' => $e->getMessage()]; + // } + } + /** + * 通过code获取openid和session_key + * + * @param string $code + * @return array + * @throws Exception + */ + private function getSessionKey($code) + { + $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->app_id}&secret={$this->app_secret}&js_code={$code}&grant_type=authorization_code"; + $result = file_get_contents($url); + $data = json_decode($result, true); + + if (isset($data['openid']) && isset($data['session_key'])) { + return $data; + } else { + return ['code'=>10002,'msg'=>'获取openid或session_key失败']; + } + } + + /** + * 解密用户信息 + * + * @param string $encryptedData + * @param string $iv + * @param string $sessionKey + * @return array + * @throws Exception + */ + private function decryptData($encryptedData, $iv, $sessionKey) + { + // require_once 'wx_crypt/WXBizDataCrypt.php'; // 引入微信解密类 + // require_once env('root_path') . 'extend/wx_crypt/WXBizDataCrypt.php'; + // dump(ROOT_PATH . 'extend\wx_crypt\wxBizDataCrypt.php'); + require_once ROOT_PATH . 'extend\wx_crypt\wxBizDataCrypt.php'; + + $pc = new \WXBizDataCrypt($this->app_id, $sessionKey); + $errCode = $pc->decryptData($encryptedData, $iv, $data); + + if ($errCode == 0) { + return json_decode($data, true); + } else { + return ['code'=>10002,'msg'=>'解密用户信息失败('.$errCode.')']; + // throw new Exception('解密失败: ' . $errCode); + } + } + + + // 注册 + + + +} \ No newline at end of file diff --git a/application/admin/controller/Appversion.php b/application/admin/controller/Appversion.php index adbf5c6..bacad9b 100644 --- a/application/admin/controller/Appversion.php +++ b/application/admin/controller/Appversion.php @@ -40,7 +40,7 @@ class Appversion extends Base{ // } } $num = Db::table('app_version_log')->where($parameter)->count(); - $result = Db::table('app_version_log')->where($parameter)->order('is_del,id desc')->page($page,$this->page_num)->select(); + $result = Db::table('app_version_log')->where($parameter)->order('id desc')->page($page,$this->page_num)->select(); if(!$pd){ $return_data['num'] = $num; $return_data['data'] = $result; diff --git a/application/admin/controller/Kitchenscale.php b/application/admin/controller/Kitchenscale.php new file mode 100644 index 0000000..4f23559 --- /dev/null +++ b/application/admin/controller/Kitchenscale.php @@ -0,0 +1,62 @@ +'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 + 'user'=>'app_user_data',//banner + ]; + ################################################################食材管理################################################################ + ################################################################食材管理################################################################ + ################################################################食材管理################################################################ + + public function food_ingredients_index_1($page = 1){ + $data = input(); + $pd = true; + $parameter = []; + if(array_key_exists('tt', $data)){ + $page = $data['page_num']; + unset($data['page_num']); + unset($data['tt']); + $pd = false; + } + $cfc = Db::connect('cfc_db'); + $num = $cfc->table($this->kitchenscale_db_msg['foodlist1'])->count(); + $result = $cfc->table($this->kitchenscale_db_msg['foodlist1'])->order('is_del,id')->select(); + if(!$pd){ + $result['num'] = $num; + $result['data'] = $result; + return $this->msg(0,'success',$result); + } + $this->assign([ + 'result' => $result, + 'num' => $num, + ]); + return $this->fetch(); + } + + + + ################################################################other################################################################ + ################################################################other################################################################ + ################################################################other################################################################ + + + +} \ No newline at end of file diff --git a/application/admin/view/index/index.html b/application/admin/view/index/index.html index d48cfcb..5130825 100644 --- a/application/admin/view/index/index.html +++ b/application/admin/view/index/index.html @@ -159,6 +159,42 @@ +