美文网首页
微信授权登录、获取微信用户信息

微信授权登录、获取微信用户信息

作者: AC编程 | 来源:发表于2022-01-20 09:39 被阅读0次

    一、Application

    @SpringBootApplication
    public class MyApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MyApplication .class, args);
        }
    
        @Bean
        RestTemplate getRestTemplate(){
            RestTemplate restTemplate = new RestTemplate();
            //解决中文乱码问题
            restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
            return restTemplate;
        }
    }
    

    二、Service

    @Slf4j
    @Service
    public class WeiXinServiceImpl implements IWeiXinService {
    
        @Autowired
        RestTemplate restTemplate;
    
        @Autowired
        IUserService userServiceImpl;
    
        @Autowired
        IWeiXinUserService weiXinUserServiceImpl;
    
        private static final String APPID="APPID";
    
        private static final String SECRET="SECRET";
    
        /**
         * 微信授权登录
         * @param code
         * @return
         */
        @Override
        public WeiXinLoginResult wxAuthLogin(String code) {
            String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+APPID+"&secret="+SECRET+"&code="+code+"&grant_type=authorization_code";
    
            String jsonResult = restTemplate.getForObject(url,String.class);
            log.info("微信登录返回值:"+jsonResult);
    
            if(!jsonResult.contains("openid")){
                log.info("微信登录失败");
                throw new ServiceException("微信登录失败");
            }
    
            WeiXinAccessTokenResult weiXinAccessTokenResult = JSONArray.parseObject(jsonResult, WeiXinAccessTokenResult.class);
            User user = userServiceImpl.getUserByWxOpenid(weiXinAccessTokenResult.getOpenid());
    
            WeiXinLoginResult loginResult = new WeiXinLoginResult();
            loginResult.setWxOpenid(weiXinAccessTokenResult.getOpenid());
            if(user!=null){
                //已绑定 直接登录
                loginResult.setToRegistBindPhone(YesNoEnum.NO.getCode()+"");
            }else{
                //先跳转到注册绑定页面
                requestWeiXinUserinfo(weiXinAccessTokenResult.getAccess_token(),weiXinAccessTokenResult.getOpenid());
                loginResult.setToRegistBindPhone(YesNoEnum.YES.getCode()+"");
            }
            return loginResult;
        }
    
        /**
         * 获取微信用户信息
         * @param accessToken
         * @param openId
         */
        @Override
        public void requestWeiXinUserinfo(String accessToken, String openId) {
            String url = "https://api.weixin.qq.com/sns/userinfo?access_token="+accessToken+"&openid="+openId;
    
            String jsonResult = restTemplate.getForObject(url,String.class);
            log.info("获取微信用户信返回值:"+jsonResult);
    
            if(!jsonResult.contains("openid")){
                log.info("获取微信用户信息失败");
            }
            WeiXinUserDTO weiXinUserDTO = JSONArray.parseObject(jsonResult, WeiXinUserDTO.class);
            weiXinUserServiceImpl.saveOrUpdate(weiXinUserDTO);
        }
    
    }
    

    相关文章

      网友评论

          本文标题:微信授权登录、获取微信用户信息

          本文链接:https://www.haomeiwen.com/subject/jjcjhrtx.html