1. 优化前端的社交登陆的枚举

2. 将三方登陆,统一改成社交登陆
This commit is contained in:
YunaiV 2021-10-05 00:36:04 +08:00
parent 7b3a283074
commit a0a5d3a357
9 changed files with 63 additions and 75 deletions

View File

@ -105,7 +105,7 @@ public class SysAuthController {
@GetMapping("/social-login-redirect") @GetMapping("/social-login-redirect")
@ApiOperation("社交登陆的跳转") @ApiOperation("社交登陆的跳转")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "type", value = "三方类型", required = true, dataTypeClass = Integer.class), @ApiImplicitParam(name = "type", value = "社交类型", required = true, dataTypeClass = Integer.class),
@ApiImplicitParam(name = "redirectUri", value = "回调路径", dataTypeClass = String.class) @ApiImplicitParam(name = "redirectUri", value = "回调路径", dataTypeClass = String.class)
}) })
public CommonResult<String> socialLoginRedirect(@RequestParam("type") Integer type, public CommonResult<String> socialLoginRedirect(@RequestParam("type") Integer type,

View File

@ -23,7 +23,7 @@ public interface SysRedisKeyConstants {
"captcha_code:%s", // 参数为 uuid "captcha_code:%s", // 参数为 uuid
STRING, String.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC); STRING, String.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC);
RedisKeyDefine AUTH_SOCIAL_USER = new RedisKeyDefine("认证的三方用户", RedisKeyDefine AUTH_SOCIAL_USER = new RedisKeyDefine("认证的社交用户",
"auth_social_user:%d:%s", // 参数为 typecode "auth_social_user:%d:%s", // 参数为 typecode
STRING, AuthUser.class, Duration.ofDays(1)); STRING, AuthUser.class, Duration.ofDays(1));

View File

@ -17,7 +17,7 @@ public interface SysErrorCodeConstants {
ErrorCode AUTH_LOGIN_CAPTCHA_NOT_FOUND = new ErrorCode(1002000003, "验证码不存在"); ErrorCode AUTH_LOGIN_CAPTCHA_NOT_FOUND = new ErrorCode(1002000003, "验证码不存在");
ErrorCode AUTH_LOGIN_CAPTCHA_CODE_ERROR = new ErrorCode(1002000004, "验证码不正确"); ErrorCode AUTH_LOGIN_CAPTCHA_CODE_ERROR = new ErrorCode(1002000004, "验证码不正确");
ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1002000005, "未绑定账号,需要进行绑定"); ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1002000005, "未绑定账号,需要进行绑定");
ErrorCode AUTH_THIRD_OAUTH_FAILURE = new ErrorCode(1002000006, "三方授权失败,原因是:{}"); ErrorCode AUTH_THIRD_OAUTH_FAILURE = new ErrorCode(1002000006, "社交授权失败,原因是:{}");
// ========== TOKEN 模块 1002001000 ========== // ========== TOKEN 模块 1002001000 ==========
ErrorCode TOKEN_EXPIRED = new ErrorCode(1002001000, "Token 已经过期"); ErrorCode TOKEN_EXPIRED = new ErrorCode(1002001000, "Token 已经过期");

View File

@ -258,7 +258,7 @@ public class SysAuthServiceImpl implements SysAuthService {
private AuthUser obtainAuthUser(Integer type, AuthCallback authCallback) { private AuthUser obtainAuthUser(Integer type, AuthCallback authCallback) {
AuthRequest authRequest = authRequestFactory.get(SysSocialTypeEnum.valueOfType(type).getSource()); AuthRequest authRequest = authRequestFactory.get(SysSocialTypeEnum.valueOfType(type).getSource());
AuthResponse<?> authResponse = authRequest.login(authCallback); AuthResponse<?> authResponse = authRequest.login(authCallback);
log.info("[obtainAuthUser][请求三方平台 type({}) request({}) response({})]", type, JsonUtils.toJsonString(authCallback), log.info("[obtainAuthUser][请求社交平台 type({}) request({}) response({})]", type, JsonUtils.toJsonString(authCallback),
JsonUtils.toJsonString(authResponse)); JsonUtils.toJsonString(authResponse));
if (!authResponse.ok()) { if (!authResponse.ok()) {
throw exception(AUTH_THIRD_OAUTH_FAILURE, authResponse.getMsg()); throw exception(AUTH_THIRD_OAUTH_FAILURE, authResponse.getMsg());

View File

@ -34,14 +34,6 @@ public interface SysUserService {
*/ */
Long createUser(SysUserCreateReqVO reqVO); Long createUser(SysUserCreateReqVO reqVO);
/**
* 绑定第三方用户
* @param sysUserId 系统用户ID
* @param socialUSerId 第三方唯一标识
* @return
*/
Long bindSocialUSer(Long sysUserId, String socialUSerId);
/** /**
* 修改用户 * 修改用户
* *

View File

@ -4,11 +4,6 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysSocialUserDO;
import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social.SysUserSocialMapper;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.adminserver.modules.infra.service.file.InfFileService; import cn.iocoder.yudao.adminserver.modules.infra.service.file.InfFileService;
import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.profile.SysUserProfileUpdatePasswordReqVO; import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.profile.SysUserProfileUpdatePasswordReqVO;
import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.profile.SysUserProfileUpdateReqVO; import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.profile.SysUserProfileUpdateReqVO;
@ -17,10 +12,14 @@ import cn.iocoder.yudao.adminserver.modules.system.convert.user.SysUserConvert;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysDeptDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysDeptDO;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysPostDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysPostDO;
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysUserDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysUserDO;
import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social.SysUserSocialMapper;
import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.user.SysUserMapper; import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.user.SysUserMapper;
import cn.iocoder.yudao.adminserver.modules.system.service.dept.SysDeptService; import cn.iocoder.yudao.adminserver.modules.system.service.dept.SysDeptService;
import cn.iocoder.yudao.adminserver.modules.system.service.dept.SysPostService; import cn.iocoder.yudao.adminserver.modules.system.service.dept.SysPostService;
import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService; import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -33,9 +32,8 @@ import javax.annotation.Resource;
import java.io.InputStream; import java.io.InputStream;
import java.util.*; import java.util.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.*; import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
/** /**
* 用户 Service 实现类 * 用户 Service 实现类
@ -78,15 +76,6 @@ public class SysUserServiceImpl implements SysUserService {
return user.getId(); return user.getId();
} }
@Override
public Long bindSocialUSer(Long sysUserId, String socialUSerId) {
SysSocialUserDO userSocialDO = new SysSocialUserDO();
userSocialDO.setUserId(sysUserId);
// userSocialDO.setSocialUserId(socialUSerId);
userSocialMapper.insert(userSocialDO);
return userSocialDO.getUserId();
}
@Override @Override
public void updateUser(SysUserUpdateReqVO reqVO) { public void updateUser(SysUserUpdateReqVO reqVO) {
// 校验正确性 // 校验正确性

View File

@ -68,9 +68,25 @@ export const InfApiErrorLogProcessStatusEnum = {
} }
/** /**
* 用户的三方平台的类型枚举 * 用户的社交平台的类型枚举
*/ */
export const SysUserSocialTypeEnum = { export const SysUserSocialTypeEnum = {
GITEE: 10, // GITEE: {
DINGTALK: 20, // title: "码云",
// type: 10,
// source: "gitee",
// img: "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.11/gitee.png",
// },
DINGTALK: {
title: "钉钉",
type: 20,
source: "dingtalk",
img: "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.11/dingtalk.png",
},
WECHAT_ENTERPRISE: {
title: "企业微信",
type: 30,
source: "wechat_enterprise",
img: "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.11/wechat_enterprise.png",
}
} }

View File

@ -30,8 +30,8 @@
<el-form-item style="width:100%;"> <el-form-item style="width:100%;">
<div class="oauth-login" style="display:flex"> <div class="oauth-login" style="display:flex">
<div class="oauth-login-item" v-for="item in oauthProviders" :key="item.code" @click="doAuth2Login(item)"> <div class="oauth-login-item" v-for="item in SysUserSocialTypeEnum" :key="item.type" @click="doSocialLogin(item)">
<img :src=item.img height="25px" width="25px" alt="登录" > <img :src="item.img" height="25px" width="25px" alt="登录" >
<span>{{item.title}}</span> <span>{{item.title}}</span>
</div> </div>
</div> </div>
@ -48,6 +48,7 @@
import { getCodeImg,socialLoginRedirect } from "@/api/login"; import { getCodeImg,socialLoginRedirect } from "@/api/login";
import Cookies from "js-cookie"; import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt' import { encrypt, decrypt } from '@/utils/jsencrypt'
import {InfApiErrorLogProcessStatusEnum, SysUserSocialTypeEnum} from "@/utils/constants";
export default { export default {
name: "Login", name: "Login",
@ -61,23 +62,6 @@ export default {
code: "", code: "",
uuid: "" uuid: ""
}, },
oauthProviders: [{
img: "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.2/gitee.png",
title: "码云",
source: "gitee",
type: 10
}, {
img: "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.2/dingtalk.png",
title: "钉钉",
source: "dingtalk",
type: 20
} , {
img: "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.2/wechat_enterprise.png",
title: "企业微信",
source: "wechat_enterprise",
type: 30
}
],
loginRules: { loginRules: {
username: [ username: [
{ required: true, trigger: "blur", message: "用户名不能为空" } { required: true, trigger: "blur", message: "用户名不能为空" }
@ -88,18 +72,22 @@ export default {
code: [{ required: true, trigger: "change", message: "验证码不能为空" }] code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
}, },
loading: false, loading: false,
redirect: undefined redirect: undefined,
//
SysUserSocialTypeEnum: SysUserSocialTypeEnum,
}; };
}, },
watch: { // watch: {
$route: { // $route: {
handler: function(route) { // handler: function(route) {
this.redirect = route.query && route.query.redirect; // this.redirect = route.query && route.query.redirect;
}, // },
immediate: true // immediate: true
} // }
}, // },
created() { created() {
//
this.redirect = this.$route.query.redirect;
this.getCode(); this.getCode();
this.getCookie(); this.getCookie();
}, },
@ -143,16 +131,16 @@ export default {
} }
}); });
}, },
doAuth2Login(provider) { doSocialLogin(socialTypeEnum) {
// console.log("Oauth...%o", provider.code); // console.log("Oauth...%o", socialTypeEnum.code);
// //
this.loading = true; this.loading = true;
// redirectUri // redirectUri
// const redirectUri = location.origin + '/social-login'; const redirectUri = location.origin + '/social-login?type=' + socialTypeEnum.type + '&redirect=' + (this.redirect || "/"); //
const redirectUri = 'http://127.0.0.1:48080/api/gitee/callback'; // const redirectUri = 'http://127.0.0.1:48080/api/gitee/callback';
// const redirectUri = 'http://127.0.0.1:48080/api/dingtalk/callback'; // const redirectUri = 'http://127.0.0.1:48080/api/dingtalk/callback';
// //
socialLoginRedirect(provider.type, redirectUri).then((res) => { socialLoginRedirect(socialTypeEnum.type, encodeURIComponent(redirectUri)).then((res) => {
// console.log(res.url); // console.log(res.url);
window.location.href = res.data; window.location.href = res.data;
}); });

View File

@ -51,24 +51,27 @@ export default {
}, },
loading: false, loading: false,
redirect: undefined, redirect: undefined,
// //
type: undefined, type: undefined,
code: undefined, code: undefined,
state: undefined, state: undefined,
}; };
}, },
watch: { // watch: {
$route: { // $route: {
handler: function(route) { // handler: function(route) {
this.redirect = route.query && route.query.redirect; // this.redirect = route.query && route.query.redirect;
}, // },
immediate: true // immediate: true
} // }
}, // },
created() { created() {
this.getCookie(); this.getCookie();
// //
this.type = 10; // TODO this.redirect = this.$route.query.redirect;
debugger
//
this.type = this.$route.query.type;
this.code = this.$route.query.code; this.code = this.$route.query.code;
this.state = this.$route.query.state; this.state = this.$route.query.state;
this.$store.dispatch("SocialLogin", { this.$store.dispatch("SocialLogin", {