Skip to content

Commit 898f26d

Browse files
committed
一些细微的改动
1 parent 45bc474 commit 898f26d

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

src/main/java/org/hamster/weixinmp/config/WxConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* @version Jul 29, 2013
1414
*
1515
*/
16-
@Configuration
1716
@Data
17+
@Configuration
1818
public class WxConfig {
1919
private @Value("#{wxProperties.wx_token}") String token;
2020
private @Value("#{wxProperties.wx_appid}") String appid;

src/main/java/org/hamster/weixinmp/controller/WxController.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
*/
44
package org.hamster.weixinmp.controller;
55

6+
import lombok.Setter;
7+
68
import org.apache.log4j.Logger;
79
import org.dom4j.DocumentException;
810
import org.dom4j.Element;
911
import org.hamster.weixinmp.constant.WxReqTypeEnum;
1012
import org.hamster.weixinmp.controller.util.WxXmlUtil;
1113
import org.hamster.weixinmp.dao.entity.base.WxBaseMsgEntity;
14+
import org.hamster.weixinmp.dao.entity.base.WxBaseRespEntity;
15+
import org.hamster.weixinmp.dao.entity.resp.WxRespMusic;
16+
import org.hamster.weixinmp.dao.entity.resp.WxRespPicDesc;
1217
import org.hamster.weixinmp.dao.entity.resp.WxRespText;
1318
import org.hamster.weixinmp.exception.WxException;
1419
import org.hamster.weixinmp.service.WxService;
@@ -32,6 +37,7 @@ public class WxController {
3237
private static final Logger log = Logger.getLogger(WxController.class);
3338

3439
@Autowired
40+
@Setter
3541
private WxService wxService;
3642

3743
@RequestMapping(method = RequestMethod.GET)
@@ -71,8 +77,15 @@ String post(@RequestBody String requestBody) throws DocumentException {
7177
msg = wxService.saveMsgEvent(ele);
7278
break;
7379
}
74-
WxRespText respText = wxService.createRespText("Only test message, please ignore this.", msg.getToUserName(), msg.getFromUserName(), 1);
75-
return WxXmlUtil.getRespTextXML(respText).asXML();
80+
WxBaseRespEntity resp = wxService.handleMessage(msg);
81+
if (resp instanceof WxRespText) {
82+
return WxXmlUtil.getRespTextXML((WxRespText)resp).asXML();
83+
} else if (resp instanceof WxRespPicDesc) {
84+
return WxXmlUtil.getRespPicDesc((WxRespPicDesc)resp).asXML();
85+
} else if (resp instanceof WxRespMusic) {
86+
return WxXmlUtil.getRespMusic((WxRespMusic)resp).asXML();
87+
}
88+
return "";
7689
}
7790

7891
}

src/main/java/org/hamster/weixinmp/service/WxService.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import org.hamster.weixinmp.controller.util.WxXmlUtil;
3434
import org.hamster.weixinmp.dao.entity.auth.WxAuth;
3535
import org.hamster.weixinmp.dao.entity.auth.WxAuthReq;
36+
import org.hamster.weixinmp.dao.entity.base.WxBaseMsgEntity;
37+
import org.hamster.weixinmp.dao.entity.base.WxBaseRespEntity;
3638
import org.hamster.weixinmp.dao.entity.item.WxItemMusic;
3739
import org.hamster.weixinmp.dao.entity.item.WxItemPicDesc;
3840
import org.hamster.weixinmp.dao.entity.menu.WxMenuBtn;
@@ -78,38 +80,38 @@ public class WxService {
7880
public static final String DEFAULT_GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
7981

8082
@Autowired(required = false)
81-
WxAuthDao wxAuthDao;
83+
protected WxAuthDao wxAuthDao;
8284
@Autowired(required = false)
83-
WxAuthReqDao authReqDao;
85+
protected WxAuthReqDao authReqDao;
8486

8587
@Autowired(required = false)
86-
WxMsgTextDao msgTextDao;
88+
protected WxMsgTextDao msgTextDao;
8789
@Autowired(required = false)
88-
WxMsgImgDao msgImgDao;
90+
protected WxMsgImgDao msgImgDao;
8991
@Autowired(required = false)
90-
WxMsgLinkDao msgLinkDao;
92+
protected WxMsgLinkDao msgLinkDao;
9193
@Autowired(required = false)
92-
WxMsgLocDao msgLocDao;
94+
protected WxMsgLocDao msgLocDao;
9395
@Autowired(required = false)
94-
WxMsgEventDao msgEventDao;
96+
protected WxMsgEventDao msgEventDao;
9597

9698
@Autowired(required = false)
97-
WxRespTextDao respTextDao;
99+
protected WxRespTextDao respTextDao;
98100
@Autowired(required = false)
99-
WxRespPicDescDao respPicDescDao;
101+
protected WxRespPicDescDao respPicDescDao;
100102
@Autowired(required = false)
101-
WxRespMusicDao respMusicDao;
103+
protected WxRespMusicDao respMusicDao;
102104
@Autowired(required = false)
103-
WxItemPicDescDao wxItemPicDescDao;
105+
protected WxItemPicDescDao wxItemPicDescDao;
104106

105107
@Autowired(required = false)
106-
WxMenuBtnDao wxMenuBtnDao;
108+
protected WxMenuBtnDao wxMenuBtnDao;
107109

108110
@Setter
109-
private String token;
111+
protected String token;
110112

111113
@Autowired(required = false)
112-
WxConfig wxConfig;
114+
protected WxConfig wxConfig;
113115

114116
public boolean validateAuth(String signature, String timestamp,
115117
String nonce, String echostr) throws WxException {
@@ -343,8 +345,13 @@ public void createMenu(List<WxMenuBtn> wxMenuBtnList, String accessToken)
343345
throw new WxException(e);
344346
}
345347
}
348+
349+
public WxBaseRespEntity handleMessage(WxBaseMsgEntity msg) {
350+
WxRespText respText = createRespText("Only test message, please ignore this.", msg.getToUserName(), msg.getFromUserName(), 1);
351+
return respText;
352+
}
346353

347-
private static String getParameterizedUrl(String url, String... args) {
354+
protected static String getParameterizedUrl(String url, String... args) {
348355
String result = url;
349356
for (int i = 0; i < args.length; i += 2) {
350357
String p = args[i];
@@ -354,7 +361,7 @@ private static String getParameterizedUrl(String url, String... args) {
354361
return result;
355362
}
356363

357-
private static String getStringToHash(String timestamp, String nonce,
364+
protected static String getStringToHash(String timestamp, String nonce,
358365
String token) {
359366
List<String> list = new ArrayList<String>();
360367
list.add(timestamp);
@@ -372,7 +379,7 @@ private static String getStringToHash(String timestamp, String nonce,
372379

373380
/**
374381
*/
375-
private static String hash(String str) {
382+
protected static String hash(String str) {
376383
try {
377384
MessageDigest md = MessageDigest.getInstance("SHA-1");
378385
byte[] b = md.digest(str.getBytes());

0 commit comments

Comments
 (0)