diff --git a/admin/WasteSorting.php b/admin/WasteSorting.php
index 8bede2f..879bc04 100644
--- a/admin/WasteSorting.php
+++ b/admin/WasteSorting.php
@@ -27,9 +27,6 @@ $(function() {
}, false);
});
});
-function doList() {
- console.info("查看垃圾数据")
-}
diff --git a/api/BaiduImage.php b/api/BaiduImage.php
index 1163e2c..200df27 100644
--- a/api/BaiduImage.php
+++ b/api/BaiduImage.php
@@ -53,35 +53,31 @@ 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);
+ $result = BaiduAiRequest::request_token();
+ if (array_key_exists("status", $result) and $result["status"]) {
+ DbUtil::query("select * from api_token limit 0,1", $token_result);
+ } else {
+ json_res(json_encode(array("img_parse" => false, "msg" => "无法调用百度API,请联系管理员")));
+ return;
+ }
}
//获取图片参数
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"]));
+ $image_res = BaiduAiRequest::request_with_param("https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general", array("access_token" => $token_result->getToken()->getAccessToken(), "image" => $_POST["image"]));
query_waste_soring($image_res);
} else if ($_FILES["file"]["error"] > 0) {
- json_res(<< false, "msg" => "照片上传失败")));
} 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));
+ $image_res = BaiduAiRequest::request_with_param("https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general", array("access_token" => $token_result->getToken()->getAccessToken(), "image" => $image));
query_waste_soring($image_res);
} else {
- json_res(<< false, "msg" => "缺少图片参数")));
}
diff --git a/baidu/BaiduAiRequest.php b/baidu/BaiduAiRequest.php
index 4561e43..e5e3628 100644
--- a/baidu/BaiduAiRequest.php
+++ b/baidu/BaiduAiRequest.php
@@ -7,51 +7,78 @@ require_once $_SERVER["DOCUMENT_ROOT"] . "/database/DbUtil.php";
//api请求
class BaiduAiRequest
{
- private static $client_id = "keaVPslHREMgVd6FwiEQqcCx";
- private static $client_secret = "vtsDKjRVbcALDc61GAflw8UAtmGYkIPX";
- private $url;
- /**
- * BaiduAiRequest constructor.
- * @param $url
- */
- public function __construct($url)
+ public static function get_token_config()
{
- $this->url = $url;
+ $config_query = new QueryParam();
+ DbUtil::query("select * from param where param_key in('api_key','secret_key')", $config_query);
+ if (!empty($config_query->getParamList())) {
+ $config_result = $config_query->getParamList();
+ if (array_key_exists("api_key", $config_result) && array_key_exists("secret_key", $config_result)) {
+ if ($config_result["api_key"] instanceof Param and $config_result["secret_key"] instanceof Param
+ and !empty($config_result["api_key"]->getParamValue()) and !empty($config_result["secret_key"]->getParamValue())) {
+ return array("api_key" => $config_result["api_key"]->getParamValue(), "secret_key" => $config_result["secret_key"]->getParamValue());
+ }
+ }
+ }
+ return null;
+ }
+
+ public static function request_token()
+ {
+ $config_result = self::get_token_config();
+ if (empty($config_result)) {
+ error("初始化配置失败,请联系管理员");
+// return << "初始化配置失败,请联系管理员", "status" => false);
+ } else {
+ return self::request_token_with_config($config_result["api_key"], $config_result["secret_key"]);
+ }
}
- function request_token()
+ public static function request_token_with_config($apikey, $secret_key)
{
try {
- if (empty(self::$client_id) or empty(self::$client_secret)) {
- throw new Exception("");
- }
- $post_data['grant_type'] = 'client_credentials';
- $post_data['client_id'] = self::$client_id;
- $post_data['client_secret'] = self::$client_secret;
- $json_res = $this->request_with_param($post_data);
- if (array_key_exists("error", $json_res)) {
- throw new Exception("请求异常");
+ $config_result = self::get_token_config();
+ if (empty($config_result)) {
+ error("初始化配置失败,请联系管理员");
+// return << "初始化配置失败,请联系管理员", "status" => false);
} else {
- DbUtil::delete("delete from api_token");
- $token = new ApiToken(null, $json_res["refresh_token"], $json_res["expires_in"], null, $json_res["scope"], $json_res["session_key"], $json_res["access_token"], $json_res["session_secret"]);
- DbUtil::insert("insert into api_token (refresh_token, expires_in, end_time, scope, session_key, access_token, session_secret) values (?,?,?,?,?,?,?)",
- $types = "sisssss", $token->getRefreshToken(), $token->getExpiresIn(), date_format(date_create(date(default_format))->add(date_interval_create_from_date_string($token->getExpiresIn() . " seconds")), default_format), $token->getScope(), $token->getSessionKey(), $token->getAccessToken(), $token->getSessionSecret());
- info("token保存成功");
- return $token;
+ $post_data['grant_type'] = 'client_credentials';
+ $post_data['client_id'] = $apikey;
+ $post_data['client_secret'] = $secret_key;
+
+ $json_res = self::request_with_param("https://aip.baidubce.com/oauth/2.0/token", $post_data);
+ if (array_key_exists("error", $json_res)) {
+ return array("msg" => "百度API请求失败,请联系管理员", "status" => false, "res" => $json_res);
+ } else {
+ DbUtil::delete("delete from api_token");
+ $token = new ApiToken(null, $json_res["refresh_token"], $json_res["expires_in"], null, $json_res["scope"], $json_res["session_key"], $json_res["access_token"], $json_res["session_secret"]);
+ DbUtil::insert("insert into api_token (refresh_token, expires_in, end_time, scope, session_key, access_token, session_secret) values (?,?,?,?,?,?,?)",
+ $types = "sisssss", $token->getRefreshToken(), $token->getExpiresIn(), date_format(date_create(date(default_format))->add(date_interval_create_from_date_string($token->getExpiresIn() . " seconds")), default_format), $token->getScope(), $token->getSessionKey(), $token->getAccessToken(), $token->getSessionSecret());
+ info("token保存成功");
+// return $token;
+ return array("msg" => "百度API请求成功", "status" => true, "token" => $token);
+ }
}
} catch (Exception $e) {
- $GLOBALS["default_log"]->error($e);
- return << "百度API请求失败,请联系管理员", "status" => false);
}
}
// 发送请求
- function request_with_param($param)
+ public static function request_with_param($url, $param)
{
try {
@@ -60,11 +87,11 @@ class BaiduAiRequest
$o .= "$k=" . urlencode($v) . "&";
}
$param = substr($o, 0, -1);
- if (empty($this->url) || empty($param)) {
+ if (empty($url) || empty($param)) {
return false;
}
$curlPost = $param;
- $curl = curl_init($this->url);//初始化curl
+ $curl = curl_init($url);//初始化curl
curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
diff --git a/database/Query.php b/database/Query.php
index 8dd20f8..f6c9e59 100644
--- a/database/Query.php
+++ b/database/Query.php
@@ -5,6 +5,7 @@ require_once $_SERVER["DOCUMENT_ROOT"] . "/entity/Garbage.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/entity/Question.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/entity/Answer.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/entity/Manager.php";
+require_once $_SERVER["DOCUMENT_ROOT"] . "/entity/Param.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/database/DbUtil.php";
//Token查询
abstract class AbstractTokenQuery implements DoExcute
@@ -305,3 +306,33 @@ class QueryManager implements DoExcute
}
+//查询系统参数
+class QueryParam implements DoExcute
+{
+ private $param_list = array();
+
+ public function bind_param(mysqli_stmt $stmt, array $param = null)
+ {
+ if (!empty($param)) {
+ $stmt->bind_param("sss", $param[0], ...array_slice($param, 1));
+ }
+ }
+
+ public function doResult(mysqli_stmt $stmt)
+ {
+ $stmt->bind_result($col1, $col2, $col3);
+ while ($stmt->fetch()) {
+ $this->param_list[$col1] = new Param($col1, $col2, $col3);
+ }
+ }
+
+ /**
+ * @return array
+ */
+ public function getParamList(): array
+ {
+ return $this->param_list;
+ }
+
+
+}
\ No newline at end of file
diff --git a/entity/Param.php b/entity/Param.php
new file mode 100644
index 0000000..3d4e73a
--- /dev/null
+++ b/entity/Param.php
@@ -0,0 +1,48 @@
+param_key = $param_key;
+ $this->param_value = $param_value;
+ $this->param_desc = $param_desc;
+ }
+
+ /**
+ * @return mixed
+ */
+ public function getParamKey()
+ {
+ return $this->param_key;
+ }
+
+ /**
+ * @return mixed
+ */
+ public function getParamValue()
+ {
+ return $this->param_value;
+ }
+
+ /**
+ * @return mixed
+ */
+ public function getParamDesc()
+ {
+ return $this->param_desc;
+ }
+
+
+}
\ No newline at end of file
diff --git a/index.php b/index.php
index bf2075f..76e81b5 100644
--- a/index.php
+++ b/index.php
@@ -16,7 +16,7 @@ if (!check_login()) {
require_once $_SERVER["DOCUMENT_ROOT"] . "/entity/Menu.php";
//菜单信息
-$menus = array("API" => new Menu("百度API配置", "/admin/ApiConfig.php"), new Menu("垃圾分类", "/admin/WasteSorting.php"), new Menu("题目管理", "/admin/Question.php"));
+$menus = array("API" => new Menu("百度AK/SK配置", "/admin/ApiConfig.php"), new Menu("垃圾分类", "/admin/WasteSorting.php"), new Menu("题目管理", "/admin/Question.php"));
diff --git a/script/db.sql b/script/db.sql
index 09f92e3..db2a816 100644
--- a/script/db.sql
+++ b/script/db.sql
@@ -33,7 +33,7 @@ create table param
# 参数名
param_key varchar(16) primary key,
# 参数值
- param_value varchar(16),
+ param_value varchar(128),
# 参数描述
param_desc varchar(32)
);
@@ -76,4 +76,9 @@ create table manager
# 超级管理员账号
insert manager (manager_name, manager_pwd)
-values ('admin', md5('admin'));
\ No newline at end of file
+values ('admin', md5('admin'));
+
+# 初始化参数
+insert param
+values ('api_key', null, null),
+ ('secret_key', null, null);
\ No newline at end of file
diff --git a/test.php b/test.php
index c97f247..70bfe42 100644
--- a/test.php
+++ b/test.php
@@ -24,8 +24,8 @@
//EOF;
//var_dump(array_search(new Answer(1, 2, 3, 4), array(new Answer(4, 5, 6, 7), new Answer(2, 2, 3, 4))));
-$s1 = "dotcoo world!";
-$s2 = "dotcoo";
-$s3 = "hello dotcoo";
-var_dump(substr($s1, -strlen($s2)) === $s2);
+//$s1 = "dotcoo world!";
+//$s2 = "dotcoo";
+//$s3 = "hello dotcoo";
+//var_dump(substr($s1, -strlen($s2)) === $s2);