• 牛客网项目-开发注册功能


    前言

    本文是对牛客网项目的总结,本文主要讲解页面注册逻辑

    1. 当我们点击首页的注册按钮时,会跳转到注册页面,然后再祖册页面提交账号,密码邮箱后会跳转到首页或者直接登录页面进行登录,这个操作可以自己设定

    【设计逻辑】 

    1. 访问注册页面
      • 点击区域内的连接打开注册页面
    2. 提交注册数据
      • 通过表单提交数据
      • 服务端验证账号是否存在,邮箱是否已经注册服务端发送激活邮件

    3. 激活注册账号
      • 点击邮件中的连接,访问服务端激活服务。

    首先两点在于

    1、进行服务端的校验工作,首先用户名可以设置为唯一操作,其次密码长度校验,然后邮箱唯一性正确性的校验。

    2、激活账号,我们会生成一个连接发送到邮箱中,执行服务端的账号激活业务。

    【Service层】

    1. public Map register(User user) {
    2. Map map = new HashMap<>();
    3. if (user == null) {
    4. throw new IllegalArgumentException("参数不能为空");
    5. }
    6. if (StrUtil.isBlank(user.getUsername())) {
    7. map.put("usernameMessage", "账号不能为空");
    8. return map;
    9. }
    10. if (StrUtil.isBlank(user.getPassword())) {
    11. map.put("passwordMessage", "密码不能为空");
    12. return map;
    13. }
    14. User u = userMapper.selectByName(user.getUsername());
    15. if (u != null) {
    16. map.put("usernameMessage", "账号已经存在");
    17. return map;
    18. }
    19. u = userMapper.selectByEmail(user.getEmail());
    20. if (u != null) {
    21. map.put("emailMessage", "邮箱已经存在");
    22. return map;
    23. }
    24. // 注册用户--设置默认的值
    25. user.setSalt(CommunityUtil.getRandom5());
    26. user.setPassword(CommunityUtil.getMd5(user.getPassword() + user.getSalt()));
    27. user.setType(0);
    28. // 状态参数为0表示用户尚未激活
    29. user.setStatus(0);
    30. // 生成激活码。
    31. user.setActivationCode(CommunityUtil.getUUID());
    32. user.setHeaderUrl(String.format("http://images.nowcoder.com/head/%dt.png",new Random().nextInt(1000)));
    33. user.setCreateTime(new Date());
    34. userMapper.insertUser(user);
    35. // 发送激活邮件---
    36. Context context = new Context();
    37. context.setVariable("email", user.getEmail());
    38. //"http://localhost:8080/community/activation/101/code";
    39. String url = domain + contextPath + "/activation" + "/" +user.getId() + "/" + user.getActivationCode();
    40. context.setVariable("url", url);
    41. String content = templateEngine.process("/mail/activation", context);
    42. System.out.println(content);
    43. mailClient.sendMail(user.getEmail(),"激活账号",content);
    44. return map;
    45. }

    controller层

    1. @PostMapping("/register")
    2. public String register(Model model, User user) {
    3. Map map = userService.register(user);
    4. if (map == null || map.isEmpty()) {
    5. model.addAttribute("msg", "注册成功");
    6. model.addAttribute("target", "/index");
    7. return "/site/operate-result";
    8. }
    9. // 失败则继续返回在登陆页面
    10. model.addAttribute("usernameMsg", map.get("usernameMsg"));
    11. model.addAttribute("passwordMessage", map.get("passwordMessage"));
    12. model.addAttribute("emailMessage", map.get("emailMessage"));
    13. return "/site/register";
    14. }

    邮件发送

    由于邮件发送业务也不是很难,这里找了一个相对详细的进行参考。主要分为以下几步

    • 开启邮箱的stmp服务
    • 引入配置
    • 编写代码

    Springboot实现发送邮件功能_12程序猿的博客-CSDN博客

    【controller层】账号激活逻辑

    1. @GetMapping(path = "/activation/{userId}/{code}")
    2. public String activation(Model model, @PathVariable("userId") int userId, @PathVariable("code") String code) {
    3. int result = userService.activation(userId, code);
    4. if (result == ACTIVATION_SUCCESS) {
    5. model.addAttribute("msg", "激活成功");
    6. model.addAttribute("target", "/login");
    7. } else if (result == ACTIVATION_REPEATE) {
    8. model.addAttribute("msg", "已经激活");
    9. model.addAttribute("target", "/index");
    10. } else {
    11. model.addAttribute("msg", "激活失败");
    12. model.addAttribute("target", "/index");
    13. }
    14. return "/site/operate-result";
    15. }

    【service层】

    1. public int activation(int id,String code) {
    2. User user = userMapper.selectById(id);
    3. // 如果用户状态为1表示已经激活过了
    4. if (user.getStatus() == 1){
    5. return ACTIVATION_REPEATE;
    6. // 否则判断激活码是否正确进行激活
    7. } else if (user.getActivationCode().equals(code)) {
    8. userMapper.updateStatus(id,1);
    9. return ACTIVATION_SUCCESS;
    10. }
    11. // 激活码错误,不能激活
    12. return ACTIVATION_FAIL;
    13. }

    总结流程

    开发注册功能:完成这个功能,客户端需要发送三次请求。

    第一次请求:访问注册页面

    第二次请求:客户端填写相应的信息与数据,将其发送给服务端。

    第三次请求:点击邮件中的链接,访问服务端的

    1. @GetMapping("/register")
    2. public String getRegisterPath() {
    3.    return "/site/register";
    4. }

    激活服务。

    接下来我们针对这三次请求编写代码。:关键在于thymeleaf一段标签的复用。

    首先我们在首页,然后点击注册按钮跳转至注册页面,需要的工作在Controller中添加一个方法给前端返回注册页面的路径,让前端跳转。至此完成第一个请求。

     
    

    填写完表单之后,点击立即注册,就会访问后端的Controller的一个方法。完成以下几个功能

    1、校验数据是否正确

    2、将数据插入到数据库中

    3、发送激活邮件。

    4、返回数据给前端,然后由前端进行页面跳转到操作页面

    发送的激活邮件如下:

    最后点击此链接进行账号激活。至此注册页面完成。

  • 相关阅读:
    python图像处理实验中记录(包括图像读取、图像缩放、图像格式转换等)
    超声波测距毕设单机版本-张怡君
    微服务实践之通信(OpenFeign)详解-SpringCloud(2021.0.x)-6
    随笔-那些快乐的日子
    4-5 实现极简IoC容器
    esp32-C3 CAN接口使用
    C#的LINQ查询
    用Scapy模块中的sr()函数提供一个程序的例子
    探索macOS上的最佳MySQL客户端工具
    【Rust 笔记】18-宏
  • 原文地址:https://blog.csdn.net/abc123mma/article/details/127909682