SpringBoot整合CRM客户管理系统(一)

  • 2022-01-19
  • Admin

1. 主要技术

SpringBoot+Mybatis+spring+mysql+layui

2. 目前的项目图

 

 

 3. 目前的实现功能

(1)登录功能

登录的逻辑主要是下判断是否存在该用户,如果存在该用户就比较密码是否正确,如果正确登录成功。

主要是三层:

controller:接受请求、返回结果

  • 接受前端的参数

  • 调用service层的登录方法

  • 返回给前端

    1. @PostMapping("login")
    2. @ResponseBody
    3. public ResultInfo userLogin(String userName,String userPwd){
    4. ResultInfo resultInfo=new ResultInfo();
    5. UserModel userModel=userService.userLogin(userName,userPwd);
    6. resultInfo.setResult(userModel);
    7. return resultInfo;
    8. }

service层:业务逻辑层(非空判断、条件判断)

  • 判空操作

  • 调用dao层通过用户名查询对象

    1. @Resource
    2. private UserMapper userMapper;
    3. /***
    4. * login成功后返回一个userModel
    5. * 1.检查用户、密码是否为空
    6. * 2.通过用户名查询user
    7. * 3.检查密码是否正确
    8. * 4.生成一个usermodel返回给controller
    9. * @param userName
    10. * @param userPwd
    11. * @return
    12. */
    13. @Override
    14. public UserModel userLogin(String userName,String userPwd){
    15. checkLoginParams(userName,userPwd);
    16. User user=userMapper.queryByUserByName(userName);
    17. AssertUtil.isTrue(user==null,"用户姓名不存在");
    18. checkUserPwd(userPwd,user.getUserPassword());
    19. return buildUserInfo(user);
    20. }
    21. private UserModel buildUserInfo(User user) {
    22. UserModel userModel=new UserModel();
    23. userModel.setUserIdStr(UserIDBase64.encoderUserID(user.getId()));
    24. userModel.setUserName(user.getUserName());
    25. userModel.setTrueName(user.getTrueName());
    26. return userModel;
    27. }
    28. private void checkUserPwd(String userPwd, String userPassword) {
    29. userPwd= Md5Util.encode(userPwd);
    30. AssertUtil.isTrue(!userPwd.equals(userPassword),"用户密码不正确");
    31. }
    32. /***
    33. * 检查用户名、密码是否为空
    34. * @param userName
    35. * @param userPwd
    36. */
    37. private void checkLoginParams(String userName, String userPwd) {
    38. AssertUtil.isTrue(StringUtils.isBlank(userName),"用户姓名不能为空");
    39. AssertUtil.isTrue(StringUtils.isBlank(userPwd),"用户密码不能为空");
    40. }

其中AssertUtil为工具类:

  1. public class AssertUtil {
  2. public static void isTrue(Boolean flag,String msg){
  3. if(flag){
  4. throw new ParamsException(msg);
  5. }
  6. }
  7. }

dao层:与数据层相关

(2)修改密码

思路:

controller层:

接受前端传到的三个参数(旧密码、新密码、确认的新密码)

利用request获取到cookie中的主键ID

调用service层的修改密码方法

返回给前端参数

  1. @ResponseBody
  2. @PostMapping("updatePassword")
  3. public ResultInfo updatePassword(HttpServletRequest request,String oldPassword, String newPassword, String repeatPassword){
  4. ResultInfo resultInfo=new ResultInfo();
  5. System.out.println(oldPassword);
  6. try{
  7. Integer userId= LoginUserUtil.releaseUserIdFromCookie(request);
  8. userService.updatePassword(userId,oldPassword,newPassword,repeatPassword);
  9. }catch (ParamsException p){
  10. resultInfo.setCode(p.getCode());
  11. resultInfo.setMsg(p.getMsg());
  12. p.printStackTrace();
  13. }catch (Exception e){
  14. resultInfo.setCode(500);
  15. resultInfo.setMsg("登录失败");
  16. e.printStackTrace();
  17. }
  18. return resultInfo;
  19. }
  20. @RequestMapping("toPasswordPage")
  21. public String toPasswordPage(){
  22. return "user/password";
  23. }

service层:

接受controller的四个参数

通过id查询用户user

参数校验:

user是否为空

旧密码是否为空,旧密码是否错误

新密码是否为空,新密码是否与旧密码相同

确认新密码是否为空,确认信密码是否与新密码相同

对新密码进行加密

调用dao层进行密码更新

执行更新判断受影响行数,判断是否更新成功

  1. @Override
  2. @Transactional(propagation = Propagation.REQUIRED)
  3. public void updatePassword(Integer userId,String oldPwd,String newPwd,String repeatPwd ){
  4. User user=userMapper.selectByPrimaryKey(userId);
  5. AssertUtil.isTrue(user==null,"待更新记录不存在");
  6. System.out.println(oldPwd);
  7. checkPasswordParams(user,oldPwd,newPwd,repeatPwd);
  8. user.setUserPassword(Md5Util.encode(newPwd));
  9. userMapper.updateByPrimaryKeySelective(user);
  10. AssertUtil.isTrue(userMapper.updateByPrimaryKeySelective(user)<1,"修改密码失败");
  11. }
  12. private void checkPasswordParams(User user,String oldPwd, String newPwd, String repeatPwd) {
  13. AssertUtil.isTrue(StringUtils.isBlank(oldPwd),"旧密码不能为空");
  14. AssertUtil.isTrue(user.getUserPassword().equals(Md5Util.encode(oldPwd)),"原始密码不正确");
  15. AssertUtil.isTrue(StringUtils.isBlank(newPwd),"新密码不能为空");
  16. AssertUtil.isTrue(newPwd.equals(oldPwd),"新密码不能与旧密码相同");
  17. AssertUtil.isTrue(StringUtils.isBlank(repeatPwd),"确认新密码不能为空");
  18. AssertUtil.isTrue(!repeatPwd.equals(newPwd),"确认新密码与新密码不一致");
  19. }

dao层:

数据库的update操作

(3)记住我、退出功能

记住我和退出主要都是通过cookies进行实现,主要在前端js就可以实现

  1. // 如果点击记住我 设置cookie 过期时间7天
  2. if($("input[type='checkbox']").is(':checked')){
  3. // 写入cookie 7天
  4. $.cookie("userId",result.userIdStr, { expires: 7 });
  5. $.cookie("userName",result.userName, { expires: 7 });
  6. $.cookie("trueName",result.trueName, { expires: 7 });
  7. }
  1. $(".login-out").click(function () {
  2. layer.confirm('是否登出当前用户?', {icon: 3, title:'提示'}, function(index){
  3. $.removeCookie("userId",{path:"/"})
  4. $.removeCookie("userName",{path:"/"})
  5. $.removeCookie("trueName",{path:"/"})
  6. window.parent.location.href = ctx + "/index";
  7. layer.close(index);
  8. });

(4)拦截器

当用户未登录的时候不能直接访问到主页面main

 拦截器:

  1. /**
  2. * 非法访问拦截
  3. */
  4. public class NoLoginInterceptor extends HandlerInterceptorAdapter {
  5. @Resource
  6. private UserServiceImpl userService;
  7. /**
  8. * 判断用户是否是登录状态
  9. * 获取Cookie对象,解析用户ID的值
  10. * 如果用户ID不为空,且在数据库中存在对应的用户记录,表示请求合法
  11. * 否则,请求不合法,进行拦截,重定向到登录页面
  12. * @param request
  13. * @param response
  14. * @param handler
  15. * @return
  16. * @throws Exception
  17. */
  18. @Override
  19. public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
  20. Object handler) throws Exception {
  21. // 获取Cookie中的用户ID
  22. Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
  23. // 判断用户ID是否不为空,且数据库中存在对应的用户记录
  24. if (null == userId || null == userService.selectByPrimaryKey(userId)) {
  25. // 抛出未登录异常
  26. throw new UnLoginException();
  27. }
  28. return true;
  29. }
  30. }

配置拦截器:

  1. @Configuration
  2. public class MvcConfig extends WebMvcConfigurerAdapter {
  3. @Bean
  4. public NoLoginInterceptor noLoginInterceptor() {
  5. return new NoLoginInterceptor();
  6. }
  7. /**
  8. * 添加拦截器
  9. * @param registry
  10. */
  11. @Override
  12. public void addInterceptors(InterceptorRegistry registry) {
  13. // 需要一个实现HandlerInterceptor接口的拦截器实例,这里使用的是 NoLoginInterceptor
  14. registry.addInterceptor(noLoginInterceptor())
  15. // 用于设置拦截器的过滤路径规则
  16. .addPathPatterns("/**")
  17. // 用于设置不需要拦截的过滤规则
  18. .excludePathPatterns("/index","/user/login","/css/**","/images/**","/js/**","/lib/**");
  19. }
  20. }

4. 异常 

(1)参数异常

  1. /**
  2. * 自定义参数异常
  3. */
  4. public class ParamsException extends RuntimeException {
  5. private Integer code=300;
  6. private String msg="参数异常!";
  7. public ParamsException() {
  8. super("参数异常!");
  9. }
  10. public ParamsException(String msg) {
  11. super(msg);
  12. this.msg = msg;
  13. }
  14. public ParamsException(Integer code) {
  15. super("参数异常!");
  16. this.code = code;
  17. }
  18. public ParamsException(Integer code, String msg) {
  19. super(msg);
  20. this.code = code;
  21. this.msg = msg;
  22. }
  23. public Integer getCode() {
  24. return code;
  25. }
  26. public void setCode(Integer code) {
  27. this.code = code;
  28. }
  29. public String getMsg() {
  30. return msg;
  31. }

联系站长

QQ:769220720

Copyright © SibooSoft All right reserved 津ICP备19011444号