精简WeixinAccount类及其配置文件
This commit is contained in:
@@ -36,6 +36,7 @@ import com.foxinmy.weixin4j.mp.model.KfAccount;
|
||||
import com.foxinmy.weixin4j.mp.model.KfSession;
|
||||
import com.foxinmy.weixin4j.mp.model.MenuSetting;
|
||||
import com.foxinmy.weixin4j.mp.model.QRParameter;
|
||||
import com.foxinmy.weixin4j.mp.model.QRResult;
|
||||
import com.foxinmy.weixin4j.mp.model.SemQuery;
|
||||
import com.foxinmy.weixin4j.mp.model.SemResult;
|
||||
import com.foxinmy.weixin4j.mp.model.User;
|
||||
@@ -641,22 +642,6 @@ public class WeixinProxy {
|
||||
return massApi.uploadVideo(video);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分组群发
|
||||
*
|
||||
* @param tuple
|
||||
* 消息元件
|
||||
* @param groupId
|
||||
* 分组ID
|
||||
* @return 群发后的消息ID
|
||||
* @see {@link #massMessage(MassTuple,boolean,int)}
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String massByGroupId(MassTuple tuple, int groupId)
|
||||
throws WeixinException {
|
||||
return massApi.massByGroupId(tuple, groupId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 群发消息
|
||||
* <p>
|
||||
@@ -685,9 +670,9 @@ public class WeixinProxy {
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html#.E6.A0.B9.E6.8D.AE.E5.88.86.E7.BB.84.E8.BF.9B.E8.A1.8C.E7.BE.A4.E5.8F.91.E3.80.90.E8.AE.A2.E9.98.85.E5.8F.B7.E4.B8.8E.E6.9C.8D.E5.8A.A1.E5.8F.B7.E8.AE.A4.E8.AF.81.E5.90.8E.E5.9D.87.E5.8F.AF.E7.94.A8.E3.80.91">根据分组群发</a>
|
||||
*/
|
||||
public String massMessage(MassTuple tuple, boolean isToAll, int groupId)
|
||||
public String massByGroupId(MassTuple tuple, boolean isToAll, int groupId)
|
||||
throws WeixinException {
|
||||
return massApi.massMessage(tuple, isToAll, groupId);
|
||||
return massApi.massByGroupId(tuple, isToAll, groupId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1072,26 +1057,16 @@ public class WeixinProxy {
|
||||
* 生成带参数的二维码
|
||||
*
|
||||
* @param parameter
|
||||
* 二维码参数
|
||||
* 二维码参数
|
||||
* @return 二维码结果对象
|
||||
* @throws WeixinException
|
||||
* @see com.foxinmy.weixin4j.mp.model.QRResult
|
||||
* @see com.foxinmy.weixin4j.mp.model.QRParameter
|
||||
* @see com.foxinmy.weixin4j.mp.api.QrApi
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/18/28fc21e7ed87bec960651f0ce873ef8a.html">生成二维码</a>
|
||||
*/
|
||||
public byte[] getQRData(QRParameter parameter) throws WeixinException {
|
||||
return qrApi.getQRData(parameter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成带参数的二维码
|
||||
*
|
||||
* @return 二维码图片解析后的地址 开发者可根据该地址自行生成需要的二维码图片
|
||||
* @throws WeixinException
|
||||
* @see {@link #getQRData(QRParameter)}
|
||||
*/
|
||||
public String getQRUrl(QRParameter parameter) throws WeixinException {
|
||||
*/
|
||||
public QRResult createQR(QRParameter parameter) throws WeixinException {
|
||||
return qrApi.createQR(parameter);
|
||||
}
|
||||
|
||||
@@ -1099,10 +1074,10 @@ public class WeixinProxy {
|
||||
* 生成带参数的二维码
|
||||
*
|
||||
* @return 硬盘存储的文件对象
|
||||
* @throws WeixinException
|
||||
* @throws WeixinException
|
||||
* @see {@link #createQR(QRParameter)}
|
||||
*/
|
||||
public File getQRFile(QRParameter parameter) throws WeixinException {
|
||||
*/
|
||||
public File createQRFile(QRParameter parameter) throws WeixinException {
|
||||
return qrApi.createQRFile(parameter);
|
||||
}
|
||||
|
||||
|
||||
@@ -85,22 +85,6 @@ public class MassApi extends MpApi {
|
||||
|
||||
/**
|
||||
* 分组群发
|
||||
*
|
||||
* @param tuple
|
||||
* 消息元件
|
||||
* @param groupId
|
||||
* 分组ID
|
||||
* @return 群发后的消息ID
|
||||
* @see {@link com.foxinmy.weixin4j.mp.api.MassApi#massMessage(MassTuple,boolean,int)}
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String massByGroupId(MassTuple tuple, int groupId)
|
||||
throws WeixinException {
|
||||
return massMessage(tuple, false, groupId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 群发消息
|
||||
* <p>
|
||||
* 在返回成功时,意味着群发任务提交成功,并不意味着此时群发已经结束,所以,仍有可能在后续的发送过程中出现异常情况导致用户未收到消息,
|
||||
* 如消息有时会进行审核、服务器不稳定等,此外,群发任务一般需要较长的时间才能全部发送完毕
|
||||
@@ -126,7 +110,7 @@ public class MassApi extends MpApi {
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html#.E6.A0.B9.E6.8D.AE.E5.88.86.E7.BB.84.E8.BF.9B.E8.A1.8C.E7.BE.A4.E5.8F.91.E3.80.90.E8.AE.A2.E9.98.85.E5.8F.B7.E4.B8.8E.E6.9C.8D.E5.8A.A1.E5.8F.B7.E8.AE.A4.E8.AF.81.E5.90.8E.E5.9D.87.E5.8F.AF.E7.94.A8.E3.80.91">根据分组群发</a>
|
||||
*/
|
||||
public String massMessage(MassTuple tuple, boolean isToAll, int groupId)
|
||||
public String massByGroupId(MassTuple tuple, boolean isToAll, int groupId)
|
||||
throws WeixinException {
|
||||
if (tuple instanceof MpNews) {
|
||||
MpNews _news = (MpNews) tuple;
|
||||
@@ -175,7 +159,7 @@ public class MassApi extends MpApi {
|
||||
public String massArticleByGroupId(List<MpArticle> articles, int groupId)
|
||||
throws WeixinException {
|
||||
String mediaId = uploadArticle(articles);
|
||||
return massByGroupId(new MpNews(mediaId), groupId);
|
||||
return massByGroupId(new MpNews(mediaId), false, groupId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -57,15 +57,16 @@ public class MenuApi extends MpApi {
|
||||
public String process(Object object, String name,
|
||||
Object value) {
|
||||
if (object instanceof Button && name.equals("content")) {
|
||||
ButtonType buttonType = ((Button) object)
|
||||
.getFormatType();
|
||||
if (buttonType == ButtonType.view) {
|
||||
return "url";
|
||||
} else if (buttonType == ButtonType.media_id
|
||||
|| buttonType == ButtonType.view_limited) {
|
||||
return "media_id";
|
||||
} else {
|
||||
return "key";
|
||||
ButtonType buttonType = ((Button) object).getType();
|
||||
if (buttonType != null) {
|
||||
if (ButtonType.view == buttonType) {
|
||||
return "url";
|
||||
} else if (ButtonType.media_id == buttonType
|
||||
|| ButtonType.view_limited == buttonType) {
|
||||
return "media_id";
|
||||
} else {
|
||||
return "key";
|
||||
}
|
||||
}
|
||||
}
|
||||
return name;
|
||||
@@ -117,8 +118,8 @@ public class MenuApi extends MpApi {
|
||||
public JsonResult deleteMenu() throws WeixinException {
|
||||
String menu_delete_uri = getRequestUri("menu_delete_uri");
|
||||
Token token = tokenHolder.getToken();
|
||||
WeixinResponse response = weixinClient.get(String.format(menu_delete_uri,
|
||||
token.getAccessToken()));
|
||||
WeixinResponse response = weixinClient.get(String.format(
|
||||
menu_delete_uri, token.getAccessToken()));
|
||||
|
||||
return response.getAsJsonResult();
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ public class OauthApi extends MpApi {
|
||||
*/
|
||||
public String getAuthorizeURL() {
|
||||
String appId = DEFAULT_WEIXIN_ACCOUNT.getId();
|
||||
String redirectUri = ConfigUtil.getValue("redirect_uri");
|
||||
String redirectUri = ConfigUtil.getValue("oauth_redirect_uri");
|
||||
return getAuthorizeURL(appId, redirectUri, "state", "snsapi_login");
|
||||
}
|
||||
|
||||
|
||||
@@ -5,11 +5,12 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.mp.model.QRParameter;
|
||||
import com.foxinmy.weixin4j.mp.model.QRResult;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
import com.foxinmy.weixin4j.util.Weixin4jConst;
|
||||
@@ -37,42 +38,25 @@ public class QrApi extends MpApi {
|
||||
*
|
||||
* @param parameter
|
||||
* 二维码参数
|
||||
* @return 二维码图片解析后的地址 开发者可根据该地址自行生成需要的二维码图片
|
||||
* @return 二维码结果对象
|
||||
* @throws WeixinException
|
||||
* @see com.foxinmy.weixin4j.mp.model.QRResult
|
||||
* @see com.foxinmy.weixin4j.mp.model.QRParameter
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/18/28fc21e7ed87bec960651f0ce873ef8a.html">生成二维码</a>
|
||||
*/
|
||||
public String getQRUrl(QRParameter parameter) throws WeixinException {
|
||||
return doQR(parameter).getString("url");
|
||||
}
|
||||
|
||||
private JSONObject doQR(QRParameter parameter) throws WeixinException {
|
||||
public QRResult createQR(QRParameter parameter) throws WeixinException {
|
||||
Token token = tokenHolder.getToken();
|
||||
String qr_uri = getRequestUri("qr_ticket_uri");
|
||||
WeixinResponse response = weixinClient.post(
|
||||
String.format(qr_uri, token.getAccessToken()),
|
||||
parameter.getContent());
|
||||
return response.getAsJson();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成带参数的二维码
|
||||
*
|
||||
* @param parameter
|
||||
* 二维码参数
|
||||
* @return byte数据包
|
||||
* @throws WeixinException
|
||||
* @see com.foxinmy.weixin4j.mp.model.QRParameter
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/18/28fc21e7ed87bec960651f0ce873ef8a.html">生成二维码</a>
|
||||
*/
|
||||
public byte[] getQRData(QRParameter parameter) throws WeixinException {
|
||||
String ticket = doQR(parameter).getString("ticket");
|
||||
String qr_uri = getRequestUri("qr_image_uri");
|
||||
WeixinResponse response = weixinClient.get(String
|
||||
.format(qr_uri, ticket));
|
||||
|
||||
return response.getContent();
|
||||
QRResult result = response.getAsObject(new TypeReference<QRResult>() {
|
||||
});
|
||||
qr_uri = getRequestUri("qr_image_uri");
|
||||
response = weixinClient.get(String.format(qr_uri, result.getTicket()));
|
||||
result.setContent(response.getContent());
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,9 +71,10 @@ public class QrApi extends MpApi {
|
||||
* @throws WeixinException
|
||||
* @see <a
|
||||
* href="mp.weixin.qq.com/wiki/18/28fc21e7ed87bec960651f0ce873ef8a.html">二维码</a>
|
||||
* @see #createQR(QRParameter)
|
||||
* @see com.foxinmy.weixin4j.mp.model.QRParameter
|
||||
*/
|
||||
public File getQRFile(QRParameter parameter) throws WeixinException {
|
||||
public File createQRFile(QRParameter parameter) throws WeixinException {
|
||||
String qr_path = ConfigUtil.getValue("qr_path",
|
||||
Weixin4jConst.DEFAULT_QRCODE_PATH);
|
||||
String filename = String.format("%s_%s_%d.jpg", parameter.getQrType()
|
||||
@@ -99,11 +84,11 @@ public class QrApi extends MpApi {
|
||||
if (parameter.getQrType().ordinal() > 0 && file.exists()) {
|
||||
return file;
|
||||
}
|
||||
byte[] datas = getQRData(parameter);
|
||||
QRResult qrResult = createQR(parameter);
|
||||
OutputStream os = null;
|
||||
try {
|
||||
os = new FileOutputStream(file);
|
||||
os.write(datas);
|
||||
os.write(qrResult.getContent());
|
||||
} catch (IOException e) {
|
||||
throw new WeixinException(e.getMessage());
|
||||
} finally {
|
||||
@@ -111,7 +96,7 @@ public class QrApi extends MpApi {
|
||||
if (os != null) {
|
||||
os.close();
|
||||
}
|
||||
} catch (IOException ignore) {
|
||||
} catch (IOException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.foxinmy.weixin4j.mp.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
|
||||
/**
|
||||
* 二维码
|
||||
*
|
||||
* @className QRResult
|
||||
* @author jy
|
||||
* @date 2015年7月29日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class QRResult implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 7730781702153258151L;
|
||||
|
||||
private String ticket;
|
||||
private String url;
|
||||
@JSONField(name = "expire_seconds")
|
||||
private int expireSeconds;
|
||||
private byte[] content;
|
||||
|
||||
public String getTicket() {
|
||||
return ticket;
|
||||
}
|
||||
|
||||
public void setTicket(String ticket) {
|
||||
this.ticket = ticket;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public int getExpireSeconds() {
|
||||
return expireSeconds;
|
||||
}
|
||||
|
||||
public void setExpireSeconds(int expireSeconds) {
|
||||
this.expireSeconds = expireSeconds;
|
||||
}
|
||||
|
||||
public byte[] getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(byte[] content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "QRResult [ticket=" + ticket + ", url=" + url
|
||||
+ ", expireSeconds=" + expireSeconds + ", content="
|
||||
+ Arrays.toString(content) + "]";
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,7 @@
|
||||
# \u6d4b\u8bd5\u4e4b\u7528 \u6b63\u5f0f\u73af\u5883\u4e0bcopy\u4e00\u4efd\u5230classpath
|
||||
# \u516c\u4f17\u53f7\u4fe1\u606f
|
||||
account={"id":"wx4ab8f8de58159a57","secret":"1d4eb0f4bf556aaed539f30ed05ca795",\
|
||||
"token":"\u5f00\u653e\u8005\u7684token",\
|
||||
"encodingAesKey":"\u516c\u4f17\u53f7\u8bbe\u7f6e\u4e86\u52a0\u5bc6\u65b9\u5f0f\u4e14\u4e3a\u300c\u5b89\u5168\u6a21\u5f0f\u300d\u65f6\u9700\u8981\u586b\u5165",\
|
||||
"mchId":"V3.x\u7248\u672c\u4e0b\u7684\u5fae\u4fe1\u5546\u6237\u53f7 \u670d\u52a1\u53f7\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165",\
|
||||
"version":2,\
|
||||
"partnerId":"V2\u7248\u672c\u4e0b\u7684\u8d22\u4ed8\u901a\u7684\u5546\u6237\u53f7 \u670d\u52a1\u53f7\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165",\
|
||||
"partnerKey":"V2\u7248\u672c\u4e0b\u7684\u8d22\u4ed8\u901a\u5546\u6237\u6743\u9650\u5bc6\u94a5Key \u670d\u52a1\u53f7\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165",\
|
||||
"paySignKey":"\u5fae\u4fe1\u652f\u4ed8\u4e2d\u8c03\u7528API\u7684\u5bc6\u94a5 \u670d\u52a1\u53f7\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165"}
|
||||
@@ -23,4 +20,4 @@ ca_file=/tmp/weixin4j/xxxxx.p12
|
||||
# ca_file=classpath:xxxxx.pfx
|
||||
|
||||
# oauth\u6388\u6743\u540e\u91cd\u5b9a\u5411\u7684url
|
||||
redirect_uri=
|
||||
oauth_redirect_uri=
|
||||
Reference in New Issue
Block a user