设计一个需要用户量的网站时,用户功能模块是非常重要的,该文章介绍用户登录注册模块的开发。

知识点概况:

  • 用户注册与登录
  • cookie和session
  • 继承swagger2 api
  • 分类设计与实现
  • 首页商品推荐
  • 商品详情与评论渲染
  • 购物车与订单
  • 微信与支付宝支付

1.用户注册登录流程分类

image.png

1.1.邮箱注册登录

image.png

1.2.手机号注册登录

image.png

2.用户注册-判断用户名存在

正常的开发流程是从低至上,顺序为pojo、mapper、service、controller

2.1 mapper

mapper是自动生成得代码,目前业务逻辑相对简单,不需要自定义功能,代码如下:

  1. package mapper;
  2. import com.architects.my.mapper.MyMapper;
  3. import com.architects.pojo.Users;
  4. public interface UsersMapper extends MyMapper<Users> {
  5. }

2.2 service

image.png

  • UserService ```java package com.architects.service;

/**

  • @ClassName UserService
  • @Description:
  • @Author ning.chai@foxmail.com
  • @Date 2021/2/19 0019
  • @Version V1.0 **/ public interface UserService {

    boolean queryUsername(String username);

}

  1. - userServiceImpl
  2. ```java
  3. package com.architects.service.impl;
  4. import com.architects.mapper.UsersMapper;
  5. import com.architects.pojo.Users;
  6. import com.architects.service.UserService;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import tk.mybatis.mapper.entity.Example;
  10. /**
  11. * @ClassName userServiceImpl
  12. * @Description:
  13. * @Author ning.chai@foxmail.com
  14. * @Date 2021/2/19 0019
  15. * @Version V1.0
  16. **/
  17. @Service
  18. public class UserServiceImpl implements UserService {
  19. @Autowired
  20. private UsersMapper usersMapper;
  21. @Override
  22. public boolean queryUsername(String username) {
  23. Example example = new Example(Users.class);
  24. Example.Criteria criteria = example.createCriteria();
  25. criteria.andEqualTo("username", username);
  26. Users result = usersMapper.selectOneByExample(example);
  27. return result == null ? false : true;
  28. }
  29. }

2.3 controller

  1. package com.architects.service.impl;
  2. import com.architects.mapper.UsersMapper;
  3. import com.architects.pojo.Users;
  4. import com.architects.service.UserService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import tk.mybatis.mapper.entity.Example;
  8. /**
  9. * @ClassName userServiceImpl
  10. * @Description:
  11. * @Author ning.chai@foxmail.com
  12. * @Date 2021/2/19 0019
  13. * @Version V1.0
  14. **/
  15. @Service
  16. public class UserServiceImpl implements UserService {
  17. @Autowired
  18. private UsersMapper usersMapper;
  19. @Override
  20. public boolean queryUsername(String username) {
  21. Example example = new Example(Users.class);
  22. Example.Criteria criteria = example.createCriteria();
  23. criteria.andEqualTo("username", username);
  24. Users result = usersMapper.selectOneByExample(example);
  25. return result == null ? false : true;
  26. }
  27. }

3.自定义数据结构

一般页面的数据返回格式,会带有status、msg、data,例如天猫登录接口返回的格式如下:
image.png

此时我们需要一个响应工具类

JSONVO

  1. package com.architects.utils;
  2. import com.fasterxml.jackson.annotation.JsonIgnore;
  3. /**
  4. * 本类可提供给H5/ios/安卓/公众号/小程序使用
  5. * 前端接受此类数据(json object)后,可自行根据业务去实现相关功能
  6. *
  7. * 200:表示成功
  8. * 500:表示错误,错误信息在msg字段中
  9. * 501:bean验证错误,不管多少个错误都以map形式返回
  10. * 502:拦截器拦截到用户token出错
  11. * 555:异常抛出信息
  12. * 556:用户qq校验异常
  13. */
  14. public final class JSONVO {
  15. // 定义jackson对象
  16. // private static final ObjectMapper MAPPER = new ObjectMapper();
  17. // 响应业务状态
  18. private Integer status;
  19. // 响应消息
  20. private String msg;
  21. // 响应中的数据
  22. private Object data;
  23. @JsonIgnore
  24. private String ok; // 不使用
  25. public static JSONVO build(Integer status, String msg, Object data) {
  26. return new JSONVO(status, msg, data);
  27. }
  28. public static JSONVO build(Integer status, String msg, Object data, String ok) {
  29. return new JSONVO(status, msg, data, ok);
  30. }
  31. public static JSONVO ok(Object data) {
  32. return new JSONVO(data);
  33. }
  34. public static JSONVO ok() {
  35. return new JSONVO(null);
  36. }
  37. public static JSONVO errorMsg(String msg) {
  38. return new JSONVO(500, msg, null);
  39. }
  40. public static JSONVO errorMap(Object data) {
  41. return new JSONVO(501, "error", data);
  42. }
  43. public static JSONVO errorTokenMsg(String msg) {
  44. return new JSONVO(502, msg, null);
  45. }
  46. public static JSONVO errorException(String msg) {
  47. return new JSONVO(555, msg, null);
  48. }
  49. public static JSONVO errorUserQQ(String msg) {
  50. return new JSONVO(556, msg, null);
  51. }
  52. public JSONVO() {
  53. }
  54. private JSONVO(Integer status, String msg, Object data) {
  55. this.status = status;
  56. this.msg = msg;
  57. this.data = data;
  58. }
  59. private JSONVO(Integer status, String msg, Object data, String ok) {
  60. this.status = status;
  61. this.msg = msg;
  62. this.data = data;
  63. this.ok = ok;
  64. }
  65. private JSONVO(Object data) {
  66. this.status = 200;
  67. this.msg = "OK";
  68. this.data = data;
  69. }
  70. public Boolean isOK() {
  71. return this.status == 200;
  72. }
  73. public Integer getStatus() {
  74. return status;
  75. }
  76. public void setStatus(Integer status) {
  77. this.status = status;
  78. }
  79. public String getMsg() {
  80. return msg;
  81. }
  82. public void setMsg(String msg) {
  83. this.msg = msg;
  84. }
  85. public Object getData() {
  86. return data;
  87. }
  88. public void setData(Object data) {
  89. this.data = data;
  90. }
  91. public String getOk() {
  92. return ok;
  93. }
  94. public void setOk(String ok) {
  95. this.ok = ok;
  96. }
  97. }

改造原有的controller代码

  1. package com.architects.controller;
  2. import com.architects.service.UserService;
  3. import com.architects.utils.JSONVO;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9. import org.springframework.web.bind.annotation.RestController;
  10. /**
  11. * @ClassName PassportController
  12. * @Description:
  13. * @Author ning.chai@foxmail.com
  14. * @Date 2021/2/19 0019
  15. * @Version V1.0
  16. **/
  17. @RestController
  18. @RequestMapping("passport")
  19. public class PassportController {
  20. @Autowired
  21. private UserService userService;
  22. @GetMapping("/usernameIsExist")
  23. public JSONVO usernameIsExist(@RequestParam String username){
  24. if (StringUtils.isBlank(username)) {
  25. return JSONVO.errorMsg("用户名不能为空");
  26. }
  27. boolean isExist = userService.queryUsername(username);
  28. if (isExist) {
  29. return JSONVO.errorMsg("用户名已存在");
  30. }
  31. return JSONVO.ok();
  32. }
  33. }

请求结果

image.png

4. 用户注册-创建service