✨ Introducing new features. 支持小程序登录
This commit is contained in:
1 parent
6c910f5a53
commit
0be9e9bc96
5 files changed
+109
No files matched your search
+6
@@ -89,6 +89,12 @@ public interface SecurityConstants {
|
||||
String WX_AUTHORIZATION_CODE_URL = "https://api.weixin.qq.com/sns/oauth2/access_token" +
|
||||
"?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
|
||||
|
||||
/**
|
||||
* 微信小程序OPENID
|
||||
*/
|
||||
String MINI_APP_AUTHORIZATION_CODE_URL = "https://api.weixin.qq.com/sns/jscode2session" +
|
||||
"?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
|
||||
|
||||
/**
|
||||
* 码云获取token
|
||||
*/
|
||||
|
||||
+5
@@ -48,6 +48,11 @@ public enum LoginTypeEnum {
|
||||
*/
|
||||
WECHAT("WX", "微信登录"),
|
||||
|
||||
/**
|
||||
* 微信小程序
|
||||
*/
|
||||
MINI_APP("MINI", "微信小程序"),
|
||||
|
||||
/**
|
||||
* 码云登录
|
||||
*/
|
||||
|
||||
@@ -112,6 +112,11 @@ public class SysUser implements Serializable {
|
||||
*/
|
||||
@ApiModelProperty(value = "微信openid")
|
||||
private String wxOpenid;
|
||||
/**
|
||||
* 微信小程序openId
|
||||
*/
|
||||
@ApiModelProperty(value = "微信小程序openid")
|
||||
private String miniOpenid;
|
||||
/**
|
||||
* QQ openid
|
||||
*/
|
||||
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.admin.handler;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.pig4cloud.pigx.admin.api.dto.UserInfo;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysSocialDetails;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysUser;
|
||||
import com.pig4cloud.pigx.admin.mapper.SysSocialDetailsMapper;
|
||||
import com.pig4cloud.pigx.admin.service.SysUserService;
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.common.core.constant.enums.LoginTypeEnum;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2019年11月02日
|
||||
* <p>
|
||||
* 微信小程序
|
||||
*/
|
||||
@Slf4j
|
||||
@Component("MINI")
|
||||
@AllArgsConstructor
|
||||
public class MiniAppLoginHandler extends AbstractLoginHandler {
|
||||
private final SysUserService sysUserService;
|
||||
private final SysSocialDetailsMapper sysSocialDetailsMapper;
|
||||
|
||||
/**
|
||||
* 小程序登录传入code
|
||||
* <p>
|
||||
* 通过code 调用qq 获取唯一标识
|
||||
*
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String identify(String code) {
|
||||
SysSocialDetails condition = new SysSocialDetails();
|
||||
condition.setType(LoginTypeEnum.MINI_APP.getType());
|
||||
SysSocialDetails socialDetails = sysSocialDetailsMapper.selectOne(new QueryWrapper<>(condition));
|
||||
|
||||
String url = String.format(SecurityConstants.MINI_APP_AUTHORIZATION_CODE_URL
|
||||
, socialDetails.getAppId(), socialDetails.getAppSecret(), code);
|
||||
String result = HttpUtil.get(url);
|
||||
log.debug("微信小程序响应报文:{}", result);
|
||||
|
||||
Object obj = JSONUtil.parseObj(result).get("openid");
|
||||
return obj.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* openId 获取用户信息
|
||||
*
|
||||
* @param openId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public UserInfo info(String openId) {
|
||||
SysUser user = sysUserService
|
||||
.getOne(Wrappers.<SysUser>query()
|
||||
.lambda().eq(SysUser::getMiniOpenid, openId));
|
||||
|
||||
if (user == null) {
|
||||
log.info("微信小程序未绑定:{}", openId);
|
||||
return null;
|
||||
}
|
||||
return sysUserService.findUserInfo(user);
|
||||
}
|
||||
}
|
||||
+4
@@ -59,6 +59,7 @@ public class SysSocialDetailsServiceImpl extends ServiceImpl<SysSocialDetailsMap
|
||||
public Boolean bindSocial(String type, String code) {
|
||||
String identify = loginHandlerMap.get(type).identify(code);
|
||||
SysUser sysUser = sysUserMapper.selectById(SecurityUtils.getUser().getId());
|
||||
|
||||
if (LoginTypeEnum.GITEE.getType().equals(type)) {
|
||||
sysUser.setGiteeLogin(identify);
|
||||
} else if (LoginTypeEnum.OSC.getType().equals(type)) {
|
||||
@@ -67,7 +68,10 @@ public class SysSocialDetailsServiceImpl extends ServiceImpl<SysSocialDetailsMap
|
||||
sysUser.setWxOpenid(identify);
|
||||
} else if (LoginTypeEnum.QQ.getType().equals(type)) {
|
||||
sysUser.setQqOpenid(identify);
|
||||
} else if (LoginTypeEnum.MINI_APP.getType().equals(type)) {
|
||||
sysUser.setMiniOpenid(identify);
|
||||
}
|
||||
|
||||
sysUserMapper.updateById(sysUser);
|
||||
//更新緩存
|
||||
cacheManager.getCache(CacheConstants.USER_DETAILS).evict(sysUser.getUsername());
|
||||
|
||||
Reference in new issue
Block a user