优化:openid无感登录返回用户信息及token
This commit is contained in:
@@ -297,7 +297,7 @@ public class WxLoginController extends BaseController {
|
||||
throw new BusinessException("小程序配置不正确");
|
||||
}
|
||||
|
||||
@ApiOperation("获取微信openId")
|
||||
@ApiOperation("获取微信openId并更新")
|
||||
@PostMapping("/getWxOpenId")
|
||||
public ApiResult<?> getWxOpenId(@RequestBody UserParam userParam) {
|
||||
final User loginUser = getLoginUser();
|
||||
@@ -424,12 +424,17 @@ public class WxLoginController extends BaseController {
|
||||
|
||||
@ApiOperation("openid无感登录")
|
||||
@PostMapping("/loginByOpenId")
|
||||
public ApiResult<?> loginByOpenId(@RequestBody Mp mp) {
|
||||
System.out.println("mp = " + mp);
|
||||
public ApiResult<?> loginByOpenId(@RequestBody Mp mp,HttpServletRequest request) {
|
||||
// 获取小程序配置信息
|
||||
String key1 = "AppId:".concat(mp.getTenantId().toString());
|
||||
String key2 = "AppSecret:".concat(mp.getTenantId().toString());
|
||||
String AppId = redisUtil.get(key1);
|
||||
String AppSecret = redisUtil.get(key2);
|
||||
if (StrUtil.isBlank(AppId) || StrUtil.isBlank(AppSecret)) {
|
||||
final JSONObject setting = settingService.getBySettingKey("mp-weixin");
|
||||
AppId = setting.getString("appId");
|
||||
AppSecret = setting.getString("appSecret");
|
||||
}
|
||||
|
||||
// 请求微信接口获取openid
|
||||
String apiUrl = "https://api.weixin.qq.com/sns/jscode2session";
|
||||
@@ -443,13 +448,15 @@ public class WxLoginController extends BaseController {
|
||||
String openid = jsonObject.getString("openid");
|
||||
String sessionKey = jsonObject.getString("session_key");
|
||||
String unionid = jsonObject.getString("unionid");
|
||||
System.out.println("openid = " + openid);
|
||||
System.out.println("unionid = " + unionid);
|
||||
|
||||
if (StrUtil.isNotBlank(openid)) {
|
||||
User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenid, openid).last("limit 1"));
|
||||
if (ObjectUtil.isNotEmpty(user)) {
|
||||
return success("登录成功", user);
|
||||
// 签发token
|
||||
String access_token = JwtUtil.buildToken(new JwtSubject(user.getUsername(), user.getTenantId()),
|
||||
configProperties.getTokenExpireTime(), configProperties.getTokenKey());
|
||||
loginRecordService.saveAsync(user.getUsername(), LoginRecord.TYPE_REGISTER, null, user.getTenantId(), request);
|
||||
return success("登录成功", new LoginResult(access_token, user));
|
||||
}
|
||||
return fail("用户未注册", openid);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user