• 极客专栏正式上线!欢迎访问 https://www.jikewenku.com/topic.html
  • 极客专栏正式上线!欢迎访问 https://www.jikewenku.com/topic.html

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

项目实战 Geekerstar 10个月前 (06-22) 373次浏览 已收录 0个评论 扫描二维码
文章目录[隐藏]

UserController.java

/*
 * @Description: 用户登出
 *
 * @auther: Geekerstar(jikewenku.com)
 * @date: 2018/6/22 11:29
 * @param: [session]
 * @return: com.mmall.common.ServerResponse<java.lang.String>
 */
@RequestMapping(value = "logout.do",method = RequestMethod.GET)
@ResponseBody
public ServerResponse<String> 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<String> register(User user){
    return iUserService.register(user);
}

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

IUserService.java

ServerResponse<String> register(User user);

ServerResponse<String> checkValid(String str,String type);

UserServiceImpl.java

public ServerResponse<String> 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<String> 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

<select id="checkEmail" resultType="int" parameterType="string" >
  select count(1) from mmall_user
  where email = #{email}
</select>

UserMapper.java

int checkEmail(String email);

丨极客文库, 版权所有丨如未注明 , 均为原创丨
本网站采用知识共享署名-非商业性使用-相同方式共享 3.0 中国大陆许可协议进行授权
转载请注明原文链接:从零到企业级SSM电商项目实战教程(十八)登出,注册,校验功能开发
喜欢 (0)
[247507792@qq.com]
分享 (0)
Geekerstar
关于作者:
本站技术支持

您必须 登录 才能发表评论!

  • 精品技术教程
  • 编程资源分享
  • 问答交流社区
  • 极客文库知识库

客服QQ


QQ:2248886839


工作时间:09:00-23:00