瀏覽代碼

优化授权登录

ZaiZai 1 年之前
父節點
當前提交
026e416ba9

+ 1 - 1
public/version.json

@@ -1,3 +1,3 @@
 {
-  "value": "20240202170209"
+  "value": "20240221114759"
 }

+ 9 - 1
src/api/modules/other.js

@@ -113,6 +113,14 @@ export const roletree = (form, msg = true) => httpApi({
       },
 }, msg)
 
+//获取租户详情
+export const getTenantDetail = (id) => httpApi({
+    url: '/api/blade-system/tenant/detail',
+    method: 'get',
+    params: {
+        tenantId: id,
+    },
+}, true)
 
 //获取更新信息
 export const getVersionJson = () => httpApi({
@@ -138,4 +146,4 @@ export const saveSmsTimeout = (form, msg = true) => httpApi({
     url: '/api/blade-business/task/save-sms-timeout',
     method: 'get',
     params: form,
-}, msg)
+}, msg)

+ 30 - 78
src/layout/modules/Cascader.vue

@@ -5,7 +5,7 @@
         </div>
         <el-cascader
             ref="ElCascaderRef"
-            v-model="projectValue" class="hc-header-cascader"
+            v-model="contractId" class="hc-header-cascader"
             :clearable="userInfo?.role_id === '1123598816738675201'"
             :filterable="userInfo?.role_id === '1123598816738675201'"
             :options="projectContract"
@@ -18,8 +18,8 @@
 <script setup>
 import { onMounted, ref, watch } from 'vue'
 import { useAppStore } from '~src/store'
-import { getArrValue } from 'js-fast-way'
 import { initProjectContract } from '~sto/app'
+import { isNullES } from 'js-fast-way'
 
 //事件
 const emit = defineEmits(['change', 'send'])
@@ -28,95 +28,47 @@ const emit = defineEmits(['change', 'send'])
 const store = useAppStore()
 const userInfo = ref(store.getUserInfo)
 
-//项目合同段
-const projectInfo = ref({})
-const contractInfo = ref({})
-const projectContract = ref([])
-const projectValue = ref(null)
 const projectProps = ref({
     value: 'id',
     label: 'projectAlias',
     children: 'contractInfoList',
 })
 
+//项目合同段
+const projectContract = ref(store.projectContract)
+const projectInfo = ref(store.projectInfo)
+const contractInfo = ref(store.contractInfo)
+const contractId = ref(store.contractId)
+
 //监听
-watch(() => store.getProjectContract, (val) => {
-    projectContractData(getArrValue(val))
+watch(() => [
+    store.projectContract,
+    store.projectInfo,
+    store.contractInfo,
+    store.projectId,
+    store.contractId,
+], ([arr, project, contract, pid, cid]) => {
+    projectContract.value = arr
+    projectInfo.value = project
+    contractInfo.value = contract
+    contractId.value = cid
+    setSend(pid, cid)
 })
 
 //渲染完成
-onMounted(() => {
-    setInitData()
+onMounted(async () => {
+    await initProjectContract()
+    setSend(store.projectId, store.contractId)
 })
 
-const setInitData = async () => {
-    try {
-        if (!store.projectId) {
-            await initProjectContract()
-        }
-        projectContractData(store.getProjectContract)
-    } catch (error) {
-        console.log(error)
-    }
-}
-
-//处理项目合同段数据
-const projectContractData = (projectContractData) => {
-    if (projectContractData.length > 0) {
-        //处理别名
-        projectContractData.forEach(item => {
-            let contractArr = item['contractInfoList'] || []
-            contractArr.forEach(items => {
-                items['projectAlias'] = items['name']
-            })
-        })
-        //处理其他数据
-        projectContract.value = projectContractData
-        const projectId = store.getProjectId //项目ID
-        const contractId = store.getContractId //合同段ID
-        const UserProjectInfo = store.getProjectInfo
-        const UserContractInfo = store.getContractInfo
-        //查询缓存的选中ID是否存在
-        const pid = projectContractData.findIndex(item => Number(item.id) === Number(projectId))
-        const contractList = projectContractData[pid]?.contractInfoList || []
-        const cid = contractList.findIndex(item => Number(item.id) === Number(contractId))
-        //如果缓存的选中ID不存在
-        if (cid === -1) {
-            //取项目数组中的第一个数据
-            let letProjectInfo = projectContractData[0]
-            let contractInfoList = letProjectInfo?.contractInfoList || []
-            let letContractInfo = contractInfoList[0] || {}
-            projectValue.value = letContractInfo?.id
-            projectInfo.value = letProjectInfo
-            contractInfo.value = letContractInfo
-            //设置缓存
-            store.setProjectInfo(letProjectInfo)
-            store.setContractInfo(letContractInfo)
-            store.setProjectId(letProjectInfo?.id)
-            store.setContractId(letContractInfo?.id)
-            emit('send', {
-                projectId: letProjectInfo?.id,
-                contractId: letContractInfo?.id,
-            })
-        } else {
-            projectValue.value = String(contractId)
-            projectInfo.value = UserProjectInfo
-            contractInfo.value = UserContractInfo
-            emit('send', {
-                projectId: projectId,
-                contractId: contractId,
-            })
-        }
-    } else {
-        projectContract.value = []
-        projectValue.value = null
-        projectInfo.value = {}
-        contractInfo.value = {}
-        emit('send', {
-            projectId: '',
-            contractId: '',
-        })
+const setSend = (pid, cid) => {
+    if (isNullES(pid) || isNullES(cid)) {
+        return
     }
+    emit('send', {
+        projectId: pid,
+        contractId: cid,
+    })
 }
 
 //项目被选择

+ 26 - 2
src/plugins/HcTopMenu.js

@@ -1,4 +1,8 @@
-import { getArrValue } from 'js-fast-way'
+import { isPathUrl } from '~uti/tools'
+import { getToken } from '~src/api/util/auth'
+import { getStoreValue } from '~uti/storage'
+import { getTenantDetail } from '~api/other'
+import { getArrValue, getObjValue, newWindow } from 'js-fast-way'
 
 export default class HcTopMenu {
 
@@ -21,6 +25,26 @@ export default class HcTopMenu {
     }
 
     static async setMenuItem(item) {
-        return getArrValue(item?.children)
+        if (isPathUrl(item?.path)) {
+            let token = getToken(), domain = item?.path
+            const tenantId = getStoreValue('tenantId') ?? ''
+            const projectId = getStoreValue('projectId') ?? ''
+            const contractId = getStoreValue('contractId') ?? ''
+            if (item?.code === 'to-client-url' || item?.code === 'to-measure-url') {
+                if (tenantId === '000000' || !tenantId) {
+                    domain = item?.path
+                } else {
+                    const { error, code, data } = await getTenantDetail(tenantId)
+                    if (!error && code === 200) {
+                        const url = getObjValue(data).domainUrl
+                        domain = url ? url : item?.path
+                    }
+                }
+            }
+            //跳转到新页面
+            newWindow(`${domain}/#/auth?token=${token}&tid=${tenantId}&pid=${projectId}&cid=${contractId}`)
+        } else {
+            return getArrValue(item?.children)
+        }
     }
 }

+ 77 - 35
src/store/modules/app.js

@@ -24,60 +24,102 @@ export const initProjectContract = async () => {
 //获取默认项目信息
 export const getProjectContract = async () => {
     const { error, data } = await projectApi.getProjectAndContract()
-    const projectList = getArrValue(data)
-    if (error || projectList.length <= 0) {
-        window.$message?.error('没有相关项目权限')
+    let projectList = await getProjectArr(error, data)
+    if (projectList.length <= 0) {
         return { error: true, data: [] }
     }
-    //获取默认项目合同段数据
-    const defaultProject = await getDefaultProject()
-    let projectInfo = {}, contractInfo = {}
-    if (defaultProject.code === 200) {
-        projectInfo = defaultProject.project
-        contractInfo = defaultProject.contract
-    } else {
-        //过滤空合同段的项目合同段数据
-        const projectArr = projectList.filter(({ contractInfoList }) => {
-            const contractList = getArrValue(contractInfoList)
-            return contractList.length > 0
-        })
-        if (projectArr.length <= 0) {
-            window.$message?.error('没有相关项目权限')
-            return { error: true, data: [] }
-        }
-        //获取第一个项目的第一个合同段数据
-        const contractList = projectArr[0].contractInfoList
-        projectInfo = projectList[0]
-        contractInfo = contractList[0]
+    //获取缓存的项目合同段数据
+    const isStore = await getStoreProjecInfo(projectList)
+    if (!isStore) {
+        const isDefault = await getDefaultProject(projectList)
+        if (!isDefault) return { error: true, data: [] }
     }
     //获取按钮权限
     await initButtons()
-    //设置项目合同段数据
-    store.setProjectInfo(projectInfo)
-    store.setProjectId(projectInfo.id)
-    store.setContractInfo(contractInfo)
-    store.setContractId(contractInfo.id)
-    store.setProjectContract(projectList)
+    //返回数据
     return { error: false, data: projectList }
 }
 
+//根据缓存获取项目合同段数据
+const getStoreProjecInfo = async (arr) => {
+    const projectId = store.projectId //项目ID
+    const contractId = store.contractId //合同段ID
+    //查询缓存的选中ID是否存在
+    const pid = arr.findIndex(item => Number(item.id) === Number(projectId))
+    const contractList = getArrValue(arr[pid]?.contractInfoList)
+    const cid = contractList.findIndex(item => Number(item.id) === Number(contractId))
+    //如果缓存的选中ID不存在
+    if (cid === -1) {
+        return false
+    }
+    //获取项目合同段数据
+    const projectInfo = await getProjectInfo(projectId)
+    const contractInfo = await getContractInfo(contractId)
+    if (isNullES(projectInfo) || isNullES(contractInfo)) {
+        return false
+    }
+    setProjectStore(projectInfo, contractInfo)
+    return true
+}
+
 //获取默认项目信息
-const getDefaultProject = async () => {
+const getDefaultProject = async (projectList) => {
     const { error, status, data } = await projectApi.getDefaultProject()
     if (!error && status === 200 && !isNullES(data)) {
         const { projectId, contractId } = getObjValue(data)
         if (!projectId || !contractId) {
-            return { code: 300 }
+            return false
         }
         const projectInfo = await getProjectInfo(projectId)
         const contractInfo = await getContractInfo(contractId)
         if (isNullES(projectInfo) || isNullES(contractInfo)) {
-            return { code: 300 }
+            return false
         }
-        return { code: 200, project: projectInfo, contract: contractInfo }
-    } else {
-        return { code: 300 }
+        setProjectStore(projectInfo, contractInfo)
+        return true
+    }
+    //获取第一个项目的第一个合同段数据
+    const contractList = getArrValue(projectList[0]?.contractInfoList)
+    const projectInfo = projectList[0]
+    const contractInfo = contractList[0]
+    //缓存数据
+    setProjectStore(projectInfo, contractInfo)
+    return true
+}
+
+//缓存数据
+const setProjectStore = (project, contract) => {
+    store.setProjectInfo(project)
+    store.setProjectId(project.id)
+    store.setContractInfo(contract)
+    store.setContractId(contract.id)
+}
+
+const getProjectArr = async (error, data) => {
+    let projectList = getArrValue(data)
+    if (error || projectList.length <= 0) {
+        window.$message?.error('没有相关项目权限')
+        return []
+    }
+    //处理合同段的别名
+    projectList.forEach(item => {
+        let contractArr = item['contractInfoList'] || []
+        contractArr.forEach(items => {
+            items['projectAlias'] = items['name']
+        })
+    })
+    //过滤空合同段的项目合同段数据
+    const projectArr = projectList.filter(({ contractInfoList }) => {
+        const contractList = getArrValue(contractInfoList)
+        return contractList.length > 0
+    })
+    if (projectArr.length <= 0) {
+        window.$message?.error('没有相关项目权限')
+        return []
     }
+    store.setProjectContract(projectArr)
+    //返回数据
+    return projectArr
 }
 
 //获取项目信息

+ 9 - 0
src/utils/tools.js

@@ -84,13 +84,22 @@ export const setAppName = (name) => {
     const title = window.document.title
     window.document.title = `${title} - ${name}`
 }
+
+
 //替换http为https
 export const setUrlHttps = (url) => {
     if (!url) return url
     return url.replace('http://', 'https://')
 }
+
+
 //删除-字符串
 export const setTimeString = (str) => {
     if (!str) return str
     return str.replace(/-/g, '')
 }
+
+//判断是否为网址
+export const isPathUrl = (path) => {
+    return /^(https?:|mailto:|tel:)/.test(path)
+}

+ 1 - 1
src/views/home/datav.vue

@@ -132,7 +132,7 @@ const projectId = ref(store.projectId)
 const setInitData = async () => {
     isLoading.value = true
     try {
-        if (!store.projectId) {
+        if (!store.projectContract) {
             await initProjectContract()
         }
         project.value = getObjValue(store.getProjectInfo)

+ 7 - 4
src/views/user/auth.vue

@@ -37,19 +37,22 @@ const isErrorShow = ref(false)
 onMounted(() => {
     //先清理下缓存
     store.clearStoreData()
-    // 域名后加  /#/auth?token=xxxxxxx&tenantid=xxxxxxx
-    const { token, tenantid } = getObjValue(useRoutes.query)
+    // 域名后加  /#/auth?token=xxx&tid=xxx&pid=xxx&cid=xxx
+    const { token, tid, pid, cid } = getObjValue(useRoutes.query)
     if (token) {
         isErrorShow.value = false
+        //缓存数据
         store.setTokenVal(token)
-        queryCurrentUserData(tenantid)
+        store.setProjectId(pid)
+        store.setContractId(cid)
+        //处理授权登录
+        queryCurrentUserData(tid)
     } else {
         loading.value = false
         isErrorShow.value = true
     }
 })
 
-
 //获取租户id
 const getTenantIdApi = async () => {
     const { data } = await getTenantID(getTopUrl())