Ver Fonte

feat: 完善嵌入的小数的功能

1. 根据权限判断是否有返回按钮
2. 新增 退出登录 按钮
3. 为通用的 hasPermission 方法添加注释
zhupei@smartai.com há 1 ano atrás
pai
commit
cfe53aaf68
2 ficheiros alterados com 132 adições e 14 exclusões
  1. 8 1
      src/utils/permission.js
  2. 124 13
      src/views/Xiaoshu.vue

+ 8 - 1
src/utils/permission.js

@@ -1,6 +1,13 @@
 import { localST_UserInfo, localST_RoutesList } from '../base/storage';
 
-// 判断是否有该权限
+/**
+ * 根据 系统资源标识符 判断当前用户对某个资源是否拥有权限
+ * 所有可用的资源标识符,请查看下方链接中的定义
+ * 大模型2.0 -> 权限管理 -> 资源管理
+ * @see http://smartai.com:8293/#/permission/resource
+ * @param {String} str - 资源标识符,例如:system:menu:list
+ * @returns {Boolean}
+ */
 export const hasPermission = (str) => {
   try {
     const user_nfo = localST_UserInfo.getItem();

+ 124 - 13
src/views/Xiaoshu.vue

@@ -24,12 +24,18 @@ import WujieVue from 'wujie-vue3';
 import LayoutExperience from './layout/LayoutExperience.vue';
 import ContentContainer from './layout/ContentContainer.vue';
 
+import useLogout from './account/use-logout';
+
 import { useCurrentUserStore } from '../base/store/use-current-user';
 
 import { getConfigField } from '../config/index';
 
+import { hasPermission } from '../utils/permission';
+
 const router = useRouter();
 
+const logout = useLogout();
+
 const { bus } = WujieVue;
 
 const props = defineProps({});
@@ -47,6 +53,41 @@ const containerStyle = {
   padding: 0
 };
 
+/**
+ * 获取小数返回按钮的链接
+ * 返回一个合法的路由字符串,例如 /chat/qa
+ * 或者
+ * 一个空字符串,表示无任何可用的路由
+ * @returns {String}
+ */
+function getXiaoshuBackUrl() {
+  /**
+   * 系统资源
+   */
+  const sys_resource_list = {
+    'system:menu:conversation': '/chat/qa', // 会话
+    'system:conversation:qa': '/chat/qa', // 会话进行中
+    'system:conversation:hisroty': '/chat/history', // 会话,历史记录
+    'system:conversation:board': '/chat/board', // 会话,智能体大全
+    'system:menu:intelligent': '/intelligent', // 智能体
+    'system:menu:knowledge': '/knowledge' // 知识库管理
+  };
+
+  let back_route = ''; // 返回的地址
+
+  for (let key in sys_resource_list) {
+    const has_permission = hasPermission(key);
+
+    if (has_permission) {
+      // 有权限
+      back_route = sys_resource_list[key];
+      break;
+    }
+  }
+
+  return back_route;
+}
+
 onMounted(() => {
   const currentUser = useCurrentUserStore();
 
@@ -56,6 +97,9 @@ onMounted(() => {
   const info_val = { userName: user_name, token: token };
   const info_str = JSON.stringify(info_val);
 
+  // 小数菜单下方,返回 按钮对应的链接
+  const back_url = getXiaoshuBackUrl();
+
   /**
    * 插入到小数项目中的脚本
    */
@@ -65,23 +109,75 @@ onMounted(() => {
     window.ASSETS_BASE_URL = '${xiaoshu_assets_url}';
 
     localStorage.setItem('xintongxiaoshu', '${info_str}');
+
+    const observer = new MutationObserver((mutationsList) => {
+      mutationsList.forEach((mutation) => {
+        if (mutation.type === "childList") {
+          // console.log("A child node has been added or removed.");
+          // console.log(mutation);
+
+          if(mutation.addedNodes.length > 0){
+            // console.log('新节点被添加', mutation.addedNodes);
+            // console.log(mutation);
+
+            const addedNodes = mutation.addedNodes;
+            addedNodes.forEach((item, index) => {
+              const klass_name = item.className;
+              if(klass_name === 'arco-trigger-popup arco-trigger-position-rt arco-popover'){
+                // popover
+                // 可能是用户名,账号
+                const box = item.querySelector('.xiaoshu-aside-left-menus-pop-container');
+
+                const div = document.createElement('div');
+                div.setAttribute('class', 'xiaoshu-aside-left-menus-logout-btn');
+                const span = document.createElement('span');
+                span.innerText = '退出登录'
+                div.appendChild(span);
+                box.appendChild(div);
+                div.addEventListener('click', () => {
+                  window.$wujie?.bus.$emit("xiaoshuAsideLogout");
+                });
+              }
+            });
+          }
+
+          if(mutation.removedNodes.length > 0){
+            // console.log('节点被移除', mutation.removedNodes);
+            // console.log(mutation);
+          }
+        } else if (mutation.type === "attributes") {
+          // console.log('属性被修改了');
+        }else{
+          // console.log('其他');
+        }
+      });
+    });
+    observer.observe(document.body, {childList: true, subtree: true})
   `;
 
-  // TODO: 根据权限判断是否有 返回 按钮
-  const insert_script_after_str = `
-    const box = document.querySelector('.xiaoshu-aside-left-menus-container');
+  // 根据权限判断是否有 返回 按钮
+  let insert_script_after_str = '';
 
-    if(box){
-      const btn = document.createElement('div');
-      box.appendChild(btn);
+  if (back_url) {
+    // 有返回链接
+    insert_script_after_str = `
+      const box = document.querySelector('.xiaoshu-aside-left-menus-container');
 
-      btn.innerHTML = '<span class="xiaoshu-aside-left-back-btn">返回</span>';
+      if(box){
+        const btn = document.createElement('div');
+        box.appendChild(btn);
 
-      btn.addEventListener('click', () => {
-        window.$wujie?.bus.$emit("xiaoshuAsideBack");
-      });
-    }
-  `;
+        btn.innerHTML = '<span class="xiaoshu-aside-left-back-btn">返回</span>';
+
+        btn.addEventListener('click', () => {
+          window.$wujie?.bus.$emit("xiaoshuAsideBack");
+        });
+      }
+    `;
+  } else {
+    // 无返回按钮
+    insert_script_after_str = `console.log('cant back');`;
+  }
 
   const insert_style_str = `
     .xiaoshu-aside-left-back-btn{
@@ -106,13 +202,28 @@ onMounted(() => {
       padding-bottom: 0;
       border-bottom: none;
     }
+    .xiaoshu-aside-left-menus-logout-btn{
+      cursor: pointer;
+      text-align: center;
+      margin-top: 10px;
+    }
   `;
 
   /**
    * 监听子系统的事件
    */
   bus.$on('xiaoshuAsideBack', function () {
-    router.push('/chat/qa');
+    if (back_url) {
+      // 有返回链接
+      router.push(back_url);
+    }
+  });
+
+  /**
+   * 监听子系统的退出登录事件
+   */
+  bus.$on('xiaoshuAsideLogout', function () {
+    logout();
   });
 
   plugins.value = [