fix wechat share

main
Revers 8 months ago
parent 8783b25c06
commit 9e5312faeb

@ -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;
}

@ -69,7 +69,7 @@ public class WeChatAouth2Service {
public Result getShareToken(String url){ public Result getShareToken(String url){
String ticket = (String)redisTemplate.opsForValue().get("shareTicket"); String ticket = (String)redisTemplate.opsForValue().get("shareTicket");
if(ticket != null && StringUtils.isNotBlank(ticket)) if(ticket != null && StringUtils.isNotBlank(ticket))
return Result.success(sha1Signature(url,ticket)); return sha1Signature(url, ticket);
Aouth2WeChatStableTokenResponse accessToken = getAccessShareToken(); Aouth2WeChatStableTokenResponse accessToken = getAccessShareToken();
if(accessToken == null || accessToken.getAccess_token() == null ) if(accessToken == null || accessToken.getAccess_token() == null )
@ -80,7 +80,7 @@ public class WeChatAouth2Service {
return Result.failed("分享微信失败"); return Result.failed("分享微信失败");
redisTemplate.opsForValue().set("shareTicket",aouth2WeChatJSAPIResponse.getTicket(),6000,TimeUnit.SECONDS); 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){ // public Result loginOrRegister(WeChatLoginReq loginReq,String ipAddress){
@ -101,9 +101,14 @@ public class WeChatAouth2Service {
// } // }
public String sha1Signature(String url,String ticket) { public Result sha1Signature(String url,String ticket) {
String signature = "jsapi_ticket=" + ticket + "&noncestr=" + UUID.randomUUID()
+ "&timestamp=" +System.currentTimeMillis() / 1000 + "&url=" + url; Aouth2WeChatShareResponse aouth2WeChatShareResponse = new Aouth2WeChatShareResponse();
String timestamp = Long.toString(System.currentTimeMillis() / 1000);
String noncestr = String.valueOf(UUID.randomUUID());
String signature = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr
+ "&timestamp=" + timestamp + "&url=" + url;
try { try {
//指定sha1算法 //指定sha1算法
MessageDigest digest = MessageDigest.getInstance("SHA-1"); MessageDigest digest = MessageDigest.getInstance("SHA-1");
@ -120,13 +125,17 @@ public class WeChatAouth2Service {
} }
hexString.append(shaHex); 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) { } catch (NoSuchAlgorithmException e) {
e.printStackTrace(); e.printStackTrace();
} }
return null; return Result.failed();
} }
public Result login(WeChatLoginReq loginReq,String ipAddress){ public Result login(WeChatLoginReq loginReq,String ipAddress){

Loading…
Cancel
Save