一.微信登录流程:
1.微信开放平台登录注册,创建网站应用AppId,AppSecret值和回调域名
2.通过AppId和redirect_uri获取Code值 state(唯一凭证,随便写)
https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=回调域地址&response_type=code&scope=snsapi_login&state=state#wechat_redirect
3.跳转到微信提供扫码页面,微信用户允许授权第三方应用后,微信会拉起应用或重定向到第三方网站,并且带上授权临时票据code参数
4.通过code参数,AppId和AppSecret的值,使用文档换取access_token、openid
5.最后通过使用access_token和openid获取微信用户的信息(头像,地址,昵称等...)
二.java后台处理流程:
1.导入相关依赖
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<!--处理json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
2.添加工具类
package cn.pc.demo.util;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import java.io.IOException;
/**
* 使用httpclient组件发送http请求
* get:现在只用到get
* post
*/
public class HttpClientUtils {
/**
* 发送get请求
* @param url 请求地址
* @return 返回内容 json
*/
public static String httpGet(String url){
// 1 创建发起请求客户端
try {
HttpClient client = new HttpClient();
// 2 创建要发起请求-tet
GetMethod getMethod = new GetMethod(url);
getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf8");
// 3 通过客户端传入请求就可以发起请求,获取响应对象
client.executeMethod(getMethod);
// 4 提取响应json字符串返回
String result = new String(getMethod.getResponseBodyAsString().getBytes("utf8"));
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
3.微信登录(用户存在直接登录,不存在绑定手机号等个人信息)
@Override
public AjaxResult wechat(Map<String, String> map) {
// 1.code不能为空
String code = map.get("code");
String binderUrl = map.get("binderUrl");
if(StringUtils.isEmpty(code)){
throw new MyException("信息获取失败");
}
// 2.根据code从微信获取token 使用httpClient "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
String getTokenUrl = WxConstants.GET_TOKEN_URL
.replace("APPID",WxConstants.APPID)
.replace("SECRET",WxConstants.SECRET)
.replace("CODE",code);
String httpGet = HttpClientUtils.httpGet(getTokenUrl);
// 3.拿到token+openId
JSONObject jsonObject = JSONObject.parseObject(httpGet);
String access_token = jsonObject.getString("access_token");
String openid = jsonObject.getString("openid");
// 4.判断openId是已经存在(查询t_wxUser) 如果已经有了并且userid不为空,直接免密登录 如果为空,需要让用户绑定个人用户信息
WxUser wxUser = wxUserMapper.loadByOpenId(openid);
Map<String, Object> mapLog = new HashMap<>();
if(wxUser!=null){
Long user_id = wxUser.getUser_id();
LoginInfo loginInfo = loginInfoMapper.findByUser_id(user_id);
loginInfo.setPassword("");
loginInfo.setSalt("");
//缓存用户信息在redis
String token = UUID.randomUUID().toString();
redisTemplate.opsForValue().set(token,loginInfo,30, TimeUnit.MINUTES);
mapLog.put("token",token);
mapLog.put("loginInfo",loginInfo);
return AjaxResult.createSuccess(mapLog);
}
// 返回绑定页面+token+openId 前端帮我们跳转到绑定页面
String param = binderUrl+"?access_token="+access_token+"&openid="+openid;
mapLog.put("param",param);
return AjaxResult.createErrorAndObj(mapLog);
}
4.获取用户信息
//4.获取微信用户信息"https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";
String obj = HttpClientUtils.httpGet(WxConstants.GET_USER_URL
.replace("ACCESS_TOKEN", wxUserDto.getAccess_token())
.replace("OPENID", wxUserDto.getOpenid()));
//将obj转换为wxUser对象
WxUser wxUser = obj2WxUser(obj);
微信提供的用户信息可以在微信提供的API中查到