【微信】网站应用扫码登录/微信公众号code登录

微信网站应用的扫码登录和微信公众号code登录流程是一样的,微信网站应用可以绑定多个公众号,但是在一个网站应用下,所有的公众号都只会有同一个unionId,用于区分不同的用户,code由前端调微信接口获取,后端只管接收参数即可

下列的参数,appId和appsecret是微信公众号的,webAppId和webAppSecret是网站应用的,我这里通过配置读取

@Value("${wx.appId}")
private String appId;

@Value("${wx.appsecret}")
private String appsecret;

@Value("${web.appId}")
private String webAppId;

@Value("${web.appsecret}")
private String webAppSecret;


@ApiOperation(value = "尝试code登录", produces = "application/json")
@Log(action = "attemptLogin", modelName = "WxMpController", description = "尝试code登录")
@Pass
@GetMapping(value = "/mp/attemptLogin")
public ResponseModel attemptLogin(HttpServletRequest request,
                                           @RequestParam String code,
                                           @RequestParam(required = false) String state) {
    // 获取微信用户数据                                   
    WxUserVo wxUserVo = getWxUserVo(appId, code, state);
    String wxId = wxUserVo.getUnionId();
    // 通过unionId查询用户
    User userByOpenId = userService.getUserByOpenId(wxId);
    // 通过用户登录
    return ResponseHelper.succeed(userService.loginByWxId(userByOpenId ));
}

// 获取微信用户数据

private WxUserVo getWxUserVo( String code, String state) {
       try {
           String format = "";
           // 通过state是否为空区分是公众号登录还是网站应用登录
           if (ComUtil.isEmpty(state)) {
               format = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", appId, appsecret, code);
           } else {
               format = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", webAppId, webAppSecret, code);
           }
           log.info("format:{}", format);
           // 发生请求获取accessToken 结果
           final String json = HttpUtil.get(format);
           // 将结果则转为AccessToken 实体
           final AccessToken accessToken = JSON.parseObject(json, AccessToken.class);
           log.info("获取的token{}", accessToken);
          	// 从accessToken 中获取用户openId
           String openid = accessToken.getOpenid();

           String userUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken.getAccess_token() + "&openid=" + openid + "&lang=zh_CN";
           // 通过openId发生请求给微信获取用户扫码信息
           String userJson = HttpUtil.get(userUrl);
           log.info("userJson:{}", userJson);
           final UserInfo userInfo = JSON.parseObject(userJson, UserInfo.class);
           log.info("获取的userInfo{}", userInfo);
           // 构建结果对象返回
           WxUserVo wxUserVo = WxUserVo.builder()
                   .userName(new String(userInfo.getNickName().getBytes("ISO-8859-1"), "UTF-8"))
                   .avatar(userInfo.getHeadimgurl())
                   .unionId(userInfo.getUnionId())
                   .build();
           if (ComUtil.isEmpty(state)) {
               wxUserVo.setWxId(userInfo.getOpenId());
           }
           return wxUserVo;
       } catch (Exception e) {
           log.info(e.getMessage(), e);
           log.info("异常错误:{}", e);
           throw new BusinessException(BusinessExceptionCode.WX_OPEN_ID_ERROR_CODE);
       }
   }


   //  UserInfo 实体
   @Data
   @AllArgsConstructor
   @NoArgsConstructor
   public static class UserInfo {
       private String nickName;
       private int sex;
       private String province;
       private String city;
       private String country;
       private String headimgurl;
       private String openId;
       private String unionId;
   }
   
   // AccessToken 实体
   @Data
   @AllArgsConstructor
   @NoArgsConstructor
   public static class AccessToken {
       private String access_token;
       private int expires_in;
       private String refresh_token;
       private String openid;
       private String scope;
   }

你可能感兴趣的:(微信,微信)