微信官网开发流程网址:
做之前需要在微信开放平台注册账号添加网站应用,绑定跳转域名地址。获取网站应用appid和密钥.
java相关代码:
#根据前端传的code查询openid获取用户信息
//网站微信授权登录获取access_token地址
private static final String PC_WX__SC_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";
@Transactional(rollbackFor = Exception.class)
@Override
public ReceptionUser pcWxScLogin(ReceptionUser receptionUser) {
String openid = null;
ReceptionUser setUser = new ReceptionUser();
try{
String url = PC_WX__SC_ACCESS_TOKEN_URL
+"?appid="+webAppId
+"&secret="+webAppSecret
+"&code="+receptionUser.getCode()+"&grant_type=authorization_code";
String result = HttpUtils.sendGet(url);
if (StrUtil.isNotBlank(result)){
JSONObject jsonObjectResult = JSON.parseObject(result);
if (jsonObjectResult != null && jsonObjectResult.size() > 0){
//获取openid
openid = jsonObjectResult.getString("openid");
}
}
}catch (Exception e){
e.printStackTrace();
throw new ServiceException("微信扫码授权失败");
}
if (StrUtil.isBlank(openid)){
throw new ServiceException("微信扫码授权失败");
}
//查询是否存在该openID的用户
ReceptionUser user = receptionUserMapper.selectOne(new LambdaQueryWrapper<ReceptionUser>()
.eq(ReceptionUser::getWebAppUserOpenId,openid));
if (user != null){
return user;
}else{
//不存在的时候直接返回openid
setUser.setWebAppUserOpenId(openid);
return setUser;
}
}
#绑定手机号省略。绑定手机号的同时保存openid。