| | |
| | | package org.yeshi.utils.wx;
|
| | |
|
| | | import java.io.File;
|
| | | import java.util.Map;
|
| | |
|
| | | import javax.servlet.http.HttpServletRequest;
|
| | |
|
| | | import org.yeshi.utils.HttpUtil;
|
| | |
|
| | | import net.sf.json.JSONObject;
|
| | |
|
| | | //小程序客服消息处理
|
| | | public class XCXKeFuUtil {
|
| | | public final static String TYPE_IMAGE = "image";
|
| | |
|
| | | public static JSONObject parseHuiHuaMsg(HttpServletRequest request, String xmlContent, String token,
|
| | | String encodingASEKey, String appId) {
|
| | | String msgSignature = request.getParameter("msg_signature");
|
| | | String timeStamp = request.getParameter("timestamp");
|
| | | String nonce = request.getParameter("nonce");
|
| | | JSONObject root = new JSONObject();
|
| | | Map<String, String> map = WXUtil.parseXML(xmlContent, msgSignature, timeStamp, nonce, token, encodingASEKey,
|
| | | appId);
|
| | |
|
| | | JSONObject data = new JSONObject();
|
| | | data.put("openid", map.get("FromUserName"));
|
| | | data.put("ghid", map.get("ToUserName"));
|
| | | data.put("sessionFrom", map.get("SessionFrom"));
|
| | | root.put("data", data);
|
| | | if ("user_enter_tempsession".equalsIgnoreCase(map.get("Event"))) {
|
| | | root.put("code", 0);// 进入会话事件
|
| | | } else
|
| | | root.put("code", 1);// 其他事件
|
| | | return root;
|
| | | }
|
| | |
|
| | | private static boolean sendMsg(String acessToken, JSONObject data) {
|
| | | String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + acessToken;
|
| | | String result = HttpUtil.post(url, data.toString());
|
| | | return true;
|
| | | }
|
| | |
|
| | | public static String uploadFile(String accessToken, String type, File f) {
|
| | | String url = String.format("https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s",
|
| | | accessToken, type);
|
| | | String result = HttpUtil.post(url, "media", f);
|
| | | JSONObject resultObject = JSONObject.fromObject(result);
|
| | | return resultObject.optString("media_id");
|
| | | }
|
| | |
|
| | | // 发送文字消息
|
| | | public static boolean sendTextMsg(String acessToken, String openId, String text) {
|
| | |
|
| | | JSONObject data = new JSONObject();
|
| | | data.put("touser", openId);
|
| | | data.put("msgtype", "text");
|
| | | JSONObject textObject = new JSONObject();
|
| | | textObject.put("content", text);
|
| | | data.put("text", textObject);
|
| | | return sendMsg(acessToken, data);
|
| | | }
|
| | |
|
| | | // 发送图片消息
|
| | | public static boolean sendImageMsg(String acessToken, String openId, String mediaId) {
|
| | | JSONObject data = new JSONObject();
|
| | | data.put("touser", openId);
|
| | | data.put("msgtype", "image");
|
| | | JSONObject imageObject = new JSONObject();
|
| | | imageObject.put("media_id", mediaId);
|
| | | data.put("image", imageObject);
|
| | | return sendMsg(acessToken, data);
|
| | | }
|
| | |
|
| | | // 发送图文消息
|
| | | public static boolean sendTextImageMsg(String acessToken, String openId, String title, String desc, String url,
|
| | | String thumbUrl) {
|
| | | JSONObject data = new JSONObject();
|
| | | data.put("touser", openId);
|
| | | data.put("msgtype", "link");
|
| | | JSONObject linkObject = new JSONObject();
|
| | | linkObject.put("title", title);
|
| | | linkObject.put("description", desc);
|
| | | linkObject.put("url", url);
|
| | | linkObject.put("thumb_url", thumbUrl);
|
| | | data.put("link", linkObject);
|
| | | return sendMsg(acessToken, data);
|
| | | }
|
| | | |
| | | |
| | |
|
| | | }
|
| | | package org.yeshi.utils.wx; |
| | | |
| | | import java.io.File; |
| | | import java.util.Map; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | |
| | | import org.yeshi.utils.HttpUtil; |
| | | |
| | | import net.sf.json.JSONObject; |
| | | |
| | | //小程序客服消息处理 |
| | | public class XCXKeFuUtil { |
| | | public final static String TYPE_IMAGE = "image"; |
| | | |
| | | public static JSONObject parseHuiHuaMsg(HttpServletRequest request, String xmlContent, String token, |
| | | String encodingASEKey, String appId) { |
| | | String msgSignature = request.getParameter("msg_signature"); |
| | | String timeStamp = request.getParameter("timestamp"); |
| | | String nonce = request.getParameter("nonce"); |
| | | JSONObject root = new JSONObject(); |
| | | Map<String, String> map = WXUtil.parseXML(xmlContent, msgSignature, timeStamp, nonce, token, encodingASEKey, |
| | | appId); |
| | | |
| | | JSONObject data = new JSONObject(); |
| | | data.put("openid", map.get("FromUserName")); |
| | | data.put("ghid", map.get("ToUserName")); |
| | | data.put("sessionFrom", map.get("SessionFrom")); |
| | | root.put("data", data); |
| | | if ("user_enter_tempsession".equalsIgnoreCase(map.get("Event"))) { |
| | | root.put("code", 0);// 进入会话事件 |
| | | } else |
| | | root.put("code", 1);// 其他事件 |
| | | return root; |
| | | } |
| | | |
| | | private static boolean sendMsg(String acessToken, JSONObject data) { |
| | | String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + acessToken; |
| | | String result = HttpUtil.post(url, data.toString()); |
| | | return true; |
| | | } |
| | | |
| | | public static String uploadFile(String accessToken, String type, File f) { |
| | | String url = String.format("https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s", |
| | | accessToken, type); |
| | | String result = HttpUtil.post(url, "media", f); |
| | | JSONObject resultObject = JSONObject.fromObject(result); |
| | | return resultObject.optString("media_id"); |
| | | } |
| | | |
| | | // 发送文字消息 |
| | | public static boolean sendTextMsg(String acessToken, String openId, String text) { |
| | | |
| | | JSONObject data = new JSONObject(); |
| | | data.put("touser", openId); |
| | | data.put("msgtype", "text"); |
| | | JSONObject textObject = new JSONObject(); |
| | | textObject.put("content", text); |
| | | data.put("text", textObject); |
| | | return sendMsg(acessToken, data); |
| | | } |
| | | |
| | | // 发送图片消息 |
| | | public static boolean sendImageMsg(String acessToken, String openId, String mediaId) { |
| | | JSONObject data = new JSONObject(); |
| | | data.put("touser", openId); |
| | | data.put("msgtype", "image"); |
| | | JSONObject imageObject = new JSONObject(); |
| | | imageObject.put("media_id", mediaId); |
| | | data.put("image", imageObject); |
| | | return sendMsg(acessToken, data); |
| | | } |
| | | |
| | | // 发送图文消息 |
| | | public static boolean sendTextImageMsg(String acessToken, String openId, String title, String desc, String url, |
| | | String thumbUrl) { |
| | | JSONObject data = new JSONObject(); |
| | | data.put("touser", openId); |
| | | data.put("msgtype", "link"); |
| | | JSONObject linkObject = new JSONObject(); |
| | | linkObject.put("title", title); |
| | | linkObject.put("description", desc); |
| | | linkObject.put("url", url); |
| | | linkObject.put("thumb_url", thumbUrl); |
| | | data.put("link", linkObject); |
| | | return sendMsg(acessToken, data); |
| | | } |
| | | |
| | | |
| | | |
| | | } |