Commit a2e50818 by 303514581@qq.com

2019/4/8 新增获取wechat用户信息接口

parent 837524b6
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
......@@ -71,7 +72,7 @@
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
......@@ -115,6 +116,40 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- crawler工具包 -->
<dependency>
<groupId>com.zhiwei.crawler</groupId>
<artifactId>crawler-core</artifactId>
<version>0.3.5-SNAPSHOT</version>
</dependency>
<!-- 日志 -->
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-1.2-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
......
......@@ -48,4 +48,25 @@ public class GetWechatOpenUrl {
logger.info("请求token的url:" + url);
return url.toString();
}
/**
* 获取用户信息url
* @Title: getUserInfo
* @Description: 获取用户信息url
* @param @param userInfoUrl
* @param @param access_token
* @param @param openid
* @param @return 设定文件
* @return String 返回类型
*/
public String getUserInfo(String userInfoUrl, String access_token, String openid) {
StringBuilder url = new StringBuilder();
url.append(userInfoUrl);
url.append("?access_token=");
url.append(access_token);
url.append("&openid=");
url.append(openid);
logger.info("获取用户信息url:" + url);
return url.toString();
}
}
package com.zhiwei.Tool;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.crawler.core.HttpBoot;
import com.zhiwei.crawler.utils.RequestUtils;
import com.zhiwei.crawler.utils.RequestUtils.HttpMethod;
import okhttp3.Request;
import okhttp3.Response;
/**
* 自用工具类
* @ClassName: Tools
* @Description: 自用工具类
* @author shentao
* @date 2019年4月8日 上午10:20:19
*/
public class Tools {
/** HttpBoot **/
private static HttpBoot httpBoot = new HttpBoot.Builder().build();
/**
* get请求
*
* @Title: httpGet
* @Description: get请求
* @param @param url
* @param @return 设定文件
* @return String 返回类型
*/
public static String httpGet(String url) {
Request request = RequestUtils.wrapGet(url);
try (Response response = httpBoot.syncCall(request)) {
return response.body().string();
} catch (Exception e) {
return null;
}
}
/**
* post form请求
*
* @Title: httpPostForm
* @Description: post form请求
* @param @param urlNewProject
* @param @param form
* @param @return 设定文件
* @return String 返回类型
*/
public static String httpPostForm(String url, Map<String, Object> form) {
Request request = RequestUtils.wrapPost(url, new HashMap<>(), form);
try (Response response = httpBoot.syncCall(request)) {
return response.body().string();
} catch (Exception e) {
return null;
}
}
/**
* post RequestBody请求
*
* @Title: httpPostRequestBody
* @Description: post RequestBody请求
* @param @param url
* @param @param info
* @param @return 设定文件
* @return String 返回类型
*/
public static String httpPostRequestBody(String url, String info) {
Request request = RequestUtils.wrapPost(url, "application/json", info);
try (Response response = httpBoot.syncCall(request)) {
return response.body().string();
} catch (Exception e) {
return null;
}
}
/**
* delete 请求
*
* @Title: httpDelete
* @Description: delete 请求
* @param @param url
* @param @param info
* @param @return 设定文件
* @return String 返回类型
*/
public static String httpDelete(String url, String info) {
Request request = RequestUtils.wrap(HttpMethod.DELETE, url, "application/json", info);
try (Response response = httpBoot.syncCall(request)) {
return response.body().string();
} catch (Exception e) {
return null;
}
}
/**
* put 请求
*
* @Title: httpPut
* @Description: put 请求
* @param @param url
* @param @param info
* @param @return 设定文件
* @return String 返回类型
*/
public static String httpPut(String url, String info) {
Request request = RequestUtils.wrap(HttpMethod.PUT, url, "application/json", info);
try (Response response = httpBoot.syncCall(request)) {
return response.body().string();
} catch (Exception e) {
return null;
}
}
public static String returnCommonError(String errormsg) {
JSONObject error = new JSONObject();
error.put("errcode", -2);
error.put("errmsg", errormsg);
return error.toJSONString();
}
}
......@@ -28,6 +28,16 @@ public class WechatConfig {
private String getTokenUrl;
// 授权类型
private String grantType;
// 获取用户个人信息(UnionID机制)地址
private String userInfoUrl;
public String getUserInfoUrl() {
return userInfoUrl;
}
public void setUserInfoUrl(String userInfoUrl) {
this.userInfoUrl = userInfoUrl;
}
public String getGetCodeUrl() {
return getCodeUrl;
......
......@@ -15,10 +15,13 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.Tool.GetWechatOpenUrl;
import com.zhiwei.Tool.Tools;
import com.zhiwei.config.WechatConfig;
import com.zhiwei.entity.WeChatTokenEntity;
......@@ -76,15 +79,32 @@ public class WechatLoginController {
if (httpresponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
resultToken = EntityUtils.toString(httpresponse.getEntity(), "utf-8");
}
System.out.println(resultToken);
// System.out.println(resultToken);
WeChatTokenEntity entity = JSONObject.parseObject(resultToken, WeChatTokenEntity.class);
System.err.println(entity.toString());
// System.err.println(entity.toString());
logger.info(entity.toString());
String sendurl = yoururl + "?token=" + entity.getAccess_token() + "&uid=" + entity.getOpenid()
+ "&expires_in=" + entity.getExpires_in();
String sendurl = yoururl + "?access_token=" + entity.getAccess_token() + "&openid=" + entity.getOpenid()
+ "&expires_in=" + entity.getExpires_in()+ "&unionid=" + entity.getUnionid();
response.sendRedirect(sendurl);
} catch (IOException e) {
logger.error("微信回调请求失败,错误信息:{},错误位置:{}", e.getMessage(), e.getStackTrace());
}
}
@RequestMapping(value = "/WechatLogin/getUserInfo", method = RequestMethod.GET)
public String getWechatUserInfo(@RequestParam(value = "access_token") String access_token,
@RequestParam(value = "openid") String openid,
HttpServletResponse response, HttpServletRequest request) {
// https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
try {
String url = getUrl.getUserInfo(config.getUserInfoUrl(), access_token, openid);
return Tools.httpGet(url);
} catch (Exception e) {
logger.error("微信请求用户信息失败,错误信息:{},错误位置:{}", e.getMessage(), e.getStackTrace());
return Tools.returnCommonError("zhiwei授权接口出错");
}
}
}
......@@ -14,4 +14,6 @@ wechat.scope=snsapi_login
#获取token地址
wechat.getTokenUrl=https://api.weixin.qq.com/sns/oauth2/access_token
#授权类型
wechat.grantType=authorization_code
\ No newline at end of file
wechat.grantType=authorization_code
#获取用户个人信息(UnionID机制)地址
wechat.userInfoUrl=https://api.weixin.qq.com/sns/userinfo
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment