2019-02-18 10:01:04 +08:00

121 lines
4.2 KiB
Java

package com.tenwa.lease.app.message;
import java.util.List;
import jbo.com.tenwa.lease.comm.LB_MESSAGE_CONFIG;
import jbo.com.tenwa.lease.comm.LB_SMS_NOTICE_TASKS;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;
import com.amarsoft.are.jbo.BizObject;
import com.amarsoft.are.jbo.BizObjectManager;
import com.amarsoft.are.jbo.JBOFactory;
import com.amarsoft.are.jbo.JBOTransaction;
import com.amarsoft.are.util.StringFunction;
import com.tenwa.lease.util.SendMessageUtil;
public class SmsController {
private String ids;
private Logger logger = Logger.getLogger(this.getClass());
public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
public String sendMessage(JBOTransaction tx){
try{
//将发送过来的参数按照@切分出来
String[] smsID = ids.split("@");
BizObjectManager bomLSNT = JBOFactory.getBizObjectManager(LB_SMS_NOTICE_TASKS.CLASS_NAME,tx);
for(String id:smsID){
//通过ID获取表中的BizObject对象
BizObject boLSNT = bomLSNT.createQuery("ID=:ID").setParameter("ID", id).getSingleResult(true);
String result = null;
try {
//通过HttpClient发送短信
// result = SendMessageUtil.sendMessageByHttpClient(boLSNT.getAttribute("PHONE_NUMBER").getString(), boLSNT.getAttribute("SMS_CONTENT").getString());
result = "{'status':101,'count':0,'list':''}";
} catch (Exception e) {
logger.error("短信发送任务执行失败。", e);
result = "发送失败,发送遇到异常:" + e.getMessage();
}
//使用Jsonlib解析返回结果
JSONObject object = JSONObject.fromObject(result);
String statuscode = "";
if(object != null){
statuscode = object.get("status").toString();
}
boLSNT.setAttributeValue("SMS_TYPE","IMMEDIATELY");
//判断返回的状态码
boLSNT.setAttributeValue("SEND_RESULT",SmsController.getResultCodeInfo(statuscode));
boLSNT.setAttributeValue("SEND_TIME",StringFunction.getTodayNow());
boLSNT.setAttributeValue("SEND_FLAG",1);
bomLSNT.saveObject(boLSNT);
}
}catch(Exception e){
e.printStackTrace();
return "ERROR";
}
return "SUCCESS";
}
public String selectBalance(JBOTransaction tx){
try {
//获取数据库中短信配置的相关参数
List<BizObject> messageConfigList = JBOFactory.getBizObjectManager(LB_MESSAGE_CONFIG.CLASS_NAME, tx).createQuery("1=1").getResultList(false);
BizObject messageConfig = messageConfigList.get(0);
//用户名
String sendUsername = messageConfig.getAttribute("SENDER_").getString();
//密码
String sendPassword = messageConfig.getAttribute("SENDER_PASSWORD_").getString();
HttpClient httpClient = new HttpClient();
//余额查询的接口url
String url = "http://api.app2e.com/getBalance.api.php?pwd="+sendPassword+"&username="+sendUsername;
GetMethod getMethod = new GetMethod(url);
int statusCode = httpClient.executeMethod(getMethod);
System.out.println("状态码:"+statusCode);
String result = "";
if (statusCode == HttpStatus.SC_OK) {
result = getMethod.getResponseBodyAsString();
}else{
result = statusCode + "";
}
//使用Jsonlib解析返回结果
JSONObject jsonObject=JSONObject.fromObject(result);
//取出剩余的余额
String balance=jsonObject.get("balance").toString();
getMethod.releaseConnection();
return balance;
} catch (Exception e) {
e.printStackTrace();
return "查询失败";
}
}
//用于判断返回结果中的状态码
public static String getResultCodeInfo(String code){
String codeMessage = "";
switch(code){
case "100": codeMessage = "全部成功"; break;
case "101": codeMessage = "参数错误"; break;
case "102": codeMessage = "号码错误"; break;
case "103": codeMessage = "当日余量不足"; break;
case "104": codeMessage = "请求超时"; break;
case "105": codeMessage = "用户余量不足"; break;
case "106": codeMessage = "非法用户"; break;
case "107": codeMessage = "提交号码超限"; break;
case "111": codeMessage = "签名不合法"; break;
case "120": codeMessage = "内容长度超长,请不要超过500个字"; break;
case "121": codeMessage = "内容中有屏蔽词"; break;
case "131": codeMessage = "IP非法"; break;
default : codeMessage = code; break;
}
return codeMessage;
}
}