diff --git a/luoo_user/src/main/java/com/luoo/user/dto/response/Aouth2WeChatShareResponse.java b/luoo_user/src/main/java/com/luoo/user/dto/response/Aouth2WeChatShareResponse.java new file mode 100644 index 0000000..c179e7d --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dto/response/Aouth2WeChatShareResponse.java @@ -0,0 +1,15 @@ +package com.luoo.user.dto.response; + +import lombok.Data; + +/** + * @author Revers. + * @date 2024/03/29 20:41 + **/ + +@Data +public class Aouth2WeChatShareResponse { + private String timestamp; + private String nonceStr; + private String signature; +} diff --git a/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java b/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java index c9f300e..03bee17 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java +++ b/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java @@ -69,7 +69,7 @@ public class WeChatAouth2Service { public Result getShareToken(String url){ String ticket = (String)redisTemplate.opsForValue().get("shareTicket"); if(ticket != null && StringUtils.isNotBlank(ticket)) - return Result.success(sha1Signature(url,ticket)); + return sha1Signature(url, ticket); Aouth2WeChatStableTokenResponse accessToken = getAccessShareToken(); if(accessToken == null || accessToken.getAccess_token() == null ) @@ -80,7 +80,7 @@ public class WeChatAouth2Service { return Result.failed("分享微信失败"); redisTemplate.opsForValue().set("shareTicket",aouth2WeChatJSAPIResponse.getTicket(),6000,TimeUnit.SECONDS); - return Result.success(sha1Signature(url,aouth2WeChatJSAPIResponse.getTicket())); + return sha1Signature(url,aouth2WeChatJSAPIResponse.getTicket()); } // public Result loginOrRegister(WeChatLoginReq loginReq,String ipAddress){ @@ -101,9 +101,14 @@ public class WeChatAouth2Service { // } - public String sha1Signature(String url,String ticket) { - String signature = "jsapi_ticket=" + ticket + "&noncestr=" + UUID.randomUUID() - + "×tamp=" +System.currentTimeMillis() / 1000 + "&url=" + url; + public Result sha1Signature(String url,String ticket) { + + Aouth2WeChatShareResponse aouth2WeChatShareResponse = new Aouth2WeChatShareResponse(); + String timestamp = Long.toString(System.currentTimeMillis() / 1000); + String noncestr = String.valueOf(UUID.randomUUID()); + + String signature = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + + "×tamp=" + timestamp + "&url=" + url; try { //指定sha1算法 MessageDigest digest = MessageDigest.getInstance("SHA-1"); @@ -120,13 +125,17 @@ public class WeChatAouth2Service { } hexString.append(shaHex); } - return hexString.toString().toUpperCase(); + + aouth2WeChatShareResponse.setSignature(hexString.toString().toUpperCase()); + aouth2WeChatShareResponse.setTimestamp(timestamp); + aouth2WeChatShareResponse.setNonceStr(noncestr); + return Result.success(aouth2WeChatShareResponse); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } - return null; + return Result.failed(); } public Result login(WeChatLoginReq loginReq,String ipAddress){