短信平台message封装

service

import com.alibaba.fastjson.JSON;
import com.pacific.rsp.recruitment.model.po.rm.request.SMSParam;
import com.pacific.rsp.recruitment.util.HttpUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 短信平台message封装
 * Created by Administrator on 2018/7/26.
 * @author  xxx
 */
@Service
public class SMSService {
    private static final Logger logger = LogManager.getLogger();
    private static final String HttpUtilStr = "http://10.55.5.34:8080/api/sys/mess/";

    //发送并保存短信
    private static final String HttpUtilSetStr = "http://10.55.5.34:8080/api/sys/messinfo/";
    /**
     * 短信发送
     *
     * @param param
     * @return
     */
    public String sendTextMessage(SMSParam param) throws IOException, ClassNotFoundException, JSONException {
        //状态码 是-5 代表 直接挂
        String restStrSendMessage = "-5";
        if (param != null && param.getSMSId() != null & !param.getSMSId().equals("")) {
            //发送请求获得当前短信模板对应的模板内容
            String restStr = HttpUtil.sendPost(HttpUtilStr + "getListByMessageTemplete", JSON.toJSONString(param));
            // JSONObject obj = JSONObject.fromObject(restStr);
            //有数据回传回来
            if (restStr != null) {
                //然后用Iterator迭代器遍历取值,建议用反射机制解析到封装好的对象中
                JSONObject jsonObject = new JSONObject(restStr);
                System.out.print("jsonObject"+jsonObject);
                if (jsonObject.getJSONArray("data").length()>0){ //说明存在一条数据
                    //获取模板内容字符串
                    String contentStr = jsonObject.getJSONArray("data").getJSONObject(0).getString("content");
                    System.out.print("contentStr" + contentStr);
                    List<String> listContent = new ArrayList<String>();
                    //拼接短信模板
                    String temporaryStr="";
                    if (contentStr != null && !contentStr.equals("")) {
                        for (int i = 0; i < param.getRecipientPhoneNumber().size(); i++) {
                            temporaryStr="";
                            temporaryStr = contentStr.replace("{name}", param.getRecipientName().get(i));
                            System.out.print("getRecipientName:"+param.getRecipientName().get(i));
                            temporaryStr = temporaryStr.replace("{time}", param.getNotificationTime().get(i));
                            temporaryStr = temporaryStr.replace("{address}", param.getNotificationAddress().get(i));
                            listContent.add(temporaryStr);
                        }
                        param.setContent(listContent);
                    }
                    //拼接完成就可以直接发送了啊
                    if (param.getContent().size() > 0) {
                        restStrSendMessage = HttpUtil.sendPost(HttpUtilSetStr + "setListBySendMessageInfo", JSON.toJSONString(param));
                    }
                } else  {
                    //-3 代表不存在或者请求短信模板失败检查短信模板id
                    restStrSendMessage="-3";
                }
            } else {
                restStrSendMessage="-3";
            }
        }
      return   restStrSendMessage;
    }
}

Controller

import com.alibaba.fastjson.JSON;
import com.pacific.rsp.recruitment.model.po.rm.request.SMSParam;
import com.pacific.rsp.recruitment.service.SMSService;
import com.pacific.rsp.recruitment.util.JsonResult;
import com.pacific.rsp.recruitment.web.controller.BaseController;
import io.swagger.annotations.ApiOperation;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * 短信接收接口
 * Created by Administrator on 2018/7/26.
 */
@RestController
@RequestMapping("api/rm/sms")
public class SMSController extends BaseController {
    @Autowired
    SMSService smsService;
    @ApiOperation(value = "短信发送接口",notes = "短信发送接口")
    @RequestMapping(value = "/sendTextMessage",method = RequestMethod.POST)
    public JsonResult sendTextMessage(@RequestBody SMSParam param) throws ParseException, IOException, ClassNotFoundException, JSONException {
        String resultStr =  smsService.sendTextMessage(param);
        logger.info("class list:{}", JSON.toJSON(resultStr)); //resultStr
        Map<String,Object> resultMap = new HashMap<>(2);
        resultMap.put("resultStr", resultStr);
        return JsonResult.getResult(resultStr);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值