添加了列出所有图片的功能

This commit is contained in:
Kakune55 2023-12-19 23:11:59 +08:00
parent 7b35f88fee
commit 09fde30b3c
3 changed files with 158 additions and 0 deletions

99
Web/list.html Normal file
View File

@ -0,0 +1,99 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
margin: 20px;
padding: 0;
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
.gallery {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
gap: 20px; /* 左右留出的空白 */
}
.gallery-item {
width: calc(20% - 20px); /* 五张图片占据的宽度 */
margin-bottom: 20px;
box-sizing: border-box;
overflow: hidden;
border: 1px solid #ddd;
border-radius: 8px;
background-color: #fff;
transition: transform 0.3s ease-in-out;
cursor: pointer;
}
.gallery-item:hover {
transform: scale(1.05);
}
.gallery img {
width: 100%;
height: auto;
border-bottom: 1px solid #ddd;
border-radius: 8px 8px 0 0;
}
.info-box {
padding: 10px;
text-align: center;
}
</style>
<title>图片展示页</title>
</head>
<body>
<div class="gallery" id="gallery">
<!-- 图片将通过JavaScript动态添加到这里 -->
</div>
<script>
document.addEventListener("DOMContentLoaded", function() {
// Ajax请求从服务器获取图片ID数组
const xhr = new XMLHttpRequest();
xhr.open("GET", "/idlist", true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
const imageIds = JSON.parse(xhr.responseText);
displayImages(imageIds);
}
};
xhr.send();
function displayImages(imageIds) {
const gallery = document.getElementById("gallery");
// 动态添加图片和信息到展示页
imageIds.forEach(function(id) {
const galleryItem = document.createElement("div");
galleryItem.classList.add("gallery-item");
const img = document.createElement("img");
img.src = `/img/mini?id=${id}`;
const infoBox = document.createElement("div");
infoBox.classList.add("info-box");
infoBox.innerText = `ID: ${id}`;
// 点击事件,跳转到对应链接
galleryItem.addEventListener("click", function() {
window.location.href = `/info?id=${id}`;
});
galleryItem.appendChild(img);
galleryItem.appendChild(infoBox);
gallery.appendChild(galleryItem);
});
}
});
</script>
</body>
</html>

View File

@ -83,4 +83,34 @@ func GetFileName(linkID string) string {
return ""
}
return md5+ext
}
func QueryId() ([]string, error) {
var result []string
db, err := sql.Open("sqlite3", "./data/database.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
rows, err := db.Query("SELECT link FROM mytable")
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
return nil, err
}
result = append(result, name)
}
if err := rows.Err(); err != nil {
return nil, err
}
return result, nil
}

29
main.go
View File

@ -3,6 +3,7 @@ package main
import (
"Pixel/database"
"crypto/md5"
"encoding/json"
"fmt"
"html/template"
"image"
@ -63,9 +64,11 @@ func init() {
func main() {
http.HandleFunc("/info", showimg)
http.HandleFunc("/info/list", showlist)
http.HandleFunc("/upload", upload)
http.HandleFunc("/img/",downloadHandler)//设置访问的路由
http.HandleFunc("/img/mini",displayThumbnailHandler)
http.HandleFunc("/idlist",arrayHandler)
fmt.Println("Web服务器已启动")
err := http.ListenAndServe(":9090", nil) //设置监听的端口
if err != nil {
@ -78,6 +81,11 @@ func showimg(w http.ResponseWriter, r *http.Request) {
t.Execute(w, "Hello")
}
func showlist(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("Web/list.html")
t.Execute(w, "Hello")
}
// 处理/upload 逻辑
func upload(w http.ResponseWriter, r *http.Request) {
@ -230,3 +238,24 @@ func displayThumbnailHandler(w http.ResponseWriter, r *http.Request) {
return
}
}
func arrayHandler(w http.ResponseWriter, r *http.Request) {
// 获取数组数据
data, err := database.QueryId()
if err != nil {
http.Error(w, "数组数据获取失败", http.StatusInternalServerError)
return
}
// 将数组数据转换为 JSON 格式
responseData, err := json.Marshal(data)
if err != nil {
http.Error(w, "无法处理数组数据", http.StatusInternalServerError)
return
}
// 设置响应头,指定内容类型为 JSON
w.Header().Set("Content-Type", "application/json")
// 将 JSON 数据写入响应体
w.Write(responseData)
}