|
@@ -1,7 +1,9 @@
|
|
|
package com.chuanghai.repair.controller;
|
|
package com.chuanghai.repair.controller;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.chuanghai.repair.VO.IdCardNameVO;
|
|
|
import com.chuanghai.repair.config.WeixiaoConfig;
|
|
import com.chuanghai.repair.config.WeixiaoConfig;
|
|
|
import com.chuanghai.repair.entity.RepairsStudent;
|
|
import com.chuanghai.repair.entity.RepairsStudent;
|
|
|
import com.chuanghai.repair.service.RepairsAdminService;
|
|
import com.chuanghai.repair.service.RepairsAdminService;
|
|
@@ -9,6 +11,10 @@ import com.chuanghai.repair.service.RepairsOpenIDService;
|
|
|
import com.chuanghai.repair.service.RepairsStudentService;
|
|
import com.chuanghai.repair.service.RepairsStudentService;
|
|
|
import com.chuanghai.repair.service.RepairsWorkService;
|
|
import com.chuanghai.repair.service.RepairsWorkService;
|
|
|
import com.chuanghai.repair.utils.CreateTokenUtil;
|
|
import com.chuanghai.repair.utils.CreateTokenUtil;
|
|
|
|
|
+import com.chuanghai.repair.utils.ReturnValueUtil;
|
|
|
|
|
+import com.chuanghai.repair.utils.UnicodeUtil;
|
|
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpEntity;
|
|
import org.springframework.http.HttpEntity;
|
|
@@ -66,8 +72,8 @@ public class WxController {
|
|
|
String cardNumber = "";
|
|
String cardNumber = "";
|
|
|
try {
|
|
try {
|
|
|
String grant_type = "authorization_code";
|
|
String grant_type = "authorization_code";
|
|
|
- // String redirect_uri = "https://jtishfw.ncjti.edu.cn/baoxiu/repairApi/wx/getCardNumber";
|
|
|
|
|
- String redirect_uri = "http://binguo.vaiwan.com/wx/getCardNumber";
|
|
|
|
|
|
|
+ String redirect_uri = "https://jtishfw.ncjti.edu.cn/baoxiu/repairApi/wx/getCardNumber";
|
|
|
|
|
+// String redirect_uri = "http://binguo.vaiwan.com/wx/getCardNumber";
|
|
|
String url = "https://open.wecard.qq.com/connect/oauth2/token"; //用于用户 获取基础的access_token
|
|
String url = "https://open.wecard.qq.com/connect/oauth2/token"; //用于用户 获取基础的access_token
|
|
|
Map<String, String> param = new HashMap<>();
|
|
Map<String, String> param = new HashMap<>();
|
|
|
param.put("wxcode", wxcode);
|
|
param.put("wxcode", wxcode);
|
|
@@ -139,6 +145,7 @@ public class WxController {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
System.out.println("cardNumber-----------------"+cardNumber);
|
|
System.out.println("cardNumber-----------------"+cardNumber);
|
|
|
|
|
+ System.out.println("wxcode-----------------"+wxcode);
|
|
|
System.out.println("accessTokenaccessTokenaccessToken-------- "+accessToken);
|
|
System.out.println("accessTokenaccessTokenaccessToken-------- "+accessToken);
|
|
|
|
|
|
|
|
RepairsStudent repairsStudent = new RepairsStudent(studentId, studentName, studentSex,
|
|
RepairsStudent repairsStudent = new RepairsStudent(studentId, studentName, studentSex,
|
|
@@ -158,4 +165,117 @@ public class WxController {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 微校授权回调地址
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param wxcode 微校code
|
|
|
|
|
+ * @param state 透传数据
|
|
|
|
|
+ * @return
|
|
|
|
|
+ * @apiNote 用户跳转到微校授权链接上,微校服务器接收用户授权后会携带wxcode回调到该接口上,该接口使用wxcode获取到用户的信息。
|
|
|
|
|
+ * 如果用户信息获取成功,该接口会以 【fontendUrl + /?cardNumber=wx123456&name=测试用户】 的形式跳回到前端应用页面;
|
|
|
|
|
+ * 如果用户信息获取失败,该接口会以 【fontendUrl + /error/?errorMsg=获取用户信息失败】 的形式跳回到前端应用页面。
|
|
|
|
|
+ * <strong>注意:</strong>
|
|
|
|
|
+ * fontendUrl + 路由地址:前端项目地址+页面路由地址,用于前端页面接收用户的信息或错误信息,该地址需要前端人员与后台开发人员协调好。
|
|
|
|
|
+ * </p>
|
|
|
|
|
+ */
|
|
|
|
|
+ @GetMapping("weixiao/auth")
|
|
|
|
|
+ public String weixiaoAuth(String wxcode, String state) {
|
|
|
|
|
+ String idCard = "";
|
|
|
|
|
+ String name = "";
|
|
|
|
|
+
|
|
|
|
|
+ String tokenUrl = "https://open.wecard.qq.com/connect/oauth2/token";
|
|
|
|
|
+ Map<String, String> tokenParams = new HashMap<>();
|
|
|
|
|
+ tokenParams.put("wxcode", wxcode);
|
|
|
|
|
+ tokenParams.put("app_key", weixiaoConfig.getAppKey());
|
|
|
|
|
+ tokenParams.put("app_secret", weixiaoConfig.getAppSecret());
|
|
|
|
|
+ tokenParams.put("grant_type", "authorization_code");
|
|
|
|
|
+ tokenParams.put("redirect_uri", state); // 该地址需要与发起授权地址保持一直
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ // wecode换取token
|
|
|
|
|
+ RestTemplate client = new RestTemplate();
|
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
+ HttpEntity<Map<String, String>> request = new HttpEntity<>(tokenParams, headers);
|
|
|
|
|
+ //ResponseEntity<String> tokenResponse = client.postForEntity(tokenUrl, tokenParams, String.class);
|
|
|
|
|
+
|
|
|
|
|
+ ResponseEntity<String> tokenResponse = client.postForEntity(tokenUrl, request, String.class);
|
|
|
|
|
+
|
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
+ Map<String, Object> jsonMap = mapper.readValue(tokenResponse.getBody(), new TypeReference<Map<String, Object>>() {
|
|
|
|
|
+ });
|
|
|
|
|
+ String accessToken = (String) jsonMap.get("access_token");
|
|
|
|
|
+
|
|
|
|
|
+ // token换取用户信息
|
|
|
|
|
+ String userInfoUrl = "https://open.wecard.qq.com/connect/oauth/get-user-info";
|
|
|
|
|
+ Map<String, String> userInfoParam = new HashMap<>();
|
|
|
|
|
+ userInfoParam.put("access_token", accessToken);
|
|
|
|
|
+ ResponseEntity<String> userInfoResponse = client.postForEntity(userInfoUrl, userInfoParam, String.class);
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> userInfoMap = mapper.readValue(userInfoResponse.getBody(), new TypeReference<Map<String, Object>>() {
|
|
|
|
|
+ });
|
|
|
|
|
+ String cardNumber = userInfoMap.get("card_number").toString();
|
|
|
|
|
+
|
|
|
|
|
+ String idCardName = getIdCard(cardNumber, accessToken);
|
|
|
|
|
+ String[] split = idCardName.split("-");
|
|
|
|
|
+ if (split.length > 0) {
|
|
|
|
|
+ idCard = split[0];
|
|
|
|
|
+ name = split[1];
|
|
|
|
|
+ }
|
|
|
|
|
+ String encodeName =URLEncoder.encode(name,"utf-8");
|
|
|
|
|
+// return "redirect:" + "http://192.168.161.36:8080/jiaofei/qianyueH5/#/pages/index/index/?idCard=" + idCard + "&name=" + encodeName;
|
|
|
|
|
+//
|
|
|
|
|
+ return "redirect:" + "https://chtech.ncjti.edu.cn/jiaofei/qianyueH5/#/pages/index/index/?idCard=" + idCard + "&name=" + encodeName;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ return "redirect:" + "https://chtech.ncjti.edu.cn/jiaofei/qianyueH5/#/pages/index/index/#/error/";
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 通过card_number查询身份证
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param cardNumber
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @GetMapping("getIdCard")
|
|
|
|
|
+ @ResponseBody
|
|
|
|
|
+ public String getIdCard(String cardNumber, String accessToken) {
|
|
|
|
|
+
|
|
|
|
|
+ RestTemplate client = new RestTemplate();
|
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
+ String userInfoUrl = "https://open.wecard.qq.com/cgi-bin/user/search";
|
|
|
|
|
+ Map<String, String> userInfoParam = new HashMap<>();
|
|
|
|
|
+ userInfoParam.put("access_token", accessToken);
|
|
|
|
|
+ userInfoParam.put("keywords", cardNumber);
|
|
|
|
|
+ ResponseEntity<String> userInfoResponse = client.postForEntity(userInfoUrl, userInfoParam, String.class);
|
|
|
|
|
+ JSONObject jsonObj = JSON.parseObject(userInfoResponse.getBody());
|
|
|
|
|
+ JSONArray userlist = jsonObj.getJSONArray("userlist");
|
|
|
|
|
+ String id_card = "";
|
|
|
|
|
+ String name = "";
|
|
|
|
|
+ try {
|
|
|
|
|
+ Map<String, Object> userInfoMap = mapper.readValue(userlist.get(0).toString(), new TypeReference<Map<String, Object>>() {
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if (userInfoMap.get("id_card") != null) {
|
|
|
|
|
+ id_card = userInfoMap.get("id_card").toString();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (userInfoMap.get("name") != null) {
|
|
|
|
|
+ name = userInfoMap.get("name").toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ return id_card + "-" + name;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
}
|
|
}
|