|
|
// 定义包名为 com.controller
|
|
|
package com.controller;
|
|
|
|
|
|
// 导入用于格式化日期的 SimpleDateFormat 类
|
|
|
import java.text.SimpleDateFormat;
|
|
|
// 导入 ArrayList 类,用于创建动态数组
|
|
|
import java.util.ArrayList;
|
|
|
// 导入 Arrays 类,用于操作数组
|
|
|
import java.util.Arrays;
|
|
|
// 导入 Calendar 类,用于处理日期和时间
|
|
|
import java.util.Calendar;
|
|
|
// 导入 Map 接口,用于存储键值对
|
|
|
import java.util.Map;
|
|
|
// 导入 HashMap 类,实现 Map 接口
|
|
|
import java.util.HashMap;
|
|
|
// 导入 Iterator 接口,用于遍历集合
|
|
|
import java.util.Iterator;
|
|
|
// 导入 Date 类,用于表示日期和时间
|
|
|
import java.util.Date;
|
|
|
// 导入 List 接口,用于创建列表
|
|
|
import java.util.List;
|
|
|
// 导入 HttpServletRequest 类,用于封装 HTTP 请求信息
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
// 导入自定义的验证工具类
|
|
|
import com.utils.ValidatorUtils;
|
|
|
// 导入 Apache Commons Lang3 库中的 StringUtils 类,用于处理字符串
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
// 导入 Spring 框架的自动注入注解
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
// 导入 Spring 框架的日期格式化注解
|
|
|
import org.springframework.format.annotation.DateTimeFormat;
|
|
|
// 导入 Spring 框架的路径变量注解
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
// 导入 Spring 框架的请求体注解
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
// 导入 Spring 框架的请求映射注解
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
// 导入 Spring 框架的请求参数注解
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
// 导入 Spring 框架的 REST 控制器注解
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
// 导入 MyBatis-Plus 框架的实体包装器类
|
|
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
|
|
// 导入 MyBatis-Plus 框架的包装器接口
|
|
|
import com.baomidou.mybatisplus.mapper.Wrapper;
|
|
|
// 导入自定义的忽略认证注解
|
|
|
import com.annotation.IgnoreAuth;
|
|
|
|
|
|
// 导入用户实体类
|
|
|
import com.entity.YonghuEntity;
|
|
|
// 导入用户视图类
|
|
|
import com.entity.view.YonghuView;
|
|
|
|
|
|
// 导入用户服务类
|
|
|
import com.service.YonghuService;
|
|
|
// 导入令牌服务类
|
|
|
import com.service.TokenService;
|
|
|
// 导入自定义的分页工具类
|
|
|
import com.utils.PageUtils;
|
|
|
// 导入自定义的响应结果类
|
|
|
import com.utils.R;
|
|
|
// 导入自定义的 MD5 工具类
|
|
|
import com.utils.MD5Util;
|
|
|
// 导入自定义的 MyBatis-Plus 工具类
|
|
|
import com.utils.MPUtil;
|
|
|
// 导入自定义的通用工具类
|
|
|
import com.utils.CommonUtil;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 用户
|
|
|
* 后端接口
|
|
|
* @author
|
|
|
* @email
|
|
|
* @date 2021-05-09 15:46:14
|
|
|
*/
|
|
|
// 声明该类为 REST 控制器
|
|
|
@RestController
|
|
|
// 定义该控制器的请求映射路径
|
|
|
@RequestMapping("/yonghu")
|
|
|
public class YonghuController {
|
|
|
// 自动注入用户服务类的实例
|
|
|
@Autowired
|
|
|
private YonghuService yonghuService;
|
|
|
|
|
|
// 自动注入令牌服务类的实例
|
|
|
@Autowired
|
|
|
private TokenService tokenService;
|
|
|
|
|
|
/**
|
|
|
* 登录
|
|
|
*/
|
|
|
// 忽略认证注解,允许未认证用户访问该接口
|
|
|
@IgnoreAuth
|
|
|
// 定义登录接口的请求映射路径
|
|
|
@RequestMapping(value = "/login")
|
|
|
// 登录方法,接收用户名、密码、验证码和 HTTP 请求作为参数
|
|
|
public R login(String username, String password, String captcha, HttpServletRequest request) {
|
|
|
// 根据用户名查询用户实体
|
|
|
YonghuEntity user = yonghuService.selectOne(new EntityWrapper<YonghuEntity>().eq("yonghuzhanghao", username));
|
|
|
// 如果用户不存在或密码不正确
|
|
|
if(user==null || !user.getMima().equals(password)) {
|
|
|
// 返回错误信息
|
|
|
return R.error("账号或密码不正确");
|
|
|
}
|
|
|
|
|
|
// 生成用户令牌
|
|
|
String token = tokenService.generateToken(user.getId(), username,"yonghu", "用户" );
|
|
|
// 返回成功信息并携带令牌
|
|
|
return R.ok().put("token", token);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 注册
|
|
|
*/
|
|
|
// 忽略认证注解,允许未认证用户访问该接口
|
|
|
@IgnoreAuth
|
|
|
// 定义注册接口的请求映射路径
|
|
|
@RequestMapping("/register")
|
|
|
// 注册方法,接收用户实体作为请求体
|
|
|
public R register(@RequestBody YonghuEntity yonghu){
|
|
|
// 验证用户实体(注释掉,可能后续需要启用)
|
|
|
//ValidatorUtils.validateEntity(yonghu);
|
|
|
// 根据用户名查询用户实体
|
|
|
YonghuEntity user = yonghuService.selectOne(new EntityWrapper<YonghuEntity>().eq("yonghuzhanghao", yonghu.getYonghuzhanghao()));
|
|
|
// 如果用户已存在
|
|
|
if(user!=null) {
|
|
|
// 返回错误信息
|
|
|
return R.error("注册用户已存在");
|
|
|
}
|
|
|
// 生成用户 ID,使用当前时间戳
|
|
|
Long uId = new Date().getTime();
|
|
|
// 设置用户 ID
|
|
|
yonghu.setId(uId);
|
|
|
// 插入用户实体到数据库
|
|
|
yonghuService.insert(yonghu);
|
|
|
// 返回成功信息
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 退出
|
|
|
*/
|
|
|
// 定义退出接口的请求映射路径
|
|
|
@RequestMapping("/logout")
|
|
|
// 退出方法,接收 HTTP 请求作为参数
|
|
|
public R logout(HttpServletRequest request) {
|
|
|
// 使当前会话无效
|
|
|
request.getSession().invalidate();
|
|
|
// 返回退出成功信息
|
|
|
return R.ok("退出成功");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取用户的session用户信息
|
|
|
*/
|
|
|
// 定义获取用户会话信息接口的请求映射路径
|
|
|
@RequestMapping("/session")
|
|
|
// 获取用户会话信息方法,接收 HTTP 请求作为参数
|
|
|
public R getCurrUser(HttpServletRequest request){
|
|
|
// 从会话中获取用户 ID
|
|
|
Long id = (Long)request.getSession().getAttribute("userId");
|
|
|
// 根据用户 ID 查询用户实体
|
|
|
YonghuEntity user = yonghuService.selectById(id);
|
|
|
// 返回成功信息并携带用户实体
|
|
|
return R.ok().put("data", user);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 密码重置
|
|
|
*/
|
|
|
// 忽略认证注解,允许未认证用户访问该接口
|
|
|
@IgnoreAuth
|
|
|
// 定义密码重置接口的请求映射路径
|
|
|
@RequestMapping(value = "/resetPass")
|
|
|
// 密码重置方法,接收用户名和 HTTP 请求作为参数
|
|
|
public R resetPass(String username, HttpServletRequest request){
|
|
|
// 根据用户名查询用户实体
|
|
|
YonghuEntity user = yonghuService.selectOne(new EntityWrapper<YonghuEntity>().eq("yonghuzhanghao", username));
|
|
|
// 如果用户不存在
|
|
|
if(user==null) {
|
|
|
// 返回错误信息
|
|
|
return R.error("账号不存在");
|
|
|
}
|
|
|
// 将用户密码重置为 123456
|
|
|
user.setMima("123456");
|
|
|
// 更新用户实体到数据库
|
|
|
yonghuService.updateById(user);
|
|
|
// 返回密码重置成功信息
|
|
|
return R.ok("密码已重置为:123456");
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 后端列表
|
|
|
*/
|
|
|
// 定义后端列表接口的请求映射路径
|
|
|
@RequestMapping("/page")
|
|
|
// 后端列表方法,接收请求参数、用户实体和 HTTP 请求作为参数
|
|
|
public R page(@RequestParam Map<String, Object> params,YonghuEntity yonghu,
|
|
|
HttpServletRequest request){
|
|
|
// 创建用户实体的包装器
|
|
|
EntityWrapper<YonghuEntity> ew = new EntityWrapper<YonghuEntity>();
|
|
|
// 调用用户服务类的查询分页方法
|
|
|
PageUtils page = yonghuService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, yonghu), params), params));
|
|
|
|
|
|
// 返回成功信息并携带分页数据
|
|
|
return R.ok().put("data", page);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前端列表
|
|
|
*/
|
|
|
// 定义前端列表接口的请求映射路径
|
|
|
@RequestMapping("/list")
|
|
|
// 前端列表方法,接收请求参数、用户实体和 HTTP 请求作为参数
|
|
|
public R list(@RequestParam Map<String, Object> params,YonghuEntity yonghu,
|
|
|
HttpServletRequest request){
|
|
|
// 创建用户实体的包装器
|
|
|
EntityWrapper<YonghuEntity> ew = new EntityWrapper<YonghuEntity>();
|
|
|
// 调用用户服务类的查询分页方法
|
|
|
PageUtils page = yonghuService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, yonghu), params), params));
|
|
|
// 返回成功信息并携带分页数据
|
|
|
return R.ok().put("data", page);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 列表
|
|
|
*/
|
|
|
// 定义列表接口的请求映射路径
|
|
|
@RequestMapping("/lists")
|
|
|
// 列表方法,接收用户实体作为参数
|
|
|
public R list( YonghuEntity yonghu){
|
|
|
// 创建用户实体的包装器
|
|
|
EntityWrapper<YonghuEntity> ew = new EntityWrapper<YonghuEntity>();
|
|
|
// 设置包装器的查询条件
|
|
|
ew.allEq(MPUtil.allEQMapPre( yonghu, "yonghu"));
|
|
|
// 返回成功信息并携带用户视图列表
|
|
|
return R.ok().put("data", yonghuService.selectListView(ew));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询
|
|
|
*/
|
|
|
// 定义查询接口的请求映射路径
|
|
|
@RequestMapping("/query")
|
|
|
// 查询方法,接收用户实体作为参数
|
|
|
public R query(YonghuEntity yonghu){
|
|
|
// 创建用户实体的包装器
|
|
|
EntityWrapper< YonghuEntity> ew = new EntityWrapper< YonghuEntity>();
|
|
|
// 设置包装器的查询条件
|
|
|
ew.allEq(MPUtil.allEQMapPre( yonghu, "yonghu"));
|
|
|
// 调用用户服务类的查询视图方法
|
|
|
YonghuView yonghuView = yonghuService.selectView(ew);
|
|
|
// 返回查询成功信息并携带用户视图
|
|
|
return R.ok("查询用户成功").put("data", yonghuView);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 后端详情
|
|
|
*/
|
|
|
// 定义后端详情接口的请求映射路径
|
|
|
@RequestMapping("/info/{id}")
|
|
|
// 后端详情方法,接收用户 ID 作为路径变量
|
|
|
public R info(@PathVariable("id") Long id){
|
|
|
// 根据用户 ID 查询用户实体
|
|
|
YonghuEntity yonghu = yonghuService.selectById(id);
|
|
|
// 返回成功信息并携带用户实体
|
|
|
return R.ok().put("data", yonghu);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前端详情
|
|
|
*/
|
|
|
// 定义前端详情接口的请求映射路径
|
|
|
@RequestMapping("/detail/{id}")
|
|
|
// 前端详情方法,接收用户 ID 作为路径变量
|
|
|
public R detail(@PathVariable("id") Long id){
|
|
|
// 根据用户 ID 查询用户实体
|
|
|
YonghuEntity yonghu = yonghuService.selectById(id);
|
|
|
// 返回成功信息并携带用户实体
|
|
|
return R.ok().put("data", yonghu);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 后端保存
|
|
|
*/
|
|
|
// 定义后端保存接口的请求映射路径
|
|
|
@RequestMapping("/save")
|
|
|
// 后端保存方法,接收用户实体作为请求体和 HTTP 请求作为参数
|
|
|
public R save(@RequestBody YonghuEntity yonghu, HttpServletRequest request){
|
|
|
// 生成用户 ID,使用当前时间戳加上随机数
|
|
|
yonghu.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
|
|
|
// 验证用户实体(注释掉,可能后续需要启用)
|
|
|
//ValidatorUtils.validateEntity(yonghu);
|
|
|
// 根据用户名查询用户实体
|
|
|
YonghuEntity user = yonghuService.selectOne(new EntityWrapper<YonghuEntity>().eq("yonghuzhanghao", yonghu.getYonghuzhanghao()));
|
|
|
// 如果用户已存在
|
|
|
if(user!=null) {
|
|
|
// 返回错误信息
|
|
|
return R.error("用户已存在");
|
|
|
}
|
|
|
// 重新设置用户 ID,使用当前时间戳
|
|
|
yonghu.setId(new Date().getTime());
|
|
|
// 插入用户实体到数据库
|
|
|
yonghuService.insert(yonghu);
|
|
|
// 返回成功信息
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前端保存
|
|
|
*/
|
|
|
// 定义前端保存接口的请求映射路径
|
|
|
@RequestMapping("/add")
|
|
|
// 前端保存方法,接收用户实体作为请求体和 HTTP 请求作为参数
|
|
|
public R add(@RequestBody YonghuEntity yonghu, HttpServletRequest request){
|
|
|
// 生成用户 ID,使用当前时间戳加上随机数
|
|
|
yonghu.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
|
|
|
// 验证用户实体(注释掉,可能后续需要启用)
|
|
|
//ValidatorUtils.validateEntity(yonghu);
|
|
|
// 根据用户名查询用户实体
|
|
|
YonghuEntity user = yonghuService.selectOne(new EntityWrapper<YonghuEntity>().eq("yonghuzhanghao", yonghu.getYonghuzhanghao()));
|
|
|
// 如果用户已存在
|
|
|
if(user!=null) {
|
|
|
// 返回错误信息
|
|
|
return R.error("用户已存在");
|
|
|
}
|
|
|
// 重新设置用户 ID,使用当前时间戳
|
|
|
yonghu.setId(new Date().getTime());
|
|
|
// 插入用户实体到数据库
|
|
|
yonghuService.insert(yonghu);
|
|
|
// 返回成功信息
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 修改
|
|
|
*/
|
|
|
// 定义修改接口的请求映射路径
|
|
|
@RequestMapping("/update")
|
|
|
// 修改方法,接收用户实体作为请求体和 HTTP 请求作为参数
|
|
|
public R update(@RequestBody YonghuEntity yonghu, HttpServletRequest request){
|
|
|
// 验证用户实体(注释掉,可能后续需要启用)
|
|
|
//ValidatorUtils.validateEntity(yonghu);
|
|
|
// 更新用户实体到数据库
|
|
|
yonghuService.updateById(yonghu);//全部更新
|
|
|
// 返回成功信息
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 删除
|
|
|
*/
|
|
|
// 定义删除接口的请求映射路径
|
|
|
@RequestMapping("/delete")
|
|
|
// 删除方法,接收用户 ID 数组作为请求体
|
|
|
public R delete(@RequestBody Long[] ids){
|
|
|
// 批量删除用户实体
|
|
|
yonghuService.deleteBatchIds(Arrays.asList(ids));
|
|
|
// 返回成功信息
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 提醒接口
|
|
|
*/
|
|
|
// 定义提醒接口的请求映射路径
|
|
|
@RequestMapping("/remind/{columnName}/{type}")
|
|
|
// 提醒方法,接收列名、HTTP 请求、类型和请求参数作为参数
|
|
|
public R remindCount(@PathVariable("columnName") String columnName, HttpServletRequest request,
|
|
|
@PathVariable("type") String type,@RequestParam Map<String, Object> map) {
|
|
|
// 将列名放入请求参数中
|
|
|
map.put("column", columnName);
|
|
|
// 将类型放入请求参数中
|
|
|
map.put("type", type);
|
|
|
|
|
|
// 如果类型为 2
|
|
|
if(type.equals("2")) {
|
|
|
// 创建日期格式化对象
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
// 创建日历对象
|
|
|
Calendar c = Calendar.getInstance();
|
|
|
// 提醒开始日期
|
|
|
Date remindStartDate = null;
|
|
|
// 提醒结束日期
|
|
|
Date remindEndDate = null;
|
|
|
// 如果请求参数中包含提醒开始时间
|
|
|
if(map.get("remindstart")!=null) {
|
|
|
// 获取提醒开始时间并转换为整数
|
|
|
Integer remindStart = Integer.parseInt(map.get("remindstart").toString());
|
|
|
// 设置日历时间为当前时间
|
|
|
c.setTime(new Date());
|
|
|
// 增加指定天数
|
|
|
c.add(Calendar.DAY_OF_MONTH,remindStart);
|
|
|
// 获取提醒开始日期
|
|
|
remindStartDate = c.getTime();
|
|
|
// 将提醒开始日期格式化后放入请求参数中
|
|
|
map.put("remindstart", sdf.format(remindStartDate));
|
|
|
}
|
|
|
// 如果请求参数中包含提醒结束时间
|
|
|
if(map.get("remindend")!=null) {
|
|
|
// 获取提醒结束时间并转换为整数
|
|
|
Integer remindEnd = Integer.parseInt(map.get("remindend").toString());
|
|
|
// 设置日历时间为当前时间
|
|
|
c.setTime(new Date());
|
|
|
// 增加指定天数
|
|
|
c.add(Calendar.DAY_OF_MONTH,remindEnd);
|
|
|
// 获取提醒结束日期
|
|
|
remindEndDate = c.getTime();
|
|
|
// 将提醒结束日期格式化后放入请求参数中
|
|
|
map.put("remindend", sdf.format(remindEndDate));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 创建用户实体的包装器
|
|
|
Wrapper<YonghuEntity> wrapper = new EntityWrapper<YonghuEntity>();
|
|
|
// 如果请求参数中包含提醒开始时间
|
|
|
if(map.get("remindstart")!=null) {
|
|
|
// 设置包装器的大于等于查询条件
|
|
|
wrapper.ge(columnName, map.get("remindstart"));
|
|
|
}
|
|
|
// 如果请求参数中包含提醒结束时间
|
|
|
if(map.get("remindend")!=null) {
|
|
|
// 设置包装器的小于等于查询条件
|
|
|
wrapper.le(columnName, map.get("remindend"));
|
|
|
}
|
|
|
|
|
|
|
|
|
// 统计符合条件的用户数量
|
|
|
int count = yonghuService.selectCount(wrapper);
|
|
|
// 返回成功信息并携带统计数量
|
|
|
return R.ok().put("count", count);
|
|
|
}
|
|
|
|
|
|
|
|
|
} |