fix wechat share

main
Revers 10 months ago
parent 26cc96e9c8
commit 16ac7675c4

@ -83,4 +83,6 @@ public class Constants {
public static final String HTTPS_API_WEIXIN_QQ_JSAPI = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
public static final String HTTPS_API_WEIXIN_QQ_COM_SNS_USERINFO = "https://api.weixin.qq.com/sns/userinfo";
public static final String HTTPS_API_WEIXIN_QQ_COM_CGI_BIN_STABLE_TOKEN = "https://api.weixin.qq.com/cgi-bin/stable_token";
}

@ -1,23 +1,12 @@
package com.luoo.user.controller;
import annotation.GlobalInterceptor;
import annotation.VerifyParam;
import api.Result;
import api.StatusCode;
import com.luoo.user.dto.request.WeChatBindReq;
import com.luoo.user.dto.request.WeChatLoginReq;
import com.luoo.user.dto.request.WeChatShareReq;
import com.luoo.user.service.WeChatAouth2Service;
import dto.UserLoginDto;
import enums.RequestFrequencyTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import controller.BaseController;
import util.JwtUtil;
import javax.servlet.http.HttpServletRequest;
/**
* @author Revers.
@ -35,8 +24,8 @@ public class WeChatAouth2Controller extends BaseController {
@ApiOperation(value = "分享获取微信token", notes = "成功后返回token,不成功返回null")
@PostMapping("/getShareAccessToken")
public Result getShareAccessToken(@RequestBody WeChatShareReq weChatShareReq) {
return weChatAouth2Service.getShareToken(weChatShareReq);
public Result getShareAccessToken() {
return weChatAouth2Service.getShareToken();
}
// @ApiOperation(value = "微信登录/注册",notes = "微信登录/注册,成功后返回authorization")

@ -1,14 +0,0 @@
package com.luoo.user.dto.request;
import lombok.Data;
/**
* @author Revers.
* @date 2024/03/25 19:21
**/
@Data
public class WeChatShareReq {
private String code;
}

@ -0,0 +1,14 @@
package com.luoo.user.dto.response;
import lombok.Data;
/**
* @author Revers.
* @date 2024/03/28 22:59
**/
@Data
public class Aouth2WeChatStableTokenResponse {
private String access_token;
private String expires_in;
}

@ -4,13 +4,9 @@ import api.Result;
import api.StatusCode;
import com.alibaba.fastjson.JSON;
import com.luoo.user.dao.UserInfoDao;
import com.luoo.user.dto.response.Aouth2WeChatJSAPIResponse;
import com.luoo.user.dto.response.*;
import com.luoo.user.dto.request.WeChatBindReq;
import com.luoo.user.dto.request.WeChatLoginReq;
import com.luoo.user.dto.request.WeChatShareReq;
import com.luoo.user.dto.response.Aouth2WeChatErrorResponse;
import com.luoo.user.dto.response.Aouth2WeChatTokenResponse;
import com.luoo.user.dto.response.Aouth2WeChatUserinfoResponse;
import com.luoo.user.pojo.UserInfo;
import constants.Constants;
import dto.UserLoginDto;
@ -20,6 +16,8 @@ import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@ -65,8 +63,12 @@ public class WeChatAouth2Service {
@Autowired
private UserInfoDao userInfoDao;
public Result getShareToken(WeChatShareReq weChatShareReq){
Aouth2WeChatTokenResponse accessToken = getAccessShareToken(weChatShareReq.getCode());
public Result getShareToken(){
String ticket = (String)redisTemplate.opsForValue().get("shareTicket");
if(ticket != null && StringUtils.isNotBlank(ticket))
return Result.success(ticket);
Aouth2WeChatStableTokenResponse accessToken = getAccessShareToken();
if(accessToken == null || accessToken.getAccess_token() == null )
return Result.failed("分享微信失败");
@ -74,6 +76,7 @@ public class WeChatAouth2Service {
if(aouth2WeChatJSAPIResponse == null || aouth2WeChatJSAPIResponse.getTicket() == null)
return Result.failed("分享微信失败");
redisTemplate.opsForValue().set("shareTicket",aouth2WeChatJSAPIResponse.getTicket(),6000,TimeUnit.SECONDS);
return Result.success(aouth2WeChatJSAPIResponse.getTicket());
}
@ -192,20 +195,32 @@ public class WeChatAouth2Service {
public Aouth2WeChatTokenResponse getAccessShareToken(String code){
StringBuffer url = new StringBuffer();
url.append(Constants.HTTPS_API_WEIXIN_QQ_COM_SNS_OAUTH2_ACCESS_TOKEN);
url.append("?appid=").append(urlEncode(appid_share));
url.append("&secret=").append(urlEncode(secret_share));
url.append("&code=").append(urlEncode(code));
url.append("&grant_type=authorization_code");
String string = new String(get(url.toString()));
public Aouth2WeChatStableTokenResponse getAccessShareToken(){
StringBuffer json = new StringBuffer();
json.append("'{");
json.append("\"grant_type\":").append("\"client_credential\",");
json.append("\"appid\":").append("\"").append(appid_share).append("\",");
json.append("\"secret\":").append("\"").append(secret_share).append("\",");
json.append("\"force_refresh\":").append("false");
json.append("}'");
log.info("getAccessShareToken:" + string);
Aouth2WeChatTokenResponse aouth2WeChatTokenResponse = JSON.parseObject(string, Aouth2WeChatTokenResponse.class);
//saveTokenToRedis(aouth2WeChatTokenResponse);
String string = new String(post(Constants.HTTPS_API_WEIXIN_QQ_COM_CGI_BIN_STABLE_TOKEN,json.toString()));
return aouth2WeChatTokenResponse;
log.info("jsapi:" + string);
Aouth2WeChatStableTokenResponse aouth2WeChatJSAPIResponse = JSON.parseObject(string, Aouth2WeChatStableTokenResponse.class);
return aouth2WeChatJSAPIResponse;
}
private Aouth2WeChatJSAPIResponse getjsapi(String access_token) {
StringBuffer url = new StringBuffer();
url.append(Constants.HTTPS_API_WEIXIN_QQ_JSAPI);
url.append("?access_token=").append(urlEncode(access_token));
url.append("&type=").append("jsapi");
String string = new String(get(url.toString()));
log.info("jsapi:" + string);
Aouth2WeChatJSAPIResponse aouth2WeChatJSAPIResponse = JSON.parseObject(string, Aouth2WeChatJSAPIResponse.class);
return aouth2WeChatJSAPIResponse;
}
public String getWechatUsername(String access_token,String openid){
@ -249,16 +264,7 @@ public class WeChatAouth2Service {
return false;
}
private Aouth2WeChatJSAPIResponse getjsapi(String access_token) {
StringBuffer url = new StringBuffer();
url.append(Constants.HTTPS_API_WEIXIN_QQ_JSAPI);
url.append("?access_token=").append(urlEncode(access_token));
url.append("&type=").append("jsapi");
String string = new String(get(url.toString()));
log.info("jsapi:" + string);
Aouth2WeChatJSAPIResponse aouth2WeChatJSAPIResponse = JSON.parseObject(string, Aouth2WeChatJSAPIResponse.class);
return aouth2WeChatJSAPIResponse;
}
private static String urlEncode(String str) {
@ -292,6 +298,33 @@ public class WeChatAouth2Service {
return res;
}
private static byte[] post(String url,String json) {
byte[] res = null;
try {
HttpPost httpPost = new HttpPost(URI.create(url));
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(new StringEntity(json));
HttpResponse response = new DefaultHttpClient().execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
StringBuilder sb = new StringBuilder();
for (String temp = reader.readLine(); temp != null; temp = reader.readLine()) {
sb.append(temp);
}
res = sb.toString().trim().getBytes(StandardCharsets.UTF_8);
}
} catch (IOException e) {
}
return res;
}
private void saveTokenToRedis(Aouth2WeChatTokenResponse aouth2WeChatTokenResponse){
if(aouth2WeChatTokenResponse != null && aouth2WeChatTokenResponse.getOpenid() != null ){
if(aouth2WeChatTokenResponse.getAccess_token() != null)

Loading…
Cancel
Save