从零到企业级SSM电商项目实战教程(十八)登出,注册,校验功能开发

UserController.java

/*
 * @Description: 用户登出
 *
 * @auther: Geekerstar(jikewenku.com)
 * @date: 2018/6/22 11:29
 * @param: [session]
 * @return: com.mmall.common.ServerResponse
 */
@RequestMapping(value = "logout.do",method = RequestMethod.GET)
@ResponseBody
public ServerResponse logout(HttpSession session){
    session.removeAttribute(Const.CURRENT_USER);
    return ServerResponse.createBySuccess();
}

/*
 * @Description: 用户注册
 *
 * @auther: Geekerstar(jikewenku.com)
 * @date: 2018/6/22 12:09
 * @param: 
 * @return: 
 */

@RequestMapping(value = "register.do",method = RequestMethod.GET)
@ResponseBody
public ServerResponse register(User user){
    return iUserService.register(user);
}

public ServerResponse checkValid(String str,String type){
    return iUserService.checkValid(str,type);
}

IUserService.java

ServerResponse register(User user);

ServerResponse checkValid(String str,String type);

UserServiceImpl.java

public ServerResponse register(User user){
//    int resultCount = userMapper.checkUsername(user.getUsername());
//    if(resultCount > 0){
//        return ServerResponse.createByErrorMessgae("用户名已存在");
//    }
    ServerResponse validResponse = this.checkValid(user.getUsername(),Const.USERNAME);
    if(!validResponse.isSuccess()){
        return validResponse;
    }

    validResponse = this.checkValid(user.getEmail(),Const.EMAIL);
    if(!validResponse.isSuccess()){
        return validResponse;
    }

//      resultCount = userMapper.checkEmail(user.getEmail());
//      if(resultCount > 0){
//          return ServerResponse.createByErrorMessgae("email已存在");
//        }
    user.setRole(Const.Role.ROLE_CUSTOMER);
    //MD5加密
    user.setPassword(MD5Util.MD5EncodeUtf8(user.getPassword()));

    int resultCount = userMapper.insert(user);
    if(resultCount == 0){
        return ServerResponse.createByErrorMessgae("注册失败");
    }
    return ServerResponse.createBySuccessMessage("注册成功");
}

public ServerResponse checkValid(String str,String type){
    if(StringUtils.isNoneBlank(type)){
        //开始校验
        if(Const.USERNAME.equals(type)){
            int resultCount = userMapper.checkUsername(str);
            if(resultCount > 0){
                return ServerResponse.createByErrorMessgae("用户名已存在");
            }
        }
        if(Const.EMAIL.equals(type)){
            int resultCount = userMapper.checkEmail(str);
            if(resultCount > 0){
                return ServerResponse.createByErrorMessgae("email已存在");
            }
        }

    }else{
        return ServerResponse.createByErrorMessgae("参数错误");
    }
    return ServerResponse.createBySuccessMessage("校验成功");
}

MD5Util.java放到util包里

package com.mmall.util;


import java.security.MessageDigest;

/**
 * Created by weimin
 */
public class MD5Util {

    private static String byteArrayToHexString(byte b[]) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++)
            resultSb.append(byteToHexString(b[i]));

        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n += 256;
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    /**
     * 返回大写MD5
     *
     * @param origin
     * @param charsetname
     * @return
     */
    private static String MD5Encode(String origin, String charsetname) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (charsetname == null || "".equals(charsetname))
                resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
            else
                resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
        } catch (Exception exception) {
        }
        return resultString.toUpperCase();
    }

    public static String MD5EncodeUtf8(String origin) {
        return MD5Encode(origin, "utf-8");
    }


    private static final String hexDigits[] = {"0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

}

Const.java

package com.mmall.common;

/**
 * @Author: Geekerstar(jikewenku.com)
 * @Date: 2018/6/22 10:33
 * @Description:
 */
public class Const {
    public static final String CURRENT_USER = "currentUser";

    public static final String EMAIL = "email";
    public static final String USERNAME = "username";

    public interface Role{
        int ROLE_CUSTOMER = 0; //普通用户
        int ROLE_ADMIN = 1; //管理员
    }
}

UserMapper.xml


UserMapper.java

int checkEmail(String email);
本站所有文章均由网友分享,仅用于参考学习用,请勿直接转载,如有侵权,请联系网站客服删除相关文章。若由于商用引起版权纠纷,一切责任均由使用者承担
极客文库 » 从零到企业级SSM电商项目实战教程(十八)登出,注册,校验功能开发

Leave a Reply

欢迎加入「极客文库」,成为原创作者从这里开始!

立即加入 了解更多