短信接口实现



短信接口实体:
entity:endPoint(端口地址);accessKeyId(接入键ID);
accessKeySecret(接入密钥);topic(主题);signName(签名);isSend(是否发生)


发送短信接口并写入日志:
public SendResult send(SmsType smsType, String value,String phone){
        //-------------------------发送短信END----------------------------------//
        SendResult sendResult = sendType(smsType, value, phone);
        //-------------------------发送短信END----------------------------------//




        //-------------------------写日志START----------------------------------//
        SmsLogModel smsLogModel = null;
        smsLogModel = new SmsLogModel();
        smsLogModel.setId(UUID.randomUUID().toString());
        smsLogModel.setContent(value);
        smsLogModel.setCreateTime(new Date());
        smsLogModel.setPhone(phone);
        if(sendResult != null){
            smsLogModel.setSendStatus(1);
        }else{
            smsLogModel.setSendStatus(2);
        }
        smsLogMapper.insert(smsLogModel);
        //--------------------------写日志END---------------------------------//


        return sendResult;
    }
    短信编辑工具类:
     public static TopicMessage send(SmsType smsType, String value, String tel) {
        /**
         * Step 1. 获取主题引用
         */
        CloudAccount account = new CloudAccount(SmsConfig.ACCESS_KEYID, SmsConfig.ACCESS_KEYSECRET, SmsConfig.ENDPOINT);
        MNSClient client = account.getMNSClient();
        CloudTopic topic = client.getTopicRef(SmsConfig.TOPIC);
        /**
         * Step 2. 设置SMS消息体(必须)
         *
         * 注:目前暂时不支持消息内容为空,需要指定消息内容,不为空即可。
         */
        RawTopicMessage msg = new RawTopicMessage();
        msg.setMessageBody("sms-message");
        /**
         * Step 3. 生成SMS消息属性
         */
        MessageAttributes messageAttributes = new MessageAttributes();
        BatchSmsAttributes batchSmsAttributes = new BatchSmsAttributes();
        // 3.1 设置发送短信的签名(SMSSignName)
        batchSmsAttributes.setFreeSignName(SmsConfig.SIGN_NAME);
        // 3.2 设置发送短信使用的模板(SMSTempateCode)
        batchSmsAttributes.setTemplateCode(smsType.getTemplate_code());
        // 3.3 设置发送短信所使用的模板中参数对应的值(在短信模板中定义的,没有可以不用设置)
        BatchSmsAttributes.SmsReceiverParams smsReceiverParams = new BatchSmsAttributes.SmsReceiverParams();
        smsReceiverParams.setParam("code", value);
        // 3.4 增加接收短信的号码
        batchSmsAttributes.addSmsReceiver(tel, smsReceiverParams);
        messageAttributes.setBatchSmsAttributes(batchSmsAttributes);
        try {
            /**
             * Step 4. 发布SMS消息
             */
            TopicMessage ret = topic.publishMessage(msg, messageAttributes);
            System.out.println("MessageId: " + ret.getMessageId());
            System.out.println("MessageMD5: " + ret.getMessageBodyMD5());
            return ret;
        } catch (ServiceException se) {
            System.out.println(se.getErrorCode() + se.getRequestId());
            System.out.println(se.getMessage());
            se.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            client.close();
        }
        return null;
    }


    短信发送实现接口类:
    public SendResult sendType(SmsType smsType, String value, String phone) {
        TopicMessage topicMessage = SmsUtil.send(smsType,value,phone);
        return new SendResult();
    }

你可能感兴趣的:(短信接口实现)