fix wechat share

main
Revers 8 months ago
parent 16ac7675c4
commit 8783b25c06

@ -1,6 +1,8 @@
package com.luoo.user.controller; package com.luoo.user.controller;
import annotation.VerifyParam;
import api.Result; import api.Result;
import com.luoo.user.dto.request.WeChatShareReq;
import com.luoo.user.service.WeChatAouth2Service; import com.luoo.user.service.WeChatAouth2Service;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
@ -24,8 +26,9 @@ public class WeChatAouth2Controller extends BaseController {
@ApiOperation(value = "分享获取微信token", notes = "成功后返回token,不成功返回null") @ApiOperation(value = "分享获取微信token", notes = "成功后返回token,不成功返回null")
@PostMapping("/getShareAccessToken") @PostMapping("/getShareAccessToken")
public Result getShareAccessToken() { public Result getShareAccessToken(@RequestBody @VerifyParam WeChatShareReq weChatShareReq) {
return weChatAouth2Service.getShareToken();
return weChatAouth2Service.getShareToken(weChatShareReq.getUrl());
} }
// @ApiOperation(value = "微信登录/注册",notes = "微信登录/注册,成功后返回authorization") // @ApiOperation(value = "微信登录/注册",notes = "微信登录/注册,成功后返回authorization")

@ -0,0 +1,19 @@
package com.luoo.user.dto.request;
import annotation.VerifyParam;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @author Revers.
* @date 2024/03/29 20:32
**/
@Data
public class WeChatShareReq {
@ApiModelProperty(name = "url", value = "当前网页的URL不包含#及其后面部分", required = true)
@VerifyParam(required = true)
private String url;
}

@ -31,6 +31,9 @@ import java.io.UnsupportedEncodingException;
import java.net.URI; import java.net.URI;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@ -63,10 +66,10 @@ public class WeChatAouth2Service {
@Autowired @Autowired
private UserInfoDao userInfoDao; private UserInfoDao userInfoDao;
public Result getShareToken(){ 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(ticket); return Result.success(sha1Signature(url,ticket));
Aouth2WeChatStableTokenResponse accessToken = getAccessShareToken(); Aouth2WeChatStableTokenResponse accessToken = getAccessShareToken();
if(accessToken == null || accessToken.getAccess_token() == null ) if(accessToken == null || accessToken.getAccess_token() == null )
@ -77,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(aouth2WeChatJSAPIResponse.getTicket()); return Result.success(sha1Signature(url,aouth2WeChatJSAPIResponse.getTicket()));
} }
// public Result loginOrRegister(WeChatLoginReq loginReq,String ipAddress){ // public Result loginOrRegister(WeChatLoginReq loginReq,String ipAddress){
@ -97,6 +100,35 @@ public class WeChatAouth2Service {
// return Result.success(token); // return Result.success(token);
// } // }
public String sha1Signature(String url,String ticket) {
String signature = "jsapi_ticket=" + ticket + "&noncestr=" + UUID.randomUUID()
+ "&timestamp=" +System.currentTimeMillis() / 1000 + "&url=" + url;
try {
//指定sha1算法
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.update(signature.getBytes());
//获取字节数组
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexString = new StringBuffer();
// 字节数组转换为 十六进制 数
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString().toUpperCase();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public Result login(WeChatLoginReq loginReq,String ipAddress){ public Result login(WeChatLoginReq loginReq,String ipAddress){
Aouth2WeChatTokenResponse accessToken = getAccessToken(loginReq.getCode()); Aouth2WeChatTokenResponse accessToken = getAccessToken(loginReq.getCode());

Loading…
Cancel
Save