parent
b3e02a497b
commit
acf258b3b0
@ -1,14 +1,87 @@ |
||||
<?php |
||||
|
||||
header("Content-type:application/json"); |
||||
require_once "../config.php"; |
||||
require_once __ROOT__ . "/baidu/BaiduAiRequest.php"; |
||||
require_once __ROOT__ . "/entity/Garbage.php"; |
||||
require_once __ROOT__ . "/database/Query.php"; |
||||
require_once __ROOT__ . "/api/JsonResponse.php"; |
||||
|
||||
if (isset($_POST["image"])) { |
||||
//查询垃圾分类 |
||||
function query_waste_soring($image_res) |
||||
{ |
||||
// 匹配结果大于0才进行处理 |
||||
if (isset($image_res["result_num"]) && $image_res["result_num"] > 0) { |
||||
// 匹配关键字 |
||||
$keyword = array(); |
||||
$sql = ""; |
||||
// 去除匹配结果数组 |
||||
foreach ($image_res["result"] as $result) { |
||||
// 筛选置信值大于0.5的结果 |
||||
if ($result["score"] >= 0.5) { |
||||
array_push($keyword, '%' . $result["keyword"] . '%'); |
||||
// 拼接模糊查询语句 |
||||
$sql = $sql . " or name like ?"; |
||||
} |
||||
} |
||||
// 如果筛选结果大于0处理 |
||||
if (count($keyword) > 0) { |
||||
|
||||
$sql = "select * from garbage where" . substr($sql, 3); |
||||
info("查询sql" . $sql); |
||||
$garbage_result = getGarbageQuery($sql, $keyword); |
||||
json_res(json_encode(array("img_parse" => true, "query" => true, "img_res" => $image_res, "result" => $garbage_result->getGarbageObjArray()), JSON_UNESCAPED_UNICODE)); |
||||
|
||||
} else { |
||||
json_res(json_encode(array("img_parse" => true, "img_res" => $image_res))); |
||||
} |
||||
} else { |
||||
json_res(json_encode(array("img_parse" => false))); |
||||
} |
||||
} |
||||
|
||||
|
||||
// 查询token |
||||
$token_result = new class() extends AbstractTokenQuery |
||||
{ |
||||
public function bind_param(mysqli_stmt $stmt, array $param) |
||||
{ |
||||
|
||||
echo <<<EOF |
||||
{"msg":"合法图片参数"} |
||||
EOF; |
||||
} |
||||
}; |
||||
DbUtil::query("select * from api_token limit 0,1", $token_result); |
||||
|
||||
// toekn如果失效,则更新token |
||||
if (strtotime(date(default_format)) >= strtotime($token_result->getToken()->getEndTime())) { |
||||
info("token失效了,更新token"); |
||||
$updateToken = new BaiduAiRequest("https://aip.baidubce.com/oauth/2.0/token"); |
||||
$updateToken->request_token(); |
||||
DbUtil::query("select * from api_token limit 0,1", $token_result); |
||||
} |
||||
|
||||
|
||||
//获取图片参数 |
||||
if (isset($_POST["image"])) { |
||||
// 请求通用物体和场景识别高级版接口 |
||||
$image_req = new BaiduAiRequest("https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general"); |
||||
// 获取解析结果 |
||||
$image_res = $image_req->request_with_param(array("access_token" => $token_result->getToken()->getAccessToken(), "image" => $_POST["image"])); |
||||
query_waste_soring($image_res); |
||||
} else if ($_FILES["file"]["error"] > 0) { |
||||
json_res(<<<EOF |
||||
{"msg":"文件上传失败"} |
||||
EOF |
||||
); |
||||
} else if ($_FILES["file"]["error"] == 0) { |
||||
// base64加密文件二进制数据 |
||||
$image = base64_encode(file_get_contents($_FILES["file"]["tmp_name"])); |
||||
// 请求通用物体和场景识别高级版接口 |
||||
$image_req = new BaiduAiRequest("https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general"); |
||||
// 获取解析结果 |
||||
$image_res = $image_req->request_with_param(array("access_token" => $token_result->getToken()->getAccessToken(), "image" => $image)); |
||||
query_waste_soring($image_res); |
||||
} else { |
||||
echo <<<EOF |
||||
json_res(<<<EOF |
||||
{"msg":"缺少图片参数"} |
||||
EOF; |
||||
EOF |
||||
); |
||||
} |
@ -0,0 +1,10 @@ |
||||
<?php |
||||
|
||||
|
||||
//返回json |
||||
function json_res($json_str) |
||||
{ |
||||
// 设置json响应头 |
||||
header("Content-type:application/json"); |
||||
echo $json_str; |
||||
} |
@ -0,0 +1,45 @@ |
||||
<?php |
||||
|
||||
require_once "../config.php"; |
||||
require_once __ROOT__ . "/api/JsonResponse.php"; |
||||
require_once __ROOT__ . "/database/DbUtil.php"; |
||||
require_once __ROOT__ . "/database/Query.php"; |
||||
|
||||
|
||||
//关键字查询 |
||||
class QueryText |
||||
{ |
||||
public static function get($sql, $keyword): AbstractGarbageQuery |
||||
{ |
||||
$garbage_result = new class() extends AbstractGarbageQuery |
||||
{ |
||||
// 参数绑定 |
||||
public function bind_param(mysqli_stmt $stmt, array $param) |
||||
{ |
||||
if (count($param) > 1) { |
||||
$stmt->bind_param(str_repeat("s", count($param)), $param[0], ...array_slice($param, 1)); |
||||
} else { |
||||
$stmt->bind_param(str_repeat("s", count($param)), $param[0]); |
||||
} |
||||
|
||||
} |
||||
}; |
||||
|
||||
|
||||
DbUtil::query($sql, $garbage_result, $keyword); |
||||
|
||||
return $garbage_result; |
||||
} |
||||
} |
||||
|
||||
if (empty($_GET)) { |
||||
json_res(json_encode(array("status" => false, "error" => "非法请求"), JSON_UNESCAPED_UNICODE)); |
||||
} else if (isset($_GET["keyword"]) and !empty($_GET["keyword"])) { |
||||
$result = getGarbageQuery("select * from garbage where name like ?", array("%" . $_GET["keyword"] . "%")); |
||||
json_res(json_encode(array("status" => true, "result" => $result->getGarbageObjArray(), "keyword" => $_GET["keyword"]), JSON_UNESCAPED_UNICODE)); |
||||
} else if (isset($_GET["category"]) and is_numeric($_GET["category"])) { |
||||
$result = getGarbageQuery("select * from garbage where category = ?", array((int)$_GET["category"])); |
||||
json_res(json_encode(array("status" => true, "result" => $result->getGarbageObjArray(), "category" => $_GET["category"]), JSON_UNESCAPED_UNICODE)); |
||||
} else { |
||||
json_res(json_encode(array("status" => false, "error" => "非法查询参数"), JSON_UNESCAPED_UNICODE)); |
||||
} |
@ -1,13 +1,14 @@ |
||||
<?php |
||||
|
||||
//数据库配置 |
||||
define("db_config", array( |
||||
"servername" => "mylinux", |
||||
"username" => "sukura", |
||||
"password" => "123456", |
||||
"dbname" => "waste_sorting" |
||||
)); |
||||
|
||||
//默认日期格式 |
||||
define("default_format", "Y-m-d H:i:s"); |
||||
//根目录 |
||||
//define("__ROOT__", dirname(__FILE__)); |
||||
|
||||
define("__ROOT__", dirname(__FILE__)); |
||||
|
||||
define("__ROOT__", "D:/JetBrains/PhpstormProjects/WasteSorting"); |
@ -0,0 +1,103 @@ |
||||
<?php |
||||
|
||||
require_once __ROOT__ . "/entity/ApiToken.php"; |
||||
require_once __ROOT__ . "/entity/Garbage.php"; |
||||
|
||||
//Token查询 |
||||
abstract class AbstractTokenQuery implements DoExcute |
||||
{ |
||||
// token查询结果 |
||||
private $token_array = array(); |
||||
|
||||
/** |
||||
* TokenQuery constructor. |
||||
*/ |
||||
public function __construct() |
||||
{ |
||||
} |
||||
|
||||
|
||||
// 绑定toekn信息 |
||||
public function doResult(mysqli_stmt $stmt) |
||||
{ |
||||
/** |
||||
* |
||||
* $col1 $id |
||||
* $col2 $refresh_token |
||||
* $col3 $expires_in |
||||
* $col4 $end_time |
||||
* $col5 $scope |
||||
* $col6 $session_key |
||||
* $col7 $access_token |
||||
* $col8 $session_secret |
||||
*/ |
||||
$stmt->bind_result($col1, $col2, $col3, $col4, $col5, $col6, $col7, $col8); |
||||
while ($stmt->fetch()) { |
||||
array_push($this->token_array, new ApiToken($col1, $col2, $col3, $col4, $col5, $col6, $col7, $col8)); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @return array |
||||
*/ |
||||
public function getTokenArray(): array |
||||
{ |
||||
return $this->token_array; |
||||
} |
||||
|
||||
function getToken(): ApiToken |
||||
{ |
||||
return $this->token_array[0]; |
||||
} |
||||
|
||||
|
||||
} |
||||
|
||||
//垃圾分类查询 |
||||
abstract class AbstractGarbageQuery implements DoExcute |
||||
{ |
||||
// 垃圾分类查询结果 |
||||
private $garbage_obj_array = array(); |
||||
|
||||
|
||||
public function doResult(mysqli_stmt $stmt) |
||||
{ |
||||
|
||||
$stmt->bind_result($col1, $col2, $col3, $col4); |
||||
while ($stmt->fetch()) { |
||||
$g = new Garbage($col1, $col2, $col3, $col4); |
||||
array_push($this->garbage_obj_array, $g); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* @return array |
||||
*/ |
||||
public function getGarbageObjArray(): array |
||||
{ |
||||
return $this->garbage_obj_array; |
||||
} |
||||
|
||||
|
||||
} |
||||
|
||||
function getGarbageQuery($sql, $keyword): AbstractGarbageQuery |
||||
{ |
||||
$garbage_result = new class() extends AbstractGarbageQuery |
||||
{ |
||||
public function bind_param(mysqli_stmt $stmt, array $param) |
||||
{ |
||||
if (count($param) > 1) { |
||||
$stmt->bind_param(str_repeat("s", count($param)), $param[0], ...array_slice($param, 1)); |
||||
} else { |
||||
$stmt->bind_param(str_repeat("s", count($param)), $param[0]); |
||||
} |
||||
|
||||
} |
||||
}; |
||||
|
||||
DbUtil::query($sql, $garbage_result, $keyword); |
||||
|
||||
return $garbage_result; |
||||
} |
@ -0,0 +1,68 @@ |
||||
<?php |
||||
|
||||
//垃圾分类 |
||||
class Garbage implements JsonSerializable |
||||
{ |
||||
private $name; |
||||
private $category; |
||||
private $create_at; |
||||
private $update_at; |
||||
|
||||
/** |
||||
* Garbage constructor. |
||||
* @param $name |
||||
* @param $category |
||||
* @param $create_at |
||||
* @param $update_at |
||||
*/ |
||||
public function __construct($name, $category, $create_at, $update_at) |
||||
{ |
||||
$this->name = $name; |
||||
$this->category = $category; |
||||
$this->create_at = $create_at; |
||||
$this->update_at = $update_at; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getName() |
||||
{ |
||||
return $this->name; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getCategory() |
||||
{ |
||||
return $this->category; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getCreateAt() |
||||
{ |
||||
return $this->create_at; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getUpdateAt() |
||||
{ |
||||
return $this->update_at; |
||||
} |
||||
|
||||
|
||||
public function jsonSerialize() |
||||
{ |
||||
$data = []; |
||||
foreach ($this as $key => $val) { |
||||
if ($val !== null) $data[$key] = $val; |
||||
} |
||||
return $data; |
||||
} |
||||
|
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,3 +1,23 @@ |
||||
<?php |
||||
require "config.php"; |
||||
echo __ROOT__; |
||||
include "config.php"; |
||||
include __ROOT__ . "/database/DbUtil.php"; |
||||
|
||||
echo <<<EOF |
||||
<html> |
||||
<head> |
||||
<meta charset="utf-8"> |
||||
<title>菜鸟教程(runoob.com)</title> |
||||
</head> |
||||
<body> |
||||
|
||||
<form action="/api/BaiduImage.php" method="post" enctype="multipart/form-data"> |
||||
<label for="file">文件名:</label> |
||||
<input type="file" name="file" id="file"><br> |
||||
<input type="submit" name="submit" value="提交"> |
||||
</form> |
||||
|
||||
</body> |
||||
</html> |
||||
EOF; |
||||
|
||||
|
||||
|
Loading…
Reference in new issue