|
|
@@ -0,0 +1,200 @@
|
|
|
+package com.tsf.demo.provider.controller;
|
|
|
+
|
|
|
+import cn.hutool.core.net.URLDecoder;
|
|
|
+import cn.hutool.core.util.CharsetUtil;
|
|
|
+import cn.hutool.core.util.HexUtil;
|
|
|
+import cn.hutool.crypto.digest.DigestUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.parser.Feature;
|
|
|
+import com.tsf.demo.provider.common.siyuan.BeanSortUtil;
|
|
|
+import com.tsf.demo.provider.common.siyuan.CommonReq;
|
|
|
+import com.tsf.demo.provider.common.siyuan.Testa;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
+import org.apache.http.HttpException;
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.impl.client.*;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.*;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.net.URI;
|
|
|
+import java.net.URISyntaxException;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Locale;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@CrossOrigin
|
|
|
+@RequestMapping("/siYuanUser")
|
|
|
+@Api(value = "思源用户中心接口", tags = "思源用户中心接口")
|
|
|
+public class SiYuanUserController {
|
|
|
+ @Value("${ten.mp.urlPrefix}")
|
|
|
+ private String tenMpUrlPrefix;
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+
|
|
|
+ //openId = getUserInfo("0012Lmll2zmGF7409Pnl2wJspn42Lmlf");
|
|
|
+ String openId = "omsuv5YIgMYQNdpawZE2dAAGiZmw";
|
|
|
+ //获取思源用户中心sign
|
|
|
+ String appSecret= "623f2cc1cc663d3c9b7b1ac63becf234";
|
|
|
+ String appId = "20180117547016";
|
|
|
+ String signType = "MD5";
|
|
|
+ Long timestamp = System.currentTimeMillis();
|
|
|
+ String version = "1.0";
|
|
|
+ JSONObject jsonObjectParam = new JSONObject(true);
|
|
|
+ jsonObjectParam.put("uniqueId",openId);
|
|
|
+ jsonObjectParam.put("toonType","10200001");
|
|
|
+ String toSignStr = "appId="+appId+"&"+
|
|
|
+ "appSecret="+appSecret+"&"+
|
|
|
+ "bizParams="+jsonObjectParam.toJSONString()+"&"+
|
|
|
+ "signType="+signType+"&"+
|
|
|
+ "timestamp="+timestamp+"&"+
|
|
|
+ "version="+version;
|
|
|
+ String sign = DigestUtils.md5Hex(toSignStr).toUpperCase();
|
|
|
+
|
|
|
+ String toSignStr1 = "appId="+appId+"&"+
|
|
|
+ "appSecret="+appSecret+"&"+
|
|
|
+ "bizParams="+ URLEncoder.encode(jsonObjectParam.toJSONString())+"&"+
|
|
|
+ "signType="+signType+"&"+
|
|
|
+ "timestamp="+timestamp+"&"+
|
|
|
+ "version="+version;
|
|
|
+ //获取思源卡中心sign结束
|
|
|
+ //利用sign测试接口
|
|
|
+ //拼接请求体
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("appId",appId);
|
|
|
+ jsonObject.put("appSecret","623f2cc1cc663d3c9b7b1ac63becf234");
|
|
|
+ jsonObject.put("bizParams",jsonObjectParam);
|
|
|
+ jsonObject.put("sign",sign);
|
|
|
+ jsonObject.put("signType",signType);
|
|
|
+ jsonObject.put("timestamp",timestamp);
|
|
|
+ jsonObject.put("version","1.0");
|
|
|
+ String httpUrl = "http://test-t600.systoon.com/uias/api/np/getLpToken?";
|
|
|
+ RestTemplate client = new RestTemplate();
|
|
|
+ //HttpHeaders headers = new HttpHeaders();
|
|
|
+ //headers.add("lpToken",sign);
|
|
|
+ //headers.add("toonType","10200001");
|
|
|
+// headers.setContentType(MediaType.);
|
|
|
+// HttpEntity<JSONObject> requestEntity = new HttpEntity(jsonObject,headers);
|
|
|
+ JSONObject exchange = null;
|
|
|
+ exchange = client.getForObject(httpUrl+toSignStr1+"&sign="+sign, JSONObject.class,jsonObject);
|
|
|
+ System.out.println(exchange.get("code"));
|
|
|
+ //HttpGet httpGet = new HttpGet();
|
|
|
+
|
|
|
+// 设置参数
|
|
|
+
|
|
|
+// try {
|
|
|
+// CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
+// httpGet.setURI(new URI(httpUrl+toSignStr1+"&sign="+sign));
|
|
|
+// HttpResponse httpResponse = httpclient.execute(httpGet);
|
|
|
+
|
|
|
+// 获取返回的数据
|
|
|
+
|
|
|
+// org.apache.http.HttpEntity entity = httpResponse.getEntity();
|
|
|
+// System.out.println(EntityUtils.toString(entity, "UTF-8"));
|
|
|
+// } catch (URISyntaxException e) {
|
|
|
+// throw new HttpException("请求url格式错误。"+e.getMessage());
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+ // String willSignStr =
|
|
|
+//// CommonReq req = new CommonReq();
|
|
|
+//// req.setAppId("20180117547016");
|
|
|
+//// Testa a = new Testa();
|
|
|
+//// a.setCardTypeId(11);
|
|
|
+//// a.setAppId("1001119");
|
|
|
+//// req.setData(JSON.toJSONString(a));
|
|
|
+//// req.setTimestamp(1628048715007L);
|
|
|
+//// String toSign = BeanSortUtil.json2SortString(req) + appSecret;
|
|
|
+//// String sign = HexUtil.encodeHexStr(DigestUtil.sha256(toSign, CharsetUtil.UTF_8));
|
|
|
+//// req.setSign(sign);
|
|
|
+// //获取思源用户中心sign结束
|
|
|
+// //利用sign测试接口
|
|
|
+// String httpUrl = "http://test-t600.systoon.com/platform-card/card/getCardAppList";
|
|
|
+// RestTemplate client = new RestTemplate();
|
|
|
+// HttpHeaders headers = new HttpHeaders();
|
|
|
+// headers.add("lpToken",sign);
|
|
|
+// headers.add("toonType","10200001");
|
|
|
+// headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
|
|
+// HttpEntity<Testa> requestEntity = new HttpEntity(a,headers);
|
|
|
+// ResponseEntity<Object> exchange = null;
|
|
|
+// exchange = client.exchange(httpUrl, HttpMethod.POST, requestEntity, Object.class);
|
|
|
+// System.out.println(exchange.getBody());
|
|
|
+ }
|
|
|
+
|
|
|
+// public String getUserInfo(@RequestParam(name = "code") String code) throws Exception {
|
|
|
+// System.out.println("code" + code);
|
|
|
+// String url = "https://api.weixin.qq.com/sns/jscode2session";
|
|
|
+// url += "?appid=xxxxxxxxxxxxx";//自己的appid
|
|
|
+// url += "&secret=xxxxxxxxxxxxxxxxxxx";//自己的appSecret
|
|
|
+// url += "&js_code=" + code;
|
|
|
+// url += "&grant_type=authorization_code";
|
|
|
+// url += "&connect_redirect=1";
|
|
|
+// String res = null;
|
|
|
+// CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
+// // DefaultHttpClient();
|
|
|
+// HttpGet httpget = new HttpGet(url); //GET方式
|
|
|
+// CloseableHttpResponse response = null;
|
|
|
+// // 配置信息
|
|
|
+// RequestConfig requestConfig = RequestConfig.custom() // 设置连接超时时间(单位毫秒)
|
|
|
+// .setConnectTimeout(5000) // 设置请求超时时间(单位毫秒)
|
|
|
+// .setConnectionRequestTimeout(5000) // socket读写超时时间(单位毫秒)
|
|
|
+// .setSocketTimeout(5000) // 设置是否允许重定向(默认为true)
|
|
|
+// .setRedirectsEnabled(false).build(); // 将上面的配置信息 运用到这个Get请求里
|
|
|
+// httpget.setConfig(requestConfig); // 由客户端执行(发送)Get请求
|
|
|
+// response = httpClient.execute(httpget); // 从响应模型中获取响应实体
|
|
|
+// HttpEntity responseEntity = (HttpEntity) response.getEntity();
|
|
|
+// System.out.println("响应状态为:" + response.getStatusLine());
|
|
|
+// if (responseEntity != null) {
|
|
|
+// res = EntityUtils.toString(responseEntity);
|
|
|
+// System.out.println("响应内容长度为:" + responseEntity.getContentLength());
|
|
|
+// System.out.println("响应内容为:" + res);
|
|
|
+// }
|
|
|
+// // 释放资源
|
|
|
+// if (httpClient != null) {
|
|
|
+// httpClient.close();
|
|
|
+// }
|
|
|
+// if (response != null) {
|
|
|
+// response.close();
|
|
|
+// }
|
|
|
+// JSONObject jo = JSON.parseObject(res);
|
|
|
+// String openid = jo.getString("openid");
|
|
|
+// System.out.println("openid" + openid);
|
|
|
+// return openid;
|
|
|
+// }
|
|
|
+ //获取用小程序用户的openId
|
|
|
+ public static String getUserInfo(@RequestParam(name= "code")String code) throws Exception {
|
|
|
+ RestTemplate client = new RestTemplate();
|
|
|
+ String url = "http://gw-test.bjzhengxin.com.cn/api/mp/login";
|
|
|
+ HttpHeaders headers1 = new HttpHeaders();
|
|
|
+ headers1.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
+ MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
|
|
|
+ map.add("appid", "wxa6edcc091ed14847");
|
|
|
+ map.add("paasid", "tyrz");
|
|
|
+ map.add("code", "061x8Z000NvOkM1BsP300iCAwh1x8Z0O");
|
|
|
+ HttpEntity<MultiValueMap<String, String>> request = new HttpEntity(map,headers1);
|
|
|
+ ResponseEntity<String> result = client.postForEntity(url, request, String.class);
|
|
|
+ String body = new String(URLDecoder.decode(result.getBody().getBytes(StandardCharsets.UTF_8)));
|
|
|
+ System.out.println(body);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(body.substring(0,body.length()-1));
|
|
|
+ System.out.println(jsonObject.get("errcode"));
|
|
|
+ return JSON.toJSONString(result.getBody());
|
|
|
+ }
|
|
|
+
|
|
|
+}
|