From 09976bbab9944c8e02dcfdd4b1e9f8226cfb8cf3 Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Wed, 23 May 2018 13:42:54 +0800 Subject: [PATCH 1/4] tmp --- .../core/SimpleClientApplyService.java | 2 +- .../impls/SimpleClientApplyServiceImpl.java | 40 +++++++++++++++++++ .../web/SimpleClientApplyController.java | 2 +- .../merchants/beans/NewAccountBean.java | 10 +++++ src/main/resources/application.properties | 5 +++ 5 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/main/java/au/com/royalpay/payment/manage/application/core/impls/SimpleClientApplyServiceImpl.java 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 008d754c4..9e02379d4 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 @@ -4,7 +4,7 @@ import au.com.royalpay.payment.manage.merchants.beans.NewAccountBean; import com.alibaba.fastjson.JSONObject; public interface SimpleClientApplyService { - String getSMSVerifyCode(String codeKey); + String getSMSVerifyCode(String codeKey,String phoneNumber,String nationCode); JSONObject newAccount(NewAccountBean accountBean); 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 new file mode 100644 index 000000000..658c11e0d --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/application/core/impls/SimpleClientApplyServiceImpl.java @@ -0,0 +1,40 @@ +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.tools.utils.sms.SmsSingleSender; + +import com.alibaba.fastjson.JSONObject; + +import org.springframework.beans.factory.annotation.Value; + +public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { + + @Value("${royalpay.sms.appid}") + private int appId; + @Value("${royalpay.sms.appkey}") + private String appKey; + + private final SmsSingleSender smsSingleSender = new SmsSingleSender(appId,appKey); + + @Override + String getSMSVerifyCode(String codeKey,String phoneNumber,String nationCode){ +// smsSingleSender.sendWithParam(); + return null; + } + + @Override + public JSONObject newAccount(NewAccountBean accountBean) { + return null; + } + + @Override + public void deleteSMSVerifyCodeKey(String codeKey) { + + } + + @Override + public String partnerSignIn(JSONObject account) { + return null; + } +} 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 1a8a47e00..b7ad70d2d 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 @@ -21,7 +21,7 @@ public class SimpleClientApplyController { @RequestMapping(value = "/account/{codeKey}", method = RequestMethod.POST) public void registerAccount(@PathVariable String codeKey, @RequestBody @Valid NewAccountBean accountBean, Errors errors, HttpServletResponse response) throws Exception { HttpUtils.handleValidErrors(errors); - String codeValue = simpleClientApplyService.getSMSVerifyCode(codeKey); + String codeValue = simpleClientApplyService.getSMSVerifyCode(codeKey,accountBean.getContactPhone(),accountBean.getNation_code()); if (codeValue == null || !codeValue.equals(accountBean.getUsername())) { throw new Exception("Verification code has expired or is not correct"); } diff --git a/src/main/java/au/com/royalpay/payment/manage/merchants/beans/NewAccountBean.java b/src/main/java/au/com/royalpay/payment/manage/merchants/beans/NewAccountBean.java index ca16c3f56..4fd5f8981 100644 --- a/src/main/java/au/com/royalpay/payment/manage/merchants/beans/NewAccountBean.java +++ b/src/main/java/au/com/royalpay/payment/manage/merchants/beans/NewAccountBean.java @@ -18,6 +18,8 @@ public class NewAccountBean { private String displayName; @JSONField(name = "contact_phone") private String contactPhone; + @JSONField(name = "nation_code") + private String nation_code = "+61"; private int role = PartnerRole.CASHIER.getCode(); public JSONObject toJson() { @@ -70,4 +72,12 @@ public class NewAccountBean { public void setContactPhone(String contactPhone) { this.contactPhone = contactPhone; } + + public String getNation_code() { + return nation_code; + } + + public void setNation_code(String nation_code) { + this.nation_code = nation_code; + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index bc53561d1..490a6d3f3 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -106,3 +106,8 @@ mail.mailgun.domain=mail.royalpay.com.au mail.mailgun.default.merchantlist=merchants@mail.royalpay.com.au +############## +##短信key +############## +royalpay.sms.appid=1400035361 +royalpay.sms.appkey=d6e7cc6400ecd159963c1972cdb088cf From 48cc6509c0ca6185eb97788a43a178dd6aa2d054 Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Wed, 23 May 2018 14:09:56 +0800 Subject: [PATCH 2/4] tmp --- .../core/SimpleClientApplyService.java | 4 +- .../impls/SimpleClientApplyServiceImpl.java | 46 +++++++++++++++---- .../web/SimpleClientApplyController.java | 2 +- 3 files changed, 42 insertions(+), 10 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 9e02379d4..00f5f5aaa 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 @@ -4,11 +4,13 @@ import au.com.royalpay.payment.manage.merchants.beans.NewAccountBean; import com.alibaba.fastjson.JSONObject; public interface SimpleClientApplyService { - String getSMSVerifyCode(String codeKey,String phoneNumber,String nationCode); + String getSMSVerifyCode(String codeKey); JSONObject newAccount(NewAccountBean accountBean); void deleteSMSVerifyCodeKey(String codeKey); String partnerSignIn(JSONObject account); + + String getSmsCodeAndSend(); } 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 658c11e0d..56cbe39cb 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 @@ -1,12 +1,22 @@ 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.tools.utils.sms.SmsSingleSender; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; -import com.alibaba.fastjson.JSONObject; +import javax.annotation.Resource; +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.time.DateFormatUtils; import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.redis.core.StringRedisTemplate; + +import com.alibaba.fastjson.JSONObject; + +import au.com.royalpay.payment.manage.application.core.SimpleClientApplyService; +import au.com.royalpay.payment.manage.merchants.beans.NewAccountBean; +import au.com.royalpay.payment.tools.env.SysConfigManager; +import au.com.royalpay.payment.tools.utils.sms.SmsSingleSender; public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { @@ -14,13 +24,18 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { private int appId; @Value("${royalpay.sms.appkey}") private String appKey; + @Resource + private SysConfigManager sysConfigManager; + @Resource + private StringRedisTemplate stringRedisTemplate; private final SmsSingleSender smsSingleSender = new SmsSingleSender(appId,appKey); - + // TODO: 2018/5/23 kira 模板申请通过后修改 + private final int REGISTER_CLIENT_TEMPLID = 123; + private final String REGISTER_CLIENT_PREFIX = "REGISTER_CLIENT"; @Override - String getSMSVerifyCode(String codeKey,String phoneNumber,String nationCode){ -// smsSingleSender.sendWithParam(); - return null; + public String getSMSVerifyCode(String codeKey){ + return null; } @Override @@ -37,4 +52,19 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { public String partnerSignIn(JSONObject account) { return null; } + + @Override + public String getSmsCodeAndSend(String phoneNumber,String nationCode) { + JSONObject sysConfig = sysConfigManager.getSysConfig(); + List param = new ArrayList<>(); + String register_client_key = REGISTER_CLIENT_PREFIX+RandomStringUtils.random(6, true, true); + param.add(register_client_key); + String expireMin = (String) sysConfig.getOrDefault("sms.verification.code.expire",3); + param.add(expireMin); + stringRedisTemplate.boundValueOps(register_client_key).set(register_client_key, Long.parseLong(expireMin), TimeUnit.MINUTES); + + param.add(DateFormatUtils.format(coupon.getDate("expiry"), "yyyy-MM-dd")); + smsSingleSender.sendWithParam(nationCode,phoneNumber,REGISTER_CLIENT_TEMPLID,,"","",""); + return register_client_key; + } } 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 b7ad70d2d..1a8a47e00 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 @@ -21,7 +21,7 @@ public class SimpleClientApplyController { @RequestMapping(value = "/account/{codeKey}", method = RequestMethod.POST) public void registerAccount(@PathVariable String codeKey, @RequestBody @Valid NewAccountBean accountBean, Errors errors, HttpServletResponse response) throws Exception { HttpUtils.handleValidErrors(errors); - String codeValue = simpleClientApplyService.getSMSVerifyCode(codeKey,accountBean.getContactPhone(),accountBean.getNation_code()); + String codeValue = simpleClientApplyService.getSMSVerifyCode(codeKey); if (codeValue == null || !codeValue.equals(accountBean.getUsername())) { throw new Exception("Verification code has expired or is not correct"); } From 543964051bbccfe716888f8be6b0495ac13a2938 Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Wed, 23 May 2018 16:23:13 +0800 Subject: [PATCH 3/4] tmp --- .../core/SimpleClientApplyService.java | 8 +- .../impls/SimpleClientApplyServiceImpl.java | 92 +++++++++++++++---- .../web/SimpleClientApplyController.java | 22 ++++- .../templates/mail/register_application.html | 55 +++++++++++ 4 files changed, 154 insertions(+), 23 deletions(-) create mode 100644 src/main/resources/templates/mail/register_application.html 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 00f5f5aaa..147f63f4b 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 @@ -4,7 +4,7 @@ import au.com.royalpay.payment.manage.merchants.beans.NewAccountBean; import com.alibaba.fastjson.JSONObject; public interface SimpleClientApplyService { - String getSMSVerifyCode(String codeKey); + void verifyRegisterSMSCode(String codeKey, String accountName); JSONObject newAccount(NewAccountBean accountBean); @@ -12,5 +12,9 @@ public interface SimpleClientApplyService { String partnerSignIn(JSONObject account); - String getSmsCodeAndSend(); + String getSmsCodeAndSend(String phoneNumber,String nationCode,String accountName); + + void sendVerifyEmail(String address,JSONObject client); + + String generateRegisterProcessKey(String accountName); } 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 56cbe39cb..b2fef1ed9 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 @@ -1,21 +1,28 @@ package au.com.royalpay.payment.manage.application.core.impls; import java.util.ArrayList; -import java.util.List; +import java.util.HashSet; +import java.util.Set; import java.util.concurrent.TimeUnit; import javax.annotation.Resource; import org.apache.commons.lang3.RandomStringUtils; -import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.StringRedisTemplate; +import org.thymeleaf.context.Context; +import org.thymeleaf.spring4.SpringTemplateEngine; import com.alibaba.fastjson.JSONObject; import au.com.royalpay.payment.manage.application.core.SimpleClientApplyService; import au.com.royalpay.payment.manage.merchants.beans.NewAccountBean; +import au.com.royalpay.payment.manage.system.core.MailGunService; 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.mail.SendMail; import au.com.royalpay.payment.tools.utils.sms.SmsSingleSender; public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { @@ -26,16 +33,27 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { private String appKey; @Resource private SysConfigManager sysConfigManager; + @Resource + private MailGunService mailGunService; + @Resource + private SpringTemplateEngine thymeleaf; @Resource private StringRedisTemplate stringRedisTemplate; - private final SmsSingleSender smsSingleSender = new SmsSingleSender(appId,appKey); - // TODO: 2018/5/23 kira 模板申请通过后修改 - private final int REGISTER_CLIENT_TEMPLID = 123; + private final SmsSingleSender smsSingleSender = new SmsSingleSender(appId, appKey); + private final int REGISTER_CLIENT_TEMPLID = 126008; private final String REGISTER_CLIENT_PREFIX = "REGISTER_CLIENT"; + private final String REGISTER_CLIENT_PHONE_EXPIRE_PREFIX = "REGISTER_CLIENT_PHONE_EXPIRE"; + @Override - public String getSMSVerifyCode(String codeKey){ - return null; + public void verifyRegisterSMSCode(String codeKey,String accountName) { + String rediskey = getRegisterClientRedisKey(accountName); + String codeValue = stringRedisTemplate.boundValueOps(rediskey).get(); + if (codeValue == null || !codeValue.equals(codeKey)) { + throw new BadRequestException("Verification code has expired or is not correct"); + } + stringRedisTemplate.delete(rediskey); +// stringRedisTemplate.delete(getRegisterClientPhoneExpireRedisKey(phoneNumber)); } @Override @@ -54,17 +72,59 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { } @Override - public String getSmsCodeAndSend(String phoneNumber,String nationCode) { + public String getSmsCodeAndSend(String phoneNumber, String nationCode,String accountName) { + String reidsCheckCodeKey = getRegisterClientPhoneExpireRedisKey(phoneNumber); + String value = stringRedisTemplate.boundValueOps(reidsCheckCodeKey).get(); + if (StringUtils.isNotEmpty(value)) { + throw new BadRequestException("SMS has been sent,Please check your messages or try again in 3 minutes."); + } + JSONObject sysConfig = sysConfigManager.getSysConfig(); - List param = new ArrayList<>(); - String register_client_key = REGISTER_CLIENT_PREFIX+RandomStringUtils.random(6, true, true); - param.add(register_client_key); - String expireMin = (String) sysConfig.getOrDefault("sms.verification.code.expire",3); + ArrayList param = new ArrayList<>(); + String registerClientKey = getRegisterClientRedisKey(accountName); + String registerClientCode = RandomStringUtils.random(6, true, true); + param.add(registerClientCode); + String expireMin = (String) sysConfig.getOrDefault("sms.verification.code.expire", 3); param.add(expireMin); - stringRedisTemplate.boundValueOps(register_client_key).set(register_client_key, Long.parseLong(expireMin), TimeUnit.MINUTES); + try { + smsSingleSender.sendWithParam(nationCode, phoneNumber, REGISTER_CLIENT_TEMPLID, param, "", "", ""); + } catch (Exception e) { + throw new ServerErrorException("System Error"); + } + stringRedisTemplate.boundValueOps(registerClientKey).set(registerClientCode, Long.parseLong(expireMin), TimeUnit.MINUTES); + stringRedisTemplate.boundValueOps(reidsCheckCodeKey).set(reidsCheckCodeKey, Long.parseLong(expireMin), TimeUnit.MINUTES); + + return registerClientCode; + } + + @Override + public void sendVerifyEmail(String address,JSONObject client) { + Context ctx = new Context(); + ctx.setVariable("url", url); + final String content = thymeleaf.process("mail/register_application", ctx); + - param.add(DateFormatUtils.format(coupon.getDate("expiry"), "yyyy-MM-dd")); - smsSingleSender.sendWithParam(nationCode,phoneNumber,REGISTER_CLIENT_TEMPLID,,"","",""); - return register_client_key; + SendMail sendMail = new SendMail(); + Set to = new HashSet<>(); + to.add(address); + sendMail.setMailTos(to); + sendMail.setFrom("info@mail.royalpay.com.au"); + sendMail.setTitle("Royalpay"); + sendMail.setContent(content); + sendMail.setTags(tags); + mailGunService.sendMail(sendMail); + } + + @Override + public String generateRegisterProcessKey(String accountName) { + + return null; + } + + private String getRegisterClientRedisKey(String accountName){ + return REGISTER_CLIENT_PREFIX +accountName; + } + private String getRegisterClientPhoneExpireRedisKey(String phoneNumber){ + return REGISTER_CLIENT_PHONE_EXPIRE_PREFIX +phoneNumber; } } 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 1a8a47e00..88b1fd437 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 @@ -19,15 +19,27 @@ public class SimpleClientApplyController { private SimpleClientApplyService simpleClientApplyService; @RequestMapping(value = "/account/{codeKey}", method = RequestMethod.POST) - public void registerAccount(@PathVariable String codeKey, @RequestBody @Valid NewAccountBean accountBean, Errors errors, HttpServletResponse response) throws Exception { + public void registerAccount(@PathVariable String codeKey, @RequestBody @Valid NewAccountBean accountBean, Errors errors, HttpServletResponse response) + throws Exception { HttpUtils.handleValidErrors(errors); - String codeValue = simpleClientApplyService.getSMSVerifyCode(codeKey); - if (codeValue == null || !codeValue.equals(accountBean.getUsername())) { - throw new Exception("Verification code has expired or is not correct"); - } + simpleClientApplyService.verifyRegisterSMSCode(codeKey,accountBean.getUsername()); + JSONObject account = simpleClientApplyService.newAccount(accountBean); simpleClientApplyService.deleteSMSVerifyCodeKey(codeKey); String statusKey = simpleClientApplyService.partnerSignIn(account); HttpUtils.setCookie(response, CommonConsts.CODE_KEY, statusKey); } + + +// @RequestMapping(value = "/account/mail/verify", method = RequestMethod.GET) +// public void registerAccount(@PathVariable String codeKey, @RequestBody @Valid NewAccountBean accountBean, Errors errors, HttpServletResponse response) +// throws Exception { +// HttpUtils.handleValidErrors(errors); +// simpleClientApplyService.verifyRegisterSMSCode(codeKey, accountBean.getContactPhone()); +// +// JSONObject account = simpleClientApplyService.newAccount(accountBean); +// simpleClientApplyService.deleteSMSVerifyCodeKey(codeKey); +// String statusKey = simpleClientApplyService.partnerSignIn(account); +// HttpUtils.setCookie(response, CommonConsts.CODE_KEY, statusKey); +// } } diff --git a/src/main/resources/templates/mail/register_application.html b/src/main/resources/templates/mail/register_application.html new file mode 100644 index 000000000..a3e88d8fd --- /dev/null +++ b/src/main/resources/templates/mail/register_application.html @@ -0,0 +1,55 @@ + + + + + + + \ No newline at end of file From 1842e10bb3a5b4961340b9f7e272be891f561f19 Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Wed, 23 May 2018 17:58:05 +0800 Subject: [PATCH 4/4] tmp --- .../core/SimpleClientApplyService.java | 17 +- .../impls/SimpleClientApplyServiceImpl.java | 127 +++++++++----- .../web/SimpleClientApplyController.java | 46 +++-- .../resources/templates/mail/verify_mail.html | 158 ++++++++++++++++++ 4 files changed, 288 insertions(+), 60 deletions(-) create mode 100644 src/main/resources/templates/mail/verify_mail.html 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 147f63f4b..762a81c00 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 @@ -4,17 +4,22 @@ import au.com.royalpay.payment.manage.merchants.beans.NewAccountBean; import com.alibaba.fastjson.JSONObject; public interface SimpleClientApplyService { - void verifyRegisterSMSCode(String codeKey, String accountName); + void verifyRegisterSMSCode(String codeKey, String phoneNumber); JSONObject newAccount(NewAccountBean accountBean); - void deleteSMSVerifyCodeKey(String codeKey); - String partnerSignIn(JSONObject account); - String getSmsCodeAndSend(String phoneNumber,String nationCode,String accountName); + String getAndSendSmsCode(String phoneNumber, String nationCode); + + String checkOrGenerateRegisterProcessKey(String accountName, String codeKey); + + void deleteRegisterProcessKey(String codeKey); + + void sendVerifyEmail(String address, int client_id); + + String checkOrGenerateVerifyMailKey(String address, String codeKey); - void sendVerifyEmail(String address,JSONObject client); + void deleteVerifyMailKey(String codeKey); - String generateRegisterProcessKey(String accountName); } 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 b2fef1ed9..c0cca8e03 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 @@ -1,29 +1,34 @@ package au.com.royalpay.payment.manage.application.core.impls; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.TimeUnit; +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.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.mail.SendMail; +import au.com.royalpay.payment.tools.utils.sms.SmsSingleSender; -import javax.annotation.Resource; +import com.alibaba.fastjson.JSONObject; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.util.Assert; import org.thymeleaf.context.Context; import org.thymeleaf.spring4.SpringTemplateEngine; -import com.alibaba.fastjson.JSONObject; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; -import au.com.royalpay.payment.manage.application.core.SimpleClientApplyService; -import au.com.royalpay.payment.manage.merchants.beans.NewAccountBean; -import au.com.royalpay.payment.manage.system.core.MailGunService; -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.mail.SendMail; -import au.com.royalpay.payment.tools.utils.sms.SmsSingleSender; +import javax.annotation.PostConstruct; +import javax.annotation.Resource; public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { @@ -37,23 +42,31 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { private MailGunService mailGunService; @Resource private SpringTemplateEngine thymeleaf; + @Resource + private ClientManager clientManager; @Resource private StringRedisTemplate stringRedisTemplate; private final SmsSingleSender smsSingleSender = new SmsSingleSender(appId, appKey); private final int REGISTER_CLIENT_TEMPLID = 126008; private final String REGISTER_CLIENT_PREFIX = "REGISTER_CLIENT"; - private final String REGISTER_CLIENT_PHONE_EXPIRE_PREFIX = "REGISTER_CLIENT_PHONE_EXPIRE"; + 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(){ + tags.add("account"); + } @Override - public void verifyRegisterSMSCode(String codeKey,String accountName) { - String rediskey = getRegisterClientRedisKey(accountName); + public void verifyRegisterSMSCode(String codeKey,String phoneNumber) { + String rediskey = getRegisterClientRedisKey(phoneNumber); String codeValue = stringRedisTemplate.boundValueOps(rediskey).get(); if (codeValue == null || !codeValue.equals(codeKey)) { throw new BadRequestException("Verification code has expired or is not correct"); } stringRedisTemplate.delete(rediskey); -// stringRedisTemplate.delete(getRegisterClientPhoneExpireRedisKey(phoneNumber)); } @Override @@ -61,19 +74,14 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { return null; } - @Override - public void deleteSMSVerifyCodeKey(String codeKey) { - - } - @Override public String partnerSignIn(JSONObject account) { return null; } @Override - public String getSmsCodeAndSend(String phoneNumber, String nationCode,String accountName) { - String reidsCheckCodeKey = getRegisterClientPhoneExpireRedisKey(phoneNumber); + public String getAndSendSmsCode(String phoneNumber, String nationCode) { + String reidsCheckCodeKey = getRegisterClientRedisKey(phoneNumber); String value = stringRedisTemplate.boundValueOps(reidsCheckCodeKey).get(); if (StringUtils.isNotEmpty(value)) { throw new BadRequestException("SMS has been sent,Please check your messages or try again in 3 minutes."); @@ -81,7 +89,6 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { JSONObject sysConfig = sysConfigManager.getSysConfig(); ArrayList param = new ArrayList<>(); - String registerClientKey = getRegisterClientRedisKey(accountName); String registerClientCode = RandomStringUtils.random(6, true, true); param.add(registerClientCode); String expireMin = (String) sysConfig.getOrDefault("sms.verification.code.expire", 3); @@ -91,19 +98,20 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { } catch (Exception e) { throw new ServerErrorException("System Error"); } - stringRedisTemplate.boundValueOps(registerClientKey).set(registerClientCode, Long.parseLong(expireMin), TimeUnit.MINUTES); - stringRedisTemplate.boundValueOps(reidsCheckCodeKey).set(reidsCheckCodeKey, Long.parseLong(expireMin), TimeUnit.MINUTES); - + stringRedisTemplate.boundValueOps(getRegisterClientRedisKey(phoneNumber)).set(registerClientCode,Long.parseLong(expireMin), TimeUnit.MINUTES); return registerClientCode; } @Override - public void sendVerifyEmail(String address,JSONObject client) { + 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); Context ctx = new Context(); - ctx.setVariable("url", url); + 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<>(); to.add(address); @@ -113,18 +121,59 @@ public class SimpleClientApplyServiceImpl implements SimpleClientApplyService { sendMail.setContent(content); sendMail.setTags(tags); mailGunService.sendMail(sendMail); + } @Override - public String generateRegisterProcessKey(String accountName) { + public String checkOrGenerateRegisterProcessKey(String accountName, String codeKey) { + Assert.notNull(accountName,"accountName can't be null"); + if(StringUtils.isNotEmpty(codeKey)){ + String redisAccountName = stringRedisTemplate.boundValueOps(getRegisterClientProcessRedisKey(codeKey)).get(); + if(!accountName.equals(redisAccountName)){ + throw new BadRequestException("Data error,Please re-register for safety."); + }else { + return codeKey; + } + } + String key = RandomStringUtils.random(10, true, false); + stringRedisTemplate.boundValueOps(getRegisterClientProcessRedisKey(key)).set(accountName,1,TimeUnit.HOURS); + return key; + } - return null; + @Override + public void deleteRegisterProcessKey(String codeKey) { + stringRedisTemplate.delete(getRegisterClientProcessRedisKey(codeKey)); } - private String getRegisterClientRedisKey(String accountName){ - return REGISTER_CLIENT_PREFIX +accountName; + @Override + 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)){ + throw new BadRequestException("Data error"); + }else { + return codeKey; + } + } + String key = RandomStringUtils.random(10, true, false); + stringRedisTemplate.boundValueOps(getRegisterClientProcessRedisKey(key)).set(address,1,TimeUnit.HOURS); + return key; + + } + + @Override + public void deleteVerifyMailKey(String codeKey) { + stringRedisTemplate.delete(getVerifyMailRedisKey(codeKey)); + } + + private String getRegisterClientRedisKey(String phoneNumber){ + return REGISTER_CLIENT_PREFIX +phoneNumber; + } + private String getRegisterClientProcessRedisKey(String codeKey){ + return REGISTER_CLIENT_PROCESS_PREFIX+codeKey; } - private String getRegisterClientPhoneExpireRedisKey(String phoneNumber){ - return REGISTER_CLIENT_PHONE_EXPIRE_PREFIX +phoneNumber; + 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 88b1fd437..4d724308c 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 @@ -4,42 +4,58 @@ import au.com.royalpay.payment.manage.application.core.SimpleClientApplyService; import au.com.royalpay.payment.manage.merchants.beans.NewAccountBean; import au.com.royalpay.payment.tools.CommonConsts; import au.com.royalpay.payment.tools.http.HttpUtils; + import com.alibaba.fastjson.JSONObject; + +import org.springframework.stereotype.Controller; import org.springframework.validation.Errors; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; -@RestController +@Controller @RequestMapping("/register") public class SimpleClientApplyController { @Resource private SimpleClientApplyService simpleClientApplyService; + @Resource @RequestMapping(value = "/account/{codeKey}", method = RequestMethod.POST) + @ResponseBody public void registerAccount(@PathVariable String codeKey, @RequestBody @Valid NewAccountBean accountBean, Errors errors, HttpServletResponse response) throws Exception { HttpUtils.handleValidErrors(errors); - simpleClientApplyService.verifyRegisterSMSCode(codeKey,accountBean.getUsername()); + simpleClientApplyService.verifyRegisterSMSCode(codeKey,accountBean.getContactPhone()); JSONObject account = simpleClientApplyService.newAccount(accountBean); - simpleClientApplyService.deleteSMSVerifyCodeKey(codeKey); String statusKey = simpleClientApplyService.partnerSignIn(account); HttpUtils.setCookie(response, CommonConsts.CODE_KEY, statusKey); } -// @RequestMapping(value = "/account/mail/verify", method = RequestMethod.GET) -// public void registerAccount(@PathVariable String codeKey, @RequestBody @Valid NewAccountBean accountBean, Errors errors, HttpServletResponse response) -// throws Exception { -// HttpUtils.handleValidErrors(errors); -// simpleClientApplyService.verifyRegisterSMSCode(codeKey, accountBean.getContactPhone()); -// -// JSONObject account = simpleClientApplyService.newAccount(accountBean); -// simpleClientApplyService.deleteSMSVerifyCodeKey(codeKey); -// String statusKey = simpleClientApplyService.partnerSignIn(account); -// HttpUtils.setCookie(response, CommonConsts.CODE_KEY, statusKey); -// } + @RequestMapping(value = "/account/mail/{address}/verify/{codeKey}/jump", method = RequestMethod.GET) + public ModelAndView jumpVerifyMail(@PathVariable String codeKey, @PathVariable String address){ + simpleClientApplyService.checkOrGenerateVerifyMailKey(address,codeKey); + ModelAndView view = new ModelAndView("verify_mail"); + view.addObject("codeKey", codeKey); + return view; + } + + @RequestMapping(value = "/account/mail/{address}/verify/{codeKey}", method = RequestMethod.POST) + @ResponseBody + public void verifyMail(@PathVariable String codeKey, @PathVariable String address,@RequestBody JSONObject account){ + simpleClientApplyService.checkOrGenerateVerifyMailKey(address,codeKey); + + ModelAndView view = new ModelAndView("verify_mail"); + view.addObject("codeKey", codeKey); + + simpleClientApplyService.deleteVerifyMailKey(codeKey); + } } diff --git a/src/main/resources/templates/mail/verify_mail.html b/src/main/resources/templates/mail/verify_mail.html new file mode 100644 index 000000000..37ce3c593 --- /dev/null +++ b/src/main/resources/templates/mail/verify_mail.html @@ -0,0 +1,158 @@ + + + + + + RoyalPay | Reset Password + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +