You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
wastesortingserver/admin/ApiConfig.php

109 lines
4.2 KiB

<?php
require_once $_SERVER["DOCUMENT_ROOT"] . "/head.php";
getMenu("百度AK/SK配置");
require_once $_SERVER["DOCUMENT_ROOT"] . "/database/DbUtil.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/database/Query.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/Log.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/admin/Alert.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/baidu/BaiduAiRequest.php";
if (empty($_REQUEST) or !empty($_GET["check"])) {
$result = BaiduAiRequest::get_token_config();
if (!empty($result)) {
$api_key = $result["api_key"];
$secret_key = $result["secret_key"];
}
echo <<<EOF
<!DOCTYPE html>
<html lang="zh">
<script>
$(function() {
$('[data-toggle="tooltip"]').tooltip();
Array.prototype.filter.call($("form.needs-validation"), function(form) {
form.addEventListener('submit', function(event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
});
function doSubmit(action) {
$("form").attr("action",action).submit()
}
</script>
<body>
<div class="container py-5">
<div class="list-group d-flex justify-content-center align-items-center w-100">
<form class="col-6 needs-validation" novalidate method="post">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">API Key</span>
</div>
<input type="text" class="form-control" placeholder="API Key" name="api_key" value="$api_key" required>
<div class="invalid-feedback">
API Key不能为空
</div>
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">API Key</span>
</div>
<input type="text" class="form-control" placeholder="API Key" name="secret_key" value="$secret_key" required>
<div class="invalid-feedback">
Secret Key不能为空
</div>
</div>
<div class="col-6 m-auto">
EOF;
if (isset($_GET["check"]) and $_GET["check"] == "true") {
echo '<button class="btn btn-success btn-block" onclick="doSubmit(\'' . $_SERVER["PHP_SELF"] . '?action=update\')">保存</button>';
} else {
echo '<button class="btn btn-warning btn-block" onclick="doSubmit(\'' . $_SERVER["PHP_SELF"] . '?action=test\')">测试</button>';
}
echo <<<EOF
<a class="btn btn-dark btn-block" href="/">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
EOF;
} else if (!empty($_GET["action"])) {
if (empty($_POST["api_key"])) {
error_res("API Key不能为空", $_SERVER["PHP_SELF"]);
} else if (empty($_POST["secret_key"])) {
error_res("Secret Key不能为空", $_SERVER["PHP_SELF"]);
} else if ($_GET["action"] == "test") {
$api_key = $_POST["api_key"];
$secret_key = $_POST["secret_key"];
$result = BaiduAiRequest::request_token_with_config($api_key, $secret_key);
if (array_key_exists("status", $result) and $result["status"]) {
info_res("AK/SK测试通过", $_SERVER["PHP_SELF"] . "?check=true");
} else {
warn_res("AK/SK测试失败", $_SERVER["PHP_SELF"] . "?check=false");
}
} else if ($_GET["action"] == "update") {
try {
DbUtil::update("update param set param_value=? where param_key=?", "ss", $_POST["api_key"], "api_key");
DbUtil::update("update param set param_value=? where param_key=?", "ss", $_POST["secret_key"], "secret_key");
info_res("保存成功", $_SERVER["PHP_SELF"]);
} catch (Exception $e) {
error($e);
error_res("保存异常请联系管理员", $_SERVER["PHP_SELF"]);
}
} else {
error_res("非法操作!", $_SERVER["PHP_SELF"]);
}
} else {
error_res("非法操作!", $_SERVER["PHP_SELF"]);
}