https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#0
看微信开放平台 h5网页授权
第一个接口
根据当前用户token 获取用户id,查询用户是否授权
代码只可参考不能赋值
@GetMapping("/h5-auth")
@ApiOperation(value = "用户h5端是否授权",notes="传入用户id")
public Result isAuth(){
根据token 获取登录用户id 判断静默登录是否授权
String userId = getAppUserId();
AppUser appUser = appUserService.getById(userId);
if(StringUtils.isEmpty(appUser.getH5OpenId())){
return Result.ok(false);
}else{
return Result.ok(true);
}
}
第二步 根据用户授权,获取用户openid
@GetMapping("/h5-openId")
@ApiOperation(value = "获取h5用户openId",notes="传入code")
@NoToken
public Result isAuth(String code){
RestTemplate restTemplate = new RestTemplate();
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={appId}&secret={secret}&code={code}&grant_type=authorization_code";
//设置请求参数
Map<String, String> params = new HashMap<>();
params.put("appId", WechatConstants.WECHAT_MP_APPID);
params.put("secret", WechatConstants.WECHAT_MP_SECRET);
params.put("code", code);
String json = restTemplate.getForObject(url, String.class, params);
JSONObject result = JSON.parseObject(json);
return Result.ok(result);
}
第三静默登录
指生成一个新的账号或者登录帐号,生成token
@NoToken
@GetMapping("/h5-invite")
@ApiOperation(value = "h5邀请静默登录接口",notes="传入openid")
public Result h5Invite(String openid, HttpServletResponse response){
QueryWrapper<AppUser> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("h5_open_id",openid);
AppUser appuser = appUserService.getOne(queryWrapper);
if(Objects.isNull(appuser) || appuser.getId()==null){
//如果没有就注册
appuser = new AppUser();
appuser.setH5OpenId(openid);
// String mark = RandomStringGeneratorUtil.generateRandomString(6);
// appuser.setMark(mark);/**唯一标识*/
// AppUserSequence sequenceInfo = appUserSequenceService.list().get(0);
// String mark = SquenceUtil.generateCode(sequenceInfo.getSquenceId());
// appuser.setMark(mark);
IntegralConfig configure = integralConfigService.getOne(new UpdateWrapper<IntegralConfig>().eq("id",1).eq("status",1));
if(Objects.nonNull(configure)){/**积分任务不为空*/
appuser.setIntegral(new BigDecimal(configure.getIntegral()));
configure.setSumProfit(configure.getSumProfit()
.add(new BigDecimal(configure.getIntegral())));
if(!integralConfigService.updateById(configure)){
throw new RuntimeException("更新总的奖励积分");
}
}
appUserService.save(appuser);
String mark = SquenceUtil.generateCode(Long.parseLong(appuser.getId()));/**根据用户id生成唯一标识*/
appuser.setMark(mark);
if(!appUserService.update(new UpdateWrapper<AppUser>().set("mark",appuser.getMark()).eq("id",appuser.getId()))){
throw new RuntimeException("更新用户标识失败");
}
Integral integral = new Integral();
integral.setUid(appuser.getId());
integral.setCreateTime(new Date());
integral.setType(IntegralType.TYPE_INCOME);
integral.setContent("登录注册获得积分");
integral.setProfit(new BigDecimal(configure.getIntegral()));
integral.setBalance(appuser.getIntegral());
integral.setSourceType(4);
integral.setConfigId(configure.getId());
if(!integralService.save(integral)){
throw new RuntimeException("更新用户积分失败");
}
}
Map<String, Object> result = new HashMap<>();
String token = UUID.randomUUID().toString() + '-' + appuser.getId();
redisUtil.set(WechatConstants.APP_NAME + "_APP_TOKEN_" + appuser.getId(), token,60 * 60 * 24 * 7);
result.put("userInfo", appuser);
result.put("token", token);
Cookie cookie = new Cookie("token", token);
cookie.setMaxAge(60 * 60 * 24 * 7); //cookie超时时间 单位秒
cookie.setPath("/"); //设置cookie的有效路径为跟路径
response.addCookie(cookie);
return Result.OK(result);
}