diff --git a/application/KitchenScale/controller/app/Aipart.php b/application/KitchenScale/controller/app/Aipart.php new file mode 100644 index 0000000..46c651d --- /dev/null +++ b/application/KitchenScale/controller/app/Aipart.php @@ -0,0 +1,146 @@ +'app_user_cookbook',//菜谱表 + 'foodlist3'=>'app_food_type_three',//食材列表3 + 'user'=>'app_user_data',//banner + ]; + + // 百度接口参数 + protected $baidu_api_key = "3WRiEJgo0P0Zz3bmV3V1kJsS"; + protected $baidu_secret_key = "yUNCE4QpuO8Ht7kmZm7IRFwr1kECCFv4"; + protected $baidu_accesstoken_expire_time = 432000;//5天 + + // 加 bcadd(,,20) + // 减 bcsub(,,20) + // 乘 bcmul(,,20) + // 除 bcdiv(,,20) + ################################################################百度接口################################################################ + ################################################################百度接口################################################################ + ################################################################百度接口################################################################ + + + // 百度图片识别食材 + public function baidu_identify_food(){ + + try { + // 你的业务逻辑 + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('token', $data)){ + return $this->msg(10001); + } + if(!array_key_exists('img_str', $data)){ + return $this->msg(10001); + } + if(!$this->verify_data_is_ok($data['token'],'str')){ + return $this->msg(10005); + } + if(!$this->verify_data_is_ok($data['img_str'],'str')){ + return $this->msg(10005); + } + $result = $this->baidu_identify_food_action($data['img_str']); + return $result; + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // dump($data); + // die; + $this->record_api_log($data, $logContent, null); + return $this->msg(99999); + } + + } + + + // 识别食材 + private function baidu_identify_food_action($img_str){ + // dump($str); + $access_token = $this->baidu_get_accesstoken(); + if($access_token == false){ + return $this->msg(10002,'识别失败01'); + } + // dump($access_token); + // die; + $curl = curl_init(); + curl_setopt_array($curl, array( + CURLOPT_URL => "https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient?access_token=".$access_token, + CURLOPT_TIMEOUT => 30, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_SSL_VERIFYHOST => false, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_POSTFIELDS => http_build_query(array( + 'image' => $img_str, + 'top_num'=>10 + )), + CURLOPT_HTTPHEADER => array( + 'Content-Type: application/x-www-form-urlencoded', + 'Accept: application/json' + ), + )); + $response = curl_exec($curl); + curl_close($curl); + $result = json_decode($response,true); + if(array_key_exists('result',$result)){ + // return ['code'=>0,'data'=>$result['result']]; + return $this->msg(['name'=>$result['result'][0]['name']]); + }else{ + return $this->msg(10002,'识别失败02'); + } + } + + // 获取AccessToken + private function baidu_get_accesstoken(){ + $baidu_cache = cache('baidu_accesstoken'); + if($baidu_cache != false){ + // dump($baidu_cache); + // die; + return $baidu_cache; + } + $baidu_cache = cache('baidu_accesstoken'); + $curl = curl_init(); + $postData = array( + 'grant_type' => 'client_credentials', + 'client_id' => $this->baidu_api_key, + 'client_secret' => $this->baidu_secret_key + ); + curl_setopt_array($curl, array( + CURLOPT_URL => 'https://aip.baidubce.com/oauth/2.0/token', + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_SSL_VERIFYHOST => false, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POSTFIELDS => http_build_query($postData) + )); + $response = curl_exec($curl); + curl_close($curl); + $rtn = json_decode($response,true); + // dump($rtn); + if(array_key_exists('access_token',$rtn)){ + cache('baidu_accesstoken', $rtn['access_token'], ($rtn['expires_in']-$this->baidu_accesstoken_expire_time)); + return $rtn['access_token']; + }else{ + return false; + } + } + + +} \ No newline at end of file diff --git a/application/KitchenScale/controller/app/Base.php b/application/KitchenScale/controller/app/Base.php index 480f6cb..d690d48 100644 --- a/application/KitchenScale/controller/app/Base.php +++ b/application/KitchenScale/controller/app/Base.php @@ -43,6 +43,9 @@ class Base extends Controller{ ################################################################接口################################################################ // 接口记录 public function record_api_log($params, $error = null, $response = null){ + // dump($params); + // dump($error); + // die; $logContent = "接口请求参数:" . json_encode($params, JSON_UNESCAPED_UNICODE) . PHP_EOL; if ($error) { $logContent .= "错误信息:" . $error['all_content'] . PHP_EOL; @@ -55,9 +58,9 @@ class Base extends Controller{ $logContent .= "返回信息:" . json_encode($response, JSON_UNESCAPED_UNICODE) . PHP_EOL; } // 使用ThinkPHP的日志记录方法 - Log::record($logContent, 'api_log'); - + Log::record($logContent, 'api_log'); } + // 判断token是否过期 public function token_time_validate($token){ diff --git a/application/KitchenScale/controller/app/Cookbook.php b/application/KitchenScale/controller/app/Cookbook.php index d2737ce..78f9301 100644 --- a/application/KitchenScale/controller/app/Cookbook.php +++ b/application/KitchenScale/controller/app/Cookbook.php @@ -16,13 +16,18 @@ class Cookbook extends Base{ ]; protected $kitchenscale_db_msg = [ 'cookbook'=>'app_user_cookbook',//菜谱表 + 'cookbook_food_relation'=>'app_user_cookbook_food_relation',//菜谱表 '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 + 'foodlist1'=>'app_z_national_standard_food_type_1',//食材列表3 + 'foodlist2'=>'app_z_national_standard_food_type_2',//食材列表3 + 'foodlist3'=>'app_z_national_standard_food_type_3',//食材列表3 + 'foodlist4'=>'app_z_national_standard_food_type_4',//食材列表3 + 'nutrition'=>'app_user_cookbook_nutrition',//能量 + 'vitamin'=>'app_user_cookbook_vitamin',//维生素 + 'mineral'=>'app_user_cookbook_mineral',//矿物质 'user_kcal_log'=>'app_user_kcal_log',//食材列表3 'user'=>'app_user_data',//banner ]; @@ -85,7 +90,7 @@ class Cookbook extends Base{ return $return_data; // } catch (\Exception $e) { // // 捕获异常 - // $logContent["file"] = $e->getFile(); + // $logContent["flie"] = $e->getFile(); // $logContent["line"] = $e->getLine(); // $logContent['all_content'] = "异常信息:\n"; // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; @@ -94,14 +99,14 @@ class Cookbook extends Base{ // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; // // 记录日志 - // // $this->record_api_log($data, $logContent, null); + // $this->record_api_log($data, $logContent, null); // return json(['status' => 'error', 'message' => '系统错误']); // } } // 修改菜谱(OK) public function update_cookbook(){ // 尝试捕获异常 - // try { + try { if(count(input('post.')) > 0){ $data = input('post.'); } @@ -153,25 +158,25 @@ class Cookbook extends Base{ $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' => '系统错误']); - // } + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // 记录日志 + $this->record_api_log($data, $logContent, null); + return json(['status' => 'error', 'message' => '系统错误']); + } } // 根据菜谱标签查询列表(首页用)(OK) public function find_by_cook_label($data=['token'=>'','cook_label'=>4,'page'=>'1']){ // 尝试捕获异常 - // try { + try { if(count(input('post.')) > 0){ $data = input('post.'); } @@ -179,37 +184,37 @@ class Cookbook extends Base{ 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(!array_key_exists('page', $data)){ + // return $this->msg(10001,'page is miss'); + // } if(!$this->verify_data_is_ok($data['cook_label'],'intnum')){ return $this->msg(10005,'cook_label type is error'); } - if(!$this->verify_data_is_ok($data['page'],'intnum')){ - return $this->msg(10005,'page type is error'); - } + // if(!$this->verify_data_is_ok($data['page'],'intnum')){ + // return $this->msg(10005,'page type is error'); + // } $return_data = $this->find_by_cook_label_action($data); return $return_data; - // } catch (\Exception $e) { - // // 捕获异常 - // $logContent["file"] = $e->getFile(); - // $logContent["line"] = $e->getLine(); - // $logContent['all_content'] = "异常信息:\n"; - // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; - // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; - // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; - // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; - // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; - // // 记录日志 - // // $this->record_api_log($data, $logContent, null); - // return json(['status' => 'error', 'message' => '系统错误']); - // } + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // 记录日志 + $this->record_api_log($data, $logContent, null); + return json(['status' => 'error', 'message' => '系统错误']); + } } // 根据食材详细查找列表(OK) - public function find_by_food($data=['token'=>'caadd1be045a65f3','food_name'=>'猪肉','page'=>'1']){ + public function find_by_food($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','food_name'=>"2724,2670",'page'=>'1']){ // 尝试捕获异常 - // try { + try { if(count(input('post.')) > 0){ $data = input('post.'); } @@ -219,39 +224,39 @@ class Cookbook extends Base{ if(!array_key_exists('food_name', $data)){ return $this->msg(10001,'food_name is miss'); } - if(!array_key_exists('page', $data)){ - return $this->msg(10001,'page is miss'); - } + // if(!array_key_exists('page', $data)){ + // return $this->msg(10001,'page is miss'); + // } if(!$this->verify_data_is_ok($data['token'],'str')){ return $this->msg(10005,'token type is error'); } if(!$this->verify_data_is_ok($data['food_name'],'str')){ - return $this->msg(10005,'food_name type is error'); - } - if(!$this->verify_data_is_ok($data['page'],'intnum')){ - return $this->msg(10005,'page type is error'); + return $this->msg(10005,'tofood_nameken type is error'); } + // if(!$this->verify_data_is_ok($data['page'],'intnum')){ + // return $this->msg(10005,'page type is error'); + // } $return_data = $this->find_by_food_action($data); return $return_data; - // } catch (\Exception $e) { - // // 捕获异常 - // $logContent["file"] = $e->getFile(); - // $logContent["line"] = $e->getLine(); - // $logContent['all_content'] = "异常信息:\n"; - // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; - // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; - // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; - // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; - // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; - // // 记录日志 - // // $this->record_api_log($data, $logContent, null); - // return json(['status' => 'error', 'message' => '系统错误']); - // } + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // 记录日志 + $this->record_api_log($data, $logContent, null); + return json(['status' => 'error', 'message' => '系统错误']); + } } // 查询食谱的详情(OK) - public function cookbook_details($data=['token'=>'','cookbook_id'=>'21']){ + public function cookbook_details($data=['token'=>'','cookbook_id'=>'3594']){ // 尝试捕获异常 - // try { + try { if(count(input('post.')) > 0){ $data = input('post.'); } @@ -263,25 +268,25 @@ class Cookbook extends Base{ } $return_data = $this->cookbook_details_action($data); return $return_data; - // } catch (\Exception $e) { - // // 捕获异常 - // $logContent["file"] = $e->getFile(); - // $logContent["line"] = $e->getLine(); - // $logContent['all_content'] = "异常信息:\n"; - // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; - // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; - // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; - // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; - // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; - // // 记录日志 - // // $this->record_api_log($data, $logContent, null); - // return json(['status' => 'error', 'message' => '系统错误']); - // } + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // 记录日志 + $this->record_api_log($data, $logContent, null); + return json(['status' => 'error', 'message' => '系统错误']); + } } // 关注行为(OK) public function cookbook_follow($data=['token'=>'caadd1be045a65f3','being_followed'=>'caadd1be045a65f30b92aa805f1de54a']){ // 尝试捕获异常 - // try { + try { if(count(input('post.')) > 0){ $data = input('post.'); } @@ -299,25 +304,25 @@ class Cookbook extends Base{ } $return_data = $this->cookbook_follow_action($data); return $return_data; - // } catch (\Exception $e) { - // // 捕获异常 - // $logContent["file"] = $e->getFile(); - // $logContent["line"] = $e->getLine(); - // $logContent['all_content'] = "异常信息:\n"; - // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; - // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; - // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; - // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; - // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; - // // 记录日志 - // // $this->record_api_log($data, $logContent, null); - // return json(['status' => 'error', 'message' => '系统错误']); - // } + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // 记录日志 + $this->record_api_log($data, $logContent, null); + return json(['status' => 'error', 'message' => '系统错误']); + } } // 点赞收藏菜谱(OK) public function cookbook_like($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','cookbook_id'=>'12']){ // 尝试捕获异常 - // try { + try { if(count(input('post.')) > 0){ $data = input('post.'); } @@ -335,25 +340,25 @@ class Cookbook extends Base{ } $return_data = $this->cookbook_like_action($data); return $return_data; - // } catch (\Exception $e) { - // // 捕获异常 - // $logContent["file"] = $e->getFile(); - // $logContent["line"] = $e->getLine(); - // $logContent['all_content'] = "异常信息:\n"; - // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; - // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; - // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; - // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; - // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; - // // 记录日志 - // // $this->record_api_log($data, $logContent, null); - // return json(['status' => 'error', 'message' => '系统错误']); - // } + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // 记录日志 + $this->record_api_log($data, $logContent, null); + return json(['status' => 'error', 'message' => '系统错误']); + } } // 计算当前食材重量的卡路里(OK) public function food_count_kcal($data=['food_name'=>'鸡肉','food_weight'=>456.37]){ // 尝试捕获异常 - // try { + try { if(count(input('post.')) > 0){ $data = input('post.'); } @@ -372,25 +377,25 @@ class Cookbook extends Base{ $return_data = $this->food_count_kcal_action($data); return $return_data; - // } catch (\Exception $e) { - // // 捕获异常 - // $logContent["file"] = $e->getFile(); - // $logContent["line"] = $e->getLine(); - // $logContent['all_content'] = "异常信息:\n"; - // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; - // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; - // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; - // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; - // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; - // // 记录日志 - // // $this->record_api_log($data, $logContent, null); - // return json(['status' => 'error', 'message' => '系统错误']); - // } + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // 记录日志 + $this->record_api_log($data, $logContent, null); + return json(['status' => 'error', 'message' => '系统错误']); + } } // 食材列表查询接口(OK) public function find_food($data=['food_name'=>'鸡肉']){ // 尝试捕获异常 - // try { + try { if(count(input('post.')) > 0){ $data = input('post.'); } @@ -403,62 +408,62 @@ class Cookbook extends Base{ $return_data = $this->find_food_action($data); return $return_data; - // } catch (\Exception $e) { - // // 捕获异常 - // $logContent["file"] = $e->getFile(); - // $logContent["line"] = $e->getLine(); - // $logContent['all_content'] = "异常信息:\n"; - // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; - // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; - // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; - // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; - // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; - // // 记录日志 - // // $this->record_api_log($data, $logContent, null); - // return json(['status' => 'error', 'message' => '系统错误']); - // } + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // 记录日志 + $this->record_api_log($data, $logContent, null); + return json(['status' => 'error', 'message' => '系统错误']); + } } // 获取所有食材列表 public function get_food_list($data=['food_level2_id'=>'4','search_data'=>'','page'=>1]){ // 尝试捕获异常 - // try { + try { if(count(input('post.')) > 0){ $data = input('post.'); } if(!array_key_exists('food_level2_id', $data)){ return $this->msg(10001,'food_level2_id is miss'); } - if(!array_key_exists('page', $data)){ - return $this->msg(10001,'page is miss'); - } + // 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'); - } + // 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); 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' => '系统错误']); - // } + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // 记录日志 + $this->record_api_log($data, $logContent, null); + return json(['status' => 'error', 'message' => '系统错误']); + } } // 获取所有食谱label public function get_cookbook_label_list($data=['token'=>'caadd1be045a65f30b92aa805f1de54a']){ // 尝试捕获异常 - // try { + try { if(count(input('post.')) > 0){ $data = input('post.'); } @@ -471,20 +476,52 @@ class Cookbook extends Base{ $return_data = $this->get_cookbook_label_list_action($data); return $return_data; - // } catch (\Exception $e) { - // // 捕获异常 - // $logContent["file"] = $e->getFile(); - // $logContent["line"] = $e->getLine(); - // $logContent['all_content'] = "异常信息:\n"; - // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; - // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; - // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; - // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; - // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; - // // 记录日志 - // // $this->record_api_log($data, $logContent, null); - // return json(['status' => 'error', 'message' => '系统错误']); - // } + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // 记录日志 + $this->record_api_log($data, $logContent, null); + return json(['status' => 'error', 'message' => '系统错误']); + } + } + + // 获取查询页页面导航食材列表 + public function get_search_food_page_list($data=['token'=>'caadd1be045a65f30b92aa805f1de54a']){ + // 尝试捕获异常 + try { + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('token', $data)){ + return $this->msg(10001,'token is miss'); + } + if(!$this->verify_data_is_ok($data['token'],'str')){ + return $this->msg(10005,'token type is error'); + } + + $return_data = $this->get_search_food_page_list_action($data); + return $return_data; + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $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####################################################################### @@ -492,6 +529,7 @@ class Cookbook extends Base{ #######################################################################action####################################################################### public function add_cookbook_action($data){ + // 获取账号下信息以及用户信息 $user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->field('id,token,nickname,head_pic')->find(); if(!$user_data){ @@ -507,8 +545,8 @@ class Cookbook extends Base{ $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 (!array_key_exists('id', $value) || !array_key_exists('weight', $value)) { + return $this->msg(10001,'食材缺少id或者重量'); } if(!$this->verify_data_is_ok($value['name'],'str')){ return $this->msg(10005,'食材名称格式错误'); @@ -516,8 +554,8 @@ class Cookbook extends Base{ 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']); + if(!in_array($value['id'], $food_type)){ + array_push($food_type, $value['id']); } } // 检验一下step_list是否合规(步骤列表) @@ -534,10 +572,12 @@ class Cookbook extends Base{ } } } + $cfc = Db::connect('cfc_db'); // 获取账号下信息以及用户信息 $user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->field('id,token,nickname,head_pic')->find(); + // 处理食材卡路里start $kcal_data = $this->count_calorie($data['food_list'],$data['step_list']); $data['food_list'] =$kcal_data; @@ -554,19 +594,36 @@ class Cookbook extends Base{ 'describe_data'=>$data['description'], 'food_data'=>json_encode($data['food_list']), 'step_data'=>json_encode($data['step_list']), - 'food_type'=>implode(',', $food_type), + // 'food_type'=>implode(',', $food_type), 'cook_label'=>$data['cook_label'], 'create_time'=>date('Y-m-d H:i:s') ]; // dump($insert_data); // die; - $cook_book_result = $cfc->table($this->kitchenscale_db_msg['cookbook'])->insert($insert_data); - - if($cook_book_result){ + // 启动事务 + Db::startTrans(); + try{ + $cook_book_result = $cfc->table($this->kitchenscale_db_msg['cookbook'])->insertGetId($insert_data); + $food_type_insert = []; + foreach ($food_type as $key => $value) { + array_push($food_type_insert,['cookbook_id'=>$cook_book_result,'food_id'=>$value]); + } + $cfc->table($this->kitchenscale_db_msg['cookbook_food_relation'])->insertAll($food_type_insert); + // 提交事务 + Db::commit(); return $this->msg([]); - }else{ + } catch (\Exception $e) { + // 回滚事务 + Db::rollback(); return $this->msg(10002); } + + + // if($cook_book_result){ + // return $this->msg([]); + // }else{ + // return $this->msg(10002); + // } } public function update_cookbook_action($data){ // 获取账号下信息以及用户信息 @@ -584,8 +641,8 @@ class Cookbook extends Base{ $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 (!array_key_exists('id', $value) || !array_key_exists('weight', $value)) { + return $this->msg(10001,'食材缺少id或者重量'); } if(!$this->verify_data_is_ok($value['name'],'str')){ return $this->msg(10005,'食材名称格式错误'); @@ -593,8 +650,8 @@ class Cookbook extends Base{ 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']); + if(!in_array($value['id'], $food_type)){ + array_push($food_type, $value['id']); } } // 检验一下step_list是否合规(步骤列表) @@ -634,12 +691,30 @@ class Cookbook extends Base{ 'describe_data'=>$data['description'], 'food_data'=>json_encode($data['food_list']), 'step_data'=>json_encode($data['step_list']), - 'food_type'=>implode(',', $food_type), + // '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); + + // 启动事务 + Db::startTrans(); + try{ + $cook_book_result = $cfc->table($this->kitchenscale_db_msg['cookbook'])->where(['id'=>$data['cookbook_id']])->update($insert_data); + $food_type_insert = []; + foreach ($food_type as $key => $value) { + array_push($food_type_insert,['cookbook_id'=>$data['cookbook_id'],'food_id'=>$value]); + } + $cfc->table($this->kitchenscale_db_msg['cookbook_food_relation'])->where('cookbook_id',$data['cookbook_id'])->delete(); + $cfc->table($this->kitchenscale_db_msg['cookbook_food_relation'])->insertAll($food_type_insert); + // 提交事务 + Db::commit(); + return $this->msg([]); + } catch (\Exception $e) { + // 回滚事务 + Db::rollback(); + return $this->msg(10002); + } // dump($cook_book_result); // die; if($cook_book_result){ @@ -651,8 +726,8 @@ class Cookbook extends Base{ public function find_by_cook_label_action($data){ - $page_now = $data['page']; - $page_total = $data['page']; + $page_now = array_key_exists('page',$data)?$data['page']:1; + $page_total = $page_now; $page_num = 20; $cook_label = $data['cook_label']; @@ -723,28 +798,61 @@ class Cookbook extends Base{ ]); } public function find_by_food_action($data){ + + // 获取账号下信息以及用户信息 $user_data = Db::table($this->reedaw_db_msg['zhanghao'])->where(['token'=>$data['token']])->field('id,token,nickname,head_pic')->find(); if(!$user_data){ return $this->msg(20001,'账号信息错误'); } - $page_now = $data['page']; - $page_total = $data['page']; + + $page_now = array_key_exists('page',$data)?$data['page']:1; + $page_total = $page_now; $page_num = 20; $food_name = $data['food_name']; $cfc = Db::connect('cfc_db'); - $content_num = $cfc->table($this->kitchenscale_db_msg['cookbook']) - ->where("food_type like '%$food_name%'") - ->count(); - $page_total = ceil($content_num/$page_num); - $content_list = $cfc->table($this->kitchenscale_db_msg['cookbook']) - ->alias('cookbook') - ->join($this->kitchenscale_db_msg['uploadimg'].' uploadimg','cookbook.cover = uploadimg.id','LEFT') - ->where("cookbook.food_type like '%$food_name%'") - ->field('cookbook.id,cookbook.title,uploadimg.pic_url as cover,cookbook.create_user_head_pic,cookbook.create_user_nickname,cookbook.likes_num') + + // $food_name = implode(',',$food_name); + // dump($food_name); + // die; + + $content_num = $cfc->table($this->kitchenscale_db_msg['cookbook_food_relation']) + ->where("food_id in ($food_name)") + ->group('cookbook_id') + ->field('count(distinct cookbook_id) as count') // 显式指定列名 + ->select(); // 然后获取结果 + // dump($content_num); + $page_total = ceil(count($content_num)/$page_num); + + $content_list = $cfc->table($this->kitchenscale_db_msg['cookbook_food_relation']) + ->alias('a') + ->join($this->kitchenscale_db_msg['cookbook'].' b','a.cookbook_id = b.id','LEFT') + ->join($this->kitchenscale_db_msg['uploadimg'].' c','b.cover = c.id','LEFT') + ->where("a.food_id in ($food_name)") + ->field('b.id,b.title,c.pic_url as cover,b.create_user_head_pic,b.create_user_nickname,b.likes_num') + ->group('b.id,b.title,c.pic_url,b.create_user_head_pic,b.create_user_nickname,b.likes_num') ->page("$page_now,$page_num") ->select(); + + // dump($food_name); + // dump($content_num); + // dump($page_total); + // dump($content_list); + // die; + + // $content_num = $cfc->table($this->kitchenscale_db_msg['cookbook']) + // ->where("food_type like '%$food_name%'") + // ->count(); + // $page_total = ceil($content_num/$page_num); + + // $content_list = $cfc->table($this->kitchenscale_db_msg['cookbook']) + // ->alias('cookbook') + // ->join($this->kitchenscale_db_msg['uploadimg'].' uploadimg','cookbook.cover = uploadimg.id','LEFT') + // ->where("cookbook.food_type like '%$food_name%'") + // ->field('cookbook.id,cookbook.title,uploadimg.pic_url as cover,cookbook.create_user_head_pic,cookbook.create_user_nickname,cookbook.likes_num') + // ->page("$page_now,$page_num") + // ->select(); // 获取用户收藏列表 $my_collect_list = $cfc->table($this->kitchenscale_db_msg['collect_list']) @@ -764,7 +872,7 @@ class Cookbook extends Base{ } unset($content_list[$key]['ROW_NUMBER']); } - + return $this->msg([ 'page_now'=>$page_now, 'page_total'=>$page_total, @@ -860,12 +968,51 @@ class Cookbook extends Base{ }else{ $cookbook_data['collect_status'] = 'no'; } - - + // 处理营养物质 + $nutrition = $cfc->table($this->kitchenscale_db_msg['nutrition'])->where(['cookbook_id'=>$data['cookbook_id']])->field('name_en,name_ch as name,unit,value as weight,nrv')->select(); + $vitamin = $cfc->table($this->kitchenscale_db_msg['vitamin'])->where(['cookbook_id'=>$data['cookbook_id']])->field('name_en,name_ch as name,unit,value as weight,nrv')->select(); + $mineral = $cfc->table($this->kitchenscale_db_msg['mineral'])->where(['cookbook_id'=>$data['cookbook_id']])->field('name_en,name_ch as name,unit,value as weight,nrv')->select(); + + + $cookbook_data['tags'] = []; + + array_push($cookbook_data['tags'],[ + 'title'=>'所需食材', + 'desc'=>'重量', + 'list'=>$cookbook_data['food_data'], + ]); + array_push($cookbook_data['tags'],[ + 'title'=>'能量', + 'desc'=>'含量', + 'list'=>$nutrition, + ]); + array_push($cookbook_data['tags'],[ + 'title'=>'维生素', + 'desc'=>'含量', + 'list'=>$vitamin, + ]); + array_push($cookbook_data['tags'],[ + 'title'=>'矿物质', + 'desc'=>'含量', + 'list'=>$mineral, + ]); + + + // $cookbook_data['nutrition']['list'] = $nutrition; + // // $cookbook_data['nutrition']['title'] = '能量及宏量营养素'; + // $cookbook_data['nutrition']['title'] = '能量'; + // $cookbook_data['nutrition']['desc'] = '含量'; + // $cookbook_data['vitamin']['list'] = $vitamin; + // $cookbook_data['vitamin']['title'] = '维生素'; + // $cookbook_data['vitamin']['desc'] = '含量'; + // $cookbook_data['mineral']['list'] = $mineral; + // $cookbook_data['mineral']['title'] = '矿物质'; + // $cookbook_data['mineral']['desc'] = '含量'; + $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['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']); @@ -1019,13 +1166,13 @@ class Cookbook extends Base{ } public function find_food_action($data){ $cfc = Db::connect('cfc_db'); - $food_data = $cfc->query("select f3.id,f3.name,f3.kcal,f2.id as up_one_level_id,f1.id as up_two_level_id + $food_data = $cfc->query("select f3.id,f3.food_name as name,f3.pic_url,f3.Calorie_val as kcal,f3.weight_unit as unit,f2.id as up_one_level_id,f1.id as up_two_level_id from ".$this->kitchenscale_db_msg['foodlist3']." as f3 LEFT JOIN ".$this->kitchenscale_db_msg['foodlist2']." as f2 on f3.two_id = f2.id LEFT JOIN ".$this->kitchenscale_db_msg['foodlist1']." as f1 on f2.one_id = f1.id - where f3.is_del=0 AND f3.name like '%".$data['food_name']."%' + where f3.is_del=0 AND f3.food_name like '%".$data['food_name']."%' "); if(count($food_data)>0){ @@ -1035,7 +1182,12 @@ class Cookbook extends Base{ } } public function get_food_list_action($data){ - $cp_page_num = 100; + + if(!array_key_exists('page',$data)){ + $data['page'] = 1; + } + + $cp_page_num = 30; $cfc = Db::connect('cfc_db'); // 获取食材分类列表start // $foodlist1 = $cfc->table($this->kitchenscale_db_msg['foodlist1'])->where("is_del = 0")->field('id,name')->select(); @@ -1068,9 +1220,14 @@ class Cookbook extends Base{ // ->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') + ->field('id,food_name as name,pic_url,Calorie_val as kcal,weight_unit as unit') ->page($data['page'],$cp_page_num) ->select(); + + + foreach ($collect_list as $key => $value) { + $collect_list[$key]['pic_url'] = "https://tc.pcxbc.com/kitchenscale_all".$collect_list[$key]['pic_url']; + } // $return_data = []; // $temporary_arr = []; // foreach ($collect_list as $key => $value) { @@ -1121,6 +1278,43 @@ class Cookbook extends Base{ return $this->msg($cook_label); // 获取菜谱分类标签end } + + public function get_search_food_page_list_action($data){ + $cfc = Db::connect('cfc_db'); + // 获取菜谱分类标签start + // 蔬菜类id:4 6 7 + // 肉类id:10 11 14 + $data_list = $cfc->table($this->kitchenscale_db_msg['foodlist3']) + ->where('two_id in (4,6,7,10,11,14) and is_popular = 1') + ->field('id,name,two_id') + ->select(); + $result = [ + 'food'=>[ + 'title'=>'流行食材', + 'list'=>[ + [ + 'title'=>'蔬菜', + 'list'=>[] + ], + [ + 'title'=>'肉类', + 'list'=>[] + ], + ] + ], + 'log'=>[], + ]; + foreach ($data_list as $key => $value) { + if($value['two_id'] == 4 || $value['two_id'] == 6 || $value['two_id'] == 7){ + array_push($result['food']['list'][0]['list'],['id'=>$value['id'],'name'=>$value['name']]); + }else if($value['two_id'] == 10 || $value['two_id'] == 11 || $value['two_id'] == 14){ + array_push($result['food']['list'][1]['list'],['id'=>$value['id'],'name'=>$value['name']]); + } + } + + return $this->msg($result); + // 获取菜谱分类标签end + } @@ -1132,10 +1326,10 @@ class Cookbook extends Base{ // 处理食材的卡路里 public function count_calorie($data,$step){ - $foot_name = array_column($data, 'name'); + $foot_name = array_column($data, 'id'); $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("id,name,kcal")->select(); + $foot_kcal = $cfc->table($this->kitchenscale_db_msg['foodlist3'])->where("id in ('".implode("','", $foot_name)."')")->field("id,food_name as name,Calorie_val as kcal")->select(); $foot_kcal2 = []; foreach ($foot_kcal as $key => $value) { $foot_kcal2[$value['name']] = [$value['id'],$value['kcal']]; @@ -1154,19 +1348,6 @@ class Cookbook extends Base{ $data[$key]['unit'] = '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; } diff --git a/application/KitchenScale/controller/app/Countfood.php b/application/KitchenScale/controller/app/Countfood.php index c237c9f..c9677dc 100644 --- a/application/KitchenScale/controller/app/Countfood.php +++ b/application/KitchenScale/controller/app/Countfood.php @@ -18,7 +18,7 @@ class Countfood extends Base{ 'uploadimg'=>'app_user_upload_img',//素材表 'followlist'=>'app_user_follow_list',//关注列表 'collect_list'=>'app_user_collect_list',//收藏列表 - 'foodlist3'=>'app_food_type_three',//食材列表3 + 'foodlist3'=>'app_standard_food_type_three',//食材列表3 'eat_log'=>'app_user_kcal_log',//食材列表3 'user'=>'app_user_data',//banner ]; @@ -32,9 +32,9 @@ class Countfood extends Base{ ################################################################接口################################################################ // 添加每日摄入记录 - 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']]]){ + public function add_intake_food($data=['token'=>'3e5876042361c8cb42bd48c46918f737','aud_id'=>6,'meals_type'=>'早餐','food_list'=>[['id'=>2778,'name'=>"啤酒(X)",'weight'=>'150','unit'=>'g']]]){ // 尝试捕获异常 - // try { + try { if(count(input('post.')) > 0){ $data = input('post.'); } @@ -58,25 +58,25 @@ class Countfood extends Base{ } $return_data = $this->add_intake_food_action($data); return $return_data; - // } catch (\Exception $e) { - // // 捕获异常 - // $logContent["file"] = $e->getFile(); - // $logContent["line"] = $e->getLine(); - // $logContent['all_content'] = "异常信息:\n"; - // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; - // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; - // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; - // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; - // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; - // // 记录日志 - // // $this->record_api_log($data, $logContent, null); - // return json(['status' => 'error', 'message' => '系统错误']); - // } + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // 记录日志 + $this->record_api_log($data, $logContent, null); + return json(['status' => 'error', 'message' => '系统错误']); + } } // 获取记食器板块内容 public function get_countfoot_content($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>61,'time'=>'2025-03-17']){ // 尝试捕获异常 - // try { + try { if(count(input('post.')) > 0){ $data = input('post.'); } @@ -101,25 +101,25 @@ class Countfood extends Base{ $return_data = $this->get_countfoot_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' => '系统错误']); - // } + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // 记录日志 + $this->record_api_log($data, $logContent, null); + return json(['status' => 'error', 'message' => '系统错误']); + } } // 获取记食器记录 public function get_log_list($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>1,'s_time'=>'2025-03-15','e_time'=>'2025-03-16']){ // 尝试捕获异常 - // try { + try { if(count(input('post.')) > 0){ $data = input('post.'); } @@ -150,25 +150,25 @@ class Countfood extends Base{ $return_data = $this->get_log_list_action($data); return $return_data; - // } catch (\Exception $e) { - // // 捕获异常 - // $logContent["file"] = $e->getFile(); - // $logContent["line"] = $e->getLine(); - // $logContent['all_content'] = "异常信息:\n"; - // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; - // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; - // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; - // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; - // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; - // // 记录日志 - // // $this->record_api_log($data, $logContent, null); - // return json(['status' => 'error', 'message' => '系统错误']); - // } + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // 记录日志 + $this->record_api_log($data, $logContent, null); + return json(['status' => 'error', 'message' => '系统错误']); + } } // 计食器板块-设置内容 public function set_up_content($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>61]){ // 尝试捕获异常 - // try { + try { if(count(input('post.')) > 0){ $data = input('post.'); } @@ -187,25 +187,25 @@ class Countfood extends Base{ $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' => '系统错误']); - // } + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // 记录日志 + $this->record_api_log($data, $logContent, null); + return json(['status' => 'error', 'message' => '系统错误']); + } } // 设置用户的卡路里 public function set_user_kcal($data=['token'=>'caadd1be045a65f30b92aa805f1de54a','aud_id'=>61,'set_kcal'=>2000]){ // 尝试捕获异常 - // try { + try { if(count(input('post.')) > 0){ $data = input('post.'); } @@ -230,20 +230,20 @@ class Countfood extends Base{ $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' => '系统错误']); - // } + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // 记录日志 + $this->record_api_log($data, $logContent, null); + return json(['status' => 'error', 'message' => '系统错误']); + } } #######################################################################action####################################################################### #######################################################################action####################################################################### @@ -254,6 +254,7 @@ class Countfood extends Base{ // 除 bcdiv(,,20) 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']]) @@ -262,6 +263,7 @@ class Countfood extends Base{ if(!$user_data){ return $this->msg(10003); } + // 统计食物的id $food_id_arr = []; @@ -286,6 +288,7 @@ class Countfood extends Base{ } 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') @@ -320,12 +323,13 @@ class Countfood extends Base{ // 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['eat_log'])->insertAll($data['food_list']); if ($result !== count($data['food_list'])) { Db::rollback(); - return $this->msg(10001); + return $this->msg(10002,'添加数据错误'); } else { Db::commit(); return $this->msg([]); @@ -335,6 +339,11 @@ class Countfood extends Base{ Db::rollback(); return $this->msg(10002); } + + // dump($data); + // dump($food_id_arr); + // dump($food_content); + // die; } public function get_countfoot_content_action($data){ $cfc = Db::connect('cfc_db'); diff --git a/application/KitchenScale/controller/app/Index.php b/application/KitchenScale/controller/app/Index.php index 10ff122..1d9b717 100644 --- a/application/KitchenScale/controller/app/Index.php +++ b/application/KitchenScale/controller/app/Index.php @@ -18,9 +18,10 @@ class Index extends Base{ 'cookbook'=>'app_user_cookbook',//菜谱表 'cookbook_label'=>'app_user_cookbook_label',//菜谱标签表 'uploadimg'=>'app_user_upload_img',//图片素材表 - 'foodlist1'=>'app_food_type_one',//食材列表1 - 'foodlist2'=>'app_food_type_two',//食材列表2 - 'foodlist3'=>'app_food_type_three',//食材列表3 + 'foodlist1'=>'app_z_national_standard_food_type_1',//食材列表1 + 'foodlist2'=>'app_z_national_standard_food_type_2',//食材列表2 + 'foodlist3'=>'app_z_national_standard_food_type_3',//食材列表3 + 'foodlist4'=>'app_z_national_standard_food_type_4',//食材列表3 'collect_list'=>'app_user_collect_list',//点赞表 'banner'=>'app_banner_data',//banner 'version'=>'app_version_log',//版本表 @@ -157,7 +158,7 @@ class Index extends Base{ // 获取默认配置信息(包含:食材的分类列表,用户角色信息)(OK) public function get_default_config($data = ['token'=>'']){ - // try { + try { if(count(input('post.')) > 0){ $data = input('post.'); } @@ -169,25 +170,25 @@ class Index extends Base{ // } $return_data = $this->get_default_config_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); - // } + } 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 search_column($data = ['search_data'=>'鱼','token'=>'']){ - // try { + public function search_column($data = ['search_data'=>'鱼','token'=>'','page'=>1]){ + try { // 你的业务逻辑 if(count(input('post.')) > 0){ $data = input('post.'); @@ -195,25 +196,31 @@ class Index extends Base{ if(!array_key_exists('search_data', $data)){ return $this->msg(10001); } + // if(!array_key_exists('page', $data)){ + // return $this->msg(10001,'page is miss'); + // } if(!$this->verify_data_is_ok($data['search_data'],'str')){ return $this->msg(10005); } + // if(!$this->verify_data_is_ok($data['page'],'intnum')){ + // return $this->msg(10005,'page type is error'); + // } $return_data = $this->search_column_action($data); return $return_data; - // } catch (\Exception $e) { - // // 捕获异常 - // $logContent["flie"] = $e->getFile(); - // $logContent["line"] = $e->getLine(); - // $logContent['all_content'] = "异常信息:\n"; - // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; - // $logContent['all_content'] .= "接口: (search_column)\n"; - // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; - // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; - // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; - // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; - // $this->record_api_log($data, $logContent, null); - // return $this->msg(99999); - // } + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "接口: (search_column)\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + $this->record_api_log($data, $logContent, null); + return $this->msg(99999); + } } #######################################################################action####################################################################### @@ -234,9 +241,14 @@ class Index extends Base{ ['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(); + // $foodlist1 = $cfc->table($this->kitchenscale_db_msg['foodlist1'])->where("is_del = 0")->order('sort_num desc')->field('id,name')->select(); + + $foodlist1 = $cfc->query("SELECT id,name FROM ".$this->kitchenscale_db_msg['foodlist1']." WHERE is_del = 0 ORDER BY sort_num"); + // dump($foodlist1); + // die; $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) { @@ -314,13 +326,23 @@ class Index extends Base{ // $cookbook = new Cookbook(); $cfc = Db::connect('cfc_db'); + $page_now = array_key_exists('page',$data)?$data['page']:1; + $page_total = $page_now; + $page_num = 20; // 获取菜谱信息 + $content_num = $cfc->table($this->kitchenscale_db_msg['cookbook']) + ->where("title LIKE '%".$data['search_data']."%' OR describe_data LIKE '%".$data['search_data']."%'") + ->count(); + $page_total = ceil($content_num/$page_num); + $content_list = $cfc->table($this->kitchenscale_db_msg['cookbook']) ->alias('cookbook') ->join($this->kitchenscale_db_msg['uploadimg'].' uploadimg','cookbook.cover = uploadimg.id','LEFT') ->where("cookbook.title LIKE '%".$data['search_data']."%' OR cookbook.describe_data LIKE '%".$data['search_data']."%'") ->field("cookbook.id,cookbook.title,cookbook.create_user_head_pic,cookbook.create_user_nickname,cookbook.likes_num,uploadimg.pic_url as cover") + ->page("$page_now,$page_num") ->select(); + if(count($content_list)<=0){ return $this->msg([]); } @@ -366,7 +388,12 @@ class Index extends Base{ unset($content_list[$key]['ROW_NUMBER']); } } - return $this->msg($content_list); + + return $this->msg([ + 'page_now'=>$page_now, + 'page_total'=>$page_total, + 'content_list'=>$content_list + ]); } diff --git a/application/KitchenScale/controller/app/Login.php b/application/KitchenScale/controller/app/Login.php new file mode 100644 index 0000000..9130295 --- /dev/null +++ b/application/KitchenScale/controller/app/Login.php @@ -0,0 +1,705 @@ +'app_account_number', + ]; + + ################################################################接口################################################################ + ################################################################接口################################################################ + ################################################################接口################################################################ + + // 注册 + public function register_action($data = ['data'=>13408173311,'password'=>'123','code'=>'746119']){ + try { + // 你的业务逻辑 + // 验证是否前段发送过来的数据 + if(count(input('post.')) > 0){ + $data = input('post.'); + } + // 验证数据项是否完整 + if(!array_key_exists('data', $data) || !array_key_exists('password', $data) || !array_key_exists('code', $data)){ + return $this->msg(10001); + } + // 验证数据值是否合规 + if(!$data['data'] || !$data['password'] || !$data['code']){ + return $this->msg(10006); + } + if(!$this->verify_data_is_ok($data['password'],'str')){ + return $this->msg(10005); + } + if(!$this->verify_data_is_ok($data['code'],'num')){ + return $this->msg(10005); + } + // 验证是手机还是邮箱 + $montage_data = $this->is_tel_email($data['data']); + if($montage_data == false){ + return $this->msg(10005); + } + + // 查询账号是否已经注册 + $inspect_repeat = Db::table($this->login_use_db_name['1'])->where([$montage_data=>$data['data'],'is_del'=>0])->count(); + + if($inspect_repeat > 0){ + return $this->msg(10002,'注册失败,账号已存在'); + } + + // 检查验证码 + $code_result = $this->check_code($data['data'],$data['code']); + if($code_result !== true){ + return $this->msg(10002,$code_result); + } + // 验证完之后 + $set_data = []; + if($montage_data == 'tel'){ + $set_data['tel'] = $data['data']; + }else{ + $set_data['email'] = $data['data']; + } + $set_data['password'] = $data['password']; + $set_data['head_pic'] = $this->default_head_pic; + $set_data['nickname'] = '用户'.time(); + $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($data['data'].$this->create_random_string(12).time()); + $result = Db::table($this->login_use_db_name['1'])->insertGetId($set_data); + if($result){ + $return_data = $this->msg(['token'=>$set_data['token'],'aan_id'=>$result]); + }else{ + $return_data = $this->msg(10002); + } + + // 成功 + $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'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "方法: " . __METHOD__ . "\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 reset_password($data = ['data'=>'18530934717','password'=>'ceshi1','c_password'=>'ceshi1','code'=>'491661']){ + try { + // 你的业务逻辑 + // 验证是否前段发送过来的数据 + if(count(input('post.')) > 0){ + $data = input('post.'); + } + // 验证数据项是否完整 + if(!array_key_exists('data', $data) || !array_key_exists('password', $data) || !array_key_exists('c_password', $data) || !array_key_exists('code', $data)){ + return $this->msg(10001); + } + // 验证数据值是否合规 + if($data['password'] != $data['c_password']){ + return $this->msg(10003,'两次密码不一致'); + } + if($data['password'] == ''){ + return $this->msg(10003,'密码不能为空'); + } + if(!$this->verify_data_is_ok($data['password'],'str')){ + return $this->msg(10005); + } + if(!$this->verify_data_is_ok($data['code'],'num')){ + return $this->msg(10005); + } + // 检查验证码 + $code_result = $this->check_code($data['data'],$data['code']); + if($code_result !== true){ + return $this->msg(10003,$code_result); + } + $t_y = $this->is_tel_email($data['data']); + if($t_y === false){ + return $this->msg(10003,'账号格式错误'); + } + // 检查账号是否存在 + $find_data = Db::table($this->login_use_db_name['1'])->where([$t_y=>$data['data'],'is_del'=>0])->field('id,token')->find(); + if(!$find_data){ + return $this->msg(10003); + } + $result = Db::table($this->login_use_db_name['1'])->where([$t_y=>$data['data']])->update(['password'=>$data['password']]); + if($result){ + $return_data = $this->msg(['token'=>$find_data['token'],'aan_id'=>$find_data['id']]); + }else{ + $return_data = $this->msg(10002); + } + + // 成功 + $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'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "方法: " . __METHOD__ . "\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 login_action($data = ['data'=>'18530934717','validate_data'=>'0932','type'=>'login','validate_type'=>'password']){ + try { + // 你的业务逻辑 + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('data', $data) || !array_key_exists('validate_data', $data) || !array_key_exists('validate_type', $data)){ + return $this->msg(10001); + } + // 检测是否为手机 + $montage_data = $this->is_tel_email($data['data']); + if($montage_data == false){ + return $this->msg(10005); + } + + $verify_result[$montage_data] = $data['data']; + $verify_result['is_del'] = 0; + // 检测校验途径 + if($data['validate_type'] == 'code'){ + $code_name = $data['data']; + if($this->check_code($code_name,$data['validate_data']) === true){ + $result = Db::table($this->login_use_db_name['1'])->where($verify_result)->field('id,token')->find(); + if($result){ + Db::table($this->login_use_db_name['1'])->where($verify_result)->update(['login_time'=>date('Y-m-d H:i:s')]); + $return_data = $this->msg(['token'=>$result['token'],'aan_id'=>$result['id']]); + }else{ + $set_data['password'] = ''; + $set_data[$montage_data] = $data['data']; + $set_data['head_pic'] = $this->default_head_pic; + $set_data['nickname'] = '用户'.$data['data']; + $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($data['data'].$this->create_random_string(12).time()); + $result = Db::table($this->login_use_db_name['1'])->insertGetId($set_data); + if($result){ + $return_data = $this->msg(['token'=>$set_data['token'],'aan_id'=>$result],'登录成功'); + }else{ + $return_data = $this->msg(10002); + } + } + }else{ + $return_data = $this->msg(10003,'登录失败,验证码错误或失效'); + } + }else if($data['validate_type'] == 'password'){ + // $verify_result['password'] = $data['validate_data']; + $result = Db::table($this->login_use_db_name['1'])->where($verify_result)->field('id,token,password')->find(); + if($result){ + if($result['password'] == ''){ + $return_data = $this->msg(10003,'该账户未设密码,请用验证码登录'); + } + if($data['validate_data'] != $result['password']){ + $return_data = $this->msg(10003,'账号密码错误'); + }else{ + + Db::table($this->login_use_db_name['1'])->where($verify_result)->update(['login_time'=>date('Y-m-d H:i:s')]); + $return_data = $this->msg(['token'=>$result['token'],'aan_id'=>$result['id']],'登录成功'); + } + }else{ + $return_data = $this->msg(10003,'账号未注册,请先注册'); + } + }else{ + $return_data = $this->msg(10003,'校验参数错误'); + } + + // 成功 + $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'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "方法: " . __METHOD__ . "\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 wechat_quick_login(){ + 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(); + $result = $wechatService->handleWechatLogin($data['code'], $data['encryptedData'], $data['iv']); + + // die; + if($result['code'] == 0){ + // return $this->msg($result['code'],$result['msg']); + + $user_data = Db::table($this->login_use_db_name['1'])->where(['tel'=>$result['data']['phoneNumber'],'is_del'=>0])->find(); + + if($user_data){ + Db::table($this->login_use_db_name['1'])->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->login_use_db_name['1'])->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'] .= "方法: " . __METHOD__ . "\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 user_quit_account($data=['token'=>'0dafb98a10995c98b5a33b7d59d986ca']){ + try { + // 你的业务逻辑 + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('token', $data)){ + $return_data = $this->msg(10001); + } + if($this->token_time_validate($data['token']) === false){ + $return_data = $this->msg(20001); + } + + $result = Db::table($this->login_use_db_name['1'])->where(['token'=>$data['token']])->update(['login_time'=>'2024-09-01 00:00:00']); + if($result){ + $return_data = $this->msg([]); + }else{ + $return_data = $this->msg(10002); + } + // 成功 + $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'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "方法: " . __METHOD__ . "\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 delete_account($data=['token'=>'0dafb98a10995c98b5a33b7d59d986ca']){ + try { + // 你的业务逻辑 + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('token', $data)){ + $return_data = $this->msg(10001); + } + $result = Db::table($this->login_use_db_name['1'])->where(['token'=>$data['token']])->update(['is_del'=>1,'login_time'=>'2024-09-01 00:00:00']); + if($result){ + $return_data = $this->msg([]); + }else{ + $return_data = $this->msg(10002); + } + + // 成功 + $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'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "方法: " . __METHOD__ . "\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); + } + + } + + ################################################################接口################################################################ + ################################################################接口################################################################ + ################################################################接口################################################################ + + + + // 发送验证码 手机/邮箱 + /* 接口说明(发邮件) + * $data(手机或者邮箱信息) 字符串 + * $type(验证类型,是注册用,还是其他用途) 字符串 默认register(注册)(register、login、reset_password) + * $road(是手机还是邮箱还是其他) 字符串 默认tel或email + */ + //18736019909 + public function send_phone_email_code($data = ['data'=>'18736019909']){ + + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('data', $data)){ + return $this->msg(10001); + } + + if(cache($data['data'])){ + return $this->msg(10002,'60秒仅可发送一次验证码'); + } + + $num = mt_rand(100000,999999); + if (preg_match('/^\d{11}$/', $data['data'])) { + // 本公司短信 + $result = $this->send_tel_code($data['data'],$num); + // 阿里云短信 + // $sms_all = new Smsaliyun; + // $result = $sms_all->send_sms($data['data'],$num); + // dump($result); + $road = 'tel'; + }else{ + $result = $this->send_email_code([$data['data']],['title'=>'体测APP验证码','from_user_name'=>'体测APP','content'=>$num]); + $road = 'email'; + } + if(is_array($result) && $result['code'] == 0){ + cache($data['data'], $num, $this->code_time); + // return $this->msg(['code'=>$num]); + return $this->msg([]); + // return true; + }else{ + return $this->msg(10010,'验证码发送失败'); + // return false; + } + } + + ################################内部调用################################ + /* 接口说明(发手机短信) + + */ + public function send_tel_code($tel,$code){ + // 初始化cURL会话 + $ch = curl_init(); + $headers = [ + 'Accept: application/json', + 'Content-Type: application/json', + ]; + // 设置头部信息 + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + // 设置请求的URL + $url = "http://sms.ybhdmob.com/Message/Send?token=ybhdmob"; + curl_setopt($ch, CURLOPT_URL, $url); + // 设置为POST请求 + curl_setopt($ch, CURLOPT_POST, 1); + // 设置POST数据 + $postData = array( + 'phone' => $tel, + // 'content' => '【巨天】您好,欢迎使用Reedaw,您的手机验证码是:'.$code.',验证码一分钟内有效,若非本人操作,请忽略本短信' + // 'content' => '【郑州品传科技】您好,欢迎使用Reedaw,您的手机验证码是:'.$code.',验证码一分钟内有效,若非本人操作,请忽略本短信' + // 'content' => '【每日一称】您好,欢迎使用Reedaw,您的手机验证码是:'.$code.',验证码一分钟内有效,若非本人操作,请忽略本短信' + 'content' => '【小白健康】您好,欢迎使用Reedaw,您的手机验证码是:'.$code.',验证码一分钟内有效,若非本人操作,请忽略本短信' + ); + $postData = json_encode($postData); + curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); + // 设置返回结果不直接输出,而是返回到变量中 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + // 发送请求并获取响应 + $response = curl_exec($ch); + // dump($response); + // 检查是否有错误发生 + if (curl_errno($ch)) { + $error_message = curl_error($ch); + return "请求错误: " . $error_message; + } + // 关闭cURL会话 + curl_close($ch); + // 处理响应 + // dump(json_decode($response,true)); + if ($response) { + return json_decode($response,true); + } else { + echo "未收到响应"; + } + } + // 手机号区分 + function getCarrierByPhone($phone) { + // 验证手机号格式(11位数字且以1开头) + if (!preg_match('/^1[3-9]\d{9}$/', $phone)) { + return '无效手机号'; + } + + $prefix3 = substr($phone, 0, 3); + + // 2025年最新3位号段(排除4位号段) + $carriers = [ + '中国移动' => ['134', '135', '136', '137', '138', '139', '150', '151', '152', '157', '158', '159', '178', '182', '183', '184', '187', '188', '195', '197', '198'], + '中国联通' => ['130', '131', '132', '155', '156', '166', '175', '176', '185', '186', '196'], + '中国电信' => ['133', '153', '173', '177', '180', '181', '189', '190', '191', '193', '199'], + '中国广电' => ['192'] + ]; + + foreach ($carriers as $carrier => $segments) { + if (in_array($prefix3, $segments)) { + return $carrier; + } + } + + return '未知运营商'; + } + /* 接口说明(发邮件) + * $address(收件人的邮箱地址) 数组 格式: ['460834639@qq.com','460834639@qq.com'.......] + * $content(邮件的主题数据信息) 数组 格式:['title'=>'123','from_user_name'=>'123','content'=>'123'] + * $annex(附件路径信息) 字符串 + */ + public function send_email_code($address,$content,$annex=''){ + // $ad = '460834639@qq.com'; + $ad1 = '295155911@qq.com'; + $mail = new PHPMailer(); //实例化 + $mail->IsSMTP(); // 启用SMTP + $mail->Host = "smtp.126.com"; //SMTP服务器 163邮箱例子 + $mail->Port = 465; //邮件发送端口 + $mail->SMTPAuth = true; //启用SMTP认证 + $mail->SMTPSecure = 'ssl'; + $mail->CharSet = "UTF-8"; //字符集 + $mail->Encoding = "base64"; //编码方式 + $mail->Username = "tsf3920322@126.com"; //你的邮箱 + $mail->Password = "HLWXNRPUCTHJFIIX"; //你的密码(邮箱后台的授权密码) + $mail->From = "tsf3920322@126.com"; //发件人地址(也就是你的邮箱) + + // $mail->Subject = "微盟测试邮件"; //邮件标题 + $mail->Subject = $content['title']; //邮件标题 + + // $mail->FromName = "微盟体测中心"; //发件人姓名 + $mail->FromName = $content['from_user_name']; //发件人姓名 + + + for ($i=0; $i < count($address); $i++) { + $mail->AddAddress($address[$i], ""); //添加收件人(地址,昵称) + } + + if($annex != ''){ + // $url = ROOT_PATH. 'public' . DS . 'tsf' . DS .'demoooo.jpg'; + $mail->AddAttachment($annex,''); // 添加附件,并指定名称 + } + + $mail->IsHTML(true); //支持html格式内容 + + $neirong = '