| | |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.util.Collections; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | @Component |
| | | public class AliyunSmsCodeSender implements SmsCodeSender { |
| | |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void sendGuideAuditResult(String phone, String guideName, boolean approved) { |
| | | String templateCode = approved |
| | | ? properties.getGuideAuditApprovedTemplateCode() |
| | | : properties.getGuideAuditRejectedTemplateCode(); |
| | | if (!hasCommonConfiguration() || !StringUtils.hasText(templateCode)) { |
| | | LOGGER.warn("导游审核短信模板未配置,跳过发送,手机号:{},审核结果:{}", maskPhone(phone), approved); |
| | | return; |
| | | } |
| | | Map<String, String> parameters = new HashMap<String, String>(); |
| | | parameters.put("name", guideName == null ? "导游" : guideName); |
| | | try { |
| | | SendSmsResponse response = createClient().sendSms(new SendSmsRequest() |
| | | .setPhoneNumbers(phone) |
| | | .setSignName(properties.getSignName()) |
| | | .setTemplateCode(templateCode) |
| | | .setTemplateParam(objectMapper.writeValueAsString(parameters))); |
| | | ensureSuccess(phone, response == null ? null : response.getBody()); |
| | | } catch (BusinessException exception) { |
| | | throw exception; |
| | | } catch (Exception exception) { |
| | | LOGGER.error("调用阿里云导游审核短信服务失败,手机号:{}", maskPhone(phone), exception); |
| | | throw new BusinessException(502, "审核短信发送失败"); |
| | | } |
| | | } |
| | | |
| | | private Client createClient() throws Exception { |
| | | Config config = new Config() |
| | | .setAccessKeyId(properties.getAccessKeyId()) |
| | |
| | | } |
| | | |
| | | private void ensureConfigured() { |
| | | if (!StringUtils.hasText(properties.getAccessKeyId()) |
| | | || !StringUtils.hasText(properties.getAccessKeySecret()) |
| | | || !StringUtils.hasText(properties.getEndpoint()) |
| | | || !StringUtils.hasText(properties.getSignName()) |
| | | if (!hasCommonConfiguration() |
| | | || !StringUtils.hasText(properties.getLoginTemplateCode())) { |
| | | throw new BusinessException(503, "阿里云短信配置未完成"); |
| | | } |
| | | } |
| | | |
| | | private boolean hasCommonConfiguration() { |
| | | return StringUtils.hasText(properties.getAccessKeyId()) |
| | | && StringUtils.hasText(properties.getAccessKeySecret()) |
| | | && StringUtils.hasText(properties.getEndpoint()) |
| | | && StringUtils.hasText(properties.getSignName()); |
| | | } |
| | | |
| | | private String maskPhone(String phone) { |
| | | if (phone == null || phone.length() < 7) { |
| | | return "****"; |