|
|
@@ -0,0 +1,106 @@
|
|
|
+package cn.seecoder.seecoderportalserver.service;
|
|
|
+
|
|
|
+
|
|
|
+import cn.seecoder.seecoderportalserver.config.property.AliProperties;
|
|
|
+import cn.seecoder.seecoderportalserver.domain.PhoneVerification;
|
|
|
+import cn.seecoder.seecoderportalserver.repository.PhoneVerificationRepository;
|
|
|
+import cn.seecoder.seecoderportalserver.utility.RandomString;
|
|
|
+import com.aliyuncs.CommonRequest;
|
|
|
+import com.aliyuncs.CommonResponse;
|
|
|
+import com.aliyuncs.DefaultAcsClient;
|
|
|
+import com.aliyuncs.IAcsClient;
|
|
|
+import com.aliyuncs.exceptions.ClientException;
|
|
|
+import com.aliyuncs.http.MethodType;
|
|
|
+import com.aliyuncs.profile.DefaultProfile;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.validation.constraints.NotBlank;
|
|
|
+import java.time.OffsetDateTime;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+import static cn.seecoder.seecoderportalserver.utility.RandomString.DIGITS;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author Kunduin
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class AliService {
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(AliService.class);
|
|
|
+
|
|
|
+ private final RandomString randomString = new RandomString(6, DIGITS);
|
|
|
+ private final AliProperties.Profile aliProfile;
|
|
|
+ private final IAcsClient client;
|
|
|
+ private final PhoneVerificationRepository phoneVerificationRepository;
|
|
|
+
|
|
|
+ public AliService(AliProperties aliProperties, PhoneVerificationRepository phoneVerificationRepository) {
|
|
|
+ this.aliProfile = aliProperties.getProfile();
|
|
|
+ this.phoneVerificationRepository = phoneVerificationRepository;
|
|
|
+ DefaultProfile profile = DefaultProfile.getProfile(
|
|
|
+ aliProfile.getRegionId(),
|
|
|
+ aliProfile.getAccessKeyId(),
|
|
|
+ aliProfile.getAccessKeySecret());
|
|
|
+ client = new DefaultAcsClient(profile);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Async
|
|
|
+ public void sendMessage(String phone) {
|
|
|
+ sendMessage(phone, randomString.nextString());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Async
|
|
|
+ @Transactional(rollbackFor = RuntimeException.class)
|
|
|
+ public void sendMessage(@NotBlank String phone, @NotBlank String code) {
|
|
|
+ PhoneVerification phoneVerification = phoneVerificationRepository.findByPhone(phone)
|
|
|
+ .orElseGet(() -> {
|
|
|
+ PhoneVerification nextPhoneVerification = new PhoneVerification();
|
|
|
+ nextPhoneVerification.setPhone(phone);
|
|
|
+ return nextPhoneVerification;
|
|
|
+ });
|
|
|
+ phoneVerification.setCode(code);
|
|
|
+ phoneVerificationRepository.save(phoneVerification);
|
|
|
+
|
|
|
+ sendSms(phone, code);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = RuntimeException.class)
|
|
|
+ public boolean checkPhoneMessage(@NotBlank String phone, @NotBlank String code) {
|
|
|
+ Optional<PhoneVerification> optionalPhoneVerification = phoneVerificationRepository.findByPhone(phone);
|
|
|
+ log.debug("Check phone[{}] {}", phone, optionalPhoneVerification);
|
|
|
+ return optionalPhoneVerification
|
|
|
+ .map(phoneVerification ->
|
|
|
+ phoneVerification.getCode().toLowerCase().equals(code.toLowerCase()))
|
|
|
+ .orElse(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Scheduled(cron = "0 0 4 ? * *")
|
|
|
+ @Transactional(rollbackFor = RuntimeException.class)
|
|
|
+ public void clearVerification() {
|
|
|
+ OffsetDateTime yesterday = OffsetDateTime.now().minusDays(1);
|
|
|
+ phoneVerificationRepository.deleteByCodeTimeBefore(yesterday);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void sendSms(String phone, String code) {
|
|
|
+ CommonRequest request = new CommonRequest();
|
|
|
+ request.setSysMethod(MethodType.POST);
|
|
|
+ request.setSysDomain(aliProfile.getSysDomain());
|
|
|
+ request.setSysVersion(aliProfile.getSysVersion());
|
|
|
+ request.setSysAction("SendSms");
|
|
|
+ request.putQueryParameter("RegionId", aliProfile.getRegionId());
|
|
|
+ request.putQueryParameter("PhoneNumbers", phone);
|
|
|
+ request.putQueryParameter("SignName", "seeccoder");
|
|
|
+ request.putQueryParameter("TemplateCode", "SMS_181555598");
|
|
|
+ request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
|
|
|
+ try {
|
|
|
+ CommonResponse response = client.getCommonResponse(request);
|
|
|
+ log.info(response.getData());
|
|
|
+ } catch (ClientException e) {
|
|
|
+ log.error("Send sms error", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|