设计一个需要用户量的网站时,用户功能模块是非常重要的,该文章介绍用户登录注册模块的开发。
知识点概况:
- 用户注册与登录
- cookie和session
- 继承swagger2 api
- 分类设计与实现
- 首页商品推荐
- 商品详情与评论渲染
- 购物车与订单
- 微信与支付宝支付
1.用户注册登录流程分类
1.1.邮箱注册登录
1.2.手机号注册登录
2.用户注册-判断用户名存在
正常的开发流程是从低至上,顺序为pojo、mapper、service、controller
2.1 mapper
mapper是自动生成得代码,目前业务逻辑相对简单,不需要自定义功能,代码如下:
package mapper;
import com.architects.my.mapper.MyMapper;
import com.architects.pojo.Users;
public interface UsersMapper extends MyMapper<Users> {
}
2.2 service
- 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);
}
- userServiceImpl
```java
package com.architects.service.impl;
import com.architects.mapper.UsersMapper;
import com.architects.pojo.Users;
import com.architects.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
/**
* @ClassName userServiceImpl
* @Description:
* @Author ning.chai@foxmail.com
* @Date 2021/2/19 0019
* @Version V1.0
**/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UsersMapper usersMapper;
@Override
public boolean queryUsername(String username) {
Example example = new Example(Users.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("username", username);
Users result = usersMapper.selectOneByExample(example);
return result == null ? false : true;
}
}
2.3 controller
package com.architects.service.impl;
import com.architects.mapper.UsersMapper;
import com.architects.pojo.Users;
import com.architects.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
/**
* @ClassName userServiceImpl
* @Description:
* @Author ning.chai@foxmail.com
* @Date 2021/2/19 0019
* @Version V1.0
**/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UsersMapper usersMapper;
@Override
public boolean queryUsername(String username) {
Example example = new Example(Users.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("username", username);
Users result = usersMapper.selectOneByExample(example);
return result == null ? false : true;
}
}
3.自定义数据结构
一般页面的数据返回格式,会带有status、msg、data,例如天猫登录接口返回的格式如下:
JSONVO
package com.architects.utils;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* 本类可提供给H5/ios/安卓/公众号/小程序使用
* 前端接受此类数据(json object)后,可自行根据业务去实现相关功能
*
* 200:表示成功
* 500:表示错误,错误信息在msg字段中
* 501:bean验证错误,不管多少个错误都以map形式返回
* 502:拦截器拦截到用户token出错
* 555:异常抛出信息
* 556:用户qq校验异常
*/
public final class JSONVO {
// 定义jackson对象
// private static final ObjectMapper MAPPER = new ObjectMapper();
// 响应业务状态
private Integer status;
// 响应消息
private String msg;
// 响应中的数据
private Object data;
@JsonIgnore
private String ok; // 不使用
public static JSONVO build(Integer status, String msg, Object data) {
return new JSONVO(status, msg, data);
}
public static JSONVO build(Integer status, String msg, Object data, String ok) {
return new JSONVO(status, msg, data, ok);
}
public static JSONVO ok(Object data) {
return new JSONVO(data);
}
public static JSONVO ok() {
return new JSONVO(null);
}
public static JSONVO errorMsg(String msg) {
return new JSONVO(500, msg, null);
}
public static JSONVO errorMap(Object data) {
return new JSONVO(501, "error", data);
}
public static JSONVO errorTokenMsg(String msg) {
return new JSONVO(502, msg, null);
}
public static JSONVO errorException(String msg) {
return new JSONVO(555, msg, null);
}
public static JSONVO errorUserQQ(String msg) {
return new JSONVO(556, msg, null);
}
public JSONVO() {
}
private JSONVO(Integer status, String msg, Object data) {
this.status = status;
this.msg = msg;
this.data = data;
}
private JSONVO(Integer status, String msg, Object data, String ok) {
this.status = status;
this.msg = msg;
this.data = data;
this.ok = ok;
}
private JSONVO(Object data) {
this.status = 200;
this.msg = "OK";
this.data = data;
}
public Boolean isOK() {
return this.status == 200;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getOk() {
return ok;
}
public void setOk(String ok) {
this.ok = ok;
}
}
改造原有的controller代码
package com.architects.controller;
import com.architects.service.UserService;
import com.architects.utils.JSONVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName PassportController
* @Description:
* @Author ning.chai@foxmail.com
* @Date 2021/2/19 0019
* @Version V1.0
**/
@RestController
@RequestMapping("passport")
public class PassportController {
@Autowired
private UserService userService;
@GetMapping("/usernameIsExist")
public JSONVO usernameIsExist(@RequestParam String username){
if (StringUtils.isBlank(username)) {
return JSONVO.errorMsg("用户名不能为空");
}
boolean isExist = userService.queryUsername(username);
if (isExist) {
return JSONVO.errorMsg("用户名已存在");
}
return JSONVO.ok();
}
}