dev-discount
= 3 years ago
commit 9f91550bb7
  1. 1
      hai-cweb/src/main/java/com/cweb/config/AuthConfig.java
  2. 75
      hai-cweb/src/main/java/com/cweb/controller/tPigController.java
  3. 38
      hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java
  4. 44
      hai-service/src/main/java/com/hai/common/utils/WxUtils.java

@ -113,6 +113,7 @@ public class AuthConfig implements WebMvcConfigurer {
.excludePathPatterns("/wechat/*")
.excludePathPatterns("/tuanyou/*")
.excludePathPatterns("/common/*")
.excludePathPatterns("/tPig/*")
;
}

@ -0,0 +1,75 @@
package com.cweb.controller;
import com.alipay.api.domain.Data;
import com.cweb.config.SysConst;
import com.hai.common.pay.util.sdk.WXPayConstants;
import com.hai.common.security.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.HttpsUtils;
import com.hai.common.utils.MD5Util;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.common.utils.WxUtils;
import com.hai.entity.HighUser;
import com.hai.model.HighUserModel;
import com.hai.model.ResponseData;
import com.hai.service.HighUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping(value = "/tPig")
@Api(value = "千猪接口")
public class tPigController {
@Resource
private HighUserService highUserService;
@Autowired
private UserCenter userCenter;
@RequestMapping(value = "/getTPigToken", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "【小程序】获取千猪Token")
public ResponseData getH5AccessToken(HttpServletRequest request) {
try {
// 用户
// SessionObject sessionObject = userCenter.getSessionObject(request);
// HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject();
//
// HighUser highUser = highUserService.findByUserId(userInfoModel.getHighUser().getId());
Map<String, String> params = new HashMap<>();
// params.put("platformUniqueId", highUser.getUnionId());
// params.put("nickname", highUser.getName());
// params.put("mobile", highUser.getPhone());
params.put("platformUniqueId", "oArhO6WlHsR2e5LuPUbxTcGo4nsQ");
params.put("nickname", "Sum1Dream");
params.put("platformId", "10376");
params.put("mobile", "18090580471");
// params.put("timestamp", String.valueOf(new Date().getTime()));
params.put("timestamp", "1624793550754");
String sign = WxUtils.generateSignaturePig(params, "ktxb49sh2jfhgn8g" , WXPayConstants.SignType.MD5);
params.put("sign", sign);
return ResponseMsgUtil.success(HttpsUtils.doGet("https://live-test.qianzhu8.com/api/v2/platform/getToken", params , ""));
} catch (Exception e) {
return ResponseMsgUtil.exception(e);
}
}
}

@ -126,6 +126,44 @@ public class HttpsUtils {
return null;
}
public static JSONObject doGet(String url, Map<String, String> params , String o) {
String apiUrl = url;
StringBuffer param = new StringBuffer();
int i = 0;
for (String key : params.keySet()) {
if (i == 0)
param.append("?");
else
param.append("&");
param.append(key).append("=").append(params.get(key));
i++;
}
apiUrl += param;
String result = null;
HttpClient httpClient = null;
try {
if (apiUrl.startsWith("https")) {
httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory())
.setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build();
} else {
httpClient = HttpClients.createDefault();
}
HttpGet httpGet = new HttpGet(apiUrl);
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
result = IOUtils.toString(instream, "UTF-8");
}
return JSON.parseObject(result);
} catch (Exception e) {
log.error(e.getMessage(),e);
}
return null;
}
public static JSONObject doGet(String url, Map<String, String> params , Map<String , String> headers) {
String apiUrl = url;
StringBuffer param = new StringBuffer();

@ -237,7 +237,7 @@ public class WxUtils {
}
sb.append("key=").append(key);
if (WXPayConstants.SignType.MD5.equals(signType)) {
return MD5(sb.toString()).toUpperCase();
return MD5(sb.toString() , true);
}
else if (WXPayConstants.SignType.HMACSHA256.equals(signType)) {
return HMACSHA256(sb.toString(), key);
@ -289,16 +289,22 @@ public class WxUtils {
* @param data 待处理数据
* @return MD5结果
*/
public static String MD5(String data) throws Exception {
public static String MD5(String data , Boolean isUpperCase) throws Exception {
java.security.MessageDigest md = MessageDigest.getInstance("MD5");
byte[] array = md.digest(data.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString().toUpperCase();
if (isUpperCase) {
return sb.toString().toUpperCase();
} else {
return sb.toString();
}
}
/**
* 将Map转换为XML格式的字符串
*
@ -338,6 +344,38 @@ public class WxUtils {
}
/**
* 千猪生成签名
*
* @param data 待签名数据
* @param secret API密钥
* @param signType 签名方式
* @return 签名
*/
public static String generateSignaturePig(final Map<String, String> data, String secret, WXPayConstants.SignType signType) throws Exception {
Set<String> keySet = data.keySet();
String[] keyArray = keySet.toArray(new String[keySet.size()]);
Arrays.sort(keyArray);
StringBuilder sb = new StringBuilder();
for (String k : keyArray) {
if (k.equals(WXPayConstants.FIELD_SIGN)) {
continue;
}
if (data.get(k).trim().length() > 0) // 参数值为空,则不参与签名
sb.append(k).append("=").append(data.get(k).trim()).append("&");
}
String s = sb.substring(0, sb.length() - 1) + secret;
if (WXPayConstants.SignType.MD5.equals(signType)) {
return MD5(sb.toString() , false);
}
else if (WXPayConstants.SignType.HMACSHA256.equals(signType)) {
return HMACSHA256(s, secret);
}
else {
throw new Exception(String.format("Invalid sign_type: %s", signType));
}
}
}

Loading…
Cancel
Save