From 3f03b2aa18f752ad99b521db32c28e63ec080e65 Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Thu, 24 May 2018 15:45:43 +0800 Subject: [PATCH] tmp --- .../core/SimpleClientApplyService.java | 2 + .../impls/SimpleClientApplyServiceImpl.java | 83 ++++++++++++------- .../web/SimpleClientApplyController.java | 10 +-- .../SimpleClientApplyServiceImplTest.java | 26 +++++- 4 files changed, 83 insertions(+), 38 deletions(-) diff --git a/src/main/java/au/com/royalpay/payment/manage/application/core/SimpleClientApplyService.java b/src/main/java/au/com/royalpay/payment/manage/application/core/SimpleClientApplyService.java index 762a81c00..3d4f2ecca 100644 --- a/src/main/java/au/com/royalpay/payment/manage/application/core/SimpleClientApplyService.java +++ b/src/main/java/au/com/royalpay/payment/manage/application/core/SimpleClientApplyService.java @@ -22,4 +22,6 @@ public interface SimpleClientApplyService { void deleteVerifyMailKey(String codeKey); + void verifyMail(String address, JSONObject loginAccount); + } diff --git a/src/main/java/au/com/royalpay/payment/manage/application/core/impls/SimpleClientApplyServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/application/core/impls/SimpleClientApplyServiceImpl.java index 994be043e..553138a34 100644 --- a/src/main/java/au/com/royalpay/payment/manage/application/core/impls/SimpleClientApplyServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/application/core/impls/SimpleClientApplyServiceImpl.java @@ -3,13 +3,17 @@ package au.com.royalpay.payment.manage.application.core.impls; import au.com.royalpay.payment.manage.application.core.SimpleClientApplyService; import au.com.royalpay.payment.manage.merchants.beans.NewAccountBean; import au.com.royalpay.payment.manage.merchants.core.ClientManager; +import au.com.royalpay.payment.manage.signin.beans.LoginInfo; +import au.com.royalpay.payment.manage.signin.core.SignInAccountService; import au.com.royalpay.payment.manage.system.core.MailGunService; import au.com.royalpay.payment.tools.env.PlatformEnvironment; import au.com.royalpay.payment.tools.env.SysConfigManager; import au.com.royalpay.payment.tools.exceptions.BadRequestException; import au.com.royalpay.payment.tools.exceptions.ServerErrorException; +import au.com.royalpay.payment.tools.locale.LocaleSupport; import au.com.royalpay.payment.tools.mail.SendMail; import au.com.royalpay.payment.tools.utils.sms.SmsSingleSender; +import au.com.royalpay.payment.tools.utils.sms.SmsSingleSenderResult; import com.alibaba.fastjson.JSONObject; @@ -46,23 +50,26 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { private SpringTemplateEngine thymeleaf; @Resource private ClientManager clientManager; + @Resource + private SignInAccountService signInAccountService; @Resource private StringRedisTemplate stringRedisTemplate; private final SmsSingleSender smsSingleSender = new SmsSingleSender(appId, appKey); - private final int REGISTER_CLIENT_TEMPLID = 126008; +// private final int REGISTER_CLIENT_TEMPLID = 126008;//正式 + private final int REGISTER_CLIENT_TEMPLID = 110449; private final String REGISTER_CLIENT_PREFIX = "REGISTER_CLIENT"; private final String REGISTER_CLIENT_PROCESS_PREFIX = "REGISTER_CLIENT_PROCESS"; private final String VERIFY_MAIL_PREFIX = "VERIFY_MAIL"; private final List tags = new ArrayList<>(); @PostConstruct - public void init(){ + public void init() { tags.add("account"); } @Override - public void verifyRegisterSMSCode(String codeKey,String phoneNumber) { + public void verifyRegisterSMSCode(String codeKey, String phoneNumber) { String rediskey = getRegisterClientRedisKey(phoneNumber); String codeValue = stringRedisTemplate.boundValueOps(rediskey).get(); if (codeValue == null || !codeValue.equals(codeKey)) { @@ -96,23 +103,26 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { String expireMin = (String) sysConfig.getOrDefault("sms.verification.code.expire", 3); param.add(expireMin); try { - smsSingleSender.sendWithParam(nationCode, phoneNumber, REGISTER_CLIENT_TEMPLID, param, "", "", ""); + SmsSingleSenderResult result = smsSingleSender.sendWithParam(nationCode, phoneNumber, REGISTER_CLIENT_TEMPLID, param, "", "", ""); + System.out.println(result); + System.out.println(result); + System.out.println(result); } catch (Exception e) { throw new ServerErrorException("System Error"); } - stringRedisTemplate.boundValueOps(getRegisterClientRedisKey(phoneNumber)).set(registerClientCode,Long.parseLong(expireMin), TimeUnit.MINUTES); + stringRedisTemplate.boundValueOps(getRegisterClientRedisKey(phoneNumber)).set(registerClientCode, Long.parseLong(expireMin), TimeUnit.MINUTES); return registerClientCode; } @Override - public void sendVerifyEmail(String address,int client_id) { - JSONObject client= clientManager.getClientInfo(client_id); - if(client==null){ + public void sendVerifyEmail(String address, int client_id) { + JSONObject client = clientManager.getClientInfo(client_id); + if (client == null) { throw new BadRequestException("Merchant not found"); } - String key = checkOrGenerateVerifyMailKey(address,null); + String key = checkOrGenerateVerifyMailKey(address, null); Context ctx = new Context(); - ctx.setVariable("url", PlatformEnvironment.getEnv().concatUrl("/register/account/mail/"+address+"/verify/" + key+"/jump")); + ctx.setVariable("url", PlatformEnvironment.getEnv().concatUrl("/register/account/mail/" + address + "/verify/" + key + "/jump")); final String content = thymeleaf.process("mail/register_application", ctx); SendMail sendMail = new SendMail(); Set to = new HashSet<>(); @@ -128,17 +138,17 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { @Override public String checkOrGenerateRegisterProcessKey(String accountName, String codeKey) { - Assert.notNull(accountName,"accountName can't be null"); - if(StringUtils.isNotEmpty(codeKey)){ + Assert.notNull(accountName, "accountName can't be null"); + if (StringUtils.isNotEmpty(codeKey)) { String redisAccountName = stringRedisTemplate.boundValueOps(getRegisterClientProcessRedisKey(codeKey)).get(); - if(!accountName.equals(redisAccountName)){ + if (!accountName.equals(redisAccountName)) { throw new BadRequestException("Data error,Please re-register for safety."); - }else { + } else { return codeKey; } } - String key = RandomStringUtils.random(10, true, false); - stringRedisTemplate.boundValueOps(getRegisterClientProcessRedisKey(key)).set(accountName,1,TimeUnit.HOURS); + String key = RandomStringUtils.random(10, true, false); + stringRedisTemplate.boundValueOps(getRegisterClientProcessRedisKey(key)).set(accountName, 1, TimeUnit.HOURS); return key; } @@ -148,18 +158,18 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { } @Override - public String checkOrGenerateVerifyMailKey(String address, String codeKey){ - Assert.notNull(address,"address can't be null"); - if(StringUtils.isNotEmpty(codeKey)){ + public String checkOrGenerateVerifyMailKey(String address, String codeKey) { + Assert.notNull(address, "address can't be null"); + if (StringUtils.isNotEmpty(codeKey)) { String redisAddress = stringRedisTemplate.boundValueOps(getVerifyMailRedisKey(codeKey)).get(); - if(!address.equals(redisAddress)){ + if (!address.equals(redisAddress)) { throw new BadRequestException("Mail expired"); - }else { + } else { return codeKey; } } - String key = RandomStringUtils.random(10, true, false); - stringRedisTemplate.boundValueOps(getVerifyMailRedisKey(key)).set(address,1,TimeUnit.HOURS); + String key = RandomStringUtils.random(10, true, false); + stringRedisTemplate.boundValueOps(getVerifyMailRedisKey(key)).set(address, 1, TimeUnit.HOURS); return key; } @@ -169,13 +179,28 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { stringRedisTemplate.delete(getVerifyMailRedisKey(codeKey)); } - private String getRegisterClientRedisKey(String phoneNumber){ - return REGISTER_CLIENT_PREFIX +phoneNumber; + @Override + public void verifyMail(String address, JSONObject loginAccount) { + LoginInfo loginInfo = new LoginInfo(); + loginInfo.setLoginId(loginAccount.getString("userName")); + loginInfo.setPassword(loginAccount.getString("password")); + JSONObject account = signInAccountService.accountCheck(loginInfo); + JSONObject client = clientManager.getClientInfo(account.getIntValue("client_id")); + if (!address.equals(client.getString("contact_email"))) { + throw new BadRequestException(LocaleSupport.localeMessage("error.login.password")); + } + + } + + private String getRegisterClientRedisKey(String phoneNumber) { + return REGISTER_CLIENT_PREFIX + phoneNumber; } - private String getRegisterClientProcessRedisKey(String codeKey){ - return REGISTER_CLIENT_PROCESS_PREFIX+codeKey; + + private String getRegisterClientProcessRedisKey(String codeKey) { + return REGISTER_CLIENT_PROCESS_PREFIX + codeKey; } - private String getVerifyMailRedisKey(String codekey){ - return VERIFY_MAIL_PREFIX+codekey; + + private String getVerifyMailRedisKey(String codekey) { + return VERIFY_MAIL_PREFIX + codekey; } } diff --git a/src/main/java/au/com/royalpay/payment/manage/application/web/SimpleClientApplyController.java b/src/main/java/au/com/royalpay/payment/manage/application/web/SimpleClientApplyController.java index 15736eea2..e6303cea5 100644 --- a/src/main/java/au/com/royalpay/payment/manage/application/web/SimpleClientApplyController.java +++ b/src/main/java/au/com/royalpay/payment/manage/application/web/SimpleClientApplyController.java @@ -2,8 +2,6 @@ package au.com.royalpay.payment.manage.application.web; import au.com.royalpay.payment.manage.application.core.SimpleClientApplyService; import au.com.royalpay.payment.manage.merchants.beans.NewAccountBean; -import au.com.royalpay.payment.manage.signin.beans.LoginInfo; -import au.com.royalpay.payment.manage.signin.core.SignInAccountService; import au.com.royalpay.payment.tools.CommonConsts; import au.com.royalpay.payment.tools.http.HttpUtils; @@ -27,8 +25,7 @@ import javax.validation.Valid; public class SimpleClientApplyController { @Resource private SimpleClientApplyService simpleClientApplyService; - @Resource - private SignInAccountService signInAccountService; + @RequestMapping(value = "/account/{codeKey}", method = RequestMethod.POST) @ResponseBody public void registerAccount(@PathVariable String codeKey, @RequestBody @Valid NewAccountBean accountBean, Errors errors, HttpServletResponse response) @@ -55,10 +52,7 @@ public class SimpleClientApplyController { @ResponseBody public void verifyMail(@PathVariable String codeKey, @PathVariable String address,@RequestBody JSONObject account){ simpleClientApplyService.checkOrGenerateVerifyMailKey(address,codeKey); - LoginInfo loginInfo = new LoginInfo(); - loginInfo.setLoginId(account.getString("userName")); - loginInfo.setPassword(account.getString("password")); - signInAccountService.accountCheck(loginInfo); + simpleClientApplyService.verifyMail(address,account); simpleClientApplyService.deleteVerifyMailKey(codeKey); } diff --git a/src/test/java/au/com/royalpay/payment/manage/application/core/impls/SimpleClientApplyServiceImplTest.java b/src/test/java/au/com/royalpay/payment/manage/application/core/impls/SimpleClientApplyServiceImplTest.java index 555727acf..71dd9d432 100644 --- a/src/test/java/au/com/royalpay/payment/manage/application/core/impls/SimpleClientApplyServiceImplTest.java +++ b/src/test/java/au/com/royalpay/payment/manage/application/core/impls/SimpleClientApplyServiceImplTest.java @@ -32,11 +32,35 @@ public class SimpleClientApplyServiceImplTest { @Test public void getAndSendSmsCode() { + String phone = "17626011667"; + String nationCode = "+86"; + String code = simpleClientApplyService.getAndSendSmsCode(phone, nationCode); + System.out.println(code); + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + } + try { + simpleClientApplyService.getAndSendSmsCode(phone, nationCode); + + } catch (Exception e) { + System.out.println("之前应当报错,短信已经发送"); + } + simpleClientApplyService.verifyRegisterSMSCode(code, phone); + + try { + simpleClientApplyService.verifyRegisterSMSCode(code, phone); + System.out.println(code); + } catch (Exception e) { + System.out.println("报错 无法验证"); + + } + } @Test public void sendVerifyEmail() { - simpleClientApplyService.sendVerifyEmail("kira.wang@royalpay.com.au",9); + simpleClientApplyService.sendVerifyEmail("kira.wang@royalpay.com.au", 9); }