电商项目(二十六)用户模块开发(登出、注册、校验功能开发)
程序员文章站
2024-03-25 21:27:34
...
一、门户登出功能
1、门户登出功能接口设计(退出登陆)
request:
无
response:
success
{
"status": 0,
}
fail
{
"status": 1,
}
2、Controller层
//登出功能
@RequestMapping(value = "logout.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponse<String> logout(HttpSession session){
//将当前用户移除
session.removeAttribute(Const.CURRENT_USER);
return ServerResponse.createBySuccess();
}
3、测试
二、门户注册功能
1、门户注册功能接口设计
request:
username,password,email,phone,question,answer
response:
success
{
"status": 0,
"msg": "校验成功"
}
fail
{
"status": 1,
"msg": "用户已存在"
}
2、Controller层
//注册功能
@RequestMapping(value = "register.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponse<String> register(User user){
return iUserService.register(user);
}
//校验功能
@RequestMapping(value = "check_valid.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponse<String> checkValid(String str,String type){
return iUserService.checkValid(str,type);
}
3、Service层
//注册
@Service("iUserService")
public class UserServiceImpl implements IUserService {
public ServerResponse<String> register(User user) {
//注入userMapper
@Autowired
private UserMapper userMapper;
//注册之前先查询一下这个用户是不是存在
//先查询用户名,看用户名是否存在
ServerResponse<String> vaildResponse = this.checkValid(user.getUsername(),Const.USERNAME);
//校验用户名
if(!vaildResponse.isSuccess()){
return vaildResponse;
}
//校验邮箱
vaildResponse = this.checkValid(user.getEmail(),Const.EMAIL);
if(!vaildResponse.isSuccess()){
return vaildResponse;
}
//设置用户角色
user.setRole(Const.Role.ROLE_CUSTOMER);
//MD5加密
user.setPassword(MD5Util.MD5EncodeUtf8(user.getPassword()));
//插入用户
int resultCount = userMapper.insert(user);
if (resultCount == 0) {
return ServerResponse.createByErrorMessage("注册失败");
}
return ServerResponse.createBySuccessMessage("注册成功");
}
}
//校验
public ServerResponse<String> checkValid(String str,String type){
if(org.apache.commons.lang3.StringUtils.isNotBlank(type)){ //type不是空,才开始校验
/开始校验
if(Const.USERNAME.equals(type)){
//判断用户名
int resultCount = userMapper.checkUsername(str);
if(resultCount > 0 ){
return ServerResponse.createByErrorMessage("用户名已存在");
}
}
//判断email
if(Const.EMAIL.equals(type)){
int resultCount = userMapper.checkEmail(str);
if(resultCount > 0 ){
return ServerResponse.createByErrorMessage("email已存在");
}
}
}else{
return ServerResponse.createByErrorMessage("参数错误");
}
return ServerResponse.createBySuccessMessage("校验成功");
}
4、Mapper接口
public interface UserMapper {
//查询邮箱,看此邮箱是否存在
int checkEmail(String email);
}
5、Mapper.xml
<!--
查询邮箱是否存在
-->
<select id="checkEmail" resultType="int" parameterType="string" >
SELECT COUNT(1) FROM mmall_user WHERE email = #{email}
</select>
6、测试
数据库里面:
上一篇: 大数定律