首页 > 其他分享 >微信公众号授权及获取用户信息

微信公众号授权及获取用户信息

时间:2023-02-03 16:22:20浏览次数:65  
标签:code configStorage 微信 private 获取 href let 授权

1、映射配置文件
@Data
@Component
@ConfigurationProperties(prefix = "wx.mp")
public class WxMpProperties {
    /**
     * 设置微信公众号的appid
     */
    private String appId;
    /**
     * 设置微信公众号的app secret
     */
    private String secret;
    /**
     * 设置微信公众号的token
     */
    private String token;
    /**
     * 设置微信公众号的EncodingAESKey
     */
    private String aesKey;
}

2、微信配置信息

@Configuration
@Slf4j
public class WxConfig {
    @Autowired
    private WxMpProperties wxMpProperties;
    @Autowired
    private RedisTemplate redisTemplate;

    @Bean
    public WxMpService wxMpService() {
        RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
        WxRedisOps redisOps = new RedisTemplateWxRedisOps(new StringRedisTemplate(factory));
        WxMpRedisConfigImpl configStorage = new WxMpRedisConfigImpl(redisOps, Redis.getProjectName());
        configStorage.setAppId(wxMpProperties.getAppId());
        configStorage.setSecret(wxMpProperties.getSecret());
        configStorage.setToken(wxMpProperties.getToken());
        configStorage.setAesKey(wxMpProperties.getAesKey());
        WxMpService service = new WxMpServiceImpl();
        service.setWxMpConfigStorage(configStorage);
        log.info("微信公众号初始化完成...");
        return service;
    }
}

3、授权获取code

 参考微信官方文档 

let flag = confirm("该功能需要获取您的头像、昵称信息、openId");
if (flag) {
      let href = window.location.href;
      let redirect = href.indexOf("?") ? href.split("?")[0] : href;
      let appid = "**************";
      let api = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + encodeURIComponent(redirect) + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
      window.location.href = api;
}
$(function () {
    let code = getQueryString('code');
})
//获取url中的参数
function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    return r == null ? '' : unescape(r[2])
}

  

  

4、通过code获取用户基本信息

 WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
 WxOAuth2UserInfo userInfo = wxMpService.getOAuth2Service().getUserInfo(accessToken, "zh_CN");
通过userInfo可以获取用户openid、图像等基本信息
userInfo.getOpenid();//获取oopenid
Base64.getEncoder().encodeToString(userInfo.getNickname().getBytes());//获取昵称
userInfo.getHeadImgUrl();//获取图像

    

  

标签:code,configStorage,微信,private,获取,href,let,授权
From: https://www.cnblogs.com/person008/p/17089680.html

相关文章

  • 微信对话生成器,生成微信聊天记录,聊天记录生成器
    微信对话生成器,生成微信聊天记录,聊天记录生成器微信对话生成器是一款可以让你在朋友圈轻松装逼炫富的神器,你可以使用这款软件轻松制作微信对话、微信红包、等截图或视频......
  • 微信小程序地图的标记和轨迹
     1.标记图标   <viewclass="top"><mapwx:if="{{markersStatus}}"class="map"id="myMap"scale="{{scale}}"longitude="{{longitude}}"latitu......
  • Java微信转发及网络检测
    1、jar包引入<dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-mp</artifactId><version>4.2.0</version>......
  • 基于IdentityServer的系统对接微信公众号
    业务需求公司有两个业务系统,A和B,AB用户之间属于多对一的关系,数据库里面也就是两张表,A表有个外键指向B。现在需要实现以下几个功能。A用户扫描B的二维码,填写相关的注册......
  • Java 反射 (完) 类加载和反射获取信息
    三.类加载1.动态加载和静态加载基本说明反射机制是java实现动态语言的关键,也就是通过反射实现类动态加载1.静态加载:编译时加载相关的类,如果没有则报错,依赖性太强2.动态加载:......
  • chrome 浏览器插件右键打开窗口并获取tabid
     chrome.contextMenus.create({title:"Openframeinnewtab",onclick:function(info,tab){chrome.tabs.create({url:info.frame......
  • MySQL创建用户和授权
    MySQL创建用户和授权--语法createuser'用户名'@'主机'identifiedby'密码';--实例createuser'rayfoo'@'%'identifiedby'123456';--刷新,立即生效flushpr......
  • 微信小程序:自定义导航栏
    自定义导航栏使用weapp-navigation-bar组件,网址:https://github.com/mulook/weapp-navigation-bar使用说明:1、app.json配置以下属性"window":{"backgroundTe......
  • 使用webStrom使用uni-app新建微信小程序
    一、通过官网了解uni-app相关的信息uni-app官网地址:https://uniapp.dcloud.net.cn/已知uni-app微信小程序程序创建需要使用HBuilderX或者cli进行创建,再到微信开发者工具......
  • 获取访问接口的IP地址(绝对好用)
    在做自动过滤的时候我们经常会将并发访问量非常高的IP进行拉入黑名单,也就是拒绝访问处理,但是我们依然需要收集这些IP地址,那么我们今天就要用到这款代码了,这里是Java的版本我......