天天看點

短信發送

package com.chinasoft.biz.applicant.controller;

import java.io.IOException;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;

import com.chinasoft.biz.applicant.service.SendMessageService;

import com.google.gson.Gson;

import com.google.gson.GsonBuilder;

@Controller

@RequestMapping("/sendMessage")

public class SendMessageController {

@Resource

SendMessageService sendMessageServer;

Gson gson = new GsonBuilder().create(); 

@RequestMapping(value ="/send",method = RequestMethod.POST)

@ResponseBody

public void saveRegister(String phoneList ,String sendContent, HttpServletResponse response )

{

String[] result = sendMessageServer.sendMessage(phoneList,sendContent);

try {

response.getWriter().print(gson.toJson(result));

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

package com.chinasoft.biz.applicant.service;

public interface SendMessageService {

public String[]  sendMessage(String phoneList ,String sendContent);

}

package com.chinasoft.biz.applicant.service.impl;

import java.util.Map;

import org.springframework.stereotype.Service;

import com.chinasoft.biz.applicant.service.SendMessageService;

import com.chinasoft.util.MessageUtil;

@Service("sendMessageServer")

public class SendMessageServiceImpl implements SendMessageService {

@Override

public String[] sendMessage(String phoneList, String sendContent) {

// TODO Auto-generated method stub

Map<String, String>  map = MessageUtil.sendMessage(phoneList, sendContent);

String[] result = new String[]{map.get("result"),map.get("message")};

return result;

}

}

package com.chinasoft.util;

import java.io.InputStream;

import java.util.Map;

import java.util.Properties;

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMNamespace;

import org.apache.axis2.AxisFault;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.client.ServiceClient;

import com.google.common.collect.Maps;

public class MessageUtil {

    public static String nameSpace="http://tempuri.org/";

    public static String url = "http://www.chinaweimei.com/smsapi/Service.asmx?wsdl";

    public static String getBalanceName="GetBalance";

    public static String sendSMSName="SendSMS";

    public static Map<String, String> sendMessage(String phones, String contents) { 

          Map<String, String> resultMap = Maps.newHashMap();

          try {  

            Options options = new Options();  

            // 指定調用WebService的URL  

            EndpointReference targetEPR = new EndpointReference(url);  

            options.setTo(targetEPR);  

            options.setAction(nameSpace + sendSMSName);  

            ServiceClient sender = new ServiceClient();  

            sender.setOptions(options);  

            OMFactory fac = OMAbstractFactory.getOMFactory();  

            String tns = nameSpace;

            // 命名空間,有時命名空間不增加沒事,不過最好加上,因為有時有事,你懂的  

            OMNamespace omNs = fac.createOMNamespace(tns, "");  

            OMElement method = fac.createOMElement(sendSMSName, omNs);  

            OMElement user = fac.createOMElement("user", omNs);

            OMElement pass = fac.createOMElement("pass", omNs);

            OMElement moblie = fac.createOMElement("moblie", omNs);

            OMElement content = fac.createOMElement("content", omNs);

            String userName = getProValueFromName("sysTemConfig.properties", "sendMessage.user");

            String userPass = getProValueFromName("sysTemConfig.properties", "sendMessage.pass");

            user.setText(userName);

            pass.setText(userPass);

            moblie.setText(phones);

            content.setText(contents);

            method.addChild(user);

            method.addChild(pass);  

            method.addChild(moblie);  

            method.addChild(content);  

            method.build();  

            OMElement resultOME = sender.sendReceive(method);  

            String s = resultOME.getFirstElement().getText();

//            String[] result = s.split(new String("\\|"));

            resultMap.put("result", s.substring(0, 1));

            resultMap.put("message", s.substring(2, s.length()));

          } catch (AxisFault axisFault) {  

            axisFault.printStackTrace(); 

            resultMap.put("result", "0");

            resultMap.put("message", "短信發送異常");

          }  

          return resultMap;

    }  

    public static  Map<String, String> getMessageCount() {  

        Map<String, String> resultMap = Maps.newHashMap();

        try {  

          Options options = new Options();  

          // 指定調用WebService的URL  

          EndpointReference targetEPR = new EndpointReference(url);  

          options.setTo(targetEPR);  

          options.setAction(nameSpace + getBalanceName);  

          ServiceClient sender = new ServiceClient();  

          sender.setOptions(options);  

          OMFactory fac = OMAbstractFactory.getOMFactory();  

          String tns = nameSpace;

          // 命名空間,有時命名空間不增加沒事,不過最好加上,因為有時有事,你懂的  

          OMNamespace omNs = fac.createOMNamespace(tns, "");  

          OMElement method = fac.createOMElement(getBalanceName, omNs);  

          OMElement user = fac.createOMElement("user", omNs);

          OMElement pass = fac.createOMElement("pass", omNs);

          String userName = getProValueFromName("sysTemConfig.properties", "sendMessage.user");

          String userPass = getProValueFromName("sysTemConfig.properties", "sendMessage.pass");

          user.setText(userName);

          pass.setText(userPass);

          method.addChild(user);

          method.addChild(pass);  

          method.build();  

          OMElement resultOME = sender.sendReceive(method);  

          String s = resultOME.getFirstElement().getText();

//          String[] result = s.split("\\|");

          resultMap.put("result", s.substring(0, 1));

          resultMap.put("message", s.substring(2, s.length()));

        } catch (AxisFault axisFault) {  

          axisFault.printStackTrace(); 

          resultMap.put("result", "0");

          resultMap.put("message", "短信發送異常");

        }

        return resultMap;

     }

    @SuppressWarnings("static-access")

    public static String getProValueFromName(String fileName,String rootName){

        String key = "";

        try {

            InputStream inStream = MessageUtil.class.getClassLoader().getResourceAsStream(fileName);

            Properties prop = new Properties();  

            prop.load(inStream);  

            key = prop.getProperty(rootName);

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } 

        return key;

    }

    public static void main(String[] args) {

//         System.out.println(getMessageCount());

         System.out.println(sendMessage("131221", "招聘通知:同學您好,中軟國際重慶郵電大學專場招聘行程安排已發送到您網申預留郵箱,感謝您的關注。我司将于10月20日 本周五晚19:00于重郵2216教室開展招聘宣講會,歡迎參加。"));

    }

}

//發送短信

function sendMessage()

{

var ids = $("#new_candiadates_answer").jqGrid('getGridParam','selarrrow');

var sendUsers = [];

var phoneList = "";

$.each(ids, function(index, id){

var rowData = $("#new_candiadates_answer").jqGrid("getRowData", id);

var sendUser = {};

sendUser.name = rowData.examineName;

sendUser.phone = rowData.examineAccount;

phoneList += rowData.phone+",";

sendUsers.push(sendUser);

});

var phones = phoneList.substr(0, phoneList.length-1);

var html = "";

html += "<div><div style='max-height:300px;width: 400px;overflow:auto;'>" +

"<table class='userInfo table' cellspacing='0' cellpadding='0' style='width: 100%;color:#666;font-size: 14px;'><tr><td>姓名</td>&nbsp;&nbsp;&nbsp;<td>手機号碼</td></tr>" ;

$.each(sendUsers, function(i, sendUser){

html +="<tr><td>"+sendUser.name+"</td>&nbsp;&nbsp;&nbsp;<td>"+sendUser.phone+"</td></tr>";

});

html += "<tr><td>總條數:</td><td>"+sendUsers.length+"</td></tr>";

html += "</table></div>";

html += "<div>";

html += "<textarea  type='text' id= 'sendText' class='userInfoText'  maxlength='200' placeholder='請輸入發送資訊内容,限200以内!' tabindex='1'" +

" οnblur='checkText()' οnfοcus='checktips()' ></textarea><span id='sendTextError' style='color: red'></span>";

html += "</div>";

html += "</div>";

if(sendUsers.length==0){

zeroModal.error("您沒有選擇發送消息的人,請重新選擇");

return false;

}

$(html).dialog({

title : "使用者資訊顯示",

modal: true,

closeOnClickOutside : false,

clickDefaultButtonOnEnter: false,

buttons : {

"确定" : function() {

if(checkText()){

$.ajax({          

        url: basePath + 'sendMessage/send?random=' + Math.random(),

        type: "POST",  

//         dataType: "json",  

        data: {

         "phoneList" : phones.toString(),

         "sendContent":$("#sendText").val().toString()

        },  

        success: function (data) {

         data = JSON.parse(data);

         if(data[0]==1){

         zeroModal.success("短信已成功發送");

         CommUpdateSengMsg(ids,"1");

         }else if(data[0]==0){

         zeroModal.error(data[1]);

         }

     }

    });

}else {

return false;

}

},

"取消" : function() {

$(this).dialog("close"); 

}

}

});

}

繼續閱讀