1.编辑登录页面
登录页面:
login.html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录后信息放在session中</title>
<!-- 引入组件库 -->
<script src="js/jquery.min.js"></script>
<script src="js/vue.js"></script>
<script src="js/element.js"></script>
<script src="js/axios-0.18.0.js"></script>
<link rel="stylesheet" href="js/element.css">
<style>
#app {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100%;
}
.form-container {
width: 300px;
}
</style>
</head>
<body>
<div id="app">
<div class="form-container">
<el-form ref="form" :model="form" label-width="80px">
<el-form-item label="姓名">
<el-input v-model="form.username" placeholder="请输入用户名"></el-input>
</el-form-item>
<el-form-item label="密码">
<el-input v-model="form.password" placeholder="请输入密码" type="password"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">提交</el-button>
<el-button @click="onReset">取消</el-button>
</el-form-item>
</el-form>
<!-- 登录成功的弹窗 -->
<el-dialog title="提示" :visible.sync="dialogVisible">
<p>登录成功!</p>
<span slot="footer" class="dialog-footer">
<el-button type="primary" @click="dialogVisible = false; goToReaderPage();">确定</el-button>
</span>
</el-dialog>
</div>
</div>
<script>
new Vue({
el: '#app',
data() {
return {
form: {
username: '',
password: ''
},
dialogVisible: false
}
},
methods: {
onSubmit() {
var url = `/login_session`;
console.log(this.form.username);
console.log(this.form.password);
axios.post(url, {
name: this.form.username,
password: this.form.password
})
.then(response => {
this.dialogVisible = true;
})
.catch(error => {
console.error(error);
});
},
onReset() {
this.form.username = '';
this.form.password = '';
},
goToReaderPage() {
location.href = '/reader.html';
}
}
});
</script>
</body>
</html>
页面退出登录:
logout() {
var url = '/logout';
axios.get(url)
.then(response => {
console.log('Logout successful');
// 跳转到登录页面
alert('退出成功!');
location.href = 'login.html';
})
.catch(error => {
console.error(error);
alert('退出失败,请重试');
});
},
登录验证:
created() {
var url = `/readerLogin`
axios.get(url)
.then(response => {
//this.tableData = response.data;
this.tableData = response.data;
console.log(this.tableData);
this.findAll(this.pageNum);
})
.catch(error=>{
console.error(error);
})
}
后代代码:
loginController
package com.library.Controller;
import com.library.Utils.JwtUtils;
import com.library.pojo.Reader;
import com.library.pojo.Result;
import com.library.service.ReaderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestController
public class LoginController {
@Autowired
private ReaderService readerService;
@PostMapping("/login")
public Result login(@RequestBody Reader reader){
log.info("登录:{}",reader);
Reader r=readerService.login(reader);
// return r!=null?Result.success():Result.erro("登陆失败");
if(r!=null){
Map<String, Object> claims=new HashMap<>();
claims.put("id",r.getId());
claims.put("username",r.getUsername());
String jwt = JwtUtils.generateJwt(claims);
return Result.seccess(jwt);
}
return Result.erro("登陆失败");
}
@PostMapping("/login_session")
public Result login(HttpServletRequest request, @RequestBody Reader reader) {
Reader r=readerService.login(reader);
if (r != null) {
request.getSession().setAttribute("user", reader.getUsername());
String user = (String) request.getSession().getAttribute("user");
System.out.println("查询不为空。"+user);
return Result.success();
}
else{
String user = (String) request.getSession().getAttribute("user");
System.out.println("查询为空。"+user);
return Result.erro("用户名或密码错误");
}
}
@GetMapping("/logout")
public Result logout(HttpServletRequest request) {
//清理Session中保存的当前登录员工的id
request.getSession().removeAttribute("user");
return Result.seccess("退出成功");
}
@GetMapping("/readerLogin")
public Result readerlogin(HttpServletRequest request) {
String reader = (String) request.getSession().getAttribute("reader");
if (reader != null) {
return Result.success();
} else {
return Result.erro("无权限");
}
}
}
3.编写接口文档:
# 图书管理系统接口文档
## 3.读者管理
### 3.1 读者列表查询分页功能
#### 3.1.1 基本信息
> 请求路径:/reader/{page}/{pageSize}
>
> 请求方式:GET
>
> 接口描述:该接口用于读者数据的分页输出
#### 3.1.2 请求参数
参数格式:queryString
参数说明:
| 参数名称 | 是否必须 | 示例 | 备注 |
| -------- | -------- | ---------- | ---------------------------|
| page | 是 | 1 | 分页查询的页码,如果未指定,默认为1 |
| pageSize | 是 | 10 | 分页查询的每页记录数,如果未指定,默认为10 |
请求数据样例:
```shell
/reader/1/2
```
#### 3.1.3 响应数据
参数格式:application/json
参数说明:
| 参数名 | 类型 | 是否必须 | 备注 |
| -------------- | --------- | -------- | ------------------------------ |
| code | number | 必须 | 响应码,1 代表成功,0 代表失败 |
| msg | string | 非必须 | 提示信息 |
| data | object[] | 非必须 | 返回的数据 |
|\|-id | number | 非必须 | 编号 | |
|\|-username | string | 非必须 | 读者用户名 |
|\|-password | string | 非必须 | 账户密码 |
|\|-realName | string | 非必须 | 姓名 |
|\|-sex | string | 非必须 | 性别 |
|\|readerNumber | string | 非必须 | 用户编号 |
|\|-birthday | string | 非必须 | 出生日 |
|\|-address | string | 非必须 | 地址 |
|\|-tel | string | 非必须 | 电话号码 |
|\|-email | string | 非必须 | 邮箱 |
|\|-registerDate | string | 非必须 | 账号注册时间 |
响应数据案例:
```json
{
"code": 1,
"msg": "success",
"data": {
"total": 15,
"rows": [
{
"id": 4,
"username": "user66",
"password": "password6",
"realName": "张旭",
"sex": 0,
"birthday": "1997-04-30T00:00:00",
"address": "南昌大道3号",
"tel": "34567890123",
"email": "user6@test.com",
"registerDate": "2024-10-06T13:00:00",
"readerNumber": "R67890"
},
{
"id": 5,
"username": "user7",
"password": "password7",
"realName": "李四",
"sex": 1,
"birthday": "1998-06-23T00:00:00",
"address": "嘿嘿嘿",
"tel": "7895426987",
"email": "user7@test。com",
"registerDate": "2024-03-04T20:32:16",
"readerNumber": "R67891"
}
]
}
}
```
### 3.2 读者列表查询用户姓名功能
#### 3.2.1 基本信息
> 请求路径:/readers1/{realName}
>
> 请求方式:GET
>
> 接口描述:该接口用于读者数据的查询姓名的模糊查询
##### 3.2.2 请求参数
参数格式:queryString
参数说明:
| 参数名称 | 是否必须 | 示例 | 备注 |
| -------- | -------- | ---------- | ---------------------------|
| realName | 是 | 张三 | 查询读者姓名,可以模糊查询 |
请求数据样例:
```shell
/readers1/%E5%BC%A0%E4%B8%89
```
#### 3.2.3 响应数据
参数格式:application/json
参数说明:
| 参数名 | 类型 | 是否必须 | 备注 |
| ---------------- | --------- | -------- | ------------------------------ |
| code | number | 必须 | 响应码,1 代表成功,0 代表失败 |
| msg | string | 非必须 | 提示信息 |
| data | object[] | 非必须 | 返回的数据 |
|\|-id | number | 非必须 | 编号 | |
|\|-username | string | 非必须 | 读者用户名 |
|\|-password | string | 非必须 | 账户密码 |
|\|-realName | string | 非必须 | 姓名 |
|\|-sex | string | 非必须 | 性别 |
|\|readerNumber | string | 非必须 | 用户编号 |
|\|-birthday | string | 非必须 | 出生日 |
|\|-address | string | 非必须 | 地址 |
|\|-tel | string | 非必须 | 电话号码 |
|\|-email | string | 非必须 | 邮箱 |
|\|-registerDate | string | 非必须 | 账号注册时间 |
响应数据案例:
```json
{
"code": 1,
"msg": "success",
"data": [
{
"id": 6,
"username": "user1",
"password": "密码zs",
"realName": "张三",
"sex": 0,
"birthday": "1990-05-15T00:00:00",
"address": "北京市朝阳区",
"tel": "1234567890",
"email": "user1@test.com",
"registerDate": "2024-05-23T09:00:00",
"readerNumber": "R12345"
}
]
}
```
### 3.3 根据日期范围查询读者信息
#### 3.3.1 基本信息
> 请求路径:/reader/{startDate}/{endDate}/{page}/{pageSize}
>
> 请求方式:GET
>
> 接口描述:该接口用于根据选定的日期范围,筛选在这一时期内注册的读者信息
##### 1.3.2 请求参数
参数格式:路径参数
参数说明:
| 参数名 | 类型 | 是否必须 | 备注 |
| ------ | ------ | -------- | ------ |
| startDate | LocalDateTime | 必须 | 起始日期 |
| endDate | LocalDateTime | 必须 | 终止日期 |
| page | 是 | 1 | 分页查询的页码,如果未指定,默认为1 |
| pageSize | 是 | 10 | 分页查询的每页记录数,如果未指定,默认为10 |
```
/reader//2024-06-01T17:07:58/2024-06-30T17:07:58/1/2
```
#### 3.3.3 响应数据
参数格式:application/json
参数说明:
| 参数名 | 类型 | 是否必须 | 备注 |
| ---------------- | --------- | -------- | ------------------------------ |
| code | number | 必须 | 响应码,1 代表成功,0 代表失败 |
| msg | string | 非必须 | 提示信息 |
| data | object[] | 非必须 | 返回的数据 |
|\|-id | number | 非必须 | 编号 | |
|\|-username | string | 非必须 | 读者用户名 |
|\|-password | string | 非必须 | 账户密码 |
|\|-realName | string | 非必须 | 姓名 |
|\|-sex | string | 非必须 | 性别 |
|\|readerNumber | string | 非必须 | 用户编号 |
|\|-birthday | string | 非必须 | 出生日 |
|\|-address | string | 非必须 | 地址 |
|\|-tel | string | 非必须 | 电话号码 |
|\|-email | string | 非必须 | 邮箱 |
|\|-registerDate | string | 非必须 | 账号注册时间 |
响应数据案例:
```json
{
"code": 1,
"msg": "success",
"data": [
{
"id": 6,
"username": "user1",
"password": "密码zs",
"realName": "张三",
"sex": 0,
"birthday": "2024-06-15T00:00:00",
"address": "北京市朝阳区",
"tel": "1234567890",
"email": "user1@test.com",
"registerDate": "2024-05-23T09:00:00",
"readerNumber": "R12345"
}
]
}
```
### 3.4 删除单条读者信息
#### 3.4.1 基本信息
> 请求路径:/reader/{id}
>
> 请求方式:DELETE
>
> 接口描述:该接口用于根据ID删除读者信息
##### 3.3.2 请求参数
参数格式:路径参数
参数说明:
| 参数名 | 类型 | 是否必须 | 备注 |
| ------ | ------ | -------- | ------ |
| id | number | 必须 | 编号 |
请求参数样例:
```
/reader/1
```
#### 3.4.3 响应数据
参数格式:application/json
参数说明:
| 参数名 | 类型 | 是否必须 | 备注 |
| ------ | ------ | -------- | ------------------------------ |
| code | number | 必须 | 响应码,1 代表成功,0 代表失败 |
| msg | string | 非必须 | 提示信息 |
| data | object | 非必须 | 返回的数据 |
响应数据样例:
```json
{
"code":1,
"msg":"success",
"data":null
}
```
### 3.5 添加读者信息
#### 3.5.1 基本信息
> 请求路径:/readers
>
> 请求方式:POST
>
> 接口描述:该接口用于添加读者信息数据
#### 3.5.2 请求参数
格式:application/json
参数说明:
| 参数名 | 类型 | 是否必须 | 备注 |
| ------ | ------ | -------- | -------- |
|\|-username | string | 必须 | 读者用户名 |
|\|-password | string | 必须 | 账户密码 |
|\|-realName | string | 必须 | 姓名 |
|\|-sex | string | 必须 | 性别 |
|\|readerNumber | string | 必须 | 用户编号 |
|\|-birthday | string | 必须 | 出生日 |
|\|-address | string | 必须 | 地址 |
|\|-tel | string | 必须 | 电话号码 |
|\|-email | string | 必须 | 邮箱 |
请求参数样例:
```json
{
"username": "user1",
"password": "密码zs",
"realName": "张三",
"sex": 0,
"birthday": "2024-06-15T00:00:00",
"address": "北京市朝阳区",
"tel": "1234567890",
"email": "user1@test.com",
"readerNumber": "R12345"
}
```
#### 3.5.3 响应数据
参数格式:application/json
参数说明:
| 参数名 | 类型 | 是否必须 | 备注 |
| ------ | ------ | -------- | ------------------------------ |
| code | number | 必须 | 响应码,1 代表成功,0 代表失败 |
| msg | string | 非必须 | 提示信息 |
| data | object | 非必须 | 返回的数据 |
响应数据样例:
```json
{
"code":1,
"msg":"success",
"data":null
}
```
### 3.6 根据ID查询
#### 3.6.1 基本信息
> 请求路径:/reader/{id}
>
> 请求方式:GET
>
> 接口描述:该接口用于根据ID查询读者信息数据
#### 3.6.2 请求参数
参数格式:路径参数
参数说明:
| 参数名 | 类型 | 是否必须 | 备注 |
| ------ | ------ | -------- | ------ |
| id | number | 必须 | 读者ID |
请求参数样例:
```
/reader/1
```
#### 3.6.3 响应数据
参数格式:application/json
参数说明:
| 参数名 | 类型 | 是否必须 | 备注 |
| -------------- | --------- | -------- | ------------------------------ |
| code | number | 必须 | 响应码,1 代表成功,0 代表失败 |
| msg | string | 非必须 | 提示信息 |
| data | object[ ] | 非必须 | 返回的数据 |
|\|-id | number | 非必须 | 编号 | |
|\|-username | string | 非必须 | 读者用户名 |
|\|-password | string | 非必须 | 账户密码 |
|\|-realName | string | 非必须 | 姓名 |
|\|-sex | string | 非必须 | 性别 |
|\|readerNumber | string | 非必须 | 用户编号 |
|\|-birthday | string | 非必须 | 出生日 |
|\|-address | string | 非必须 | 地址 |
|\|-tel | string | 非必须 | 电话号码 |
|\|-email | string | 非必须 | 邮箱 |
|\|-registerDate | string | 非必须 | 账号注册时间 |
响应数据案例:
```json
{
"code": 1,
"msg": "success",
"data": [
{
"id": 6,
"username": "user1",
"password": "密码zs",
"realName": "张三",
"sex": 0,
"birthday": "2024-06-15T00:00:00",
"address": "北京市朝阳区",
"tel": "1234567890",
"email": "user1@test.com",
"registerDate": "2024-05-23T09:00:00",
"readerNumber": "R12345"
}
]
}
```
### 3.7 修改读者信息
#### 3.7.1 基本信息
> 请求路径:/readersPut/{id}
>
> 请求方式:PUT
>
> 接口描述:该接口用于根据id修改读者信息数据
#### 3.7.2 请求参数
格式:路径参数
参数说明:
| 参数名 | 类型 | 是否必须 | 备注 |
| ------ | ------ | -------- | -------- |
| id | number | 必须 | 读者ID |
请求参数样例:
```json
/readersPut/1
```
#### 3.7.3 响应数据
参数格式:application/json
参数说明:
| 参数名 | 类型 | 是否必须 | 备注 |
| ------ | ------ | -------- | ------------------------------ |
| code | number | 必须 | 响应码,1 代表成功,0 代表失败 |
| msg | string | 非必须 | 提示信息 |
| data | object | 非必须 | 返回的数据 |
响应数据样例:
```json
{
"code":1,
"msg":"success",
"data":null
}
```
### 3.8 增加读者信息
#### 3.8.1 基本信息
> 请求路径:/readers
>
> 请求方式:POST
>
> 接口描述:该接口用于新增读者信息数据
#### 3.7.2 请求
请求样例:
```json
/readers
```
#### 3.7.3 响应数据
参数格式:application/json
参数说明:
| 参数名 | 类型 | 是否必须 | 备注 |
| ------ | ------ | -------- | ------------------------------ |
| code | number | 必须 | 响应码,1 代表成功,0 代表失败 |
| msg | string | 非必须 | 提示信息 |
| data | object | 非必须 | 返回的数据 |
响应数据样例:
```json
{
"code":1,
"msg":"success",
"data":null
}
```