微信小程序系列之-微信小程序授权登录

简介: java后端对微信小程序进行授权登录操作

微信登录的官方文档

由于我是后端开发人员 , 因此主要讲解的是后端操作

小程序端需要调用的接口

//  获取 临时登录凭证code ,并回传到开发者服务器。
wx.login() 

后端调用接口

微信换取sessionKey和openId和unionId的接口

java 代码如下 ,使用的json序列化工具为 jackson

@Getter@Setter@Schema(description="用户信息Dto")
@Slf4jpublicstaticclassWxRegisterLoginUserimplementsSerializable {
publicstaticlongREDIS_REPEAT_TIMEOUT=5;
publicstaticStringREDIS_REPEAT_KEY_PREFIX="redis_repeat_key_";
// 微信获取 sessionKey openId unionId 的后缀privatestaticStringWX_LOGIN_URL="/sns/jscode2session";
@SerialprivatestaticfinallongserialVersionUID=7658930891407547798L;
@Schema(description="用户唯一标识")
@JsonAlias("openid")
privateStringopenId;
@Schema(description="会话密钥")
@JsonAlias("session_key")
privateStringsessionKey;
@Schema(description="用户在开放平台的唯一标识符,若当前小程序已绑定到微信开放平台帐号下会返回")
@JsonAlias("unionid")
privateStringunionId;
@Schema(description="错误码")
@JsonAlias("errcode")
privateIntegererrCode;
@Schema(description="错误详情")
@JsonAlias("errmsg")
privateStringerrMsg;
publicstaticWxRegisterLoginUserinitByWxCode(Stringcode, StringwxUrl, StringwxAppid, StringwxSecret) {
StringbuilderUrl=UrlBuilder.of(wxUrl+WX_LOGIN_URL)
                    .addQuery("appid" , wxAppid)
                    .addQuery("secret" , wxSecret)
                    .addQuery("js_code", code)
                    .addQuery("grant_type" , "authorization_code")
                    .build();
log.info("BusUserDto-wxRegisterLogin-initByWxCode-builderUrl:{}" , builderUrl);
StringwxLoginStr=HttpUtil.get(builderUrl);
log.info("UserDomainServiceImpl-wxRegisterLogin-initByWxCode-wxLoginStr : {}", wxLoginStr);
// 登陆操作WxRegisterLoginUserregisterLoginDto=JacksonUtil.toObject(wxLoginStr, WxRegisterLoginUser.class);
Assert.isTrue(registerLoginDto.validateErrorCode(),
                    () ->newBusException(BusErrorCode.WX_REGISTER_LOGIN_INFO_ERROR.getResultCode(), registerLoginDto.getErrMsg()));
returnregisterLoginDto;
        }
publicBooleanvalidateErrorCode() {
// 微信官方文档 0 代表成功 https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.htmlif (StrUtil.isNotEmpty(openId)) {
returnBoolean.TRUE;
            }
returnBoolean.FALSE;
        }

这个时候就能够拿到openId或者unionId去本地数据库查询是否存在该用户了

// 是否注册过Assert.isNull(userService.getUserByOpenId(registerLoginDto.getOpenId()), () ->newBusException(BusErrorCode.WX_REGISTER_REPEAT_ERROR));
相关文章
|
2天前
|
小程序 开发工具 Android开发
微信小程序开发工具的使用,各个配置文件详解,小程序开发快速入门(二)
微信小程序开发工具的使用,各个配置文件详解,小程序开发快速入门(二)
|
2天前
|
小程序 JavaScript 开发工具
微信小程序开发工具的使用,各个配置文件详解,小程序开发快速入门(一)
微信小程序开发工具的使用,各个配置文件详解,小程序开发快速入门(一)
|
2天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的在线课堂微信小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的在线课堂微信小程序的详细设计和实现
35 3
|
2天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的微信课堂助手小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的微信课堂助手小程序的详细设计和实现
60 3
|
2天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的微信点餐小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的微信点餐小程序的详细设计和实现
31 1
|
2天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的助农扶贫微信小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的助农扶贫微信小程序的详细设计和实现
35 2
|
2天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的微信阅读网站小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的微信阅读网站小程序的详细设计和实现
43 2
|
2天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的原创音乐小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的原创音乐小程序的详细设计和实现
13 1
|
2天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的实习记录小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的实习记录小程序的详细设计和实现
37 2
|
2天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的校园防疫微信小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的校园防疫微信小程序的详细设计和实现
29 0

热门文章

最新文章