Sfoglia il codice sorgente

✨ feat(login): 钉钉免登录

Zimo 2 settimane fa
parent
commit
070b319a9a

+ 3 - 1
.env.local

@@ -31,4 +31,6 @@ VITE_MALL_H5_DOMAIN='http://localhost:3000'
 VITE_APP_CAPTCHA_ENABLE=false
 
 # GoView域名
-VITE_GOVIEW_URL='http://127.0.0.1:3000'
+VITE_GOVIEW_URL='http://127.0.0.1:3000'
+
+

+ 3 - 0
.env.prod

@@ -34,3 +34,6 @@ VITE_MALL_H5_DOMAIN='http://mall.yudao.iocoder.cn'
 VITE_APP_CAPTCHA_ENABLE=false
 # GoView域名
 VITE_GOVIEW_URL='http://127.0.0.1:3000'
+
+VITE_DD_CORPID='dingbe7f9a7e8cffa2bd35c2f4657eb6378f'
+VITE_DD_CLIENTID='dingmr9ez0ecgbmscfeb'

+ 1 - 1
src/api/login/index.ts

@@ -17,7 +17,7 @@ export const login = (data: UserLoginVO) => {
 }
 
 export const dingTalkLogin = (data:{code:string,type:number,state:number}) => {
-  return request.post({ url: '/system/auth/h5SocialLogin', data })
+  return request.post({ url: '/system/auth/h5SocialLogin', data,headers:{'tenant-id':1} })
 }
 
 export const simpleLogin = (id: any) => {

+ 15 - 1
src/config/axios/service.ts

@@ -8,7 +8,7 @@ import errorCode from './errorCode'
 
 import { resetRouter } from '@/router'
 import { deleteUserCache } from '@/hooks/web/useCache'
-import {langHelper} from '@/utils/langHelper'
+import { langHelper } from '@/utils/langHelper'
 import { useLocaleStore } from '@/store/modules/locale'
 const tenantEnable = import.meta.env.VITE_APP_TENANT_ENABLE
 const { result_code, base_url, request_timeout } = config
@@ -49,20 +49,32 @@ service.interceptors.request.use(
         return (isToken = false)
       }
     })
+
+    debugger
+
     if (getAccessToken() && !isToken) {
       config.headers.Authorization = 'Bearer ' + getAccessToken() // 让每个请求携带自定义token
     }
+
+    debugger
+
     // 设置租户
     if (tenantEnable && tenantEnable === 'true') {
       const tenantId = getTenantId()
       if (tenantId) config.headers['tenant-id'] = tenantId
     }
+
+    debugger
+
     const method = config.method?.toUpperCase()
+
+    debugger
     // 防止 GET 请求缓存
     if (method === 'GET') {
       config.headers['Cache-Control'] = 'no-cache'
       config.headers['Pragma'] = 'no-cache'
     }
+
     // 自定义参数序列化函数
     else if (method === 'POST') {
       const contentType = config.headers['Content-Type'] || config.headers['content-type']
@@ -72,6 +84,8 @@ service.interceptors.request.use(
         }
       }
     }
+
+    debugger
     return config
   },
   (error: AxiosError) => {

+ 60 - 8
src/views/Login/Login.vue

@@ -80,6 +80,13 @@ import * as dd from 'dingtalk-jsapi'
 import { LoginForm, QrCodeForm, RegisterForm, ForgetPasswordForm } from './components'
 import { dingTalkLogin } from '../../api/login'
 
+import * as authUtil from '@/utils/auth'
+import { RouteLocationNormalizedLoaded } from 'vue-router'
+import { usePermissionStore } from '../../store/modules/permission'
+
+import * as LoginApi from '@/api/login'
+import { ElLoading } from 'element-plus'
+
 defineOptions({ name: 'Login' })
 
 const { t } = useI18n()
@@ -87,34 +94,79 @@ const appStore = useAppStore()
 const { getPrefixCls } = useDesign()
 const prefixCls = getPrefixCls('login')
 
-function loginWithDingTalk() {
+const loading = ref<any>(null)
+
+const redirect = ref<string>('')
+
+const router = useRouter()
+
+watch(
+  () => router.currentRoute.value,
+  (route: RouteLocationNormalizedLoaded) => {
+    redirect.value = route?.query?.redirect as string
+  },
+  {
+    immediate: true
+  }
+)
+
+const getTenantId = async () => {
+  if (import.meta.env.VITE_APP_TENANT_ENABLE === 'true') {
+    const res = await LoginApi.getTenantIdByName('芋道源码')
+    debugger
+    authUtil.setTenantId(res)
+  }
+}
+
+const permissionStore = usePermissionStore()
+
+async function loginWithDingTalk() {
   const ddCorpId = import.meta.env.VITE_DD_CORPID
   const ddClientId = import.meta.env.VITE_DD_CLIENTID
 
   if (!ddCorpId || !ddClientId) return
 
+  await getTenantId()
+
   dd.requestAuthCode({
     corpId: ddCorpId,
     clientId: ddClientId,
     success: (res: any) => {
       const { code } = res
-
-      dingTalkLogin({ code, type: 10, state: new Date().getTime() }).then((res) => {
+      dingTalkLogin({ code, type: 10, state: new Date().getTime() }).then(async (res) => {
         console.log('res :>> ', res)
+        loading.value = ElLoading.service({
+          lock: true,
+          text: '正在加载系统中...',
+          background: 'rgba(0, 0, 0, 0.7)'
+        })
+
+        // authUtil.removeLoginForm()
+
+        authUtil.setToken(res)
+
+        if (!redirect.value) {
+          redirect.value = '/'
+        }
+        if (redirect.value.indexOf('sso') !== -1) {
+          window.location.href = window.location.href.replace('/login?redirect=', '')
+        } else {
+          router.push({ path: redirect.value || permissionStore.addRouters[0].path }).then(() => {
+            loading.value.close()
+          })
+        }
       })
     },
     fail: (err: any) => {
       console.log('err :>> ', err)
+    },
+    complete: () => {
+      console.log('11 :>> ', 11)
     }
   })
 }
 
 function dingTalkAutoLogin() {
-  const url = window.location.href
-
-  console.log('url :>> ', url)
-  if (!url.includes('/deepoli')) return
-
   const ua = window.navigator.userAgent.toLowerCase()
 
   console.log('ua :>> ', ua)

+ 2 - 1
src/views/Login/components/LoginForm.vue

@@ -263,11 +263,13 @@ const handleLogin = async (params: any) => {
     // if (!res) {
     //   return
     // }
+    console.log('res :>> ', res)
     loading.value = ElLoading.service({
       lock: true,
       text: '正在加载系统中...',
       background: 'rgba(0, 0, 0, 0.7)'
     })
+    console.log('loginDataLoginForm.rememberMe :>> ', loginDataLoginForm.rememberMe)
     if (loginDataLoginForm.rememberMe) {
       authUtil.setLoginForm(loginDataLoginForm)
     } else {
@@ -360,4 +362,3 @@ onMounted(() => {
   }
 }
 </style>
-