图书管理
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./lib/bootstrap-v4.6.0.css">
<style>
:root {
font-size: 15px;
}
body {
padding-top: 15px;
}
th,
td {
width: 20%;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-8">
<table class="table table-bordered table-striped table-dark table-hover text-center">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">书名</th>
<th scope="col">作者</th>
<th scope="col">出版社</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">xxx</th>
<td>xxx</td>
<td>xxx</td>
<td>xxx</td>
<td>
<button type="button" class="btn btn-link btn-sm">删除</button>
</td>
</tr>
</tbody>
</table>
</div>
<div class="col-sm-4">
<div class="card text-white bg-secondary sticky-top">
<div class="card-header">添加新图书</div>
<form class="card-body bg-light" id="addForm">
<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="bookname">
</div>
<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="author">
</div>
<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="publisher">
</div>
<button class="btn btn-dark" type="submit">添加</button>
</form>
</div>
</div>
</div>
</div>
<script src="./lib/jquery-v3.6.0.js"></script>
<script src="./lib/axios.js"></script>
<script>
axios.defaults.baseURL='http://www.liulongbin.top:3009'
$(function () {
function initBooks(){
axios.get('/api/getbooks').then(({
data:res
})=>{
tbody.innerHTML= res.data.map((item)=>{
return ` <tr>
<th scope="row">${item.id}</th>
<td>${item.bookname}</td>
<td>${item.author}</td>
<td>${item.publisher}</td>
<td>
<button type="button" data-id="${item.id}"class=" btn-del btn btn-link btn-sm">删除</button>
</td>
</tr>`
}).join('')
})
}
let tbody=document.querySelector('tbody')
initBooks()
let addForm=document.querySelector("#addForm")
addForm.onsubmit=function(e){
e.preventDefault()
axios.post('/api/addbook',$(addForm).serialize()).then(({data:res})=>{
if(res!=201){
return alert(res.msg)
}
alert("恭喜你添加成功")
addForm.reset()
addForm.initBooks()
})
}
tbody.onclick=function(e){
let id=e.target.getAttribute("data-id")
if(!e.target.classList.contains('btn-del')){
return
}
axios.delete('/api/delbook',{
params:{
id
}
}).then(({data:res})=>{
if(res.code !==200){
return alert(res.msg)
}
alert("恭喜你删除成功")
initBooks()
})
}
})
</script>
</body>
</html>
文件头像上传
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>案例-头像上传</title>
<link rel="stylesheet" href="./lib/bootstrap-v4.6.0.css">
<style>
.thumb-box {
text-align: center;
margin-top: 50px;
}
.thumb {
width: 250px;
height: 250px;
object-fit: cover;
border-radius: 50%;
}
</style>
</head>
<body>
<div class="thumb-box">
<img src="./images/cover.jpg" class="img-thumbnail thumb" alt="">
<div class="mt-2">
<input multiple type="file" id="iptFile" accept="image/*" style="display: none;">
<button class="btn btn-primary" id="btnChoose">选择 & 上传图片</button>
</div>
</div>
<script src="./lib/jquery-v3.6.0.js"></script>
<script src="./lib/axios.js"></script>
<script>
let inp=document.querySelector('input')
let btn=document.querySelector("button")
let img=document.querySelector('img')
btn.onclick=function(){
inp.click()
inp.onchange=function(){
let file= this.files[0];
let fd= new FormData
fd.append('avatar',file)
console.log(...fd)
axios({
url:'http://www.liulongbin.top:3009/api/upload/avatar',
method:'post',
data:fd
}).then(({data:res})=>{
if(res.code!=200){
return alert(res.message)
}
alert("恭喜您,上传头像成功");
img.src='http://www.liulongbin.top:3009'+res.url;
})
}
}
</script>
</body>
</html>
聊天机器人
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/main.css" />
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/jquery.mousewheel.js"></script>
<script type="text/javascript" src="js/scroll.js"></script>
<title>聊天机器人</title>
</head>
<body>
<div class="wrap">
<div class="header">
<h3>小思同学</h3>
<img src="img/person01.png" alt="icon" />
</div>
<div class="main">
<ul class="talk_list" style="top: 0px;">
<li class="left_word">
<img src="img/person01.png" />
<span>你好</span>
</li>
</ul>
<div class="drag_bar" style="display: none;">
<div class="drager ui-draggable ui-draggable-handle" style="display: none; height: 412.628px;"></div>
</div>
</div>
<div class="footer">
<img src="img/person02.png" alt="icon" />
<input type="text" placeholder="说的什么吧..." class="input_txt" />
<input type="button" value="发 送" class="input_sub" />
</div>
</div>
<script src="./js/axios.js"></script>
<audio autoplay></audio>
<script>
$(function () {
resetui()
})
let input_txt = document.querySelector('.input_txt')
let input_sub = document.querySelector('.input_sub')
let talk_list = document.querySelector('.talk_list')
input_sub.onclick = function () {
let txt = input_txt.value
if (txt == '') {
input_txt.value.trim() = ""
input_txt.focus()
return alert('输入内容不能为空')
}
let newlist = document.createElement('li')
newlist.className = 'right_word'
newlist.innerHTML = `
<li class="right_word">
<img src="img/person02.png" /> <span>${txt}</span>
</li> `
talk_list.appendChild(newlist)
resetui()
input_txt.value = ''
input_txt.focus()
getMsg(txt)
function getMsg(msg) {
axios({
url: 'http://www.liulongbin.top:3009/api/robot',
method: 'get',
params: {
spoken: msg
}
}).then(({
data: res
}) => {
if (res.message == "success") {
let newli = document.createElement('li')
newli.className = 'left_word'
let text = res.data.info.text
newli.innerHTML = ` <img src="img/person01.png" /><span>${text}</span>`
talk_list.appendChild(newli)
resetui()
getvoice(text)
}
})
}
function getvoice(msg) {
axios({
url: 'http://www.liulongbin.top:3009/api/synthesize',
method: 'get',
params: {
text: msg
}
}).then(({
data: res
}) => {
if (res.code !== 200) {
return alert('语言转换失败')
}
let audio = document.querySelector('audio')
audio.src = res.voiceUrl
})
}
}
</script>
</body>
</html>