|
|
|
@ -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)
|
|
|
|
|