From 2b7ffdf19c0e525f03dc6234b58cac03b627f0a5 Mon Sep 17 00:00:00 2001 From: thiszhc <2029364173@qq.com> Date: 星期六, 01 七月 2023 02:26:58 +0800 Subject: [PATCH] 前端实现切换租户选择第三方授权登录,和优化第三方页面登录注册 --- src/views/login.vue | 17 +++++++++++++---- src/layout/components/SocialLogin/index.vue | 26 +++++++++++++++++++------- src/api/login.ts | 8 ++++++-- src/router/index.ts | 1 + 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/api/login.ts b/src/api/login.ts index 9903f5b..f60406c 100644 --- a/src/api/login.ts +++ b/src/api/login.ts @@ -66,11 +66,15 @@ * 绗笁鏂圭櫥褰� * @param source 绗笁鏂圭櫥褰曠被鍨� * */ -export function socialLogin(source: string, code: any, state: any): AxiosPromise<any> { +export function socialLogin(source: string, tenantId: string, loginType: string, code: any, state: any): AxiosPromise<any> { const data = { code, state, - source + source, + tenantId, + loginType, + clientId: 'e5cd7e4891bf95d1d19206ce24a7b32e', + grantType: 'social' }; return request({ url: '/auth/social-login', diff --git a/src/layout/components/SocialLogin/index.vue b/src/layout/components/SocialLogin/index.vue index 127007b..82fb253 100644 --- a/src/layout/components/SocialLogin/index.vue +++ b/src/layout/components/SocialLogin/index.vue @@ -3,11 +3,14 @@ </template> <script setup lang="ts"> -import {socialLogin} from '@/api/login'; -import {setToken} from '@/utils/auth'; +import { socialLogin } from '@/api/login'; +import { setToken } from '@/utils/auth'; +import Cookies from 'js-cookie'; +import { getToken } from '@/utils/auth'; +import router from '@/router'; const route = useRoute(); -const router = useRouter(); +const loading = ref(true); /** @@ -17,8 +20,16 @@ const code = route.query.code; const state = route.query.state; const source = route.query.source as string; -const loading = ref(true); -await socialLogin(source, code, state) +const tenantId = Cookies.get("tenantId") ? Cookies.get("tenantId") as string : '000000'; +const loginType = getToken() ? 'register' : 'login'; + +/** + * 閫氳繃code鑾峰彇token + * @param {string} source + * @param {string} code + * @param {string} state + */ +await socialLogin(source, tenantId, loginType, code, state) .then(async (res) => { if (res.code !== 200) { ElMessage.error(res.msg); @@ -26,9 +37,10 @@ return; } loading.value = false; - setToken(res.msg); - ElMessage.success('鐧诲綍鎴愬姛'); + // setToken(res.msg); + loginType === 'login' ? setToken(res.data) : ElMessage.success(res.msg); location.href = import.meta.env.VITE_APP_CONTEXT_PATH + 'index'; + }) .catch(() => { loading.value = false; diff --git a/src/router/index.ts b/src/router/index.ts index 76a81bc..dfd0547 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -181,4 +181,5 @@ } }); + export default router; diff --git a/src/views/login.vue b/src/views/login.vue index 9becbc3..3234864 100644 --- a/src/views/login.vue +++ b/src/views/login.vue @@ -4,7 +4,8 @@ <h3 class="title">RuoYi-Vue-Plus澶氱鎴风鐞嗙郴缁�</h3> <el-form-item prop="tenantId" v-if="tenantEnabled"> <el-select v-model="loginForm.tenantId" filterable placeholder="璇烽�夋嫨/杈撳叆鍏徃鍚嶇О" style="width: 100%"> - <el-option v-for="item in tenantList" :key="item.tenantId" :label="item.companyName" :value="item.tenantId"></el-option> + <el-option v-for="item in tenantList" :key="item.tenantId" :label="item.companyName" + :value="item.tenantId"></el-option> <template #prefix><svg-icon icon-class="company" class="el-input__icon input-icon" /></template> </el-select> </el-form-item> @@ -14,12 +15,14 @@ </el-input> </el-form-item> <el-form-item prop="password"> - <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="瀵嗙爜" @keyup.enter="handleLogin"> + <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="瀵嗙爜" + @keyup.enter="handleLogin"> <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template> </el-input> </el-form-item> <el-form-item prop="code" v-if="captchaEnabled"> - <el-input v-model="loginForm.code" size="large" auto-complete="off" placeholder="楠岃瘉鐮�" style="width: 63%" @keyup.enter="handleLogin"> + <el-input v-model="loginForm.code" size="large" auto-complete="off" placeholder="楠岃瘉鐮�" style="width: 63%" + @keyup.enter="handleLogin"> <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template> </el-input> <div class="login-code"> @@ -177,6 +180,12 @@ } } } + +//妫�娴嬬鎴烽�夋嫨妗嗙殑鍙樺寲 +watch(() => loginForm.value.tenantId, (val: string) => { + Cookies.set("tenantId", loginForm.value.tenantId, { expires: 30 }) +}); + /** * 绗笁鏂圭櫥褰� * @param type @@ -185,7 +194,7 @@ authBinding(type).then((res: any) => { if (res.code === 200) { window.location.href = res.msg; - } else { + } else { ElMessage.error(res.msg); } }); -- Gitblit v1.9.3