Commit 76c5cb18 authored by shiyu's avatar shiyu

公众号

parent 7ac8cf71
...@@ -54,4 +54,5 @@ public class OfficialAccountApi { ...@@ -54,4 +54,5 @@ public class OfficialAccountApi {
redisUtils.set(APPID, token, Long.valueOf(expires_in) - 10, TimeUnit.SECONDS); redisUtils.set(APPID, token, Long.valueOf(expires_in) - 10, TimeUnit.SECONDS);
return token; return token;
} }
} }
...@@ -4,10 +4,16 @@ package com.wwdz.ch.wx.api; ...@@ -4,10 +4,16 @@ package com.wwdz.ch.wx.api;
import com.xxdxxs.utils.XmlUtils; import com.xxdxxs.utils.XmlUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -22,11 +28,25 @@ public class OfficialAccountController { ...@@ -22,11 +28,25 @@ public class OfficialAccountController {
private final static String UNSUBSCRIBE_EVENT = "unsubscribe"; private final static String UNSUBSCRIBE_EVENT = "unsubscribe";
@Autowired
OfficialAccountApi officialAccountApi;
@RequestMapping(value ="/getSubscribeNotice")
public String getSubscribeNotice(@RequestBody String xmlInfo){ @RequestMapping(value ="/getNotice")
logger.info("微信通知回调接口原文:{}", xmlInfo); public String getSubscribeNotice(HttpServletRequest request, HttpServletResponse response){
ByteArrayOutputStream output = null;
InputStream input = null;
try { try {
output = new ByteArrayOutputStream();
input = request.getInputStream();
byte[] by = new byte[1024];
int length = 0;
while((length = input.read(by)) != -1){
output.write(by, 0, length);
}
String xmlInfo = new String(output.toByteArray(), "UTF-8");
logger.info("微信通知回调接口原文:{}", xmlInfo);
List<Map<String, String>> list = XmlUtils.fromXml(xmlInfo, "xml"); List<Map<String, String>> list = XmlUtils.fromXml(xmlInfo, "xml");
Map<String, String> contentMap = list.get(0); Map<String, String> contentMap = list.get(0);
...@@ -39,7 +59,34 @@ public class OfficialAccountController { ...@@ -39,7 +59,34 @@ public class OfficialAccountController {
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("微信通知回调接口 error : {}", e); logger.error("微信通知回调接口 error : {}", e);
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (input != null) {
try {
input.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} }
return ""; return "";
} }
@RequestMapping(value ="/getToken")
public String getToken(){
String token = null;
try {
token = officialAccountApi.getAccessToken();
} catch (Exception e) {
logger.error("微信通知回调接口 error : {}", e);
}
return token;
}
} }
package com.wwdz.ch.wx.api;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class OfficialAccountApiTest {
@Autowired
OfficialAccountApi officialAccountApi;
@Test
public void refreshAccessToken() {
officialAccountApi.getAccessToken();
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment