Browse Source

添加用户手机注册和手机注册验证接口。

zzy.zhiyuan.foxmail 8 years ago
parent
commit
7005efc7db
1 changed files with 82 additions and 6 deletions
  1. 82 6
      src/main/java/com/ekexiu/portal/service/SysService.java

+ 82 - 6
src/main/java/com/ekexiu/portal/service/SysService.java

@ -59,6 +59,35 @@ public class SysService {
59 59
	private String mailRetrievePasswordReplaceKey;
60 60
	private String mailRetrievePasswordSubject;
61 61
	private long timeLimitWithMailRetrivePassword = 10 * 60 * 1000;
62
	
63
	private String regMobilePhoneReplaceKey;
64
	private String regMobilePhoneContentTemplate;
65
	private long timeLimitWithRegMobilePhone = 60 * 1000;
66
	
67
	public String getRegMobilePhoneReplaceKey() {
68
		return regMobilePhoneReplaceKey;
69
	}
70
71
	public void setRegMobilePhoneReplaceKey(String regMobilePhoneReplaceKey) {
72
		this.regMobilePhoneReplaceKey = regMobilePhoneReplaceKey;
73
	}
74
75
	public String getRegMobilePhoneContentTemplate() {
76
		return regMobilePhoneContentTemplate;
77
	}
78
79
	public void setRegMobilePhoneContentTemplate(
80
			String regMobilePhoneContentTemplate) {
81
		this.regMobilePhoneContentTemplate = regMobilePhoneContentTemplate;
82
	}
83
84
	public long getTimeLimitWithRegMobilePhone() {
85
		return timeLimitWithRegMobilePhone;
86
	}
87
88
	public void setTimeLimitWithRegMobilePhone(long timeLimitWithRegMobilePhone) {
89
		this.timeLimitWithRegMobilePhone = timeLimitWithRegMobilePhone;
90
	}
62 91
63 92
	public String getMailRetrievePasswordSubject() {
64 93
		return mailRetrievePasswordSubject;
@ -206,14 +235,28 @@ public class SysService {
206 235
207 236
	@Post
208 237
	@Path("/reg")
209
	public String insert(@JdbcConn(true) Connection con, User user) throws SQLException {
210
		if(user.getId() == null){
238
	public String insert(@JdbcConn(true) Connection con, String state, String mobilePhone, String validateCode, String password) throws SQLException {
239
		@SuppressWarnings("unchecked")
240
		StateCode<String, String> sc = (StateCode<String, String>) JfwAppContext.getCachedObject(state);
241
		if (sc == null)
242
			return null;
243
		if (sc.getExpiredTime() < System.currentTimeMillis())
244
			return null;
245
		try {
246
			if (!sc.getKey().equals(mobilePhone) || !sc.getValue().equals(validateCode)){
247
				return null;
248
			}
249
			User user = new User();
211 250
			user.setId(StringUtil.buildUUID());
251
			String passwd = StringUtil.md5(password);
252
			user.setMobilePhone(mobilePhone);
253
			user.setPasswd(passwd);
254
			user.setUserType(0+"");
255
			this.userDao.insert(con, user);
256
			return user.getId();
257
		} finally {
258
			JfwAppContext.removeCachedObject(state);
212 259
		}
213
		String password = StringUtil.md5(user.getPasswd());
214
		user.setPasswd(password);
215
		this.userDao.insert(con, user);
216
		return user.getId();
217 260
	}
218 261
	
219 262
	@SetCookie(checkResultNull = true, path = "/", value = { "userid=result.getId()", "userMobilePhone=result.getMobilePhone()", "userType=result.getType()",
@ -369,6 +412,39 @@ public class SysService {
369 412
		return key;
370 413
	}
371 414
415
	@Get
416
	@Path("/regmobilephone")
417
	public String regMobilePhone(@JdbcConn(false) Connection con, String mobilePhone) throws JfwBaseException, SQLException {
418
419
		StateCode<String, String> sc = new StateCode<String, String>();
420
421
		final String key = JfwAppContext.cacheObjectAndGenKey(sc);
422
423
		try {
424
			Random rd = new Random();
425
			int vi = rd.nextInt(10000);
426
427
			String vc = String.format("%04d", vi);
428
			sc.setKey(mobilePhone);
429
			sc.setValue(vc);
430
			this.mobilePhoneServcie.sendMessage(mobilePhone, this.regMobilePhoneContentTemplate, this.regMobilePhoneReplaceKey, vc);
431
			long ct = System.currentTimeMillis();
432
			long et = ct + this.timeLimitWithRegMobilePhone + 5000;
433
			sc.setBuildTime(ct);
434
			sc.setExpiredTime(et);
435
			JfwAppContext.getScheduledExecutorService().schedule(new Runnable() {
436
				@Override
437
				public void run() {
438
					JfwAppContext.removeCachedObject(key);
439
				}
440
			}, this.timeLimitWithRegMobilePhone + 10000, TimeUnit.MILLISECONDS);
441
		} catch (Exception e) {
442
			JfwAppContext.removeCachedObject(key);
443
			throw new JfwBaseException(10012, "send mobile phone message to " + mobilePhone + " error", e);
444
		}
445
		return key;
446
	}
447
	
372 448
	@Post
373 449
	@Path("/bindMobilePhone")
374 450
	public boolean bindMobilePhone(@JdbcConn(true) Connection con, String state, String userid, String mobilePhone, String validateCode) throws SQLException {