sendSMS.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * resources.getEntityManagerContainer() // 实体管理容器.
  3. * resources.getContext() //上下文根.
  4. * resources.getOrganization() //组织访问接口.
  5. * requestText //请求内容.
  6. * request //请求对象.
  7. */
  8. //////////////////////////////////// config ////////////////////////////////////////////////
  9. var Config = {
  10. //soap请求地址
  11. "soapUrl": "http://10.134.73.35:8080/haSms/SendSms", //接口地址
  12. "userName": "snrlmh", //用户名
  13. "password" : "snrlmh1904" //密码
  14. };
  15. var applications = resources.getContext().applications();
  16. //返回字符串转json对象
  17. function parseResp(resp) {
  18. if (!resp || resp === null) {
  19. return {
  20. "type": "error",
  21. message: "服务响应是null"
  22. }
  23. } else {
  24. var json = JSON.parse(resp.toString());
  25. return json;
  26. }
  27. }
  28. //webserivce请求对象组装
  29. function getSoapReqData( mobile, smsBody, appointTime) {
  30. var str = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sms="http://sms.ha.mocha.com/">'+
  31. ' <soapenv:Header/>'+
  32. ' <soapenv:Body>'+
  33. ' <sms:sendSms>'+
  34. ' <arg0>'+ Config.userName +'</arg0>'+ //登陆平台账号
  35. ' <arg1>'+ Config.password +'</arg1>'+ //登录密码
  36. ' <arg2>'+ mobile +'</arg2>'+ //要发送的目的手机号
  37. ' <arg3>'+ smsBody +'</arg3>'+ //短信内容
  38. ' <arg4>'+ (appointTime || null) +'</arg4>'+ //定时发送时间,如不需要可以传null
  39. ' </sms:sendSms>'+
  40. ' </soapenv:Body>'+
  41. '</soapenv:Envelope>'
  42. return str;
  43. }
  44. //解析xml
  45. // function loadXML(xmlString) {
  46. // var DocumentBuilderFactory = Java.type('javax.xml.parsers.DocumentBuilderFactory');
  47. // var StringReader = Java.type('java.io.StringReader');
  48. // var InputSource = Java.type('org.xml.sax.InputSource');
  49. // var dbf = DocumentBuilderFactory.newInstance();
  50. // var db = dbf.newDocumentBuilder();
  51. // var sr = new StringReader(xmlString);
  52. // var is = new InputSource(sr);
  53. // var doc = db.parse(is);
  54. // var root = doc.getDocumentElement();
  55. // return root;
  56. // }
  57. function getXmlText(root, tag) {
  58. var list = root.getElementsByTagName(tag);
  59. if (list == null) return null;
  60. var node = list.item(0);
  61. if (node == null) return null;
  62. return node.getTextContent();
  63. }
  64. function parseResult( xmlStr, requestJson ){
  65. try {
  66. var DocumentBuilderFactory = Java.type('javax.xml.parsers.DocumentBuilderFactory');
  67. var StringReader = Java.type('java.io.StringReader');
  68. var InputSource = Java.type('org.xml.sax.InputSource');
  69. var dbf = DocumentBuilderFactory.newInstance();
  70. var db = dbf.newDocumentBuilder();
  71. var sr = new StringReader(xmlStr);
  72. var is = new InputSource(sr);
  73. var document = db.parse(is);
  74. var root = document.getDocumentElement();
  75. // var faultcode = getXmlText(root, "faultcode");
  76. // var faultstring = getXmlText(root, "faultstring");
  77. //返回标识代表了平台是否成功接收到了信息。如返回标识为0,后面则跟着“R”和20位的唯一ID(sid),
  78. //如果返回标识为1,后面则跟着“R”和异常编码。
  79. //例如成功为“0R201101220945324SI000”,失败则为“1R001”,按照异常表查询为身份认证失败。
  80. var result = getXmlText(root, "soap:Body");
  81. var errorText = '';
  82. print( "sms返回结果:"+result )
  83. if( result.substr(0,1) === "0" ){ //成功
  84. requestJson.resultCode = "success";
  85. requestJson.result = "成功:" + result;
  86. return {
  87. "result" : "success",
  88. "description" : "返回结果:" + result
  89. };
  90. }else{ //失败
  91. var errorCode = result.substr(2,3);
  92. switch (errorCode) {
  93. case '100':
  94. errorText = '平台服务异常';
  95. break;
  96. case '001':
  97. errorText = '身份认证失败';
  98. break;
  99. case '002':
  100. errorText = '手机号不合法';
  101. break;
  102. case '003':
  103. errorText = '信息内容大小超过允许范围';
  104. break;
  105. case '009':
  106. errorText = '定时时间早于当前时间';
  107. break;
  108. case '011':
  109. errorText = '密码为空或者账号为空或手机号为空';
  110. break;
  111. default:
  112. // code
  113. }
  114. requestJson.resultCode = "error";
  115. requestJson.result = errorText + ":" + result;
  116. print( "错误信息:" + errorText );
  117. return {
  118. "result" : "error",
  119. "description" : "返回结果:" + result + " ,错误信息" + errorCode
  120. };
  121. }
  122. }catch(e){
  123. print("解析返回结果报错:");
  124. requestJson.resultCode = "run time error";
  125. requestJson.result = "解析返回结果报错:" + e.name + ": " + e.message;
  126. print( e.printStackTrace() );
  127. return {
  128. "result" : "error",
  129. "description" : "解析返回结果报错:" + e.name + ": " + e.message
  130. }
  131. }
  132. }
  133. // <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  134. // <soap:Body>
  135. // <ns1:sendSmsResponse xmlns:ns1="http://sms.ha.mocha.com/">
  136. // <return>0R190521174853520SI000</return>
  137. // </ns1:sendSmsResponse>
  138. // </soap:Body>
  139. // </soap:Envelope>
  140. function sendRequest( xml, requestJson ){
  141. try{
  142. print("发起请求:"+xml);
  143. var ArrayList = Java.type('java.util.ArrayList');
  144. var heads = new ArrayList();
  145. var NameValuePair = Java.type('com.x.base.core.project.bean.NameValuePair');
  146. var p1 = new NameValuePair('Content-Type', 'text/xml; charset=utf-8');
  147. heads.add(p1);
  148. var HttpConnectionClass = Java.type('com.x.base.core.project.connection.HttpConnection');
  149. var resp = HttpConnectionClass.postAsString(Config.soapUrl, heads, xml);
  150. // resp = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
  151. // '<soap:Body>'+
  152. // '<ns1:sendSmsResponse xmlns:ns1="http://sms.ha.mocha.com/">'+
  153. // '<return>0R190521174853520SI000</return>'+
  154. // '</ns1:sendSmsResponse>'+
  155. // '</soap:Body>'+
  156. // '</soap:Envelope>'
  157. print( "SMS 返回:"+resp );
  158. return parseResult( resp, requestJson );
  159. }catch(e){
  160. print("发送请求出错:");
  161. requestJson.resultCode = "run time error";
  162. requestJson.result = "发送请求出错:" + e.name + ": " + e.message;
  163. print( e.printStackTrace() );
  164. return {
  165. "result" : "error",
  166. "description" : "发送请求出错:" + e.name + ": " + e.message
  167. }
  168. }
  169. }
  170. function saveLog( json ){
  171. var resp = applications.postQuery("x_query_assemble_surface","table/hrMarketSMSLog/row", JSON.stringify(json));
  172. var d = parseResp( resp );
  173. if( d && d.type == "error" ){
  174. print( "save sms log error :" + d.message );
  175. }
  176. }
  177. //接口调用参数
  178. // {
  179. // employeeCode : "处理的人员编码,比如合同到期人员、离岗学习到期人员、转岗到期人员",
  180. // person : "处理的人员名称,比如合同到期人员、离岗学习到期人员、转岗到期人员",
  181. // typeName : "类型,合同到期人员、离岗学习到期人员、转岗到期人员",
  182. // typeCode : "类型,contractNotice、levelToLearn、internalChange",
  183. // receiveMobile : "接收人员手机号",
  184. // receivePerson : "接收人员",
  185. // content : "短信内容",
  186. // resultCode : "短信发送结果编码",
  187. // result : "短信发送结果",
  188. // appointTime : "定时发送时间"
  189. // }
  190. function init(){
  191. var result ="";
  192. var responseText = "";
  193. var requestJson;
  194. try{
  195. print( "requestText="+requestText );
  196. requestJson = JSON.parse(requestText);
  197. print( "type of requestJson = " + typeof( requestJson ));
  198. if( typeof(requestJson) === "string" ){
  199. requestJson = JSON.parse(requestJson);
  200. }
  201. var mobile = requestJson.receiveMobile;
  202. var content = requestJson.content;
  203. var xml = getSoapReqData( mobile, content );
  204. result = sendRequest( xml, requestJson );
  205. }catch(e){
  206. e.printStackTrace();
  207. if( requestJson ){
  208. requestJson.resultCode = "run time error";
  209. requestJson.result = e.name + ": " + e.message;
  210. }
  211. result = {
  212. "result" : "error",
  213. "description" : e.name + ": " + e.message
  214. };
  215. }finally{
  216. if( requestJson ){
  217. saveLog( requestJson );
  218. }
  219. print("responseText="+JSON.stringify(result));
  220. return result;
  221. }
  222. }
  223. init();