diff --git a/src/main/java/com/controller/FileController.java b/src/main/java/com/controller/FileController.java index bf53f29..2c49511 100644 --- a/src/main/java/com/controller/FileController.java +++ b/src/main/java/com/controller/FileController.java @@ -2,6 +2,9 @@ package com.controller; import java.io.File; import java.io.IOException; +import java.net.URLDecoder; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.util.Date; import javax.servlet.http.HttpServletRequest; @@ -40,29 +43,33 @@ public class FileController{ public R upload(@RequestParam("file") MultipartFile file, String type,HttpServletRequest request) throws Exception { if (file.isEmpty()) { throw new EIException("上传文件不能为空"); + }String fileExt = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1); + String path = URLDecoder.decode(request.getSession().getServletContext().getRealPath("/upload"),"UTF-8"); + File dest = new File(path); + + if (!dest.getParentFile().exists()) { + dest.getParentFile().mkdirs(); + }String path2 = path.replace("target\\jspm0c59i\\upload", "src\\main\\webapp\\upload\\"); + File upload2 = new File(path2); + if (!upload2.exists()) { + upload2.mkdirs(); } - String fileExt = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1); - String fileName = new Date().getTime()+"."+fileExt; - File dest = new File(request.getSession().getServletContext().getRealPath("/upload")+"/"+fileName); - file.transferTo(dest); - /** - * 如果使用idea或者eclipse重启项目,发现之前上传的图片或者文件丢失,将下面一行代码注释打开 - * 请将以下的"D:\\ssmpiv99\\src\\main\\webapp\\upload"替换成你本地项目的upload路径, - * 并且项目路径不能存在中文、空格等特殊字符 - */ - //FileUtils.copyFile(dest, new File("D:\\ssmpiv99\\src\\main\\webapp\\upload"+"/"+fileName)); /**修改了路径以后请将该行最前面的//注释去掉**/ + String fileName = new Date().getTime() + "." + fileExt; + File dest1 = new File(dest.getAbsolutePath() + "/" + fileName); + File dest2 = new File(upload2.getAbsolutePath() + "/" + fileName); + + file.transferTo(dest1); + Files.copy(dest1.toPath(), dest2.toPath(), StandardCopyOption.REPLACE_EXISTING); if(StringUtils.isNotBlank(type) && type.equals("1")) { ConfigEntity configEntity = configService.selectOne(new EntityWrapper().eq("name", "faceFile")); if(configEntity==null) { configEntity = new ConfigEntity(); configEntity.setName("faceFile"); configEntity.setValue(fileName); - } else { + }else { configEntity.setValue(fileName); - } - configService.insertOrUpdate(configEntity); - } - return R.ok().put("file", fileName); + }configService.insertOrUpdate(configEntity); + }return R.ok().put("file", fileName); } /** diff --git a/src/main/java/com/controller/SystemintroController.java b/src/main/java/com/controller/SystemintroController.java index f68fd04..6443f7d 100644 --- a/src/main/java/com/controller/SystemintroController.java +++ b/src/main/java/com/controller/SystemintroController.java @@ -1,43 +1,45 @@ -package com.controller; - -import java.math.BigDecimal; -import java.text.SimpleDateFormat; -import java.text.ParseException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Map; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Date; -import java.util.List; -import javax.servlet.http.HttpServletRequest; -import java.io.IOException; - -import com.utils.ValidatorUtils; -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.format.annotation.DateTimeFormat; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; -import com.baomidou.mybatisplus.mapper.EntityWrapper; -import com.baomidou.mybatisplus.mapper.Wrapper; -import com.annotation.IgnoreAuth; - -import com.entity.SystemintroEntity; -import com.entity.view.SystemintroView; - -import com.service.SystemintroService; -import com.service.TokenService; -import com.utils.PageUtils; -import com.utils.R; -import com.utils.MD5Util; -import com.utils.MPUtil; -import com.utils.CommonUtil; +package com.controller; // 定义一个包,用于组织和管理类 + +import java.math.BigDecimal; // 导入BigDecimal类,用于高精度的数学计算 +import java.text.SimpleDateFormat; // 导入SimpleDateFormat类,用于日期格式化 +import java.text.ParseException; // 导入ParseException类,用于处理日期解析异常 +import java.util.ArrayList; // 导入ArrayList类,用于创建动态数组 +import java.util.Arrays; // 导入Arrays类,提供对数组操作的工具方法 +import java.util.Calendar; // 导入Calendar类,用于日期时间操作 +import java.util.Map; // 导入Map接口,用于键值对集合 +import java.util.HashMap; // 导入HashMap类,基于哈希表的Map实现 +import java.util.Iterator; // 导入Iterator接口,用于遍历集合 +import java.util.Date; // 导入Date类,用于日期时间表示 +import java.util.List; // 导入List接口,用于列表集合 +import javax.servlet.http.HttpServletRequest; // 导入HttpServletRequest接口,用于访问HTTP请求信息 +import java.io.IOException; // 导入IOException类,用于处理输入输出异常 + +import com.utils.ValidatorUtils; // 导入ValidatorUtils工具类,用于实体验证 +import org.apache.commons.lang3.StringUtils; // 导入StringUtils类,提供字符串操作的工具方法 +import org.springframework.beans.factory.annotation.Autowired; // 导入Autowired注解,用于自动注入依赖 +import org.springframework.transaction.annotation.Transactional; // 导入Transactional注解,用于声明事务管理 +import org.springframework.format.annotation.DateTimeFormat; // 导入DateTimeFormat注解,用于日期时间格式化 +import org.springframework.web.bind.annotation.PathVariable; // 导入PathVariable注解,用于将URL中的参数绑定到控制器方法的参数上 +import org.springframework.web.bind.annotation.RequestBody; // 导入RequestBody注解,用于将HTTP请求体绑定到控制器方法的参数上 +import org.springframework.web.bind.annotation.RequestMapping; // 导入RequestMapping注解,用于映射HTTP请求到控制器方法 +import org.springframework.web.bind.annotation.RequestParam; // 导入RequestParam注解,用于将请求参数绑定到控制器方法的参数上 +import org.springframework.web.bind.annotation.RestController; // 导入RestController注解,用于声明一个REST风格的控制器 + +import com.baomidou.mybatisplus.mapper.EntityWrapper; // 导入EntityWrapper类,用于构建查询条件 +import com.baomidou.mybatisplus.mapper.Wrapper; // 导入Wrapper接口,用于封装查询条件 + +import com.annotation.IgnoreAuth; // 导入IgnoreAuth注解,用于标记不需要认证的接口 + +import com.entity.SystemintroEntity; // 导入SystemintroEntity实体类,代表存储数据 +import com.entity.view.SystemintroView; // 导入SystemintroView视图类,用于展示存储数据 + +import com.service.SystemintroService; // 导入SystemintroService接口,用于存储业务操作 +import com.service.TokenService; // 导入TokenService接口,用于令牌操作(未使用) +import com.utils.PageUtils; // 导入PageUtils工具类,用于分页处理 +import com.utils.R; // 导入R工具类,用于构建统一的响应格式 +import com.utils.MD5Util; // 导入MD5Util工具类,用于MD5加密(未使用) +import com.utils.MPUtil; // 导入MPUtil工具类,提供MyBatis Plus操作的工具方法 +import com.utils.CommonUtil; // 导入CommonUtil工具类,提供通用的工具方法 /** * 关于我们 @@ -46,83 +48,75 @@ import com.utils.CommonUtil; * @email * @date 2023-02-21 09:46:06 */ -@RestController -@RequestMapping("/systemintro") +@RestController // 声明一个REST风格的控制器 +@RequestMapping("/systemintro") // 定义一个请求映射,当访问 /systemintro 路径时,会调用这个控制器 public class SystemintroController { - @Autowired - private SystemintroService systemintroService; - - - - - + @Autowired // 自动注入依赖 + private SystemintroService systemintroService; // 声明一个SystemintroService对象,用于存储业务操作 /** * 后端列表 */ - @RequestMapping("/page") + @RequestMapping("/page") // 定义一个请求映射,当访问 /page 路径时,会调用这个方法 public R page(@RequestParam Map params,SystemintroEntity systemintro, - HttpServletRequest request){ - - EntityWrapper ew = new EntityWrapper(); - - PageUtils page = systemintroService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, systemintro), params), params)); - request.setAttribute("data", page); - return R.ok().put("data", page); + HttpServletRequest request){ // 定义一个方法,接收请求参数、实体对象和HTTP请求对象 + EntityWrapper ew = new EntityWrapper(); // 创建一个实体包装器对象,用于构建查询条件 + PageUtils page = systemintroService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, systemintro), params), params)); // 调用服务层的方法,根据查询条件和分页参数进行分页查询,并返回分页结果 + request.setAttribute("data", page); // 将分页结果设置到HTTP请求对象中 + return R.ok().put("data", page); // 返回一个包含分页结果的响应对象 } /** * 前端列表 */ - @IgnoreAuth - @RequestMapping("/list") + @IgnoreAuth // 定义了一个注解,用于标记不需要权限认证的接口 + @RequestMapping("/list") // 定义一个请求映射,当访问 /list 路径时,会调用这个方法 public R list(@RequestParam Map params,SystemintroEntity systemintro, - HttpServletRequest request){ - EntityWrapper ew = new EntityWrapper(); - - PageUtils page = systemintroService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, systemintro), params), params)); - request.setAttribute("data", page); - return R.ok().put("data", page); + HttpServletRequest request){ // 定义一个方法,接收请求参数、实体对象和HTTP请求对象 + EntityWrapper ew = new EntityWrapper(); // 创建一个实体包装器对象,用于构建查询条件 + PageUtils page = systemintroService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, systemintro), params), params)); // 调用服务层的方法,根据查询条件和分页参数进行分页查询,并返回分页结果 + request.setAttribute("data", page); // 将分页结果设置到HTTP请求对象中 + return R.ok().put("data", page); // 返回一个包含分页结果的响应对象 } /** * 列表 */ - @RequestMapping("/lists") - public R list( SystemintroEntity systemintro){ - EntityWrapper ew = new EntityWrapper(); - ew.allEq(MPUtil.allEQMapPre( systemintro, "systemintro")); - return R.ok().put("data", systemintroService.selectListView(ew)); + @RequestMapping("/lists") // 定义一个请求映射,当访问 /lists 路径时,会调用这个方法 + public R list( SystemintroEntity systemintro){ // 定义一个方法,接收实体对象 + EntityWrapper ew = new EntityWrapper(); // 创建一个实体包装器对象,用于构建查询条件 + ew.allEq(MPUtil.allEQMapPre( systemintro, "systemintro")); // 使用allEq方法设置查询条件,MPUtil.allEQMapPre方法将实体对象的属性转换为Map,并添加前缀"systemintro",allEq方法将Map中的键值对作为查询条件,查询出符合条件的实体对象 + return R.ok().put("data", systemintroService.selectListView(ew)); // 根据查询条件查询出符合条件的实体对象列表,并返回一个包含实体对象列表的响应对象 } /** * 查询 */ - @RequestMapping("/query") - public R query(SystemintroEntity systemintro){ - EntityWrapper< SystemintroEntity> ew = new EntityWrapper< SystemintroEntity>(); - ew.allEq(MPUtil.allEQMapPre( systemintro, "systemintro")); - SystemintroView systemintroView = systemintroService.selectView(ew); - return R.ok("查询关于我们成功").put("data", systemintroView); + @RequestMapping("/query") // 定义一个请求映射,当访问 /query 路径时,会调用这个方法 + public R query(SystemintroEntity systemintro){ // 定义一个方法,接收实体对象 + EntityWrapper< SystemintroEntity> ew = new EntityWrapper< SystemintroEntity>(); // 创建一个实体包装器对象,用于构建查询条件 + ew.allEq(MPUtil.allEQMapPre( systemintro, "systemintro")); // 使用allEq方法设置查询条件,MPUtil.allEQMapPre方法将实体对象的属性转换为Map,并添加前缀"systemintro",allEq方法将Map中的键值对作为查询条件,查询出符合条件的实体对象 + SystemintroView systemintroView = systemintroService.selectView(ew); // 调用服务层的方法查询单个数据,并返回视图对象 + return R.ok("查询关于我们成功").put("data", systemintroView); // 返回一个包含查询结果的响应对象,并附带成功消息 } /** * 后端详情 */ - @RequestMapping("/info/{id}") - public R info(@PathVariable("id") Long id){ - SystemintroEntity systemintro = systemintroService.selectById(id); - return R.ok().put("data", systemintro); + @RequestMapping("/info/{id}") // 定义一个请求映射,当访问 /info/{id} 路径时,会调用这个方法 + public R info(@PathVariable("id") Long id){ // 定义一个方法,接收一个路径变量 id,类型为 Long + SystemintroEntity systemintro = systemintroService.selectById(id); // 调用服务层的方法根据ID查询数据,并返回实体对象 + return R.ok().put("data", systemintro); // 返回一个包含查询结果的 R 对象,其中包含键 "data" 和对应的 systemintro 实体对象 } /** * 前端详情 */ - @IgnoreAuth - @RequestMapping("/detail/{id}") - public R detail(@PathVariable("id") Long id){ - SystemintroEntity systemintro = systemintroService.selectById(id); - return R.ok().put("data", systemintro); + @IgnoreAuth // 定义了一个注解,用于标记不需要权限认证的接口 + @RequestMapping("/detail/{id}") // 定义一个请求映射,当访问 /detail/{id} 路径时,会调用这个方法 + public R detail(@PathVariable("id") Long id){ // 定义一个方法,接收一个路径变量 id,类型为 Long + SystemintroEntity systemintro = systemintroService.selectById(id); // 调用服务层的方法根据ID查询数据,并返回实体对象 + return R.ok().put("data", systemintro); // 返回一个包含查询结果的响应对象,并附带成功消息 } @@ -131,37 +125,38 @@ public class SystemintroController { /** * 后端保存 */ - @RequestMapping("/save") - public R save(@RequestBody SystemintroEntity systemintro, HttpServletRequest request){ - systemintro.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue()); - //ValidatorUtils.validateEntity(systemintro); - - systemintroService.insert(systemintro); - return R.ok(); + @RequestMapping("/save") // 定义一个请求映射,当访问 /save 路径时,会调用这个方法 + public R save(@RequestBody SystemintroEntity systemintro, HttpServletRequest request){ // 定义一个方法,接收一个JSON格式的请求体,类型为 SystemintroEntity,并接收一个 HttpServletRequest 对象 + systemintro.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue()); // 生成一个新的唯一ID,由当前时间戳加上一个随机数 + //ValidatorUtils.validateEntity(systemintro); // 注释掉的代码,用于验证 systemintro 实体对象是否符合要求 + systemintroService.insert(systemintro); // 调用服务层的方法将 systemintro 实体对象插入数据库 + return R.ok(); // 返回一个包含成功信息的 R 对象 } /** * 前端保存 */ - @RequestMapping("/add") - public R add(@RequestBody SystemintroEntity systemintro, HttpServletRequest request){ - systemintro.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue()); - //ValidatorUtils.validateEntity(systemintro); - - systemintroService.insert(systemintro); - return R.ok(); + @RequestMapping("/add") + // 定义一个请求映射,当访问 /add 路径时,会调用这个方法 + public R add(@RequestBody SystemintroEntity systemintro, HttpServletRequest request){ // 定义一个方法,接收一个请求体中的 SystemintroEntity 对象和 HttpServletRequest 对象 + systemintro.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue()); // 生成一个新的唯一ID,由当前时间戳加上一个随机数 + //ValidatorUtils.validateEntity(systemintro); // 注释掉的代码,用于验证 systemintro 实体对象是否符合要求 + systemintroService.insert(systemintro); // 调用服务层的方法将 systemintro 实体对象插入数据库 + return R.ok(); // 返回一个包含成功信息的 R 对象 } /** * 修改 */ - @RequestMapping("/update") - @Transactional - public R update(@RequestBody SystemintroEntity systemintro, HttpServletRequest request){ - //ValidatorUtils.validateEntity(systemintro); - systemintroService.updateById(systemintro);//全部更新 + @RequestMapping("/update") // 定义一个请求映射,当访问 /update 路径时,会调用这个方法 + @Transactional // 定义了一个事务注解,表示该方法中的数据库操作应该在一个事务中执行 + public R update(@RequestBody SystemintroEntity systemintro, HttpServletRequest request){ // 定义一个方法,接收一个请求体中的 SystemintroEntity 对象和 HttpServletRequest 对象 + //ValidatorUtils.validateEntity(systemintro); // 注释掉的代码,用于验证 systemintro 实体对象是否符合要求 + systemintroService.updateById(systemintro); + // 调用服务层的方法根据 ID 更新 systemintro 实体对象的所有字段 return R.ok(); + // 返回一个包含成功信息的 R 对象 } @@ -169,62 +164,57 @@ public class SystemintroController { /** * 删除 */ - @RequestMapping("/delete") - public R delete(@RequestBody Long[] ids){ - systemintroService.deleteBatchIds(Arrays.asList(ids)); - return R.ok(); + @RequestMapping("/delete") + // 定义一个请求映射,当访问 /delete 路径时,会调用这个方法 + public R delete(@RequestBody Long[] ids){ // 定义一个方法,接收一个请求体中的 Long 数组,类型为 Long[] + systemintroService.deleteBatchIds(Arrays.asList(ids)); // 调用服务层的方法,根据传入的 ID 数组批量删除数据 + return R.ok(); + // 返回一个包含成功信息的 R 对象 } /** * 提醒接口 */ - @RequestMapping("/remind/{columnName}/{type}") + @RequestMapping("/remind/{columnName}/{type}") // 定义一个请求映射,当访问 /remind/{columnName}/{type} 路径时,会调用这个方法 public R remindCount(@PathVariable("columnName") String columnName, HttpServletRequest request, - @PathVariable("type") String type,@RequestParam Map map) { - map.put("column", columnName); - map.put("type", type); + @PathVariable("type") String type,@RequestParam Map map) { // 定义一个方法,接收一个路径变量 columnName 和 type,类型为 String,以及一个请求参数 map,类型为 Map + map.put("column", columnName); // 将 columnName 添加到 map 中,键为 "column" + map.put("type", type); + // 将 type 添加到 map 中,键为 "type" - 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(type.equals("2")) { // 如果 type 等于 "2" + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 创建一个日期格式化对象,格式为 "yyyy-MM-dd" + Calendar c = Calendar.getInstance(); // 创建一个日历对象,获取当前时间 + Date remindStartDate = null; + // 创建一个日期对象,用于存储提醒开始时间 + Date remindEndDate = null; // 创建一个日期对象,用于存储提醒结束时间 + if(map.get("remindstart")!=null) { // 如果 map 中存在 "remindstart" 键 + Integer remindStart = Integer.parseInt(map.get("remindstart").toString()); // 将 "remindstart" 键对应的值转换为整数 + c.setTime(new Date()); // 设置日历对象的时间为当前时间 + c.add(Calendar.DAY_OF_MONTH,remindStart); // 将日历对象的时间增加 remindStart 天 + remindStartDate = c.getTime(); // 获取增加后的时间,并赋值给 remindStartDate + map.put("remindstart", sdf.format(remindStartDate)); // 将 remindStartDate 格式化为 "yyyy-MM-dd" 格式,并添加到 map 中,键为 "remindstart" } - 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)); + if(map.get("remindend")!=null) { // 如果 map 中存在 "remindend" 键 + Integer remindEnd = Integer.parseInt(map.get("remindend").toString()); // 将 "remindend" 键对应的值转换为整数 + c.setTime(new Date()); // 设置日历对象的时间为当前时间 + c.add(Calendar.DAY_OF_MONTH,remindEnd); // 将日历对象的时间增加 remindEnd 天 + remindEndDate = c.getTime(); // 获取增加后的时间,并赋值给 remindEndDate + map.put("remindend", sdf.format(remindEndDate)); // 将 remindEndDate 格式化为 "yyyy-MM-dd" 格式,并添加到 map 中,键为 "remindend" } } - Wrapper wrapper = new EntityWrapper(); - if(map.get("remindstart")!=null) { - wrapper.ge(columnName, map.get("remindstart")); + Wrapper wrapper = new EntityWrapper(); // 创建一个实体包装器对象,用于构建查询条件 + if(map.get("remindstart")!=null) { // 如果 map 中存在 "remindstart" 键 + wrapper.ge(columnName, map.get("remindstart")); // 设置查询条件,查询 columnName 字段大于等于 "remindstart" 键对应的值 } - if(map.get("remindend")!=null) { - wrapper.le(columnName, map.get("remindend")); + if(map.get("remindend")!=null) { // 如果 map 中存在 "remindend" 键 + wrapper.le(columnName, map.get("remindend")); // 设置查询条件,查询 columnName 字段小于等于 "remindend" 键对应的值 } - int count = systemintroService.selectCount(wrapper); - return R.ok().put("count", count); + int count = systemintroService.selectCount(wrapper); // 调用服务层的方法,根据查询条件查询符合条件的记录数,并赋值给 count + return R.ok().put("count", count); + // 返回一个包含查询结果的响应对象,其中包含键 "count" 和对应的 count 值 } - - - - - - - - - - } diff --git a/src/main/java/com/controller/UsersController.java b/src/main/java/com/controller/UsersController.java index ea0abe3..ba0ca99 100644 --- a/src/main/java/com/controller/UsersController.java +++ b/src/main/java/com/controller/UsersController.java @@ -35,140 +35,151 @@ import com.utils.ValidatorUtils; /** * 登录相关 */ -@RequestMapping("users") -@RestController -public class UsersController{ - - @Autowired - private UsersService userService; - - @Autowired - private TokenService tokenService; - - /** - * 登录 - */ - @IgnoreAuth - @PostMapping(value = "/login") - public R login(String username, String password, String captcha, HttpServletRequest request) { - UsersEntity user = userService.selectOne(new EntityWrapper().eq("username", username)); - if(user==null || !user.getPassword().equals(password)) { - return R.error("账号或密码不正确"); - } - String token = tokenService.generateToken(user.getId(),username, "users", user.getRole()); - return R.ok().put("token", token); - } - - /** - * 注册 - */ - @IgnoreAuth - @PostMapping(value = "/register") - public R register(@RequestBody UsersEntity user){ -// ValidatorUtils.validateEntity(user); - if(userService.selectOne(new EntityWrapper().eq("username", user.getUsername())) !=null) { - return R.error("用户已存在"); - } - userService.insert(user); - return R.ok(); +@RequestMapping("users") // 定义请求路径的基础 URL 为 "users" +@RestController // 标明该类是一个控制器,返回的对象直接作为 JSON 响应 +public class UsersController { + + @Autowired // 自动注入 UsersService 依赖 + private UsersService userService; + + @Autowired // 自动注入 TokenService 依赖 + private TokenService tokenService; + + /** + * 登录 + */ + @IgnoreAuth // 忽略认证,允许未登录用户访问 + @PostMapping(value = "/login") // 处理 POST 请求,路径为 "users/login" + public R login(String username, String password, String captcha, HttpServletRequest request) { + // 根据用户名查询用户 + UsersEntity user = userService.selectOne(new EntityWrapper().eq("username", username)); + // 检查用户是否存在且密码是否匹配 + if(user == null || !user.getPassword().equals(password)) { + return R.error("账号或密码不正确"); // 返回错误信息 + } + // 生成用户的 token + String token = tokenService.generateToken(user.getId(), username, "users", user.getRole()); + return R.ok().put("token", token); // 返回成功信息和 token + } + + /** + * 注册 + */ + @IgnoreAuth + // 忽略认证,允许未登录用户访问 + @PostMapping(value = "/register") // 处理 POST 请求,路径为 "users/register" + public R register(@RequestBody UsersEntity user) { + // 校验用户是否已存在 + if(userService.selectOne(new EntityWrapper().eq("username", user.getUsername())) != null) { + return R.error("用户已存在"); // 返回用户已存在的错误信息 + } + userService.insert(user); // 插入新用户 + return R.ok(); // 返回成功信息 } - /** - * 退出 - */ - @GetMapping(value = "logout") - public R logout(HttpServletRequest request) { - request.getSession().invalidate(); - return R.ok("退出成功"); - } - - /** + /** + * 退出 + */ + @GetMapping(value = "logout") + // 处理 GET 请求,路径为 "users/logout" + public R logout(HttpServletRequest request) { + request.getSession().invalidate(); // 使 session 失效,用户登出 + return R.ok("退出成功"); // 返回成功信息 + } + + /** * 密码重置 */ - @IgnoreAuth - @RequestMapping(value = "/resetPass") + @IgnoreAuth // 忽略认证,允许未登录用户访问 + @RequestMapping(value = "/resetPass") // 处理请求,路径为 "users/resetPass" public R resetPass(String username, HttpServletRequest request){ + // 根据用户名查询用户 UsersEntity user = userService.selectOne(new EntityWrapper().eq("username", username)); - if(user==null) { - return R.error("账号不存在"); + if(user == null) { + return R.error("账号不存在"); // 返回账号不存在的错误信息 } - user.setPassword("123456"); - userService.update(user,null); - return R.ok("密码已重置为:123456"); + user.setPassword("123456"); + // 将用户密码重置为 "123456" + userService.update(user, null); + // 更新用户信息 + return R.ok("密码已重置为:123456"); + // 返回成功信息 } - - /** + + /** * 列表 */ - @RequestMapping("/page") - public R page(@RequestParam Map params,UsersEntity user){ - EntityWrapper ew = new EntityWrapper(); - PageUtils page = userService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.allLike(ew, user), params), params)); - return R.ok().put("data", page); + @RequestMapping("/page") // 处理请求,路径为 "users/page" + public R page(@RequestParam Map params, UsersEntity user) { + EntityWrapper ew = new EntityWrapper(); // 创建查询条件 + // 调用服务层方法查询分页数据 + PageUtils page = userService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.allLike(ew, user), params), params)); + return R.ok().put("data", page); // 返回查询结果 } - /** + /** * 列表 */ - @RequestMapping("/list") - public R list( UsersEntity user){ - EntityWrapper ew = new EntityWrapper(); - ew.allEq(MPUtil.allEQMapPre( user, "user")); - return R.ok().put("data", userService.selectListView(ew)); + @RequestMapping("/list") // 处理请求,路径为 "users/list" + public R list(UsersEntity user) { + EntityWrapper ew = new EntityWrapper(); // 创建查询条件 + ew.allEq(MPUtil.allEQMapPre(user, "user")); // 添加所有相等条件 + return R.ok().put("data", userService.selectListView(ew)); + // 返回查询结果 } /** * 信息 */ - @RequestMapping("/info/{id}") - public R info(@PathVariable("id") String id){ - UsersEntity user = userService.selectById(id); - return R.ok().put("data", user); + @RequestMapping("/info/{id}") // 处理请求,路径为 "users/info/{id}" + public R info(@PathVariable("id") String id) { + UsersEntity user = userService.selectById(id); // 根据 ID 查找用户 + return R.ok().put("data", user); // 返回用户信息 } - + /** - * 获取用户的session用户信息 + * 获取用户的 session 用户信息 */ - @RequestMapping("/session") - public R getCurrUser(HttpServletRequest request){ - Long id = (Long)request.getSession().getAttribute("userId"); - UsersEntity user = userService.selectById(id); - return R.ok().put("data", user); + @RequestMapping("/session") // 处理请求,路径为 "users/session" + public R getCurrUser(HttpServletRequest request) { + Long id = (Long) request.getSession().getAttribute("userId"); // 从 session 中获取用户 ID + UsersEntity user = userService.selectById(id); // 根据 ID 查找用户 + return R.ok().put("data", user); // 返回用户信息 } /** * 保存 */ - @PostMapping("/save") - public R save(@RequestBody UsersEntity user){ -// ValidatorUtils.validateEntity(user); - if(userService.selectOne(new EntityWrapper().eq("username", user.getUsername())) !=null) { - return R.error("用户已存在"); + @PostMapping("/save") // 处理 POST 请求,路径为 "users/save" + public R save(@RequestBody UsersEntity user) { + // 校验用户是否已存在 + if(userService.selectOne(new EntityWrapper().eq("username", user.getUsername())) != null) { + return R.error("用户已存在"); // 返回用户已存在的错误信息 } - userService.insert(user); - return R.ok(); + userService.insert(user); // 插入新用户 + return R.ok(); // 返回成功信息 } /** * 修改 */ - @RequestMapping("/update") - public R update(@RequestBody UsersEntity user){ -// ValidatorUtils.validateEntity(user); + @RequestMapping("/update") // 处理请求,路径为 "users/update" + public R update(@RequestBody UsersEntity user) { + // 校验用户是否已存在 UsersEntity u = userService.selectOne(new EntityWrapper().eq("username", user.getUsername())); - if(u!=null && u.getId()!=user.getId() && u.getUsername().equals(user.getUsername())) { - return R.error("用户名已存在。"); + if(u != null && u.getId() != user.getId() && u.getUsername().equals(user.getUsername())) { + return R.error("用户名已存在。"); // 返回用户名已存在的错误信息 } - userService.updateById(user);//全部更新 - return R.ok(); + userService.updateById(user); // 更新用户信息 + return R.ok(); // 返回成功信息 } /** * 删除 */ - @RequestMapping("/delete") - public R delete(@RequestBody Long[] ids){ - userService.deleteBatchIds(Arrays.asList(ids)); - return R.ok(); + @RequestMapping("/delete") // 处理请求,路径为 "users/delete" + public R delete(@RequestBody Long[] ids) { + userService.deleteBatchIds(Arrays.asList(ids)); // 批量删除用户 + return R.ok(); // 返回成功信息 } -} +} \ No newline at end of file diff --git a/src/main/java/com/controller/YonghuController.java b/src/main/java/com/controller/YonghuController.java index 7fd259b..efd5279 100644 --- a/src/main/java/com/controller/YonghuController.java +++ b/src/main/java/com/controller/YonghuController.java @@ -39,6 +39,7 @@ import com.utils.MD5Util; import com.utils.MPUtil; import com.utils.CommonUtil; + /** * 用户 * 后端接口 @@ -46,139 +47,141 @@ import com.utils.CommonUtil; * @email * @date 2023-02-21 09:46:06 */ -@RestController -@RequestMapping("/yonghu") +@RestController // 标记这是一个REST控制器 +@RequestMapping("/yonghu") // 设置请求路径的前缀为/yonghu public class YonghuController { @Autowired - private YonghuService yonghuService; - + private YonghuService yonghuService; // 自动注入YonghuService服务 + @Autowired + private TokenService tokenService; // 自动注入TokenService服务 - - @Autowired - private TokenService tokenService; - - /** - * 登录 - */ - @IgnoreAuth - @RequestMapping(value = "/login") - public R login(String username, String password, String captcha, HttpServletRequest request) { - YonghuEntity u = yonghuService.selectOne(new EntityWrapper().eq("zhanghao", username)); - if(u==null || !u.getMima().equals(password)) { - return R.error("账号或密码不正确"); - } - String token = tokenService.generateToken(u.getId(), username,"yonghu", "用户" ); - return R.ok().put("token", token); - } + /** + * 登录 + */ + @IgnoreAuth // 忽略权限验证 + @RequestMapping(value = "/login") // 设置请求路径为/login + public R login(String username, String password, String captcha, HttpServletRequest request) { + // 根据用户名查询用户 + YonghuEntity u = yonghuService.selectOne(new EntityWrapper().eq("zhanghao", username)); + // 验证用户名和密码 + if(u==null || !u.getMima().equals(password)) { + return R.error("账号或密码不正确"); + } + // 生成并返回token + String token = tokenService.generateToken(u.getId(), username,"yonghu", "用户" ); + return R.ok().put("token", token); + } - - /** + /** * 注册 */ - @IgnoreAuth - @RequestMapping("/register") + @IgnoreAuth // 忽略权限验证 + @RequestMapping("/register") // 设置请求路径为/register public R register(@RequestBody YonghuEntity yonghu){ - //ValidatorUtils.validateEntity(yonghu); - YonghuEntity u = yonghuService.selectOne(new EntityWrapper().eq("zhanghao", yonghu.getZhanghao())); - if(u!=null) { - return R.error("注册用户已存在"); - } - Long uId = new Date().getTime(); - yonghu.setId(uId); + // 检查用户是否已存在 + YonghuEntity u = yonghuService.selectOne(new EntityWrapper().eq("zhanghao", yonghu.getZhanghao())); + if(u!=null) { + return R.error("注册用户已存在"); + } + // 生成用户ID并插入用户数据 + Long uId = new Date().getTime(); + yonghu.setId(uId); yonghuService.insert(yonghu); return R.ok(); } - - /** - * 退出 - */ - @RequestMapping("/logout") - public R logout(HttpServletRequest request) { - request.getSession().invalidate(); - return R.ok("退出成功"); - } - - /** + /** + * 退出 + */ + @RequestMapping("/logout") // 设置请求路径为/logout + public R logout(HttpServletRequest request) { + // 使当前会话失效 + request.getSession().invalidate(); + return R.ok("退出成功"); + } + + /** * 获取用户的session用户信息 */ - @RequestMapping("/session") + @RequestMapping("/session") // 设置请求路径为/session public R getCurrUser(HttpServletRequest request){ - Long id = (Long)request.getSession().getAttribute("userId"); + // 从session中获取用户ID + Long id = (Long)request.getSession().getAttribute("userId"); + // 根据ID查询用户信息 YonghuEntity u = yonghuService.selectById(id); return R.ok().put("data", u); } - + /** * 密码重置 */ - @IgnoreAuth - @RequestMapping(value = "/resetPass") + @IgnoreAuth // 忽略权限验证 + @RequestMapping(value = "/resetPass") // 设置请求路径为/resetPass public R resetPass(String username, HttpServletRequest request){ - YonghuEntity u = yonghuService.selectOne(new EntityWrapper().eq("zhanghao", username)); - if(u==null) { - return R.error("账号不存在"); - } + // 根据用户名查询用户 + YonghuEntity u = yonghuService.selectOne(new EntityWrapper().eq("zhanghao", username)); + if(u==null) { + return R.error("账号不存在"); + } + // 重置密码为123456 u.setMima("123456"); yonghuService.updateById(u); return R.ok("密码已重置为:123456"); } - /** * 后端列表 */ - @RequestMapping("/page") + @RequestMapping("/page") // 设置请求路径为/page public R page(@RequestParam Map params,YonghuEntity yonghu, - HttpServletRequest request){ - + HttpServletRequest request){ EntityWrapper ew = new EntityWrapper(); - - PageUtils page = yonghuService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, yonghu), params), params)); - request.setAttribute("data", page); + // 分页查询用户列表 + PageUtils page = yonghuService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, yonghu), params), params)); + request.setAttribute("data", page); return R.ok().put("data", page); } - + /** * 前端列表 */ - @IgnoreAuth - @RequestMapping("/list") + @IgnoreAuth // 忽略权限验证 + @RequestMapping("/list") // 设置请求路径为/list public R list(@RequestParam Map params,YonghuEntity yonghu, - HttpServletRequest request){ + HttpServletRequest request){ EntityWrapper ew = new EntityWrapper(); - - PageUtils page = yonghuService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, yonghu), params), params)); - request.setAttribute("data", page); + // 分页查询用户列表 + PageUtils page = yonghuService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, yonghu), params), params)); + request.setAttribute("data", page); return R.ok().put("data", page); } - /** + /** * 列表 */ - @RequestMapping("/lists") + @RequestMapping("/lists") // 设置请求路径为/lists public R list( YonghuEntity yonghu){ - EntityWrapper ew = new EntityWrapper(); - ew.allEq(MPUtil.allEQMapPre( yonghu, "yonghu")); + EntityWrapper ew = new EntityWrapper(); + ew.allEq(MPUtil.allEQMapPre( yonghu, "yonghu")); return R.ok().put("data", yonghuService.selectListView(ew)); } - /** + /** * 查询 */ - @RequestMapping("/query") + @RequestMapping("/query") // 设置请求路径为/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); + ew.allEq(MPUtil.allEQMapPre( yonghu, "yonghu")); + YonghuView yonghuView = yonghuService.selectView(ew); + return R.ok("查询用户成功").put("data", yonghuView); } - + /** * 后端详情 */ - @RequestMapping("/info/{id}") + @RequestMapping("/info/{id}") // 设置请求路径为/info/{id} public R info(@PathVariable("id") Long id){ YonghuEntity yonghu = yonghuService.selectById(id); return R.ok().put("data", yonghu); @@ -187,126 +190,108 @@ public class YonghuController { /** * 前端详情 */ - @IgnoreAuth - @RequestMapping("/detail/{id}") + @IgnoreAuth // 忽略权限验证 + @RequestMapping("/detail/{id}") // 设置请求路径为/detail/{id} public R detail(@PathVariable("id") Long id){ YonghuEntity yonghu = yonghuService.selectById(id); return R.ok().put("data", yonghu); } - - - /** * 后端保存 */ - @RequestMapping("/save") + @RequestMapping("/save") // 设置请求路径为/save public R save(@RequestBody YonghuEntity yonghu, HttpServletRequest request){ - yonghu.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue()); - //ValidatorUtils.validateEntity(yonghu); - YonghuEntity u = yonghuService.selectOne(new EntityWrapper().eq("zhanghao", yonghu.getZhanghao())); - if(u!=null) { - return R.error("用户已存在"); - } - - yonghu.setId(new Date().getTime()); + yonghu.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue()); + YonghuEntity u = yonghuService.selectOne(new EntityWrapper().eq("zhanghao", yonghu.getZhanghao())); + if(u!=null) { + return R.error("用户已存在"); + } + yonghu.setId(new Date().getTime()); yonghuService.insert(yonghu); return R.ok(); } - + /** * 前端保存 */ - @RequestMapping("/add") + @RequestMapping("/add") // 设置请求路径为/add public R add(@RequestBody YonghuEntity yonghu, HttpServletRequest request){ - yonghu.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue()); - //ValidatorUtils.validateEntity(yonghu); - YonghuEntity u = yonghuService.selectOne(new EntityWrapper().eq("zhanghao", yonghu.getZhanghao())); - if(u!=null) { - return R.error("用户已存在"); - } - - yonghu.setId(new Date().getTime()); + yonghu.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue()); + YonghuEntity u = yonghuService.selectOne(new EntityWrapper().eq("zhanghao", yonghu.getZhanghao())); + if(u!=null) { + return R.error("用户已存在"); + } + yonghu.setId(new Date().getTime()); yonghuService.insert(yonghu); return R.ok(); } - /** * 修改 */ - @RequestMapping("/update") - @Transactional + @RequestMapping("/update") // 设置请求路径为/update + @Transactional // 开启事务 public R update(@RequestBody YonghuEntity yonghu, HttpServletRequest request){ - //ValidatorUtils.validateEntity(yonghu); yonghuService.updateById(yonghu);//全部更新 return R.ok(); } - - /** * 删除 */ - @RequestMapping("/delete") + @RequestMapping("/delete") // 设置请求路径为/delete public R delete(@RequestBody Long[] ids){ yonghuService.deleteBatchIds(Arrays.asList(ids)); return R.ok(); } - + /** * 提醒接口 */ - @RequestMapping("/remind/{columnName}/{type}") - public R remindCount(@PathVariable("columnName") String columnName, HttpServletRequest request, - @PathVariable("type") String type,@RequestParam Map map) { - map.put("column", columnName); - map.put("type", type); - - 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 wrapper = new EntityWrapper(); - 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); - } - - - - - + @RequestMapping("/remind/{columnName}/{type}") // 设置请求路径为/remind/{columnName}/{type} + public R remindCount(@PathVariable("columnName") String columnName, HttpServletRequest request, + @PathVariable("type") String type,@RequestParam Map map) { + map.put("column", columnName); + map.put("type", type); + + 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 wrapper = new EntityWrapper(); + 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); + } /** * (按值统计) */ - @RequestMapping("/value/{xColumnName}/{yColumnName}") + @RequestMapping("/value/{xColumnName}/{yColumnName}") // 设置请求路径为/value/{xColumnName}/{yColumnName} public R value(@PathVariable("yColumnName") String yColumnName, @PathVariable("xColumnName") String xColumnName,HttpServletRequest request) { Map params = new HashMap(); params.put("xColumn", xColumnName); @@ -327,7 +312,7 @@ public class YonghuController { /** * (按值统计)时间统计类型 */ - @RequestMapping("/value/{xColumnName}/{yColumnName}/{timeStatType}") + @RequestMapping("/value/{xColumnName}/{yColumnName}/{timeStatType}") // 设置请求路径为/value/{xColumnName}/{yColumnName}/{timeStatType} public R valueDay(@PathVariable("yColumnName") String yColumnName, @PathVariable("xColumnName") String xColumnName, @PathVariable("timeStatType") String timeStatType,HttpServletRequest request) { Map params = new HashMap(); params.put("xColumn", xColumnName); @@ -345,11 +330,11 @@ public class YonghuController { } return R.ok().put("data", result); } - + /** * 分组统计 */ - @RequestMapping("/group/{columnName}") + @RequestMapping("/group/{columnName}") // 设置请求路径为/group/{columnName} public R group(@PathVariable("columnName") String columnName,HttpServletRequest request) { Map params = new HashMap(); params.put("column", columnName); @@ -366,18 +351,13 @@ public class YonghuController { return R.ok().put("data", result); } - - - /** * 总数量 */ - @RequestMapping("/count") + @RequestMapping("/count") // 设置请求路径为/count public R count(@RequestParam Map params,YonghuEntity yonghu, HttpServletRequest request){ EntityWrapper ew = new EntityWrapper(); int count = yonghuService.selectCount(MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, yonghu), params), params)); return R.ok().put("data", count); } - - }