uniapp微信登录片段代码,助记

手机端页面部分---忘记从哪copy的
methods: {
            weixinLogin() {
                var _this = this;
                // #ifdef APP-PLUS
                var weixinService = null;
                // http://www.html5plus.org/doc/zh_cn/oauth.html#plus.oauth.getServices
                plus.oauth.getServices(function(services) {
                    console.log(services)
                    if (services && services.length) {
                        for (var i = 0, len = services.length; i < len; i++) {
                            if (services[i].id === 'weixin') {
                                weixinService = services[i];
                                break;
                            }
                        }
                        if (!weixinService) {
                            console.log('没有微信登录授权服务');
                            return;
                        }
                        // http://www.html5plus.org/doc/zh_cn/oauth.html#plus.oauth.AuthService.authorize
                        weixinService.authorize(function(event) {
                            _this.weixinCode = event.code; //用户换取 access_token 的 code
                            console.log(_this.weixinCode);
                            _this.requestLogin();
                        }, function(error) {
                            console.error('authorize fail:' + JSON.stringify(error));
                        }, {
                            // http://www.html5plus.org/doc/zh_cn/oauth.html#plus.oauth.AuthOptions
                            appid: 'asdfghjkl' //开放平台的应用标识。暂时填个假的充数,仅做演示。
                        });
                    } else {
                        console.log('无可用的登录授权服务');
                    }
                }, function(error) {
                    console.error('getServices fail:' + JSON.stringify(error));
                });
                // #endif
            },
            requestLogin() {
                // 这里请求服务端授权登录
                uni.request({
                    url: 'xxxxxxx/login',
                    data: {
                        code: this.weixinCode
                    },
                    // TODO
                })
            }

        }



后端Java
maven依赖
<!--微信开发平台工具 https://gitee.com/binary/weixin-java-tools-->
<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-open</artifactId>
    <version>${wx.version}</version>
</dependency>
<wx.version>3.9.0</wx.version>

配置部分

WxOpenProperties

配置bean部分
@Bean
public WxMpService wxMpService() {
  WxMpService wxMpService = new WxMpServiceImpl();
  wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
  return wxMpService;
}
@Bean
public WxMpConfigStorage wxMpConfigStorage() {
  WxMpDefaultConfigImpl wxMpConfigStorage = new WxMpDefaultConfigImpl();
  wxMpConfigStorage.setAppId(wxOpenProperties.getComponentAppId());
  wxMpConfigStorage.setSecret(wxOpenProperties.getComponentSecret());
  wxMpConfigStorage.setToken(wxOpenProperties.getComponentToken());
  wxMpConfigStorage.setAesKey(wxOpenProperties.getComponentAesKey());
  return wxMpConfigStorage;
}

配置文件
#---------------------------------------wechat open
wx.open.componentAppId=
wx.open.componentSecret=
wx.open.component-aes-key=
wx.open.component-token=
@Autowired
WxMpService wxMpService;

//service 代码片段 仅供参考
//流程
WxOAuth2Service oAuth2Service = wxMpService.getOAuth2Service();
WxMpOAuth2AccessToken accessToken = null;
try {
    accessToken = oAuth2Service.getAccessToken("前端微信的code");
    WxMpUser wxMpUser = oAuth2Service.getUserInfo(accessToken, "zh_CN");
    logger.info("wxuser = {}", JsonUtils.toJson(wxMpUser));
    //TODO 你的业务代码

} catch (WxErrorException e) {
    e.printStackTrace();
    throw new BizException(ResCode.ERROR);
}

猜你喜欢

转载自blog.csdn.net/qq_36338555/article/details/113114228
今日推荐