Skip to content

Commit

Permalink
增加注册功能
Browse files Browse the repository at this point in the history
  • Loading branch information
qiurunze committed Dec 11, 2018
1 parent 959a7ea commit 70293b0
Show file tree
Hide file tree
Showing 21 changed files with 3,225 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
--在sql加上判断防止数据边为负数
--数据库加唯一索引防止用户重复购买
--redis预减库存减少数据库访问 内存标记减少redis访问 请求先入队列缓冲,异步下单,增强用户体验

#### [注册功能 -- 如果有前端的牛人加入修改几个页面那是再好不过了哈哈哈]()
#### [全局异常处理拦截]()
1.定义全局的异常拦截器
2.定义了全局异常类型
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/com/geekq/miaosha/controller/RegisterController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.geekq.miaosha.controller;

import com.geekq.miaosha.common.resultbean.ResultGeekQ;
import com.geekq.miaosha.domain.MiaoshaUser;
import com.geekq.miaosha.service.MiaoShaUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

import static com.geekq.miaosha.common.enums.ResultStatus.RESIGETER_FAIL;

@Controller
@RequestMapping("/user")
public class RegisterController {

private static Logger logger = LoggerFactory.getLogger(RegisterController.class);

@Autowired
private MiaoShaUserService miaoShaUserService;
@RequestMapping("/do_register")
public String registerIndex(){
return "register";
}

/**
* 注册网站
* @param userName
* @param passWord
* @param salt
* @return
*/
@RequestMapping("/register")
@ResponseBody
public ResultGeekQ<String> register(@RequestParam("username") String userName ,
@RequestParam("password") String passWord,
@RequestParam("salt") String salt,HttpServletResponse response ){

ResultGeekQ<String> result = ResultGeekQ.build();
boolean registerInfo = miaoShaUserService.register(response , userName,passWord,salt);
if(!registerInfo){
result.withError(RESIGETER_FAIL.getCode(),RESIGETER_FAIL.getMessage());
return result;
}
return result;
}
}
8 changes: 6 additions & 2 deletions src/main/java/com/geekq/miaosha/dao/MiaoShaUserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
@Mapper
public interface MiaoShaUserDao {

@Select("select * from miaosha_user where nickname = #{nickname}")
public MiaoshaUser getByNickname(@Param("nickname") String nickname ) ;

@Select("select * from miaosha_user where id = #{id}")
public MiaoshaUser getById(@Param("id") long id ) ;

Expand All @@ -14,7 +17,8 @@ public interface MiaoShaUserDao {
public void update(MiaoshaUser toBeUpdate);


@Insert("insert into miaosha_user (id , nickname ,password , salt ,head,register_date,last_login_date)value (#{id},#{nickname},#{password},#{salt},#{nickname},#{head},#{register_date},#{last_login_date}) ")
public long insertMiaoShaUser(MiaoshaUser miaoshaUser);
@Insert("insert into miaosha_user (id , nickname ,password , salt ,head,register_date,last_login_date)value (#{id},#{nickname},#{password},#{salt},#{head},#{registerDate},#{lastLoginDate}) ")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
public void insertMiaoShaUser(MiaoshaUser miaoshaUser);

}
1 change: 1 addition & 0 deletions src/main/java/com/geekq/miaosha/domain/MiaoshaUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@AllArgsConstructor
@NoArgsConstructor
public class MiaoshaUser {

private Long id;
private String nickname;
private String password;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/geekq/miaosha/redis/MiaoShaUserKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class MiaoShaUserKey extends BasePrefix{
public static final int TOKEN_EXPIRE = 3600 *24*2;
public static MiaoShaUserKey token = new MiaoShaUserKey(TOKEN_EXPIRE,"tk") ;
public static MiaoShaUserKey getById = new MiaoShaUserKey(0, "id");
public static MiaoShaUserKey getByNickName = new MiaoShaUserKey(0, "nickName");

public MiaoShaUserKey(int expireSeconds ,String prefix) {
super(expireSeconds,prefix);
Expand Down
50 changes: 40 additions & 10 deletions src/main/java/com/geekq/miaosha/service/MiaoShaUserService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.geekq.miaosha.service;

import com.geekq.miaosha.controller.RegisterController;
import com.geekq.miaosha.dao.MiaoShaUserDao;
import com.geekq.miaosha.domain.MiaoshaUser;
import com.geekq.miaosha.exception.GlobleException;
Expand All @@ -9,18 +10,23 @@
import com.geekq.miaosha.utils.UUIDUtil;
import com.geekq.miaosha.vo.LoginVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import java.util.Date;

import static com.geekq.miaosha.common.enums.ResultStatus.*;

@Service
public class MiaoShaUserService {

public static final String COOKIE_NAME_TOKEN = "token" ;
private static Logger logger = LoggerFactory.getLogger(MiaoShaUserService.class);

@Autowired
private MiaoShaUserDao miaoShaUserDao ;
Expand All @@ -42,47 +48,72 @@ public MiaoshaUser getByToken(HttpServletResponse response , String token) {

}

public MiaoshaUser getById(long id) {
public MiaoshaUser getByNickName(String nickName) {
//取缓存
MiaoshaUser user = redisService.get(MiaoShaUserKey.getById, ""+id, MiaoshaUser.class);
MiaoshaUser user = redisService.get(MiaoShaUserKey.getByNickName, ""+nickName, MiaoshaUser.class);
if(user != null) {
return user;
}
//取数据库
user = miaoShaUserDao.getById(id);
user = miaoShaUserDao.getByNickname(nickName);
if(user != null) {
redisService.set(MiaoShaUserKey.getById, ""+id, user);
redisService.set(MiaoShaUserKey.getByNickName, ""+nickName, user);
}
return user;
}


// http://blog.csdn.net/tTU1EvLDeLFq5btqiK/article/details/78693323
public boolean updatePassword(String token, long id, String formPass) {
public boolean updatePassword(String token, String nickName, String formPass) {
//取user
MiaoshaUser user = getById(id);
MiaoshaUser user = getByNickName(nickName);
if(user == null) {
throw new GlobleException(MOBILE_NOT_EXIST);
}
//更新数据库
MiaoshaUser toBeUpdate = new MiaoshaUser();
toBeUpdate.setId(id);
toBeUpdate.setNickname(nickName);
toBeUpdate.setPassword(MD5Utils.formPassToDBPass(formPass, user.getSalt()));
miaoShaUserDao.update(toBeUpdate);
//处理缓存
redisService.delete(MiaoShaUserKey.getById, ""+id);
redisService.delete(MiaoShaUserKey.getByNickName, ""+nickName);
user.setPassword(toBeUpdate.getPassword());
redisService.set(MiaoShaUserKey.token, token, user);
return true;
}


public boolean register(HttpServletResponse response , String userName , String passWord , String salt) {
MiaoshaUser miaoShaUser = new MiaoshaUser();
miaoShaUser.setNickname(userName);
String DBPassWord = MD5Utils.formPassToDBPass(passWord , salt);
miaoShaUser.setPassword(DBPassWord);
miaoShaUser.setRegisterDate(new Date());
miaoShaUser.setSalt(salt);
miaoShaUser.setNickname(userName);
try {
miaoShaUserDao.insertMiaoShaUser(miaoShaUser);
MiaoshaUser user = miaoShaUserDao.getById(miaoShaUser.getId());
if(user == null){
return false;
}
//生成cookie 将session返回游览器 分布式session
String token= UUIDUtil.uuid();
addCookie(response, token, user);
} catch (Exception e) {
logger.error("注册失败",e);
return false;
}
return true;
}
public boolean login(HttpServletResponse response , LoginVo loginVo) {
if(loginVo ==null){
throw new GlobleException(SYSTEM_ERROR);
}

String mobile =loginVo.getMobile();
String password =loginVo.getPassword();
MiaoshaUser user = getById(Long.valueOf(mobile));
MiaoshaUser user = getByNickName(mobile);
if(user == null) {
throw new GlobleException(MOBILE_NOT_EXIST);
}
Expand All @@ -98,7 +129,6 @@ public boolean login(HttpServletResponse response , LoginVo loginVo) {
addCookie(response, token, user);
return true ;
}

private void addCookie(HttpServletResponse response, String token, MiaoshaUser user) {
redisService.set(MiaoShaUserKey.token, token, user);
Cookie cookie = new Cookie(COOKIE_NAME_TOKEN, token);
Expand Down
Loading

0 comments on commit 70293b0

Please sign in to comment.