|
|
@@ -0,0 +1,396 @@
|
|
|
+package com.bbeng.www.GameCenter;
|
|
|
+
|
|
|
+//http请求
|
|
|
+
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+//post请求
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.HttpStatus;
|
|
|
+
|
|
|
+//json
|
|
|
+//import com.alibaba.fastjson.JSONObject;
|
|
|
+//import com.alibaba.fastjson.JSONException;
|
|
|
+//import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+
|
|
|
+
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+
|
|
|
+import org.springframework.util.DigestUtils;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+//import javax.annotation.PostConstruct;
|
|
|
+
|
|
|
+//加密
|
|
|
+import javax.crypto.Mac;
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
+
|
|
|
+import sun.misc.BASE64Decoder;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description:
|
|
|
+ * @Author: zhangxy
|
|
|
+ * @Date: Created in 2019/12/16
|
|
|
+ */
|
|
|
+@RestController // 使用@RestController替代@Controller和@ResponseBody
|
|
|
+public class Controller {
|
|
|
+ @RequestMapping("/testGet")
|
|
|
+ public String Hello(HttpServletRequest request) {
|
|
|
+
|
|
|
+ String name = request.getParameter("name");
|
|
|
+ String message = request.getParameter("message");
|
|
|
+
|
|
|
+ System.out.println("name is:" + name);
|
|
|
+ System.out.println("message is:" + message);
|
|
|
+ return "Get 请求可用";
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value = "/testPost", method = RequestMethod.POST)
|
|
|
+ public String TestPost(String name, String message) {
|
|
|
+ System.out.println("name is:" + name);
|
|
|
+ System.out.println("message is:" + message);
|
|
|
+ return "Post 请求可用";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @RequestMapping("/testMysql")
|
|
|
+ public String Hello(HttpServletRequest request) {
|
|
|
+
|
|
|
+ String name = request.getParameter("name");
|
|
|
+ String message = request.getParameter("message");
|
|
|
+
|
|
|
+ System.out.println("name is:" + name);
|
|
|
+ System.out.println("message is:" + message);
|
|
|
+ return "Get 请求可用";
|
|
|
+ }
|
|
|
+
|
|
|
+ public String channel = "14633";
|
|
|
+ public String openid = "sdsj2020";
|
|
|
+ public int time = Integer.parseInt(String.valueOf(System.currentTimeMillis()).substring(0, 10));
|
|
|
+ public String nick = "sdsj2020";
|
|
|
+ public String avatar = "https://img.m3guo.com/group4/M00/00/5A/wKgMHF_ODXGAfBHbAADCVYIwtio762.png";
|
|
|
+ public int sex = 1;
|
|
|
+ public String phone = "15017534446";
|
|
|
+
|
|
|
+ public final String appkey = "0262d5626aa24065b39eb3c185af910e";
|
|
|
+
|
|
|
+ public String openidEncoder = openid;
|
|
|
+ public String nickEncoder = nick;
|
|
|
+ public String avatarEncoder = avatar;
|
|
|
+
|
|
|
+ public final String AppKey = "AKIDcZVSyyl8O2nzsFrKNsyj5sD1eCfJlRCI";
|
|
|
+ public final String AppSecret = "ASIDZwO4MejWZK81boBTqEU6hy3Ad6D4JFHbRm4HjugGMoQe2a2HgcZ7CJJkxTy7DBjS";
|
|
|
+
|
|
|
+ static String APP_KEY = "AKIDcZVSyyl8O2nzsFrKNsyj5sD1eCfJlRCI";
|
|
|
+ static String APP_SECRET = "ASIDZwO4MejWZK81boBTqEU6hy3Ad6D4JFHbRm4HjugGMoQe2a2HgcZ7CJJkxTy7DBjS";
|
|
|
+
|
|
|
+ // @PostConstruct
|
|
|
+ @RequestMapping("/login")
|
|
|
+ public String Login(String open_id) {
|
|
|
+ openid = open_id;
|
|
|
+ nick = phone.substring(0, 3) + "****" + phone.substring(7, 11);
|
|
|
+ System.out.println("openid is:" + openid);
|
|
|
+ try {
|
|
|
+ openidEncoder = URLEncoder.encode(openid, "UTF-8")
|
|
|
+ .replaceAll("\\+", "%20")
|
|
|
+ .replaceAll("\\!", "%21")
|
|
|
+ .replaceAll("\\'", "%27")
|
|
|
+ .replaceAll("\\(", "%28")
|
|
|
+ .replaceAll("\\)", "%29")
|
|
|
+ .replaceAll("\\~", "%7E");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ nickEncoder = URLEncoder.encode(nick, "UTF-8")
|
|
|
+ .replaceAll("\\+", "%20")
|
|
|
+ .replaceAll("\\!", "%21")
|
|
|
+ .replaceAll("\\'", "%27")
|
|
|
+ .replaceAll("\\(", "%28")
|
|
|
+ .replaceAll("\\)", "%29")
|
|
|
+ .replaceAll("\\~", "%7E");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ avatarEncoder = URLEncoder.encode(avatar, "UTF-8")
|
|
|
+ .replaceAll("\\+", "%20")
|
|
|
+ .replaceAll("\\!", "%21")
|
|
|
+ .replaceAll("\\'", "%27")
|
|
|
+ .replaceAll("\\(", "%28")
|
|
|
+ .replaceAll("\\)", "%29")
|
|
|
+ .replaceAll("\\~", "%7E");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return "success";
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/getAccountInfo")
|
|
|
+ public String GetAccountInfo(String openid) {
|
|
|
+
|
|
|
+ String home_url = "http://testexapi.bqi18n.com/account/info?";
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Compute signature
|
|
|
+ *
|
|
|
+ * @param url Url
|
|
|
+ * @param method Request method
|
|
|
+ * @param map params and headers
|
|
|
+ * @return String
|
|
|
+ */
|
|
|
+ private static String computeSign(String url, String method, Map<String, Object> map) {
|
|
|
+ List<Map.Entry<String, Object>> paramList = new ArrayList<>(map.entrySet());
|
|
|
+ paramList.sort(Comparator.comparing(o -> (o.getKey())));
|
|
|
+ List<String> pairs = new ArrayList();
|
|
|
+ for (Map.Entry<String, Object> item : paramList) {
|
|
|
+ pairs.add(String.format("%s=%s", item.getKey(), item.getValue()));
|
|
|
+ }
|
|
|
+
|
|
|
+ String sign = "";
|
|
|
+ String queryString = String.join("&", pairs);
|
|
|
+ String signString = String.format("%s%s?%s", method, url, queryString);
|
|
|
+
|
|
|
+ try {
|
|
|
+ signString = URLEncoder.encode(signString, "UTF-8");
|
|
|
+ sign = GCommonUtils.getHmacSHA256(APP_SECRET, signString);
|
|
|
+ sign = Base64.getEncoder().encodeToString(sign.getBytes());
|
|
|
+// log.info("signString: {}\n sign: {}", signString, sign);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+// log.error("", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return sign;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static HttpHeaders buildHeaders(String url, String method, String action, Map<String, Object> params) {
|
|
|
+ // headers
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("AppKey", APP_KEY);
|
|
|
+ headers.add("Action", action);
|
|
|
+ headers.add("Timestamp", String.valueOf(System.currentTimeMillis()).substring(0, 10));
|
|
|
+ headers.add("Nonce", "123456");
|
|
|
+ headers.add("SignatureMethod", "HmacSHA256");
|
|
|
+
|
|
|
+ // sign
|
|
|
+ Map<String, Object> paramsMap = new HashMap<>();
|
|
|
+ Map<String, String> headersMap = headers.toSingleValueMap();
|
|
|
+ paramsMap.putAll(headersMap);
|
|
|
+ paramsMap.putAll(params);
|
|
|
+ String sign = computeSign(url, method, paramsMap);
|
|
|
+
|
|
|
+ // append Authorization to headers
|
|
|
+ headers.add("Authorization", sign);
|
|
|
+// headers.add("Source", "HOHO");
|
|
|
+
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
|
|
+ String REST_UA = "BQopen";
|
|
|
+ headers.set(HttpHeaders.USER_AGENT, REST_UA);
|
|
|
+
|
|
|
+ return headers;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String buildSign(String url, String method, String action, Map<String, Object> params) {
|
|
|
+
|
|
|
+ String APP_KEY = "AKIDcZVSyyl8O2nzsFrKNsyj5sD1eCfJlRCI";
|
|
|
+
|
|
|
+// String APP_KEY = "ASIDZwO4MejWZK81boBTqEU6hy3Ad6D4JFHbRm4HjugGMoQe2a2HgcZ7CJJkxTy7DBjS";
|
|
|
+// String srcStr1 = "POST%2Fopen%2Fv1%2Fpay%2Freceipt%2Forder%3FAction%3Dpay%26AppKey%3DAKIDcZVSyyl8O2nzsFrKNsyj5sD1eCfJlRCI%26Nonce%3D123456%26SignatureMethod%3DHmacSHA256%26Timestamp%3D1465185768000%26amount%3D1%26diallingCode%3D86%26note%3Dnote%26phone%3D18180980860";
|
|
|
+
|
|
|
+ // headers
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("AppKey", APP_KEY);
|
|
|
+ headers.add("Action", action);
|
|
|
+ headers.add("Timestamp", String.valueOf(System.currentTimeMillis()).substring(0, 10));
|
|
|
+ headers.add("Nonce", "123456");
|
|
|
+ headers.add("SignatureMethod", "HmacSHA256");
|
|
|
+
|
|
|
+ // sign
|
|
|
+ Map<String, Object> paramsMap = new HashMap<>();
|
|
|
+ Map<String, String> headersMap = headers.toSingleValueMap();
|
|
|
+ paramsMap.putAll(headersMap);
|
|
|
+ paramsMap.putAll(params);
|
|
|
+ String sign = computeSign(url, method, paramsMap);
|
|
|
+
|
|
|
+ return sign;
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/getAuthorization")
|
|
|
+ public String GetAuthorization(String openid) {
|
|
|
+
|
|
|
+// System.out.println("openid is:"+openid);
|
|
|
+ String home_url = "http://testexapi.bqi18n.com/authorized/user";
|
|
|
+ String url = home_url;
|
|
|
+ System.out.println("url is:" + url);
|
|
|
+
|
|
|
+ Map<String, Object> params = new HashMap<String, Object>() {{
|
|
|
+ put("openid", openid);
|
|
|
+ put("tokenId", "");
|
|
|
+ put("baseCurrency", "");
|
|
|
+// put("locale", locale);
|
|
|
+ }};
|
|
|
+
|
|
|
+ String headersAuthor = buildSign(home_url, "POST", "authorized", params);
|
|
|
+ System.out.println("headersAuthor is:" + headersAuthor);
|
|
|
+ String jsonParam = " { \"openid\" : " + openid + "}";
|
|
|
+
|
|
|
+
|
|
|
+ CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
|
|
|
+ CloseableHttpResponse closeableHttpResponse;
|
|
|
+
|
|
|
+ try {
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ httpPost.setHeader("Action", "authorized");
|
|
|
+ httpPost.setHeader("AppKey", AppKey);
|
|
|
+ httpPost.setHeader("Timestamp", String.valueOf(System.currentTimeMillis()).substring(0, 10));
|
|
|
+ httpPost.setHeader("Nonce", "123456");
|
|
|
+ httpPost.setHeader("SignatureMethod", "HmacSHA256");
|
|
|
+ httpPost.setHeader("Authorization", headersAuthor);
|
|
|
+
|
|
|
+// httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
|
|
|
+ StringEntity entity = new StringEntity(jsonParam, "UTF-8");
|
|
|
+
|
|
|
+// StringEntity entity = new StringEntity(jsonParam, ContentType.create("text/json", "UTF-8"));
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
|
|
|
+ httpPost.setConfig(requestConfig);
|
|
|
+
|
|
|
+ closeableHttpResponse = closeableHttpClient.execute(httpPost);
|
|
|
+
|
|
|
+ int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
|
|
|
+ if (statusCode != HttpStatus.SC_OK) {
|
|
|
+ //TODO:状态码非200代表没有正常返回,此处处理你的业务
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpEntity httpEntity = closeableHttpResponse.getEntity();
|
|
|
+ String asset_synchronization = EntityUtils.toString(httpEntity, "UTF-8");
|
|
|
+
|
|
|
+ System.out.println("asset_synchronization is:" + asset_synchronization);
|
|
|
+// JSONArray jsonArray = JSONArray.parseArray(asset_synchronization);
|
|
|
+// if (jsonArray.size() > 0) {
|
|
|
+// //TODO:判断返回的json数组是否不为空,此处即可处理你的业务
|
|
|
+// System.out.println("asset_synchronization is:"+asset_synchronization);
|
|
|
+// }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value = "/getGameList")
|
|
|
+ public String GetGameList() {
|
|
|
+// http://platform.shandw.com/getGameInfoList?channelId=14633&sec=1609102178025&sign=3124514e9d200414fdc0783b335fae4d
|
|
|
+ System.out.println("getGameList");
|
|
|
+ String MyGameListUrl = makeMyGameListUrl();
|
|
|
+ System.out.println("MyGameListUrl is:" + MyGameListUrl);
|
|
|
+ //初始化HttpClient
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ //创建HttpGet
|
|
|
+ HttpGet httpGet = new HttpGet(MyGameListUrl);
|
|
|
+ //发起请求,获取response对象
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ try {
|
|
|
+ response = httpClient.execute(httpGet);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ //获取请求状态码
|
|
|
+ //response.getStatusLine().getStatusCode();
|
|
|
+ //获取返回数据实体对象
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ //转为字符串
|
|
|
+ String result = null;
|
|
|
+ try {
|
|
|
+ result = EntityUtils.toString(entity, "UTF-8");
|
|
|
+ System.out.println("getGameList result is:" + result);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String makeMyGameListUrl() {
|
|
|
+ long sec = Long.parseLong(String.valueOf(System.currentTimeMillis()));
|
|
|
+ final String myListSignString = channel + sec + appkey;
|
|
|
+ String myListSign = DigestUtils.md5DigestAsHex(myListSignString.getBytes());
|
|
|
+ final String myListString = "channelId=" + channel + "&sec=" + sec + "&sign=" + myListSign;
|
|
|
+ String myListUrl = "http://platform.shandw.com/getGameInfoList?" + myListString;
|
|
|
+ return myListUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+// @RequestMapping(value = "/getShanWanGameCenterUrl")
|
|
|
+// public String GetShanWanGameCenterUrl(String openid) {
|
|
|
+// final String signString ="channel="+channel+"&openid="+openidEncoder+"&time="+time+"&nick="+nick+"&avatar="+avatar+"&sex="+sex+"&phone="+phone+appkey;
|
|
|
+// System.out.println("signString is:" + signString);
|
|
|
+// String sign = DigestUtils.md5DigestAsHex(signString.getBytes());
|
|
|
+// System.out.println("sign is:" + sign);
|
|
|
+// //url
|
|
|
+// String home_url = "http://www.shandw.com/auth/?";
|
|
|
+// int sdw_simple = 6;
|
|
|
+// int sdw_ld = 1;
|
|
|
+// String url =home_url+"channel="+channel+"&openid="+openidEncoder+"&time="+time+"&nick="+nickEncoder+"&avatar="+avatarEncoder+"&sex="+sex+"&phone="+phone+"&sign="+sign+"&sdw_simple="+sdw_simple+"&sdw_ld="+sdw_ld;
|
|
|
+// System.out.println("url is:" + url);
|
|
|
+//
|
|
|
+// return url;
|
|
|
+// }
|
|
|
+//
|
|
|
+// @RequestMapping(value = "/getGameUrl")
|
|
|
+// public String GetShanWanGameCenterUrl(String openid, int gid) {
|
|
|
+//
|
|
|
+// String openidEncoder = "";
|
|
|
+// try {
|
|
|
+// openidEncoder = URLEncoder.encode(openid, "UTF-8")
|
|
|
+// .replaceAll("\\+", "%20")
|
|
|
+// .replaceAll("\\!", "%21")
|
|
|
+// .replaceAll("\\'", "%27")
|
|
|
+// .replaceAll("\\(", "%28")
|
|
|
+// .replaceAll("\\)", "%29")
|
|
|
+// .replaceAll("\\~", "%7E");
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+//
|
|
|
+// final String signString ="channel="+channel+"&openid="+openidEncoder+"&time="+time+"&nick="+nick+"&avatar="+avatar+"&sex="+sex+"&phone="+phone+appkey;
|
|
|
+// System.out.println("signString is:" + signString);
|
|
|
+// String sign = DigestUtils.md5DigestAsHex(signString.getBytes());
|
|
|
+// System.out.println("sign is:" + sign);
|
|
|
+// //url
|
|
|
+// String home_url = "http://www.shandw.com/auth/?";
|
|
|
+// int sdw_simple = 6;
|
|
|
+// int sdw_ld = 1;
|
|
|
+//
|
|
|
+// String url = home_url+"channel="+channel+"&openid="+openidEncoder+"&time="+time+"&nick="+nickEncoder+"&avatar="+avatarEncoder+"&sex="+sex+"&phone="+phone+"&sign="+sign+"&sdw_simple="+sdw_simple+"&sdw_ld="+sdw_ld+gid;
|
|
|
+// System.out.println("url is:" + url);
|
|
|
+//
|
|
|
+// return url;
|
|
|
+// }
|
|
|
+}
|