Java Code Examples for com.aliyuncs.IAcsClient#getCommonResponse()

The following examples show how to use com.aliyuncs.IAcsClient#getCommonResponse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NoticeService.java    From ticket with GNU General Public License v3.0 5 votes vote down vote up
public void send(NoticeModel noticeModel) {

        try {
            Gson gson = new Gson();
            Map<String, String> map = new HashMap<>();
            map.put("name", noticeModel.getName());
            map.put("username", noticeModel.getUserName());
            map.put("password", noticeModel.getPassword());
            map.put("orderId", noticeModel.getOrderId());

            DefaultProfile profile = DefaultProfile.getProfile("default", noticeConfig.getAccessKeyId(), noticeConfig.getAccessSecret());
            IAcsClient client = new DefaultAcsClient(profile);

            CommonRequest request = new CommonRequest();
            request.setProtocol(ProtocolType.HTTPS);
            request.setMethod(MethodType.GET);
            request.setDomain(apiConfig.getNotice());
            request.setVersion("2017-05-25");
            request.setAction("SendSms");
            request.putQueryParameter("PhoneNumbers", noticeModel.getPhoneNumber());
            request.putQueryParameter("SignName", noticeConfig.getSignName());
            request.putQueryParameter("TemplateCode", noticeConfig.getTemplateCode());
            request.putQueryParameter("TemplateParam", gson.toJson(map));
            CommonResponse response = client.getCommonResponse(request);
            map = gson.fromJson(response.getData(), Map.class);
            if (map.get("Code").equals("OK")) {
                log.debug("短信通知通知完成{}!", noticeModel.getPhoneNumber());
            } else {
                log.error("短信通知失败:" + map);
            }
        } catch (Exception e) {
            log.error("短信通知失败:" + noticeModel, e);
        }

    }
 
Example 2
Source File: SmsService.java    From spring-microservice-exam with MIT License 5 votes vote down vote up
/**
 * 发送短信
 *
 * @param smsDto smsDto
 * @return SmsResponse
 * @author tangyi
 * @date 2019/06/22 13:28
 */
public SmsResponse sendSms(SmsDto smsDto) {
    DefaultProfile profile = DefaultProfile.getProfile(smsProperties.getRegionId(), smsProperties.getAppKey(), smsProperties.getAppSecret());
    IAcsClient client = new DefaultAcsClient(profile);
    CommonRequest request = new CommonRequest();
    request.setMethod(MethodType.POST);
    request.setDomain(smsProperties.getDomain());
    request.putQueryParameter("RegionId", smsProperties.getRegionId());
    request.putQueryParameter("PhoneNumbers", smsDto.getReceiver());
    request.putQueryParameter("SignName", smsProperties.getSignName());
    request.putQueryParameter("TemplateCode", smsProperties.getTemplateCode());
    request.putQueryParameter("TemplateParam", smsDto.getContent());
    request.setVersion(smsProperties.getVersion());
    request.setAction(smsProperties.getAction());
    try {
        CommonResponse response = client.getCommonResponse(request);
        log.info("response: {}", response.getData());
        if (response.getHttpStatus() != 200)
            throw new CommonException(response.getData());
        SmsResponse smsResponse = JsonMapper.getInstance().fromJson(response.getData(), SmsResponse.class);
        if (smsResponse == null)
            throw new CommonException("Parse response error");
        if (!"OK".equals(smsResponse.getCode()))
            throw new CommonException(smsResponse.getMessage());
        return smsResponse;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new CommonException("Send message failed: " + e.getMessage());
    }
}
 
Example 3
Source File: AliyunSmsSenderImpl.java    From open-cloud with MIT License 5 votes vote down vote up
@Override
public Boolean send(SmsMessage parameter) {
    boolean result = false;
    try {
        // 地域ID
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",
                accessKeyId,
                accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", parameter.getPhoneNum());
        request.putQueryParameter("SignName", parameter.getSignName());
        request.putQueryParameter("TemplateCode", parameter.getTplCode());
        request.putQueryParameter("TemplateParam", parameter.getTplParams());
        CommonResponse response = client.getCommonResponse(request);
        System.out.println(response.toString());
        JSONObject json = JSONObject.parseObject(response.getData());
        result = OK.equalsIgnoreCase(json.getString(CODE));
        log.info("result:{}", response.getData());
    } catch (Exception e) {
        log.error("发送短信失败:{}", e.getMessage(), e);
    }
    return result;
}
 
Example 4
Source File: DefaultAliSmsTemplate.java    From fast-family-master with Apache License 2.0 5 votes vote down vote up
@Override
public void sendSms(AliSmsEntity aliSmsEntity) {
    final IAcsClient acsClient = this.getAcsClient();
    final CommonRequest request = this.getAliSmsRequest(aliSmsEntity);
    final CommonResponse response;
    try {
        response = acsClient.getCommonResponse(request);
        this.parseResponse(response);
    } catch (ClientException | IOException e) {
        throw new AliSmsException(e);
    } finally {
        acsClient.shutdown();
    }
}
 
Example 5
Source File: SmsOtpAuthnAliyun.java    From MaxKey with Apache License 2.0 5 votes vote down vote up
@Override
public boolean produce(UserInfo userInfo) {
    // 手机号
    String mobile = userInfo.getMobile();
    if (mobile != null && !mobile.equals("")) {
        try {
            DefaultProfile profile = DefaultProfile.getProfile(
                                "cn-hangzhou", accessKeyId, accessSecret);
            IAcsClient client = new DefaultAcsClient(profile);

            String token = this.genToken(userInfo);
            CommonRequest request = new CommonRequest();
            request.setSysMethod(MethodType.POST);
            request.setSysDomain("dysmsapi.aliyuncs.com");
            request.setSysVersion("2017-05-25");
            request.setSysAction("SendSms");
            request.putQueryParameter("RegionId", "cn-hangzhou");
            request.putQueryParameter("PhoneNumbers", mobile);
            request.putQueryParameter("SignName", signName);
            request.putQueryParameter("TemplateCode", templateCode);
            request.putQueryParameter("TemplateParam", "{\"code\":\"" + token + "\"}");
            CommonResponse response = client.getCommonResponse(request);
            logger.debug("responseString " + response.getData());
            //成功返回
            if (response.getData().indexOf("OK") > -1) {
                this.optTokenStore.store(
                        userInfo, 
                        token, 
                        userInfo.getMobile(), 
                        OptTypes.SMS);
                return true;
            }
        } catch  (Exception e) {
            logger.error(" produce code error ", e);
        } 
    }
    return false;
}
 
Example 6
Source File: AliYunSmsUtils.java    From pre with GNU General Public License v3.0 4 votes vote down vote up
public static SmsResponse sendSms(String phone, String signName, String templateCode) {
    SmsResponse smsResponse = new SmsResponse();
    //初始化ascClient
    DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
    IAcsClient client = new DefaultAcsClient(profile);
    //组装请求对象
    CommonRequest request = new CommonRequest();
    /**
     * 使用post提交
     */
    request.setMethod(MethodType.POST);
    //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
    //request.setOutId("1454");
    request.setDomain("dysmsapi.aliyuncs.com");
    request.setVersion("2017-05-25");
    request.setAction("SendSms");
    request.putQueryParameter("RegionId", "cn-hangzhou");
    //待发送的手机号
    request.putQueryParameter("PhoneNumbers", phone);
    //短信签名
    request.putQueryParameter("SignName", signName);

    if ("登录".equals(templateCode)){
        templateCode = "SMS_172887387";
    } else if ("注册".equals(templateCode)){
        templateCode = "SMS_172887416";
    }
    //短信模板ID
    request.putQueryParameter("TemplateCode", templateCode);
    //验证码
    /**
     * 可选:模板中的变量替换JSON串,
     */
    String random = RandomStringUtils.random(6, false, true);
    request.putQueryParameter("TemplateParam", "{\"code\":\"" + random + "\"}");
    request.putQueryParameter("OutId", random);

    try {
        CommonResponse response = client.getCommonResponse(request);
        String responseData = response.getData();
        JSONObject jsonObject = JSONObject.parseObject(responseData);
        String code = (String) jsonObject.get("Code");
        if (StrUtil.isNotEmpty(code) && "OK".equals(code)) {
            //请求成功
            log.info(phone + ",发送短信成功");
            smsResponse.setSmsCode(random);
            smsResponse.setSmsPhone(phone);
            smsResponse.setSmsTime(System.nanoTime() + "");
            return smsResponse;
        } else {
            log.error(phone + ",发送短信失败:{}", jsonObject.get("Message"));
        }
    } catch (ClientException e) {
        log.error(phone + ",发送短信失败:{}", e.getMessage());
    }

    return null;
}