Commit c720b1dff9cb1f44719f8cd9ae84eca5e4cf4593

Authored by xingyu
1 parent 2513bbf0

fix: 三方登录未携带租户

yudao-ui-admin-vue3/src/api/login/index.ts
... ... @@ -57,7 +57,7 @@ export const smsLoginApi = (data: SmsLoginVO) => {
57 57 }
58 58  
59 59 // 社交授权的跳转
60   -export const socialAuthRedirectApi = (type: string, redirectUri: string) => {
  60 +export const socialAuthRedirectApi = (type: number, redirectUri: string) => {
61 61 return request.get({
62 62 url: '/system/auth/social-auth-redirect?type=' + type + '&redirectUri=' + redirectUri
63 63 })
... ...
yudao-ui-admin-vue3/src/views/Login/components/LoginForm.vue
... ... @@ -16,7 +16,7 @@
16 16 </el-form-item>
17 17 </el-col>
18 18 <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
19   - <el-form-item prop="tenantName">
  19 + <el-form-item prop="tenantName" v-if="loginData.tenantEnable === 'true'">
20 20 <el-input
21 21 type="text"
22 22 v-model="loginData.loginForm.tenantName"
... ... @@ -65,9 +65,13 @@
65 65 </el-col>
66 66 <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
67 67 <el-form-item>
68   - <el-button :loading="loginLoading" type="primary" class="w-[100%]" @click="getCode()">
69   - {{ t('login.login') }}
70   - </el-button>
  68 + <XButton
  69 + :loading="loginLoading"
  70 + type="primary"
  71 + class="w-[100%]"
  72 + :title="t('login.login')"
  73 + @click="getCode()"
  74 + />
71 75 </el-form-item>
72 76 </el-col>
73 77 <Verify
... ... @@ -81,19 +85,25 @@
81 85 <el-form-item>
82 86 <el-row justify="space-between" style="width: 100%" :gutter="5">
83 87 <el-col :span="8">
84   - <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.MOBILE)">
85   - {{ t('login.btnMobile') }}
86   - </el-button>
  88 + <XButton
  89 + class="w-[100%]"
  90 + :title="t('login.btnMobile')"
  91 + @click="setLoginState(LoginStateEnum.MOBILE)"
  92 + />
87 93 </el-col>
88 94 <el-col :span="8">
89   - <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.QR_CODE)">
90   - {{ t('login.btnQRCode') }}
91   - </el-button>
  95 + <XButton
  96 + class="w-[100%]"
  97 + :title="t('login.btnQRCode')"
  98 + @click="setLoginState(LoginStateEnum.QR_CODE)"
  99 + />
92 100 </el-col>
93 101 <el-col :span="8">
94   - <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.REGISTER)">
95   - {{ t('login.btnRegister') }}
96   - </el-button>
  102 + <XButton
  103 + class="w-[100%]"
  104 + :title="t('login.btnRegister')"
  105 + @click="setLoginState(LoginStateEnum.REGISTER)"
  106 + />
97 107 </el-col>
98 108 </el-row>
99 109 </el-form-item>
... ... @@ -103,32 +113,13 @@
103 113 <el-form-item>
104 114 <div class="flex justify-between w-[100%]">
105 115 <Icon
106   - icon="ant-design:github-filled"
107   - :size="30"
108   - class="cursor-pointer anticon"
109   - color="#999"
110   - @click="doSocialLogin('github')"
111   - />
112   - <Icon
113   - icon="ant-design:wechat-filled"
114   - :size="30"
115   - class="cursor-pointer anticon"
116   - color="#999"
117   - @click="doSocialLogin('wechat')"
118   - />
119   - <Icon
120   - icon="ant-design:alipay-circle-filled"
  116 + v-for="(item, key) in socialList"
  117 + :key="key"
  118 + :icon="item.icon"
121 119 :size="30"
122   - color="#999"
123 120 class="cursor-pointer anticon"
124   - @click="doSocialLogin('alipay')"
125   - />
126   - <Icon
127   - icon="ant-design:dingtalk-circle-filled"
128   - :size="30"
129 121 color="#999"
130   - class="cursor-pointer anticon"
131   - @click="doSocialLogin('dingtalk')"
  122 + @click="doSocialLogin(item.type)"
132 123 />
133 124 </div>
134 125 </el-form-item>
... ... @@ -154,6 +145,7 @@ import { useRouter } from &#39;vue-router&#39;
154 145 import type { RouteLocationNormalizedLoaded } from 'vue-router'
155 146 import { useI18n } from '@/hooks/web/useI18n'
156 147 import { useIcon } from '@/hooks/web/useIcon'
  148 +import { useMessage } from '@/hooks/web/useMessage'
157 149 import { required } from '@/utils/formRules'
158 150 import * as authUtil from '@/utils/auth'
159 151 import { decrypt } from '@/utils/jsencrypt'
... ... @@ -162,8 +154,13 @@ import { Verify } from &#39;@/components/Verifition&#39;
162 154 import { usePermissionStore } from '@/store/modules/permission'
163 155 import * as LoginApi from '@/api/login'
164 156 import { LoginStateEnum, useLoginState, useFormValid } from './useLogin'
  157 +import { XButton } from '@/components/XButton'
165 158  
166 159 const { t } = useI18n()
  160 +const message = useMessage()
  161 +const iconHouse = useIcon({ icon: 'ep:house' })
  162 +const iconAvatar = useIcon({ icon: 'ep:avatar' })
  163 +const iconLock = useIcon({ icon: 'ep:lock' })
167 164 const formLogin = ref()
168 165 const { validForm } = useFormValid(formLogin)
169 166 const { setLoginState, getLoginState } = useLoginState()
... ... @@ -171,9 +168,6 @@ const { currentRoute, push } = useRouter()
171 168 const permissionStore = usePermissionStore()
172 169 const redirect = ref<string>('')
173 170 const loginLoading = ref(false)
174   -const iconHouse = useIcon({ icon: 'ep:house' })
175   -const iconAvatar = useIcon({ icon: 'ep:avatar' })
176   -const iconLock = useIcon({ icon: 'ep:lock' })
177 171 const verify = ref()
178 172 const captchaType = ref('blockPuzzle') // blockPuzzle 滑块 clickWord 点击文字
179 173  
... ... @@ -201,6 +195,25 @@ const loginData = reactive({
201 195 }
202 196 })
203 197  
  198 +const socialList = [
  199 + {
  200 + icon: 'ant-design:github-filled',
  201 + type: 0
  202 + },
  203 + {
  204 + icon: 'ant-design:wechat-filled',
  205 + type: 30
  206 + },
  207 + {
  208 + icon: 'ant-design:alipay-circle-filled',
  209 + type: 0
  210 + },
  211 + {
  212 + icon: 'ant-design:dingtalk-circle-filled',
  213 + type: 20
  214 + }
  215 +]
  216 +
204 217 // 获取验证码
205 218 const getCode = async () => {
206 219 // 情况一,未开启:则直接登录
... ... @@ -214,8 +227,10 @@ const getCode = async () =&gt; {
214 227 }
215 228 //获取租户ID
216 229 const getTenantId = async () => {
217   - const res = await LoginApi.getTenantIdByNameApi(loginData.loginForm.tenantName)
218   - authUtil.setTenantId(res)
  230 + if (loginData.tenantEnable === 'true') {
  231 + const res = await LoginApi.getTenantIdByNameApi(loginData.loginForm.tenantName)
  232 + authUtil.setTenantId(res)
  233 + }
219 234 }
220 235 // 记住我
221 236 const getCookie = () => {
... ... @@ -279,14 +294,24 @@ const handleLogin = async (params) =&gt; {
279 294 }
280 295  
281 296 // 社交登录
282   -const doSocialLogin = async (type: string) => {
283   - loginLoading.value = true
284   - // 计算 redirectUri
285   - const redirectUri =
286   - location.origin + '/social-login?type=' + type + '&redirect=' + (redirect.value || '/')
287   - // 进行跳转
288   - const res = await LoginApi.socialAuthRedirectApi(type, encodeURIComponent(redirectUri))
289   - window.location.href = res
  297 +const doSocialLogin = async (type: number) => {
  298 + if (type === 0) {
  299 + message.error('此方式未配置')
  300 + } else {
  301 + loginLoading.value = true
  302 + if (loginData.tenantEnable === 'true') {
  303 + await message.prompt('请输入租户名称', t('common.reminder')).then(async ({ value }) => {
  304 + const res = await LoginApi.getTenantIdByNameApi(value)
  305 + authUtil.setTenantId(res)
  306 + })
  307 + }
  308 + // 计算 redirectUri
  309 + const redirectUri =
  310 + location.origin + '/social-login?type=' + type + '&redirect=' + (redirect.value || '/')
  311 + // 进行跳转
  312 + const res = await LoginApi.socialAuthRedirectApi(type, encodeURIComponent(redirectUri))
  313 + window.location.href = res
  314 + }
290 315 }
291 316 watch(
292 317 () => currentRoute.value,
... ...
yudao-ui-admin/src/views/login.vue
... ... @@ -53,10 +53,10 @@
53 53 <el-form-item prop="mobileCode">
54 54 <el-input v-model="loginForm.mobileCode" type="text" auto-complete="off" placeholder="短信验证码"
55 55 @keyup.enter.native="handleLogin">
56   - <template slot="icon">
  56 + <template v-slot="icon">
57 57 <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
58 58 </template>
59   - <template slot="append">
  59 + <template v-slot="append">
60 60 <span v-if="mobileCodeTimer <= 0" class="getMobileCode" @click="getSmsCode" style="cursor: pointer;">获取验证码</span>
61 61 <span v-if="mobileCodeTimer > 0" class="getMobileCode">{{ mobileCodeTimer }}秒后可重新获取</span>
62 62 </template>
... ... @@ -115,6 +115,7 @@ import {
115 115 } from "@/utils/auth";
116 116  
117 117 import Verify from '@/components/Verifition/Verify';
  118 +import {resetUserPwd} from "@/api/system/user";
118 119  
119 120 export default {
120 121 name: "Login",
... ... @@ -254,19 +255,40 @@ export default {
254 255 }
255 256 });
256 257 },
257   - doSocialLogin(socialTypeEnum) {
  258 + async doSocialLogin(socialTypeEnum) {
258 259 // 设置登录中
259 260 this.loading = true;
260   - // 计算 redirectUri
261   - const redirectUri = location.origin + '/social-login?'
262   - + encodeURIComponent('type=' + socialTypeEnum.type + '&redirect=' + (this.redirect || "/")); // 重定向不能丢
263   - // const redirectUri = 'http://127.0.0.1:48080/api/gitee/callback';
264   - // const redirectUri = 'http://127.0.0.1:48080/api/dingtalk/callback';
265   - // 进行跳转
266   - socialAuthRedirect(socialTypeEnum.type, encodeURIComponent(redirectUri)).then((res) => {
267   - // console.log(res.url);
268   - window.location.href = res.data;
269   - });
  261 + let tenant = false;
  262 + if (this.tenantEnable) {
  263 + await this.$prompt('请输入租户名称', "提示", {
  264 + confirmButtonText: "确定",
  265 + cancelButtonText: "取消"
  266 + }).then(({value}) => {
  267 + getTenantIdByName(value).then(res => {
  268 + const tenantId = res.data;
  269 + tenant = true
  270 + if (tenantId && tenantId >= 0) {
  271 + setTenantId(tenantId)
  272 + }
  273 + });
  274 + }).catch(() => {
  275 + return false
  276 + });
  277 + } else {
  278 + tenant = true
  279 + }
  280 + if(tenant){
  281 + // 计算 redirectUri
  282 + const redirectUri = location.origin + '/social-login?'
  283 + + encodeURIComponent('type=' + socialTypeEnum.type + '&redirect=' + (this.redirect || "/")); // 重定向不能丢
  284 + // const redirectUri = 'http://127.0.0.1:48080/api/gitee/callback';
  285 + // const redirectUri = 'http://127.0.0.1:48080/api/dingtalk/callback';
  286 + // 进行跳转
  287 + socialAuthRedirect(socialTypeEnum.type, encodeURIComponent(redirectUri)).then((res) => {
  288 + // console.log(res.url);
  289 + window.location.href = res.data;
  290 + });
  291 + }
270 292 },
271 293 /** ========== 以下为升级短信登录 ========== */
272 294 getSmsCode() {
... ...