From 10f1ca3f3a643ee5155539958f8270924c1f4d3e Mon Sep 17 00:00:00 2001 From: zds Date: Tue, 10 Dec 2024 17:53:49 +0800 Subject: [PATCH] zds --- Admin.java | 103 ++++++++++++++++++++++++++++++++ AdminController.java | 86 +++++++++++++++++++++++++++ AdminMapper.java | 40 +++++++++++++ AdminService.java | 135 ++++++++++++++++++++++++++++++++++++++++++ Notice.java | 62 +++++++++++++++++++ NoticeController.java | 86 +++++++++++++++++++++++++++ NoticeMapper.java | 36 +++++++++++ NoticeService.java | 79 ++++++++++++++++++++++++ TypeService.java | 75 +++++++++++++++++++++++ WebController.java | 97 ++++++++++++++++++++++++++++++ 10 files changed, 799 insertions(+) create mode 100644 Admin.java create mode 100644 AdminController.java create mode 100644 AdminMapper.java create mode 100644 AdminService.java create mode 100644 Notice.java create mode 100644 NoticeController.java create mode 100644 NoticeMapper.java create mode 100644 NoticeService.java create mode 100644 TypeService.java create mode 100644 WebController.java diff --git a/Admin.java b/Admin.java new file mode 100644 index 00000000..c62e6b84 --- /dev/null +++ b/Admin.java @@ -0,0 +1,103 @@ +package com.example.entity; + +import java.io.Serializable; + +/** + * 管理员 +*/ +public class Admin extends Account implements Serializable { + private static final long serialVersionUID = 1L; + + /** ID */ + private Integer id; + /** 用户名 */ + private String username; + /** 密码 */ + private String password; + /** 姓名 */ + private String name; + /** 电话 */ + private String phone; + /** 邮箱 */ + private String email; + /** 头像 */ + private String avatar; + /** 角色标识 */ + private String role; + + @Override + public Integer getId() { + return id; + } + + @Override + public void setId(Integer id) { + this.id = id; + } + + @Override + public String getUsername() { + return username; + } + + @Override + public void setUsername(String username) { + this.username = username; + } + + @Override + public String getPassword() { + return password; + } + + @Override + public void setPassword(String password) { + this.password = password; + } + + @Override + public String getName() { + return name; + } + + @Override + public void setName(String name) { + this.name = name; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + @Override + public String getAvatar() { + return avatar; + } + + @Override + public void setAvatar(String avatar) { + this.avatar = avatar; + } + + @Override + public String getRole() { + return role; + } + + @Override + public void setRole(String role) { + this.role = role; + } +} \ No newline at end of file diff --git a/AdminController.java b/AdminController.java new file mode 100644 index 00000000..a6f18f18 --- /dev/null +++ b/AdminController.java @@ -0,0 +1,86 @@ +package com.example.controller; + +import com.example.common.Result; +import com.example.entity.Admin; +import com.example.service.AdminService; +import com.github.pagehelper.PageInfo; +import org.springframework.web.bind.annotation.*; +import javax.annotation.Resource; +import java.util.List; + +/** + * 管理员前端操作接口 + **/ +@RestController +@RequestMapping("/admin") +public class AdminController { + + @Resource + private AdminService adminService; + + /** + * 新增 + */ + @PostMapping("/add") + public Result add(@RequestBody Admin admin) { + adminService.add(admin); + return Result.success(); + } + + /** + * 删除 + */ + @DeleteMapping("/delete/{id}") + public Result deleteById(@PathVariable Integer id) { + adminService.deleteById(id); + return Result.success(); + } + + /** + * 批量删除 + */ + @DeleteMapping("/delete/batch") + public Result deleteBatch(@RequestBody List ids) { + adminService.deleteBatch(ids); + return Result.success(); + } + + /** + * 修改 + */ + @PutMapping("/update") + public Result updateById(@RequestBody Admin admin) { + adminService.updateById(admin); + return Result.success(); + } + + /** + * 根据ID查询 + */ + @GetMapping("/selectById/{id}") + public Result selectById(@PathVariable Integer id) { + Admin admin = adminService.selectById(id); + return Result.success(admin); + } + + /** + * 查询所有 + */ + @GetMapping("/selectAll") + public Result selectAll(Admin admin ) { + List list = adminService.selectAll(admin); + return Result.success(list); + } + + /** + * 分页查询 + */ + @GetMapping("/selectPage") + public Result selectPage(Admin admin, + @RequestParam(defaultValue = "1") Integer pageNum, + @RequestParam(defaultValue = "10") Integer pageSize) { + PageInfo page = adminService.selectPage(admin, pageNum, pageSize); + return Result.success(page); + } + +} \ No newline at end of file diff --git a/AdminMapper.java b/AdminMapper.java new file mode 100644 index 00000000..3e9fcb05 --- /dev/null +++ b/AdminMapper.java @@ -0,0 +1,40 @@ +package com.example.mapper; + +import com.example.entity.Admin; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * 操作admin相关数据接口 +*/ +public interface AdminMapper { + + /** + * 新增 + */ + int insert(Admin admin); + + /** + * 删除 + */ + int deleteById(Integer id); + + /** + * 修改 + */ + int updateById(Admin admin); + + /** + * 根据ID查询 + */ + Admin selectById(Integer id); + + /** + * 查询所有 + */ + List selectAll(Admin admin); + + @Select("select * from admin where username = #{username}") + Admin selectByUsername(String username); +} \ No newline at end of file diff --git a/AdminService.java b/AdminService.java new file mode 100644 index 00000000..747170d0 --- /dev/null +++ b/AdminService.java @@ -0,0 +1,135 @@ +package com.example.service; + +import cn.hutool.core.util.ObjectUtil; +import com.example.common.Constants; +import com.example.common.enums.ResultCodeEnum; +import com.example.common.enums.RoleEnum; +import com.example.entity.Account; +import com.example.entity.Admin; +import com.example.exception.CustomException; +import com.example.mapper.AdminMapper; +import com.example.utils.TokenUtils; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import org.springframework.beans.BeanUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 管理员业务处理 + **/ +@Service +public class AdminService { + + @Resource + private AdminMapper adminMapper; + + /** + * 新增 + */ + public void add(Admin admin) { + Admin dbAdmin = adminMapper.selectByUsername(admin.getUsername()); + if (ObjectUtil.isNotNull(dbAdmin)) { + throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR); + } + if (ObjectUtil.isEmpty(admin.getPassword())) { + admin.setPassword(Constants.USER_DEFAULT_PASSWORD); + } + if (ObjectUtil.isEmpty(admin.getName())) { + admin.setName(admin.getUsername()); + } + admin.setRole(RoleEnum.ADMIN.name()); + adminMapper.insert(admin); + } + + /** + * 删除 + */ + public void deleteById(Integer id) { + adminMapper.deleteById(id); + } + + /** + * 批量删除 + */ + public void deleteBatch(List ids) { + for (Integer id : ids) { + adminMapper.deleteById(id); + } + } + + /** + * 修改 + */ + public void updateById(Admin admin) { + adminMapper.updateById(admin); + } + + /** + * 根据ID查询 + */ + public Admin selectById(Integer id) { + return adminMapper.selectById(id); + } + + /** + * 查询所有 + */ + public List selectAll(Admin admin) { + return adminMapper.selectAll(admin); + } + + /** + * 分页查询 + */ + public PageInfo selectPage(Admin admin, Integer pageNum, Integer pageSize) { + PageHelper.startPage(pageNum, pageSize); + List list = adminMapper.selectAll(admin); + return PageInfo.of(list); + } + + /** + * 登录 + */ + public Account login(Account account) { + Account dbAdmin = adminMapper.selectByUsername(account.getUsername()); + if (ObjectUtil.isNull(dbAdmin)) { + throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR); + } + if (!account.getPassword().equals(dbAdmin.getPassword())) { + throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR); + } + // 生成token + String tokenData = dbAdmin.getId() + "-" + RoleEnum.ADMIN.name(); + String token = TokenUtils.createToken(tokenData, dbAdmin.getPassword()); + dbAdmin.setToken(token); + return dbAdmin; + } + + /** + * 注册 + */ + public void register(Account account) { + Admin admin = new Admin(); + BeanUtils.copyProperties(account, admin); + add(admin); + } + + /** + * 修改密码 + */ + public void updatePassword(Account account) { + Admin dbAdmin = adminMapper.selectByUsername(account.getUsername()); + if (ObjectUtil.isNull(dbAdmin)) { + throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR); + } + if (!account.getPassword().equals(dbAdmin.getPassword())) { + throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR); + } + dbAdmin.setPassword(account.getNewPassword()); + adminMapper.updateById(dbAdmin); + } + +} \ No newline at end of file diff --git a/Notice.java b/Notice.java new file mode 100644 index 00000000..c77f7ec6 --- /dev/null +++ b/Notice.java @@ -0,0 +1,62 @@ +package com.example.entity; + +import java.io.Serializable; + +/** + * 公告信息表 +*/ +public class Notice implements Serializable { + private static final long serialVersionUID = 1L; + + /** ID */ + private Integer id; + /** 标题 */ + private String title; + /** 内容 */ + private String content; + /** 创建时间 */ + private String time; + /** 创建人 */ + private String user; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } + + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + +} \ No newline at end of file diff --git a/NoticeController.java b/NoticeController.java new file mode 100644 index 00000000..e2757d3a --- /dev/null +++ b/NoticeController.java @@ -0,0 +1,86 @@ +package com.example.controller; + +import com.example.common.Result; +import com.example.entity.Notice; +import com.example.service.NoticeService; +import com.github.pagehelper.PageInfo; +import org.springframework.web.bind.annotation.*; +import javax.annotation.Resource; +import java.util.List; + +/** + * 公告信息表前端操作接口 + **/ +@RestController +@RequestMapping("/notice") +public class NoticeController { + + @Resource + private NoticeService noticeService; + + /** + * 新增 + */ + @PostMapping("/add") + public Result add(@RequestBody Notice notice) { + noticeService.add(notice); + return Result.success(); + } + + /** + * 删除 + */ + @DeleteMapping("/delete/{id}") + public Result deleteById(@PathVariable Integer id) { + noticeService.deleteById(id); + return Result.success(); + } + + /** + * 批量删除 + */ + @DeleteMapping("/delete/batch") + public Result deleteBatch(@RequestBody List ids) { + noticeService.deleteBatch(ids); + return Result.success(); + } + + /** + * 修改 + */ + @PutMapping("/update") + public Result updateById(@RequestBody Notice notice) { + noticeService.updateById(notice); + return Result.success(); + } + + /** + * 根据ID查询 + */ + @GetMapping("/selectById/{id}") + public Result selectById(@PathVariable Integer id) { + Notice notice = noticeService.selectById(id); + return Result.success(notice); + } + + /** + * 查询所有 + */ + @GetMapping("/selectAll") + public Result selectAll(Notice notice ) { + List list = noticeService.selectAll(notice); + return Result.success(list); + } + + /** + * 分页查询 + */ + @GetMapping("/selectPage") + public Result selectPage(Notice notice, + @RequestParam(defaultValue = "1") Integer pageNum, + @RequestParam(defaultValue = "10") Integer pageSize) { + PageInfo page = noticeService.selectPage(notice, pageNum, pageSize); + return Result.success(page); + } + +} \ No newline at end of file diff --git a/NoticeMapper.java b/NoticeMapper.java new file mode 100644 index 00000000..3d2b4a0c --- /dev/null +++ b/NoticeMapper.java @@ -0,0 +1,36 @@ +package com.example.mapper; + +import com.example.entity.Notice; +import java.util.List; + +/** + * 操作notice相关数据接口 +*/ +public interface NoticeMapper { + + /** + * 新增 + */ + int insert(Notice notice); + + /** + * 删除 + */ + int deleteById(Integer id); + + /** + * 修改 + */ + int updateById(Notice notice); + + /** + * 根据ID查询 + */ + Notice selectById(Integer id); + + /** + * 查询所有 + */ + List selectAll(Notice notice); + +} \ No newline at end of file diff --git a/NoticeService.java b/NoticeService.java new file mode 100644 index 00000000..8787113d --- /dev/null +++ b/NoticeService.java @@ -0,0 +1,79 @@ +package com.example.service; + +import cn.hutool.core.date.DateUtil; +import com.example.entity.Account; +import com.example.entity.Notice; +import com.example.mapper.NoticeMapper; +import com.example.utils.TokenUtils; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import javax.annotation.Resource; +import org.springframework.stereotype.Service; +import java.util.List; + +/** + * 公告信息表业务处理 + **/ +@Service +public class NoticeService { + + @Resource + private NoticeMapper noticeMapper; + + /** + * 新增 + */ + public void add(Notice notice) { + notice.setTime(DateUtil.today()); + Account currentUser = TokenUtils.getCurrentUser(); + notice.setUser(currentUser.getUsername()); + noticeMapper.insert(notice); + } + + /** + * 删除 + */ + public void deleteById(Integer id) { + noticeMapper.deleteById(id); + } + + /** + * 批量删除 + */ + public void deleteBatch(List ids) { + for (Integer id : ids) { + noticeMapper.deleteById(id); + } + } + + /** + * 修改 + */ + public void updateById(Notice notice) { + noticeMapper.updateById(notice); + } + + /** + * 根据ID查询 + */ + public Notice selectById(Integer id) { + return noticeMapper.selectById(id); + } + + /** + * 查询所有 + */ + public List selectAll(Notice notice) { + return noticeMapper.selectAll(notice); + } + + /** + * 分页查询 + */ + public PageInfo selectPage(Notice notice, Integer pageNum, Integer pageSize) { + PageHelper.startPage(pageNum, pageSize); + List list = noticeMapper.selectAll(notice); + return PageInfo.of(list); + } + +} \ No newline at end of file diff --git a/TypeService.java b/TypeService.java new file mode 100644 index 00000000..81c930c4 --- /dev/null +++ b/TypeService.java @@ -0,0 +1,75 @@ +package com.example.service; + + +import com.example.entity.Type; +import com.example.mapper.TypeMapper; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 分类信息表业务处理 + **/ +@Service +public class TypeService { + + @Resource + private TypeMapper typeMapper; + + /** + * 新增 + */ + public void add(Type type) { + typeMapper.insert(type); + } + + /** + * 删除 + */ + public void deleteById(Integer id) { + typeMapper.deleteById(id); + } + + /** + * 批量删除 + */ + public void deleteBatch(List ids) { + for (Integer id : ids) { + typeMapper.deleteById(id); + } + } + + /** + * 修改 + */ + public void updateById(Type type) { + typeMapper.updateById(type); + } + + /** + * 根据ID查询 + */ + public Type selectById(Integer id) { + return typeMapper.selectById(id); + } + + /** + * 查询所有 + */ + public List selectAll(Type type) { + return typeMapper.selectAll(type); + } + + /** + * 分页查询 + */ + public PageInfo selectPage(Type type, Integer pageNum, Integer pageSize) { + PageHelper.startPage(pageNum, pageSize); + List list = typeMapper.selectAll(type); + return PageInfo.of(list); + } + +} \ No newline at end of file diff --git a/WebController.java b/WebController.java new file mode 100644 index 00000000..80d96d57 --- /dev/null +++ b/WebController.java @@ -0,0 +1,97 @@ +package com.example.controller; + +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import com.example.common.Result; +import com.example.common.enums.ResultCodeEnum; +import com.example.common.enums.RoleEnum; +import com.example.entity.Account; +import com.example.service.AdminService; +import com.example.service.BusinessService; +import com.example.service.UserService; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; + +/** + * 基础前端接口 + */ +@RestController +public class WebController { + + @Resource + private AdminService adminService; + @Resource + private BusinessService businessService; + @Resource + private UserService userService; + + @GetMapping("/") + public Result hello() { + return Result.success("访问成功"); + } + + /** + * 登录 + */ + @PostMapping("/login") + public Result login(@RequestBody Account account) { + if (ObjectUtil.isEmpty(account.getUsername()) || ObjectUtil.isEmpty(account.getPassword()) + || ObjectUtil.isEmpty(account.getRole())) { + return Result.error(ResultCodeEnum.PARAM_LOST_ERROR); + } + if (RoleEnum.ADMIN.name().equals(account.getRole())) { + account = adminService.login(account); + } + if (RoleEnum.BUSINESS.name().equals(account.getRole())){ + account = businessService.login(account); + } + if(RoleEnum.USER.name().equals(account.getRole())){ + account = userService.login(account); + } + return Result.success(account); + } + + /** + * 注册 + */ + @PostMapping("/register") + public Result register(@RequestBody Account account) { + if (StrUtil.isBlank(account.getUsername()) || StrUtil.isBlank(account.getPassword()) + || ObjectUtil.isEmpty(account.getRole())) { + return Result.error(ResultCodeEnum.PARAM_LOST_ERROR); + } + if (RoleEnum.ADMIN.name().equals(account.getRole())) { + adminService.register(account); + } + if (RoleEnum.BUSINESS.name().equals(account.getRole())) { + businessService.register(account); + } + if (RoleEnum.USER.name().equals(account.getRole())) { + userService.register(account); + } + return Result.success(); + } + + /** + * 修改密码 + */ + @PutMapping("/updatePassword") + public Result updatePassword(@RequestBody Account account) { + if (StrUtil.isBlank(account.getUsername()) || StrUtil.isBlank(account.getPassword()) + || ObjectUtil.isEmpty(account.getNewPassword())) { + return Result.error(ResultCodeEnum.PARAM_LOST_ERROR); + } + if (RoleEnum.ADMIN.name().equals(account.getRole())) { + adminService.updatePassword(account); + } + if (RoleEnum.BUSINESS.name().equals(account.getRole())) { + businessService.updatePassword(account); + } + if (RoleEnum.USER.name().equals(account.getRole())) { + userService.updatePassword(account); + } + return Result.success(); + } + +}