diff --git a/src/main/java/com/foxinmy/weixin4j/WeixinProxy.java b/src/main/java/com/foxinmy/weixin4j/WeixinProxy.java index 6a51f5d4..79b1db03 100644 --- a/src/main/java/com/foxinmy/weixin4j/WeixinProxy.java +++ b/src/main/java/com/foxinmy/weixin4j/WeixinProxy.java @@ -29,6 +29,7 @@ import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.http.HttpRequest; import com.foxinmy.weixin4j.http.Response; import com.foxinmy.weixin4j.model.Button; +import com.foxinmy.weixin4j.model.CustomRecord; import com.foxinmy.weixin4j.model.Following; import com.foxinmy.weixin4j.model.Group; import com.foxinmy.weixin4j.model.MpArticle; @@ -71,9 +72,8 @@ public class WeixinProxy { * 随机数 * @param signature * 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数 - * @return - * 开发者通过检验signature对请求进行相关校验。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效 - * ,成为开发者成功,否则接入失败 + * @return 开发者通过检验signature对请求进行相关校验。若确认此次GET请求来自微信服务器 + * 请原样返回echostr参数内容,则接入生效 成为开发者成功,否则接入失败 * @see 接入指南 */ @@ -91,7 +91,7 @@ public class WeixinProxy { } String _signature = null; try { - String[] a = { app_token, timestamp, nonce }; + String[] a = {app_token, timestamp, nonce}; Arrays.sort(a); StringBuilder sb = new StringBuilder(3); for (String str : a) { @@ -203,7 +203,7 @@ public class WeixinProxy { long now_time = ca.getTimeInMillis(); try { token_path = WeixinConfig.getValue("api_token_uri"); - Response response = null; + Response response = request.get(token_path); if (token_file.exists()) { token = (Token) xstream.fromXML(token_file); @@ -221,7 +221,6 @@ public class WeixinProxy { token_file.getParentFile().mkdirs(); } } - token = response.getAsObject(Token.class); token.setTime(now_time); token.setOpenid(appOpenId); @@ -557,8 +556,7 @@ public class WeixinProxy { } catch (UnsupportedEncodingException e) { e.printStackTrace(); } - JSONObject obj = response.getAsJson(); - return JSON.parseObject(obj.getString("group"), Group.class); + return response.getAsJson().getObject("group", Group.class); } /** @@ -575,8 +573,9 @@ public class WeixinProxy { Token token = getToken(); Response response = request.get(String.format(group_get_uri, token.getAccess_token())); - JSONObject obj = response.getAsJson(); - return JSON.parseArray(obj.getString("groups"), Group.class); + + return JSON.parseArray(response.getAsJson().getString("groups"), + Group.class); } /** @@ -887,7 +886,7 @@ public class WeixinProxy { /** * 删除群发消息 *

- * 请注意,只有已经发送成功的消息才能删除删除消息只是将消息的图文详情页失效,已经收到的用户,还是能在其本地看到消息卡片 + * 请注意,只有已经发送成功的消息才能删除删除消息只是将消息的图文详情页失效,已经收到的用户,还是能在其本地看到消息卡片 *

* * @param msgid @@ -896,8 +895,7 @@ public class WeixinProxy { * @see 删除群发 * @see {@link com.foxinmy.weixin4j.WeixinProxy#massNewsByGroup(List, String)} - * @see {@link com.foxinmy.weixin4j.WeixinProxy#massNewsByOpenIds(List, String...) - + * @see {@link com.foxinmy.weixin4j.WeixinProxy#massNewsByOpenIds(List, String...) */ public void deleteMassNews(String msgid) throws WeixinException { JSONObject obj = new JSONObject(); @@ -913,4 +911,46 @@ public class WeixinProxy { e.printStackTrace(); } } + + /** + * 客服聊天记录 + * + * @param openId + * 用户标识 可为空 + * @param starttime + * 查询开始时间 + * @param endtime + * 查询结束时间 每次查询不能跨日查询 + * @param pagesize + * 每页大小 每页最多拉取1000条 + * @param pageindex + * 查询第几页 从1开始 + * @throws WeixinException + * @see com.foxinmy.weixin4j.model.CustomRecord + * @see 查询客服聊天记录 + */ + public List getCustomRecord(String openId, long starttime, + long endtime, int pagesize, int pageindex) throws WeixinException { + JSONObject obj = new JSONObject(); + obj.put("openId", openId == null ? "" : openId); + obj.put("starttime", starttime); + obj.put("endtime", endtime); + obj.put("pagesize", pagesize > 1000 ? 1000 : pagesize); + obj.put("pageindex", pageindex); + String custom_record_uri = WeixinConfig.getValue("custom_record_uri"); + Token token = getToken(); + Response response = null; + try { + response = request.post(String.format(custom_record_uri, + token.getAccess_token()), + new StringRequestEntity(obj.toJSONString(), + "application/json", "UTF-8")); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + String text = response.getAsJson().getString("recordlist"); + return JSON.parseArray(text, CustomRecord.class); + } } diff --git a/src/main/java/com/foxinmy/weixin4j/model/CustomRecord.java b/src/main/java/com/foxinmy/weixin4j/model/CustomRecord.java new file mode 100644 index 00000000..ddd7da86 --- /dev/null +++ b/src/main/java/com/foxinmy/weixin4j/model/CustomRecord.java @@ -0,0 +1,96 @@ +package com.foxinmy.weixin4j.model; + +import java.io.Serializable; +import java.util.Date; + +/** + * 客服聊天记录 + * + * @className CustomRecord + * @author jy + * @date 2014年6月28日 + * @since JDK 1.7 + * @see 客服聊天记录 + */ +public class CustomRecord implements Serializable { + + private static final long serialVersionUID = -4024147769411601325L; + private String worker;// 客服账号 + private String openid;// 用户的标识 + private Opercode opercode;// 操作ID(会话状态) + private Date time;// 操作时间 + private String text;// 聊天记录 + + public String getWorker() { + return worker; + } + + public void setWorker(String worker) { + this.worker = worker; + } + + public String getOpenid() { + return openid; + } + + public void setOpenid(String openid) { + this.openid = openid; + } + + public Opercode getOpercode() { + return opercode; + } + + public void setOpercode(Opercode opercode) { + this.opercode = opercode; + } + + public Date getTime() { + return time; + } + + public void setTime(Date time) { + this.time = time; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public enum Opercode { + MISS(1000, "创建未接入会话"), ONLINE(1001, "接入会话"), CALL(1002, "主动发起会话"), CLOSE( + 1004, "关闭会话"), RASE(1005, "抢接会话"), RECEIVE1(2001, "公众号收到消息"), SEND( + 2002, "客服发送消息"), RECEIVE2(2003, "客服收到消息"); + private int code; + private String desc; + Opercode(int code, String desc) { + this.code = code; + this.desc = desc; + } + + public int getCode() { + return code; + } + public String getDesc() { + return desc; + } + + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[CustomRecord worker=").append(worker); + sb.append(" ,openid=").append(openid); + sb.append(" ,opercode=").append(opercode); + sb.append(" ,time=").append(time); + sb.append(" ,text=").append(text); + sb.append("]"); + return sb.toString(); + } +}