-
Notifications
You must be signed in to change notification settings - Fork 703
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
注册加入邮箱验证,验证码验证功能
- Loading branch information
Showing
15 changed files
with
527 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cn.tomoya.module.code.dao; | ||
|
||
import cn.tomoya.module.code.entity.Code; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by tomoya on 17-6-6. | ||
*/ | ||
@Repository | ||
public interface CodeDao extends JpaRepository<Code, Integer> { | ||
|
||
List<Code> findByCodeAndType(String code, String type); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package cn.tomoya.module.code.entity; | ||
|
||
import javax.persistence.*; | ||
import java.util.Date; | ||
|
||
/** | ||
* Created by tomoya on 17-6-6. | ||
*/ | ||
@Entity | ||
@Table(name = "pybbs_code") | ||
public class Code { | ||
|
||
@Id | ||
@GeneratedValue | ||
private int id; | ||
|
||
@Column(unique = true) | ||
private String code; | ||
|
||
@Column(name = "expire_time") | ||
private Date expireTime; | ||
|
||
private String type; | ||
|
||
@Column(name = "is_used") | ||
private boolean isUsed; | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
public void setCode(String code) { | ||
this.code = code; | ||
} | ||
|
||
public Date getExpireTime() { | ||
return expireTime; | ||
} | ||
|
||
public void setExpireTime(Date expireTime) { | ||
this.expireTime = expireTime; | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public void setType(String type) { | ||
this.type = type; | ||
} | ||
|
||
public boolean isUsed() { | ||
return isUsed; | ||
} | ||
|
||
public void setUsed(boolean used) { | ||
isUsed = used; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cn.tomoya.module.code.entity; | ||
|
||
/** | ||
* Created by tomoya on 17-6-6. | ||
*/ | ||
public enum CodeEnum { | ||
|
||
EMAIL, | ||
SMS | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/cn/tomoya/module/code/service/CodeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cn.tomoya.module.code.service; | ||
|
||
import cn.tomoya.module.code.dao.CodeDao; | ||
import cn.tomoya.module.code.entity.Code; | ||
import cn.tomoya.module.code.entity.CodeEnum; | ||
import cn.tomoya.util.DateUtil; | ||
import cn.tomoya.util.StrUtil; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by tomoya on 17-6-6. | ||
*/ | ||
@Service | ||
@Transactional | ||
public class CodeService { | ||
|
||
@Autowired | ||
private CodeDao codeDao; | ||
|
||
public Code findByCodeAndType(String code, CodeEnum type) { | ||
List<Code> codes = codeDao.findByCodeAndType(code, type.name()); | ||
if(codes.size() > 0) return codes.get(0); | ||
return null; | ||
} | ||
|
||
public void save(Code code) { | ||
codeDao.save(code); | ||
} | ||
|
||
public String genEmailCode() { | ||
String genCode = StrUtil.randomString(6); | ||
Code code = findByCodeAndType(genCode, CodeEnum.EMAIL); | ||
if(code != null) { | ||
return genEmailCode(); | ||
} else { | ||
code = new Code(); | ||
code.setCode(genCode); | ||
code.setExpireTime(DateUtil.getMinuteAfter(new Date(), 10)); | ||
code.setType(CodeEnum.EMAIL.name()); | ||
code.setUsed(false); | ||
save(code); | ||
return genCode; | ||
} | ||
} | ||
|
||
public int validateCode(String code, CodeEnum type) { | ||
Code code1 = findByCodeAndType(code, type); | ||
if(code1 == null) return 1;// 验证码不正确 | ||
if(DateUtil.isExpire(code1.getExpireTime())) return 2; // 过期了 | ||
code1.setUsed(true); | ||
save(code1); | ||
return 0; // 正常 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.