|
@@ -4,17 +4,16 @@ import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.chuanghai.h3c_reporting.common.exception.BizCodeEnume;
|
|
import com.chuanghai.h3c_reporting.common.exception.BizCodeEnume;
|
|
|
import com.chuanghai.h3c_reporting.common.exception.RRException;
|
|
import com.chuanghai.h3c_reporting.common.exception.RRException;
|
|
|
-import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
|
+import com.chuanghai.h3c_reporting.common.utils.CommonResult;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.HttpEntity;
|
|
import org.springframework.http.HttpEntity;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpHeaders;
|
|
|
-import org.springframework.http.MediaType;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
@@ -46,10 +45,12 @@ public class WxController {
|
|
|
* 通过code换取用户手机号
|
|
* 通过code换取用户手机号
|
|
|
*/
|
|
*/
|
|
|
@GetMapping("/getPhone")
|
|
@GetMapping("/getPhone")
|
|
|
- public String getPhone(String code, String state) {
|
|
|
|
|
|
|
+ @ResponseBody
|
|
|
|
|
+ public CommonResult<String> getPhone(String code, String state) {
|
|
|
if (access_token.equals("")) {
|
|
if (access_token.equals("")) {
|
|
|
getAccessToken();
|
|
getAccessToken();
|
|
|
}
|
|
}
|
|
|
|
|
+// System.out.println("access_token ======>>>" + access_token);
|
|
|
try {
|
|
try {
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
HttpHeaders headers = new HttpHeaders();
|
|
@@ -59,21 +60,12 @@ public class WxController {
|
|
|
HttpEntity<Map<String, String>> requestM = new HttpEntity<>(paramCardNumber, headers);
|
|
HttpEntity<Map<String, String>> requestM = new HttpEntity<>(paramCardNumber, headers);
|
|
|
String userInfoUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + access_token;
|
|
String userInfoUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + access_token;
|
|
|
ResponseEntity<String> responsem = restTemplate.postForEntity(userInfoUrl, requestM, String.class);
|
|
ResponseEntity<String> responsem = restTemplate.postForEntity(userInfoUrl, requestM, String.class);
|
|
|
- JSONObject obj = JSON.parseObject(responsem.getBody());
|
|
|
|
|
|
|
|
|
|
- String phoneNumber = "";
|
|
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
- if (obj != null) {
|
|
|
|
|
- try {
|
|
|
|
|
- Map<String, Object> jsonMap = objectMapper.readValue(obj.toString(), new TypeReference<Map<String, Object>>() {
|
|
|
|
|
- });
|
|
|
|
|
- // 内一层(获取有问题)
|
|
|
|
|
- phoneNumber = jsonMap.get("phone_info").toString();
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return phoneNumber;
|
|
|
|
|
|
|
+ JSONObject resPhoneInfo = JSON.parseObject(responsem.getBody());
|
|
|
|
|
+ JSONObject phoneInfo=resPhoneInfo.getJSONObject("phone_info");
|
|
|
|
|
+ String phoneNumber = phoneInfo.get("phoneNumber").toString();
|
|
|
|
|
+
|
|
|
|
|
+ return CommonResult.ok().setResult(phoneNumber);
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
throw new RRException(BizCodeEnume.WX_EXCEPTION, "获取用户绑定的手机号失败");
|
|
throw new RRException(BizCodeEnume.WX_EXCEPTION, "获取用户绑定的手机号失败");
|
|
@@ -83,18 +75,22 @@ public class WxController {
|
|
|
// 获取access_token
|
|
// 获取access_token
|
|
|
@Scheduled(cron = "0 59 0/1 * * ?") // 每1小时59分,执行一次刷新access_token
|
|
@Scheduled(cron = "0 59 0/1 * * ?") // 每1小时59分,执行一次刷新access_token
|
|
|
public void getAccessToken() {
|
|
public void getAccessToken() {
|
|
|
- Map<String, String> token = new HashMap<>();
|
|
|
|
|
- token.put("appid", appID);
|
|
|
|
|
- token.put("secret", secret);
|
|
|
|
|
- token.put("grant_type", "client_credentials");
|
|
|
|
|
- String url = "https://api.weixin.qq.com/cgi-bin/token";
|
|
|
|
|
|
|
+// Map<String, String> token = new HashMap<>();
|
|
|
|
|
+// token.put("appid", appID);
|
|
|
|
|
+// token.put("secret", secret);
|
|
|
|
|
+// token.put("grant_type", "client_credential");
|
|
|
|
|
+ String url = "https://api.weixin.qq.com/cgi-bin/token?" +
|
|
|
|
|
+ "grant_type=client_credential" +
|
|
|
|
|
+ "&appid=" + appID +
|
|
|
|
|
+ "&secret=" + secret;
|
|
|
|
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
- headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
- HttpEntity<Map<String, String>> request = new HttpEntity<>(token, headers);
|
|
|
|
|
- ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class);
|
|
|
|
|
|
|
+// HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
+// headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
+// HttpEntity<Map<String, String>> request = new HttpEntity<>(token, headers);
|
|
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
|
|
|
JSONObject objParam = JSON.parseObject(responseEntity.getBody());
|
|
JSONObject objParam = JSON.parseObject(responseEntity.getBody());
|
|
|
|
|
+
|
|
|
for (Map.Entry<String, Object> entry : objParam.entrySet()) {
|
|
for (Map.Entry<String, Object> entry : objParam.entrySet()) {
|
|
|
Object o = entry.getValue();
|
|
Object o = entry.getValue();
|
|
|
if (o instanceof String) {
|
|
if (o instanceof String) {
|