垃圾分类

test
橙橙楊 5 years ago committed by luffy
parent ded31e5320
commit 7d3681fcb3
  1. 48
      admin/Alert.php
  2. 92
      admin/Question.php
  3. 246
      admin/WasteSorting.php
  4. 2
      api/BaiduImage.php
  5. 4
      api/QueryText.php
  6. 107
      database/Query.php
  7. 6
      test.php

@ -0,0 +1,48 @@
<?php
function response_with_click($message, $class, $script)
{
echo <<<EOF
<script>
function doClick() {
$script
}
</script>
<div class="container py-5">
<div class="alert $class alert-dismissible fade show col-5 m-auto text-center" role="alert">
<strong>$message</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close" onclick="doClick()">
<span aria-hidden="true">&times;</span>
</button>
</div>
</div>
EOF;
}
function response_with_href($message, $class, $href)
{
response_with_click($message, $class, <<<EOF
location.href='$href'
EOF
);
}
function error_res($err, $href)
{
response_with_href($err, "alert-danger", $href);
}
function info_res($info, $href)
{
response_with_href($info, "alert-info", $href);
}
function warn_res($warn, $href)
{
response_with_href($warn, "alert-warning", $href);
}
function warn_res_with_click($warn, $click)
{
response_with_click($warn, "alert-warning", $click);
}

@ -18,56 +18,10 @@ require_once __ROOT__ . "/head.php";
require_once __ROOT__ . "/database/DbUtil.php";
require_once __ROOT__ . "/database/Query.php";
require_once __ROOT__ . "/Log.php";
require_once __ROOT__ . "/admin/Alert.php";
getMenu("题目管理");
function reponse_with_click($message, $class, $script)
{
echo <<<EOF
<script>
function doClick() {
$script
}
</script>
<div class="container py-5">
<div class="alert $class alert-dismissible fade show col-5 m-auto text-center" role="alert">
<strong>$message</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close" onclick="doClick()">
<span aria-hidden="true">&times;</span>
</button>
</div>
</div>
EOF;
}
function response_with_href($message, $class, $href)
{
reponse_with_click($message, $class, <<<EOF
location.href='$href'
EOF
);
}
function error_res($err)
{
response_with_href($err, "alert-danger", "/admin/Question.php");
}
function info_res($info)
{
response_with_href($info, "alert-info", "/admin/Question.php");
}
function warn_res($warn)
{
response_with_href($warn, "alert-warning", "/admin/Question.php");
}
function warn_res_with_click($warn, $click)
{
reponse_with_click($warn, "alert-warning", $click);
}
if (empty($_POST) && empty($_GET)) {
echo <<<EOF
<!DOCTYPE html>
@ -75,7 +29,7 @@ if (empty($_POST) && empty($_GET)) {
<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip();
var validation = Array.prototype.filter.call($("form.needs-validation"), function(form) {
Array.prototype.filter.call($("form.needs-validation"), function(form) {
form.addEventListener('submit', function(event) {
if (form.checkValidity() === false) {
event.preventDefault();
@ -113,9 +67,10 @@ if (empty($_POST) && empty($_GET)) {
<div class="container py-5">
<div class="text-center text-danger">tip:选中题目查看选项</div>
<div class="tab-content" id="v-pills-tabContent">
<div class="tab-pane fade show active" id="list" role="tabpanel" aria-labelledby="v-pills-home-tab">
<div class="text-center text-danger">tip:选中题目查看选项</div>
<div class="list-group d-flex justify-content-center align-items-center w-100">
EOF;
$query = new QuestionQuery();
@ -142,7 +97,7 @@ EOF
echo <<<EOF
</div>
</div>
<div id="add" class="tab-pane fade ">
<div id="add" class="tab-pane fade">
<div class="d-flex justify-content-center align-items-center w-100">
<form class="col-6 needs-validation" novalidate method="post" action="Question.php">
<input type="hidden" name="right_answer">
@ -235,17 +190,17 @@ EOF;
$answer_count = 4;
if (empty($_POST["question"])) {
error_res("问题不能为空!");
error_res("问题不能为空!", "/admin/Question.php");
} else if (empty($_POST["answer"])) {
error_res("答案内容不能为空!");
error_res("答案内容不能为空!", "/admin/Question.php");
} else if (!is_array($_POST["answer"])) {
error_res("非法答案参数");
error_res("非法答案参数", "/admin/Question.php");
} else if (count($_POST["answer"]) != $answer_count) {
error_res("答案数量不等于4个!");
error_res("答案数量不等于4个!", "/admin/Question.php");
} else if (count(array_unique($_POST["answer"])) != $answer_count) {
error_res("答案不能重复!");
error_res("答案不能重复!", "/admin/Question.php");
} else if (empty($_POST["right_answer"])) {
error_res("没有正确答案!");
error_res("没有正确答案!", "/admin/Question.php");
} else {
try {
DbUtil::insert_with_param("insert into question (question_title) values (?)", "s", new class() implements DoConn
@ -258,10 +213,10 @@ EOF;
}
}
}, $_POST["question"]);
info_res("保存成功");
info_res("保存成功", "/admin/Question.php");
} catch (Exception $e) {
error($e);
error_res("保存异常,请稍后再试或者联系管理员!");
error_res("保存异常,请稍后再试或者联系管理员!", "/admin/Question.php");
}
@ -269,14 +224,14 @@ EOF;
} else if (isset($_GET["action"]) && $_GET["action"] == "list") {
if (empty($_GET["question_id"])) {
error_res("没有指定问题id,无法查看详情信息!");
error_res("没有指定问题id,无法查看详情信息!", "/admin/Question.php");
} else if (!is_numeric($_GET["question_id"])) {
error_res("问题id不合法");
error_res("问题id不合法", "/admin/Question.php");
} else {
$query_result = new AnswerQuery();
DbUtil::query("select * from answer where question_id=?", $query_result, array($_GET["question_id"]));
if (empty($query_result->getAnswerArray())) {
error_res("问题id不合法");
error_res("问题id不合法", "/admin/Question.php");
} else {
echo '<div class="container py-5"><div class="col-6 m-auto">';
foreach ($query_result->getAnswerArray() as $item) {
@ -297,29 +252,30 @@ EOF;
} else {
error("类型不匹配");
error_res("操作异常,请联系管理员");
error_res("操作异常,请联系管理员", "/admin/Question.php");
}
}
echo '</div><button class="btn btn-info btn-block col-6 m-auto" onclick="location.href=\'/admin/Question.php\'">返回</button></div>';
echo '</div><a class="btn btn-info btn-block col-6 m-auto" href="/admin/Question.php">返回</a></div>';
}
}
} else if (isset($_GET["action"]) && $_GET["action"] == "delete") {
if (empty($_GET["question_id"])) {
error_res("缺少问题id");
error_res("缺少问题id", "/admin/Question.php");
} else if (!is_numeric($_GET["question_id"])) {
error_res("非法参数id");
error_res("非法参数id", "/admin/Question.php");
} else {
try {
DbUtil::delete("DELETE q,a FROM question as q,answer as a WHERE q.question_id=a.question_id and q.question_id=?", "i", (int)$_GET["question_id"]);
info_res("问题删除成功");
info_res("问题删除成功", "/admin/Question.php");
} catch (Exception $e) {
error($e);
error_res("操作异常,请联系管理员");
error_res("操作异常,请联系管理员", "/admin/Question.php");
}
}
} else {
\error("非法操作!");
error("非法操作!");
\error_res("非法操作!", "/admin/Question.php");
}

@ -0,0 +1,246 @@
<?php
require_once "../config.php";
require_once __ROOT__ . "/head.php";
require_once __ROOT__ . "/database/DbUtil.php";
require_once __ROOT__ . "/database/Query.php";
require_once __ROOT__ . "/Log.php";
require_once __ROOT__ . "/admin/Alert.php";
getMenu("题目管理");
if (empty($_POST) && empty($_GET)) {
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 doList() {
console.info("查看垃圾数据")
}
</script>
<body>
<ul class="nav nav-pills nav-justified">
<li class="nav-item">
<a class="nav-link active" data-toggle="tab" href="#list" role="tab">垃圾列表</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#add" role="tab">添加垃圾</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#" onclick="location.href='/'" role="tab">返回首页</a>
</li>
</ul>
<div class="container py-5">
<div class="tab-content" id="v-pills-tabContent">
<div class="tab-pane fade show active" id="list" role="tabpanel" aria-labelledby="v-pills-home-tab">
<div class="list-group d-flex justify-content-center align-items-center w-100">
EOF;
$count_query = new CountGrabage();
// 查询垃圾分类数据量
DbUtil::query("select category,count(*) from garbage group by category order by category", $count_query);
if (empty($count_query->getResult())) {
warn_res_with_click("暂无垃圾分类数据,请添加", <<<EOF
$('a[href="#add"]').click()
EOF
);
} else {
foreach ($count_query->getResult() as $item) {
$category = $item["category"];
$category_name = $item["category_name"];
$count = $item["count"];
echo <<<EOF
<a class="list-group-item list-group-item-action d-flex align-items-center col-6 m-auto justify-content-between" href="/admin/WasteSorting.php?action=list&category=$category"
data-toggle="tooltip" data-placement="right" title="查看垃圾数据" onclick="doList($category;)">
$category_name
<span class="badge badge-info badge-pill" >$count 条</span>
</a>
EOF;
}
}
echo <<<EOF
</div>
</div>
<div id="add" class="tab-pane fade">
<div class="d-flex justify-content-center align-items-center w-100">
<form class="col-6 needs-validation" novalidate method="post" action="WasteSorting.php">
<input type="hidden" name="action" value="add">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">垃圾名</span>
</div>
<input type="text" class="form-control" placeholder="垃圾名" name="name" required>
<div class="invalid-feedback">
垃圾名不为空
</div>
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">垃圾分类</span>
</div>
<select class="custom-select custom-select" name="category" required>
<option value="1">可回收垃圾</option>
<option value="2">有害垃圾</option>
<option value="4">湿垃圾</option>
<option value="8">干垃圾</option>
<option value="16">大件垃圾</option>
</select>
<div class="invalid-feedback">
请选择垃圾分类
</div>
</div>
<button class="btn btn-block btn-info">提交</button>
</form>
</div>
</div>
</body>
</html>
EOF;
} else if (isset($_POST["action"]) && $_POST["action"] == "add") {
if (empty($_POST["name"])) {
error_res("垃圾名不能为空!", "/admin/WasteSorting.php");
} else if (empty($_POST["category"])) {
error_res("垃圾分类不能为空!", "/admin/WasteSorting.php");
} else if (array_search((int)$_POST["category"], array(1, 2, 4, 8, 16)) === false) {
error_res("垃圾分类参数不合法", "/admin/WasteSorting.php");
} else {
try {
DbUtil::insert("insert into garbage (name, category, create_at, update_at) values (?,?,?,?)", "siss", $_POST["name"], $_POST["category"], date(default_format), date(default_format));
info_res("保存成功", "/admin/WasteSorting.php");
} catch (Exception $e) {
error($e);
error_res("保存操作异常,请联系管理员", "/admin/WasteSorting.php");
}
}
} else if (isset($_GET["action"]) && $_GET["action"] == "list") {
if (empty($_GET["category"])) {
error_res("垃圾分类不能为空!", "/admin/WasteSorting.php");
} else if (array_search((int)$_GET["category"], array(1, 2, 4, 8, 16)) === false) {
error_res("垃圾分类参数不合法", "/admin/WasteSorting.php");
} else {
// 分页查询
$page = (int)($_GET["page"] ?? 1);
$category_param = (int)$_GET["category"];
$count_query = new QueryGarbageCount();
// 获取记录数
DbUtil::query("select count(*) from garbage where category=?", $count_query, array($category_param));
$count_result = $count_query->getCount();
$garbage_query = new QueryGarbageWithCategory();
// 分页大小
$page_size = 10;
// 计算分页数
if ($count_result < $page_size) {
$max_page = 1;
} else if ($count_result % $page_size == 0) {
$max_page = $count_result / $page_size;
} else {
$max_page = (int)floor($count_result / $page_size) + 1;
}
DbUtil::query("select * from garbage where category=? limit ?,?", $garbage_query, array($category_param, ($page - 1) * $page_size, $page_size));
if (empty($garbage_query->getResultList())) {
error("无法查询分类" . get_category($category_param) . "的第" . $page . "数据");
error_res("非法查询,请联系管理员", "/admin/WasteSorting.php");
} else {
echo <<<EOF
<div class="container py-5">
<table class="table col-8 m-auto">
<thead>
<tr>
<th scope="col">垃圾名</th>
<th scope="col">垃圾分类</th>
<th scope="col">创建时间</th>
<th scope="col">修改时间</th>
</tr>
</thead>
<tbody>
EOF;
foreach ($garbage_query->getResultList() as $garbage) {
if ($garbage instanceof Garbage) {
$name = $garbage->getName();
$category = get_category($garbage->getCategory());
$create_at = $garbage->getCreateAt();
$update_at = $garbage->getUpdateAt();
echo <<<EOF
<tr>
<th>$name</th>
<td>$category</td>
<td>$create_at</td>
<td>$update_at</td>
</tr>
EOF;
}
}
echo <<<EOF
</tbody>
</table>
<nav aria-label="..." >
<ul class="pagination justify-content-center">
EOF;
if ($page === 1) {
echo <<<EOF
<li class="page-item disabled">
<span class="page-link">上一页</span>
</li>
EOF;
} else {
$prev = $page - 1;
echo <<<EOF
<li class="page-item">
<a class="page-link" href="/admin/WasteSorting.php?action=list&category=$category_param&page=$prev">上一页</a>
</li>
EOF;
}
for ($start_page = $page; $start_page <= $max_page and $start_page < $page + 3; $start_page++) {
echo '<li class="page-item ' . ($start_page == $page ? "active" : "") . '"><a class="page-link" href="/admin/WasteSorting.php?action=list&category=' . $category_param . '&page=' . $start_page . '">' . $start_page . '</a></li>';
}
if ($page != $max_page) {
$next = $page + 1;
echo <<<EOF
<li class="page-item">
<a class="page-link" href="/admin/WasteSorting.php?action=list&category=$category_param&page=$next">下一页</a>
</li>
EOF;
} else {
echo <<<EOF
<li class="page-item disabled">
<span class="page-link">下一页</span>
</li>
EOF;
}
echo <<<EOF
</ul>
</nav>
<div class="row justify-content-center">
<a class="btn btn-info col-4" href="/admin/WasteSorting.php">返回</a>
</div>
</div>
EOF;
}
}
} else {
error("非法操作");
error_res("非法操作", "/");
}

@ -28,7 +28,7 @@ function query_waste_soring($image_res)
$sql = "select * from garbage where" . substr($sql, 3);
info("查询sql" . $sql);
$garbage_result = getGarbageQuery($sql, $keyword);
$garbage_result = QueryGarbageWithName($sql, $keyword);
json_res(json_encode(array("img_parse" => true, "query" => true, "img_res" => $image_res, "result" => $garbage_result->getGarbageObjArray()), JSON_UNESCAPED_UNICODE));
} else {

@ -36,10 +36,10 @@ class QueryText
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"] . "%"));
$result = QueryGarbageWithName("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"]));
$result = QueryGarbageWithName("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));

@ -67,8 +67,7 @@ abstract class AbstractGarbageQuery implements DoExcute
$stmt->bind_result($col1, $col2, $col3, $col4);
while ($stmt->fetch()) {
$g = new Garbage($col1, $col2, $col3, $col4);
array_push($this->garbage_obj_array, $g);
array_push($this->garbage_obj_array, new Garbage($col1, $col2, $col3, $col4));
}
}
@ -84,7 +83,8 @@ abstract class AbstractGarbageQuery implements DoExcute
}
function getGarbageQuery($sql, $keyword): AbstractGarbageQuery
//根据垃圾名模糊匹配垃圾分类信息
function QueryGarbageWithName($sql, $keyword): AbstractGarbageQuery
{
$garbage_result = new class() extends AbstractGarbageQuery
{
@ -104,6 +104,7 @@ function getGarbageQuery($sql, $keyword): AbstractGarbageQuery
return $garbage_result;
}
//查询问题
class QuestionQuery implements DoExcute
{
@ -118,8 +119,7 @@ class QuestionQuery implements DoExcute
{
$stmt->bind_result($col1, $col2);
while ($stmt->fetch()) {
$q = new Question($col1, $col2);
array_push($this->question_array, $q);
array_push($this->question_array, new Question($col1, $col2));
}
}
@ -151,8 +151,7 @@ class AnswerQuery implements DoExcute
{
$stmt->bind_result($col1, $col2, $col3, $col4);
while ($stmt->fetch()) {
$a = new Answer($col1, $col2, $col3, $col4);
array_push($this->answer_array, $a);
array_push($this->answer_array, new Answer($col1, $col2, $col3, $col4));
}
}
@ -167,9 +166,27 @@ class AnswerQuery implements DoExcute
}
//查询问题
class QueryRandQuestion implements DoExcute
function get_category($category)
{
switch ($category) {
case 1:
return "可回收垃圾";
case 2:
return "有害垃圾";
case 4:
return "湿垃圾";
case 8:
return "干垃圾";
case 16:
return "大件垃圾";
}
}
//统计垃圾分类数据量
class CountGrabage implements DoExcute
{
private $result = array();
public function bind_param(mysqli_stmt $stmt, array $param = null)
{
@ -177,11 +194,79 @@ class QueryRandQuestion implements DoExcute
public function doResult(mysqli_stmt $stmt)
{
$stmt->bind_result($col1, $col2, $col3, $col4, $col5);
$stmt->bind_result($col1, $col2);
while ($stmt->fetch()) {
array_push($this->result, array("category" => $col1, "category_name" => get_category($col1), "count" => $col2));
}
}
/**
* @return array
*/
public function getResult(): array
{
return $this->result;
}
}
//查询某个垃圾分类下的垃圾数据
class QueryGarbageWithCategory implements DoExcute
{
private $result_list = array();
public function bind_param(mysqli_stmt $stmt, array $param = null)
{
if (!empty($param)) {
$stmt->bind_param("iii", $param[0], ...array_slice($param, 1));
}
}
public function doResult(mysqli_stmt $stmt)
{
$stmt->bind_result($col1, $col2, $col3, $col4);
while ($stmt->fetch()) {
array_push($this->result_list, new Garbage($col1, $col2, $col3, $col4));
}
}
/**
* @return array
*/
public function getResultList(): array
{
return $this->result_list;
}
}
;
class QueryGarbageCount implements DoExcute
{
private $count;
public function bind_param(mysqli_stmt $stmt, array $param = null)
{
if (!empty($param)) {
$stmt->bind_param("i", $param[0]);
}
}
public function doResult(mysqli_stmt $stmt)
{
$stmt->bind_result($col1);
while ($stmt->fetch()) {
$this->count = $col1;
}
}
/**
* @return mixed
*/
public function getCount()
{
return $this->count;
}
}

@ -3,6 +3,7 @@ require_once "config.php";
require_once __ROOT__ . "/database/DbUtil.php";
require_once __ROOT__ . "/entity/Question.php";
require_once __ROOT__ . "/entity/Answer.php";
require_once __ROOT__ . "/Log.php";
//echo <<<EOF
//<html>
@ -22,8 +23,7 @@ require_once __ROOT__ . "/entity/Answer.php";
//</html>
//EOF;
var_dump(array_search(new Answer(1, 2, 3, 4), array(new Answer(4, 5, 6, 7), new Answer(2, 2, 3, 4))));
//var_dump(array_search(new Answer(1, 2, 3, 4), array(new Answer(4, 5, 6, 7), new Answer(2, 2, 3, 4))));
var_dump(array_search(4, array(2, 5, 1)));

Loading…
Cancel
Save