接收訊息優化--微信企業號
阿新 • • 發佈:2018-11-22
微信企業號接收訊息處理優化
優化點:
- 1.所有接收訊息的返回全部呼叫主動傳送訊息,因為接收訊息的返回需要在5s內,且還需要加密,我對於自己的網路等不太放心,所以使用穩妥的辦法,只返回成功標示200;
- 2.更新祕鑰access_token,本來是想在web工程中使用子程序定時更新,但是程式碼出現異常了,所以用了簡單的辦法,若返回碼錯誤型別是access_token的過期或者異常,就去更新且寫入到配置檔案中;
- 3.將固定引數寫入到配置檔案中;
- 4.整個專案war
廢話太多,上程式碼
接收訊息呼叫主動傳送
PrintWriter out = response.getWriter ();
String sRespData = "";
if (("text").equals(Content)) {
sRespData = "<xml><ToUserName><![CDATA[" + FromUserName
+ "]]></ToUserName><FromUserName><![CDATA[wx5823bf96d3bd56c7]]></FromUserName><CreateTime>"
+ System.currentTimeMillis() + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA["
+ "response news -- test,type:text" + "]]></Content><MsgId>" + MsgId + "</MsgId><AgentID>"
+ sAgentId + "</AgentID></xml>" ;
} else if (("image").equals(Content)) {
sRespData = "200";
/**
* 在這裡呼叫主動傳送資訊,傳送除了文字資訊之外的資料
*/
String access_token = cUtils.getPorp("access_token");
System.out.println("access_token:" + access_token);
String result = MediaUploadUtil.uploadMedia(cUtils.getPorp("access_token"), "image",
"http://img15.3lian.com/2015/c1/83/d/29.jpg");
System.out.println("result---media_id:" + result);
jsparam2 = WxworkNewsType.imageType(WxworkUtil.getReturn(result).get("media_id"), FromUserName, "",
Integer.parseInt(cUtils.getPorp("AgentId")));
} else if (("active-text").equals(Content)) {
sRespData = "200";
System.out.println(cUtils.getPorp("access_token"));
jsparam2 = WxworkNewsType.textType(
"主動測試資訊:\n xxxx連線資訊<a href=\"https://mp.weixin.qq.com/debug/wxadoc/dev/framework/MINA.html \">微信小程式pai文件</a>\n <a href=\"http://www.importnew.com/18769.html \">InportNew</a>\n",
FromUserName, "", Integer.parseInt(cUtils.getPorp("AgentId")));
} else if (("active-news").equals(Content)) {
sRespData = "200";
jsparam2 = WxworkNewsType.newsType(FromUserName, "", Integer.parseInt(cUtils.getPorp("AgentId")),
"請求非同步傳送圖文資訊", "主動請求,設定成功,主動傳送圖文資訊",
"href=\"https://mp.weixin.qq.com/debug/wxadoc/dev/framework/MINA.html",
"http://img15.3lian.com/2015/c1/83/d/29.jpg", "更多資訊");
} else {
sRespData = "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName><FromUserName><![CDATA["
+ sCorpID + "]]></FromUserName><CreateTime>" + System.currentTimeMillis()
+ "</CreateTime><MsgType><![CDATA[" + "text" + "]]></MsgType><Content><![CDATA[" + Content
+ "]]></Content><MsgId>" + MsgId + "</MsgId><AgentID>" + sAgentId + "</AgentID></xml>";
}
// String sRespData = ToXmlString("response news -- test ",ToUserName);
try {
String sEncryptMsg = wxcpt.EncryptMsg(sRespData, sReqTimeStamp, sReqNonce);
// 加密成功
System.out.println("相應訊息加密結果--after encrypt sEncrytMsg: " + sEncryptMsg);
out.println(sEncryptMsg);
if (null != jsparam2 && sRespData == "200") {
String sendruslt = WxworkUtil.sendData(cUtils.getPorp("access_token"), jsparam2).toString();
System.out.println("主動傳送訊息返回結果---第一次執行:" + sendruslt);
String errcode = WxworkUtil.getRuslt(sendruslt).get("errcode");
if (!"0".equals(errcode)) {
// 當傳送小時失敗時,根據返回碼的access_token異常,更新access_toke的值,再次重新發送
// System.out.println("返回結果:"+WxworkUtil.getRuslt(sendruslt).toString());
// System.out.println("判斷返回結果:"+"ok".equals(WxworkUtil.getRuslt(sendruslt).get("errmsg"))
// );
switch (errcode) {
case "40014":
case "42001":
System.out.println("access_token,不合法或者過期,正在更新");
cUtils.setPorp("access_token", WxworkUtil
.GetForAT(cUtils.getPorp("CorpID"), cUtils.getPorp("corpsecret")).get("access_token"));
if (("image").equals(Content)) {
String result = MediaUploadUtil.uploadMedia(cUtils.getPorp("access_token"), "image",
"http://img15.3lian.com/2015/c1/83/d/29.jpg");
jsparam2 = WxworkNewsType.imageType(WxworkUtil.getReturn(result).get("media_id"),
FromUserName, "", Integer.parseInt(cUtils.getPorp("AgentId")));
}
sendruslt = WxworkUtil.sendData(cUtils.getPorp("access_token"), jsparam2).toString();
System.out.println("主動傳送訊息返回結果---二次執行:" + sendruslt);
break;
default:
System.out.println("返回碼錯誤,根據返回碼,查詢問題:微信企業號文件--全域性錯誤碼:" + errcode);
;
}
} else if ("pwu-null" != WxworkUtil.getRuslt(sendruslt).get("invaliduser")) {
System.out.println(
"主動傳送訊息的請求,異常需要核對引數 invaliduser:" + WxworkUtil.getRuslt(sendruslt).get("invaliduser"));
} else if ("0".equals(errcode)) {
System.out.println("呼叫介面成功!");
}
}
} catch (Exception e) {
e.printStackTrace();
// 加密失敗
}
}
配置檔案工具類
package util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class ConfigUtils {
private Properties prop = new Properties();
private File file=null;
/**
* @param proName
*/
public ConfigUtils(String proName){
init(proName);
}
/**
* @param proName
*/
private void init(String proName){
//String dir=System.getProperty("user.dir")+"/resource/"+proName+".properties";
//GetResource.class.getClassLoader().getResource("xx/xx.txt").getPath();
String dir=ConfigUtils.class.getClassLoader().getResource(proName+".properties").getPath();
file = new File(dir);
try {
prop.load(new FileInputStream(file));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param key
* @return
*/
public String getPorp(String key){
return (String)prop.getProperty(key);
}
/**
* @param key
* @param val
*/
public void setPorp(String key,String val){
prop.setProperty(key, val);
FileOutputStream fos=null;
try {
fos = new FileOutputStream(file);
prop.store(fos, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fos!=null){
try {
fos.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
}