Browse Source

手机6位数验证码验证

git-svn-id: svn+ssh://10.10.101.21/source/platform/platform-b2b@1269 f3bf4e98-0cf0-11e4-a00c-a99a8b9d557d
suntg 10 years ago
parent
commit
36a932d730

+ 35 - 1
src/main/java/com/uas/platform/b2b/model/Token.java

@@ -1,7 +1,10 @@
 package com.uas.platform.b2b.model;
 
 import java.io.Serializable;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.Date;
+import java.util.List;
 
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
@@ -28,6 +31,8 @@ public class Token implements Serializable{
 
 	private Date time;
 	
+	private String code;
+	
 	public Token() {
 		this.time = new Date();
 	}
@@ -36,12 +41,16 @@ public class Token implements Serializable{
 		this();
 		this.userType = type.toLowerCase();
 		this.uu = uu;
+		if("mobile".equals(this.userType))
+			this.code = randomCode();
 	}
 
 	public Token(UserType type, Long uu) {
 		this();
 		this.userType = type.toString();
 		this.uu = uu;
+		if("mobile".equals(this.userType))
+			this.code = randomCode();
 	}
 
 	public Long getId() {
@@ -76,6 +85,14 @@ public class Token implements Serializable{
 		this.time = time;
 	}
 
+	public String getCode() {
+		return code;
+	}
+
+	public void setCode(String code) {
+		this.code = code;
+	}
+
 	/**
 	 * 24小时超时
 	 * 
@@ -86,6 +103,23 @@ public class Token implements Serializable{
 	}
 
 	public enum UserType {
-		enterprise, user;
+		enterprise, user, mobile;
+	}
+	
+	/**
+	 * 生成6位数字的随机验证码
+	 * @return
+	 */
+	public String randomCode(){
+		String[] beforeShuffle = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
+		List<String> list = Arrays.asList(beforeShuffle);
+		Collections.shuffle(list);
+		StringBuilder sb = new StringBuilder();
+		for (int i = 0; i < list.size(); i++) {
+			sb.append(list.get(i));
+		}
+		String afterShuffle = sb.toString();
+		String result = afterShuffle.substring(3, 9);
+		return result;
 	}
 }

+ 8 - 1
src/main/java/com/uas/platform/b2b/support/TokenService.java

@@ -33,6 +33,9 @@ public class TokenService {
 	 */
 	public String getEncodeToken(String userType, Long uu) {
 		Token token = saveToken(new Token(userType, uu));
+		if("mobile".equals(token.getUserType())) {
+			return token.getCode();
+		}
 		return Md5Utils.encode(uu, token.getId());
 	}
 
@@ -49,7 +52,11 @@ public class TokenService {
 		if (!tokens.isEmpty()) {
 			for (Token token : tokens) {
 				String encode = Md5Utils.encode(uu, token.getId());
-				boolean certified = !token.isOutofTime() && encode.equals(encodeString);
+				boolean certified = false;
+				if("mobile".equals(token.getUserType())) // 手机验证码
+					certified = !token.isOutofTime() && token.getCode().equals(encodeString);
+				else // 邮箱验证
+					certified = !token.isOutofTime() && encode.equals(encodeString);
 				// 只使用一次,即时删除
 				tokenDao.delete(token);
 				if (certified)