WxController.java
3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.example.controller;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.wink.client.ClientResponse;
import org.apache.wink.client.Resource;
import org.apache.wink.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.service.http.ClientConfigService;
import com.example.service.http.ClientConfigServiceImpl;
import com.example.support.model.MessageEntity;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Created by JA on 17/7/3.
*/
@RequestMapping("/kmr/wx")
@RestController
public class WxController {
@Value("${wx.sign.base.url}")
String wxSignBaseUrl;
// String jssdkurl =
// "http://gfwp.gac-toyota.com.cn/GTMCfamily/index.php/campaign/api2/getjssdk";
//
// String getopenidurl =
// "http://gfwp.gac-toyota.com.cn/GTMCfamily/index.php/campaign/api2/getjssdk";
//
// @Autowired
// private HttpPostService httpPostService;
//
//
// @RequestMapping("/getjssdk")
// public MessageEntity getjssdk(HttpServletRequest request, @RequestParam
// String url) {
// MessageEntity.Builder builder = new MessageEntity.Builder(request);
// String newUrl = jssdkurl + "?url="+ url;
// Map<String, Object> map = null;
// try {
// map = httpPostService.httpPost(newUrl, null);
// } catch (IOException e) {
// e.printStackTrace();
// }
// return builder.success(true).code("2000").content(map).create();
// }
@RequestMapping("/getUser")
public MessageEntity oauth(HttpServletRequest request, @RequestParam String access_token, String openid) {
MessageEntity.Builder builder = new MessageEntity.Builder(request);
ClientConfigService configService = new ClientConfigServiceImpl();
RestClient restClient = new RestClient(configService.getClientConfig());
Resource resource = restClient.resource("https://api.weixin.qq.com/sns/userinfo");
resource.queryParam("access_token", access_token);
resource.queryParam("openid", openid);
resource.queryParam("lang", "zh_CN");
ClientResponse response = resource.get();
String responseEntity = response.getEntity(String.class);
Map readValue = null;
// System.out.println("responseEntity:::"+responseEntity);
ObjectMapper mapper = new ObjectMapper();
try {
readValue = mapper.readValue(responseEntity, Map.class);
if (null != readValue.get("errcode")) {
return builder.success(false).code("999").content(readValue).create();
}
} catch (IOException e) {
e.printStackTrace();
;
}
return builder.success(true).code("2000").content(readValue).create();
}
@RequestMapping("/wxSign")
public MessageEntity jsSign(HttpServletRequest request, @RequestParam String url)
throws UnsupportedEncodingException {
MessageEntity.Builder builder = new MessageEntity.Builder(request);
ClientConfigService configService = new ClientConfigServiceImpl();
RestClient restClient = new RestClient(configService.getClientConfig());
Resource resource = restClient.resource(wxSignBaseUrl + "?url=" + URLEncoder.encode(url, "utf-8"));
ClientResponse response = resource.get();
String responseEntity = response.getEntity(String.class);
Map readValue = null;
// System.out.println("responseEntity:::"+responseEntity);
ObjectMapper mapper = new ObjectMapper();
try {
readValue = mapper.readValue(responseEntity, Map.class);
if (null != readValue.get("errcode")) {
return builder.success(false).code("999").content(readValue).create();
}
} catch (IOException e) {
e.printStackTrace();
;
}
return builder.success(true).code("2000").content(readValue).create();
}
}