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

146 lines
5.4 KiB
PHP
Raw Normal View History

2025-07-23 21:32:05 +08:00
<?php
namespace app\KitchenScale\controller\app;
use think\Db;
use think\Cache;
class Aipart extends Base{
protected $default_head_pic = 'http://tc.pcxbc.com/tsf/head_pic.png';
protected $page_num = 10;
protected $kitchenscale_db_msg = [
'cookbook'=>'app_user_cookbook',//菜谱表
2025-09-03 19:09:13 +08:00
'foodlist3'=>'app_z_national_standard_food_type_3',//食材列表3
2025-07-23 21:32:05 +08:00
'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;
}
}
}