From 09fb469e727266e3cc75cfd7a571c674c52e3465 Mon Sep 17 00:00:00 2001 From: hurui <177768073@qq.com> Date: Tue, 21 May 2024 14:05:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/hfkj/common/utils/WxUtils.java | 44 +++++++++++++++++++ .../com/user/controller/BsUserController.java | 27 +++++++++--- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/service/src/main/java/com/hfkj/common/utils/WxUtils.java b/service/src/main/java/com/hfkj/common/utils/WxUtils.java index 0dbb81d..491c072 100644 --- a/service/src/main/java/com/hfkj/common/utils/WxUtils.java +++ b/service/src/main/java/com/hfkj/common/utils/WxUtils.java @@ -167,6 +167,50 @@ public class WxUtils { return new String(nonceChars); } + /** + * 用SHA1算法生成安全签名 + * @param data 数据 + * @return 安全签名 + * @throws Exception + */ + public static String getSHA1(final Map data) throws Exception + { + try { + Set 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) || k.equals("apiKey")) { + continue; + } + if (data.get(k) != null) // 参数值为空,则不参与签名 + { + sb.append(k).append("=").append(data.get(k)).append("&"); + } + } + String str = sb.substring(0 , sb.length()-1); + // SHA1签名生成 + MessageDigest md = MessageDigest.getInstance("SHA-1"); + md.update(str.getBytes()); + byte[] digest = md.digest(); + + StringBuffer hexstr = new StringBuffer(); + String shaHex = ""; + for (int i = 0; i < digest.length; i++) { + shaHex = Integer.toHexString(digest[i] & 0xFF); + if (shaHex.length() < 2) { + hexstr.append(0); + } + hexstr.append(shaHex); + } + return hexstr.toString(); + } catch (Exception e) { + e.printStackTrace(); + throw new Exception(String.format("Invalid sign_type: %s")); + } + } + /** * 拼接签名数据 * diff --git a/user/src/main/java/com/user/controller/BsUserController.java b/user/src/main/java/com/user/controller/BsUserController.java index e6dfd64..0682b37 100644 --- a/user/src/main/java/com/user/controller/BsUserController.java +++ b/user/src/main/java/com/user/controller/BsUserController.java @@ -7,9 +7,7 @@ import com.hfkj.common.exception.ErrorCode; import com.hfkj.common.exception.ErrorHelp; import com.hfkj.common.exception.SysCode; import com.hfkj.common.security.UserCenter; -import com.hfkj.common.utils.MemberValidateUtil; -import com.hfkj.common.utils.RedisUtil; -import com.hfkj.common.utils.ResponseMsgUtil; +import com.hfkj.common.utils.*; import com.hfkj.config.WxMaConfig; import com.hfkj.entity.BsUser; import com.hfkj.model.ResponseData; @@ -26,14 +24,12 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -139,6 +135,23 @@ public class BsUserController { } } + @RequestMapping(value = "/getH5AccessToken", method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "【H5】获取AccessToken") + public ResponseData getH5AccessToken(@RequestParam(value = "code", required = true) String code) { + try { + Map params = new HashMap<>(); + params.put("appid", "wxa075e8509802f826"); + params.put("secret", "0e606fc1378d35e359fcf3f15570b2c5"); + params.put("code", code); + params.put("grant_type", "authorization_code"); + return ResponseMsgUtil.success(HttpsUtils.doGet("https://api.weixin.qq.com/sns/oauth2/access_token", params)); + + } catch (Exception e) { + return ResponseMsgUtil.exception(e); + } + } + @RequestMapping(value = "/wechatMaPhone", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "微信小程序手机号登录")