wujingjing
2025-04-16 692c19940b25bedd5b3d0e2cf2b8e73fe440f5c6
src/components/drawer/CustomDrawer.vue
@@ -1,342 +1,278 @@
<template>
   <div class="pc-mouldboard column exampleSlide" v-show="isShowAdvanceExample">
      <div class="header flex items-center justify-center">
         <div
            class="box-border flex w-[203px] h-[32px] rounded-md items-center justify-between cursor-pointer relative border border-solid border-[#2c2d33]"
         >
            <div
               class="tabbar-item"
               :class="state.activeName === item.ID ? 'set-tabbar-active' : ''"
               @click="handleTabClick(item)"
               v-for="item in state.tabNameList"
               :key="item.ID"
            >
               {{ item.Name }}
            </div>
         </div>
         <i
            class="ywicon icon-guanbi text-[#999] text-[18px] absolute right-[14px] top-5 cursor-pointer"
            @click="handleCloseTemplate"
         ></i>
   <div class="relative h100" v-loading="loadingData" :style="`width: ${rightBox}px;transition: 0.7s ease-in;`">
      <div class="retract_icon" @click="toggleShow" v-if="!isSharePage">
         <i class="text-[#fff] transition-all" :class="isShow ? 'ywifont ywicon-zuoyoujiantou1' : 'ywifont ywicon-zuoyoujiantou'"></i>
      </div>
      <div class="pc-mouldboard column exampleSlide">
         <div class="header flex items-center justify-center">
            <i
               class="ywifont ywicon-guanbi text-[#999] text-[18px] absolute right-[14px] top-5 cursor-pointer"
               @click="handleCloseTemplate"
            ></i>
         </div>
      <div class="box-border w-[354px] flex-1 min-h-0 bg-[#e0e7fb]" v-show="state.activeName == 1">
         <div class="flex flex-col w100 h100 pt-0 pr-[15px] pb-0 pl-[20px] overflow-auto">
            <div class="flex items-center w100 h-[30px] border border-solid border-[#b2b2b2] transition-[border-color 1s] rounded-2xl">
               <el-input v-model="queryParams.sample_title" placeholder="搜索更多" class="set-input" :prefix-icon="Search" clearable>
               </el-input>
            </div>
            <div class="mt20 w100 relative">
               <div
                  class="overflow-hidden max-h-[36px] transition-[max-height 0.2s] w100 flex flex-wrap text-xs leading-[14px] m-[-5px]"
                  :class="{ 'set-expand-active': state.isShowExpand }"
               >
                  <div
                     v-for="item in state.exampleTabList"
                     :key="item.ID"
                     :class="{ 'set-label-active': state.activeLabelName === item.ID }"
                     @click="handleLabelClick(item)"
                     class="cursor-pointer m-[5px] py-[5px] px-[10px] rounded-xl border border-solid border-[#b2b2b2] bg-[#e0e7f] text-[#4c4c4c] transition-[background-color .1s, color .1s, border-color .1s]"
                  >
                     {{ item.Title }}
         <div class="box-border w-[354px] flex-1 min-h-0 bg-[#e0e7fb]" v-show="activeGroupType">
            <div class="flex flex-col w100 h100 pt-0 pr-[15px] pb-0 pl-[20px]">
               <div class="flex items-center w100 h-[30px] border border-solid border-[#b2b2b2] transition-[border-color 1s] rounded-2xl">
                  <el-input v-model="searchValue" placeholder="搜索更多" class="set-input" :prefix-icon="Search" clearable> </el-input>
               </div>
               <div class="mt10 w100 h100 relative" v-show="allData[activeGroupType]">
                  <div class="set_custom_label">
                     <div class="chanel-tags">
                        <div
                           v-for="(item, index) in allData[activeGroupType]"
                           :key="index"
                           :class="{ 'set-group-label-active': activeMainScene[activeGroupType] === item.group_id }"
                           class="flex items-center border border-solid border-[#b2b2b2] rounded-xl mx-[3px] leading-6"
                           @click="handleBusinessClick(item)"
                        >
                           <span class="set-group-name">{{ item.group_name }}</span>
                        </div>
                     </div>
                  </div>
               </div>
               <div class="bg-[#e0e7fb] absolute right-0 top-0 text-[#131313] w-[30px] h-[26px] cursor-pointer flex items-center">
                  <i class="iconfont icon-bolangneng" title="展开" @click="handleExpandClick(1)" v-show="!state.isShowExpand"></i>
                  <i class="iconfont icon-zhongduancanshu" title="收缩" @click="handleExpandClick(2)" v-show="state.isShowExpand"></i>
               </div>
               <div class="mt20 w100">
                  <div class="w100 pb-[20px]" v-for="item in queryData" :key="item.sample_id">
                  <div
                     class="overflow-hidden min-h-[34px] transition-[max-height 0.2s] w100 flex flex-wrap text-xs leading-[14px] items-center bg-[#f5f7fd]"
                  >
                     <div
                        class="cursor-pointer flex box-border p-[12px] w100 rounded bg-[#f5f7fd] transition-[background-color .2s]"
                        @click="changeExample(item)"
                        v-for="(itemChildren, index) in state.customBusinessChildList"
                        :key="index"
                        :class="{ 'set-label-active': state.activeBusinessChildName === itemChildren.group_id }"
                        @click="handleLabelChildClick(itemChildren)"
                        class="cursor-pointer m-[5px] py-[5px] pl-[10px] rounded-xl border transition-[background-color .1s, color .1s, border-color .1s] text-[#999]"
                     >
                        <i
                           class="iconfont icon-chazhaobiaodanliebiao pl-[2px] w-[16px] h-[16px] font12 text-[#fff] transform-[scale(.85)] rounded-[50%]"
                           :style="`background-color:${item.BgColor}`"
                        ></i>
                        <div class="ml-6 flex-1 min-w-0">
                           <div class="text-sm text-[#000] m-0 p-0 font-bold">{{ item.sample_title }}</div>
                           <div class="mt-[6px] text-[13px] text-[#666] h-[20px] whitespace-nowrap text-ellipsis overflow-hidden">
                              {{ item.sample_question }}
                        {{ itemChildren.group_name }}
                     </div>
                  </div>
                  <div class="mt10 overflow-auto set-height-custom" v-if="activeGroupType !== '办公助手'">
                     <div class="w100 pb-[20px]" v-for="item in finalSampleList" :key="item.sample_id">
                        <div
                           class="cursor-pointer flex box-border p-[12px] w100 rounded bg-[#f5f7fd] transition-[background-color .2s]"
                           @click="changeExample(item)"
                        >
                           <i
                              class="myiconfont icon-chazhaobiaodanliebiao pl-[2px] w-[16px] h-[16px] font12 text-[#fff] transform-[scale(.85)] rounded-[50%]"
                              :style="`background-color:${item.BgColor}`"
                           ></i>
                           <div class="ml-6 flex-1 min-w-0">
                              <div class="text-sm text-[#000] m-0 p-0 font-bold">{{ item.sample_title }}</div>
                              <div class="mt-[6px] text-[13px] text-[#666] h-[20px] whitespace-nowrap text-ellipsis overflow-hidden">
                                 {{ item.sample_question }}
                              </div>
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
            </div>
         </div>
      </div>
      <div class="box-border w-[354px] flex-1 min-h-0 bg-[#e0e7fb]" v-show="state.activeName == 2">
         <div class="flex flex-col w100 h100 pt-0 pr-[15px] pb-0 pl-[20px] overflow-auto">
            <div class="flex items-center w100 h-[30px] border border-solid border-[#b2b2b2] transition-[border-color 1s] rounded-2xl">
               <el-input v-model="instructParams.template_title" placeholder="搜索更多" class="set-input" :prefix-icon="Search" clearable>
               </el-input>
            </div>
            <div class="mt20 w100 relative">
               <div
                  class="overflow-hidden max-h-[36px] transition-[max-height 0.2s] w100 flex flex-wrap text-xs leading-[14px] m-[-5px]"
                  :class="{ 'set-expand-active': state.isShowExpand }"
               >
                  <div
                     v-for="item in state.instructList"
                     :key="item.ID"
                     :class="{ 'set-label-active': state.activeInstructName === item.ID }"
                     @click="handleInstructClick(item)"
                     class="cursor-pointer m-[5px] py-[5px] px-[10px] rounded-xl border border-solid border-[#b2b2b2] bg-[#e0e7f] text-[#4c4c4c] transition-[background-color .1s, color .1s, border-color .1s]"
                  >
                     {{ item.Title }}
                  </div>
               </div>
               <div class="bg-[#e0e7fb] absolute right-0 top-0 text-[#131313] w-[30px] h-[26px] cursor-pointer flex items-center">
                  <i class="iconfont icon-bolangneng" title="展开" @click="handleExpandClick(1)" v-show="!state.isShowExpand"></i>
                  <i class="iconfont icon-zhongduancanshu" title="收缩" @click="handleExpandClick(2)" v-show="state.isShowExpand"></i>
               </div>
               <div class="mt20 w100">
                  <div class="w100 pb-[20px] flex overflow-auto flex-col" v-for="item in showInstructList" :key="item.template_id">
                     <div class="bg-[#f5f7fd] p-[12px] transition-[background-color .2s] flex flex-col rounded-2xl">
                        <div class="flex justify-between">
                           <span class="set-title">{{ item.template_title }}</span>
                        </div>
                        <div class="example-body ellipsis">{{ item.template_value }}</div>
                        <div class="example-bottom">
                           <span class="example-item">{{ item.template_type }}</span>
                           <el-button type="primary" class="set-button" @click="templateUseClick(item)">使用</el-button>
                  <div class="mt10 w100 overflow-auto set-height-custom" v-else>
                     <div class="w100 pb-[20px] flex flex-col" v-for="item in finalSampleList" :key="item.sample_id">
                        <div class="bg-[#f5f7fd] p-[12px] transition-[background-color .2s] flex flex-col rounded-2xl">
                           <div class="flex justify-between">
                              <span class="set-title">{{ item.sample_title }}</span>
                           </div>
                           <div class="example-body ellipsis">{{ item.sample_question }}</div>
                           <div class="example-bottom">
                              <span class="example-item">{{ item.template_type }}</span>
                              <el-button type="primary" class="set-button" @click="templateUseClick(item)">使用</el-button>
                           </div>
                        </div>
                     </div>
                  </div>
                  <div class="mt10 w100" v-show="finalSampleList.length == 0"><el-empty description="暂无数据" /></div>
               </div>
            </div>
         </div>
         <el-dialog v-model="state.useInstructDialog" :title="state.instructInfo?.sample_title" width="500" :before-close="handleClose">
            <el-input v-model="state.inputInstruct" :rows="10" type="textarea"></el-input>
            <template #footer>
               <div class="dialog-footer">
                  <el-button @click="handleClose">取消</el-button>
                  <el-button type="primary" @click="submitInstruct"> 确认 </el-button>
               </div>
            </template>
         </el-dialog>
      </div>
      <el-dialog v-model="state.useInstructDialog" :title="state.instructInfo?.template_title" width="500" :before-close="handleClose">
         <el-input v-model="state.inputInstruct" :rows="10" type="textarea"></el-input>
         <template #footer>
            <div class="dialog-footer">
               <el-button @click="handleClose">取消</el-button>
               <el-button type="primary" @click="submitInstruct"> 确认 </el-button>
            </div>
         </template>
      </el-dialog>
   </div>
</template>
<script setup lang="ts">
import { Search } from '@element-plus/icons-vue';
import { onMounted, reactive, ref, toRefs, watch } from 'vue';
import { getSelectSample, getUserTemplateList } from '/@/api/ai/chat';
import { useSearch } from '/@/hooks/useSearch';
import { activeRoomId, activeSampleId, setRoomConfig } from '/@/stores/chatRoom';
import emitter from '/@/utils/mitt';
import { debounce } from '/@/utils/util';
import { computed, reactive, ref, watch } from 'vue';
import {
   activeGroupType,
   activeRoomId,
   activeSampleId,
   activeSectionAId,
   exampleSceneList,
   isSharePage,
   sceneGroupList,
   setRoomConfig,
} from '/@/stores/chatRoom';
import { convertListToTree } from '/@/utils/util';
import { groupBy } from 'lodash-es';
let state = reactive({
   tabNameList: [
      { ID: 1, Name: '提问示例' },
      { ID: 2, Name: '指令模板' },
   ],
   activeName: 1,
   exampleTabList: [
      {
         ID: 1,
         Title: '工作',
      },
      {
         ID: 2,
         Title: '职场',
      },
      {
         ID: 3,
         Title: '科学',
      },
      {
         ID: 4,
         Title: '求职',
      },
      {
         ID: 5,
         Title: '新能源,汽车,数据',
      },
      {
         ID: 6,
         Title: '写作',
      },
      {
         ID: 7,
         Title: '学习',
      },
      {
         ID: 8,
         Title: '创作',
      },
   ],
   instructList: [
      {
         ID: 1,
         Title: '文学构思',
      },
      {
         ID: 2,
         Title: '语言翻译',
      },
      {
         ID: 3,
         Title: '高效策划',
      },
      {
         ID: 4,
         Title: '顾问专家',
      },
      {
         ID: 5,
         Title: '代码编程',
      },
      {
         ID: 6,
         Title: '办公写作',
      },
      {
         ID: 7,
         Title: '健康管家',
      },
      {
         ID: 8,
         Title: '发现探索',
      },
   ],
   activeLabelName: 0,
   activeInstructName: 0,
   isShowExpand: false,
   activeBusinessName: '', //场景
   activeBusinessChildName: '',
   customBusinessList: [],
   customBusinessChildList: [],
   activeOfficeName: '', //办公
   activeOfficeChildName: '',
   customOfficeList: [],
   customOfficeChildList: [],
   activeKnowledgeName: '', //知识库
   activeKnowledgeChildName: '',
   knowledgeBaseList: [],
   customKnowledgeChildList: [],
   useInstructDialog: false,
   instructInfo: {},
   inputInstruct: '', //可修改指令
   exampleRandomContent: [],
   exampleContent: [],
   listSampleLoading: false,
   sampleMap: {},
});
const instructContentList = ref([]); //指令列表
const exampleList = ref([]); //示例列表
const props = defineProps({
   isShowAdvanceExample: {
      type: Boolean,
      default: false, //是否显示高级示例
   },
});
const { isShowAdvanceExample } = toRefs(props);
onMounted(() => {
   getSelectListSample();
   getUserTemplate();
});
const emits = defineEmits<{
   (event: 'closeExampleClick', data): void;
}>();
//获取模版列表
const getSelectListSample = async () => {
   const res = await getSelectSample({
      section_b_id: '',
   });
   // console.log(res, 63);
   state.exampleRandomContent = res.samples;
   const array = [];
   res.samples.forEach((sample, index) => {
      sample.Icon = '/static/images/wave/ChatImg.png';
      sample.BgColor = randomHexColor();
      if (index < 4) {
         array.push(sample);
      }
   });
   exampleList.value = res.samples;
   state.exampleContent = array;
};
//随机生成颜色
const randomHexColor = () => {
   return `#${Math.floor(Math.random() * 16777215)
      .toString(16)
      .padEnd(6, '0')}`;
};
//#region ====================== 传参 ======================
const isShow = defineModel('isShow', {
   type: Boolean,
});
const rightBox = computed(() => (isShow.value ? 354 : 0));
const emit = defineEmits(['updateChatInput']);
//#endregion
//#region ====================== 业务场景/知识库的list ======================
const loadingData = ref(false);
const changeExample = (item) => {
   emitter.emit('updateChatInput', item.sample_question);
   emit('updateChatInput', item.sample_question);
   setRoomConfig(activeRoomId.value, 'isAnswerByLLM', false);
   activeSampleId.value = item.sample_id;
   activeSectionAId.value = item.group_id;
};
//获取用户模板
const getUserTemplate = async () => {
   const res = await getUserTemplateList();
   instructContentList.value = res.templates;
};
//切换到指令/提问
const handleTabClick = (item) => {
   state.activeName = item.ID;
};
//标签点击事件(提问示例)
const handleLabelClick = (item) => {
   state.activeLabelName = item.ID;
};
//标签点击事件(指令模板)
const handleInstructClick = (item) => {
   state.activeInstructName = item.ID;
};
//展开收起
const handleExpandClick = (type) => {
   if (type == 1) {
      state.isShowExpand = true;
   } else {
      state.isShowExpand = false;
   }
};
//关闭高级示例
const handleCloseTemplate = () => {
   emits('closeExampleClick', false);
};
//#region ====================== templateUseClick ======================
//#endregion
//#region ====================== 办公助手使用 ======================
const templateUseClick = (row) => {
   state.useInstructDialog = true;
   state.instructInfo = row;
   state.inputInstruct = row.template_value;
   state.inputInstruct = row.sample_question;
};
const submitInstruct = () => {
   setRoomConfig(activeRoomId.value, 'isAnswerByLLM', true);
   emit('updateChatInput', state.inputInstruct);
   handleClose();
};
//#endregion
//#region ====================== 打开示例 ======================
//打开高级示例
const toggleShow = async () => {
   isShow.value = !isShow.value;
   getTableData();
};
//关闭高级示例
const handleCloseTemplate = () => {
   isShow.value = false;
};
const handleClose = () => {
   state.useInstructDialog = false;
};
const submitInstruct = () => {
   setRoomConfig(activeRoomId.value, 'isAnswerByLLM', true);
   emitter.emit('updateChatInput', state.inputInstruct);
   handleClose();
const handleBusinessClick = (item) => {
   activeMainScene.value[activeGroupType.value] = item.group_id;
   state.customBusinessChildList = item.children;
   state.activeBusinessChildName = item.children[0]?.group_id;
   if (!state.activeBusinessChildName) {
      finalSampleList.value = [];
   } else {
      finalSampleList.value = searchFilterSampleList(state.sampleMap[state.activeBusinessChildName]);
   }
};
const handleLabelChildClick = (item) => {
   state.activeBusinessChildName = item.group_id;
   finalSampleList.value = searchFilterSampleList(state.sampleMap[state.activeBusinessChildName]);
};
//#endregion
//#region ====================== 搜索模板/指令 ======================
const queryParams = ref({
   sample_title: '',
});
const { query, queryData } = useSearch(exampleList, queryParams);
const debounceQuery = debounce(query);
watch(
   () => queryParams.value.sample_title,
   (val) => {
      debounceQuery();
//#region ====================== 默认选择第一个的场景的子场景 ======================
const allData = ref({});
const activeMainScene = ref({});
const activeSubScene = ref({});
const allSubScene = ref([]);
const searchValue = ref('');
const finalSampleList = ref([]); //搜索过滤后的示例列表
const getTableData = async () => {
   const res = await sceneGroupList.value;
   let groupDataByType = groupBy(res ?? [], (item) => item.group_type);
   for (const key in groupDataByType) {
      if (Object.prototype.hasOwnProperty.call(groupDataByType, key)) {
         const value = groupDataByType[key];
         const newValue = convertListToTree(value ?? [], {
            ID: 'group_id',
            ParentID: 'p_group_id',
            Children: 'children',
         });
         groupDataByType[key] = newValue;
      }
   }
);
//指令
const instructParams = ref({
   template_title: '',
});
const { query: queryInstruct, queryData: showInstructList } = useSearch(instructContentList, instructParams);
const instructQuery = debounce(queryInstruct);
watch(
   () => instructParams.value.template_title,
   (val) => {
      instructQuery();
   const keys = Object.keys(groupDataByType);
   if (keys.includes('业务场景')) {
      const value = groupDataByType['业务场景'];
      //有业务场景,业务场景放第一
      groupDataByType = {
         业务场景: value,
         ...groupDataByType,
      };
   }
);
   allData.value = groupDataByType;
   const typeArr = Object.keys(allData.value);
   let subSceneList = [];
   if (activeGroupType.value) {
      // 初始化激活 mainScene
      activeMainScene.value = typeArr.reduce((preVal, curVal) => {
         const mainSceneList = allData.value[curVal];
         // 设置第一个激活
         preVal[curVal] = mainSceneList?.[0]?.group_id;
         // 初始化激活 subScene
         mainSceneList.map((item, index, array) => {
            if (item.children) {
               subSceneList = subSceneList.concat(item.children);
            }
            // 设置第一个激活
            activeSubScene.value[item.group_id] = array[index].children?.[0]?.group_id;
         });
         return preVal;
      }, {});
   }
   allSubScene.value = subSceneList;
   const activeMainSceneData = allData.value[activeGroupType.value];
   const activeMainSceneId = activeMainScene.value[activeGroupType.value];
   state.activeBusinessChildName = activeSubScene.value[activeMainSceneId]; //初始化业务场景的子场景默认值
   state.customBusinessChildList = activeMainSceneData.filter((item) => item.group_id === activeMainSceneId)[0].children;
   state.sampleMap = displaySampleMap();
   finalSampleList.value = searchFilterSampleList(state.sampleMap[state.activeBusinessChildName]);
};
const displaySampleMap = () => {
   const sampleMap = {};
   allSubScene.value.map(async (item) => {
      const groupId = item.group_id;
      sampleMap[groupId] = exampleSceneList.value.filter((item) => item.group_id === groupId);
   });
   return sampleMap;
};
const searchFilterSampleList = (sampleList: any[]) => {
   if (!searchValue.value) return sampleList;
   const checkKey = ['sample_question', 'sample_title'];
   const filterList = sampleList.filter((item) => {
      return checkKey.some((subItem) => item[subItem].toLowerCase().trim().includes(searchValue.value.toLowerCase().trim()));
   });
   return filterList;
};
watch(activeGroupType, () => {
   getTableData();
});
watch(searchValue, () => {
   finalSampleList.value = searchFilterSampleList(state.sampleMap[state.activeBusinessChildName]);
});
//#endregion
defineExpose({ getTableData });
</script>
<style scoped lang="scss">
.exampleSlide {
   // position: absolute;
   right: 0;
   top: 0;
   height: 100%;
@@ -344,13 +280,59 @@
   box-sizing: border-box;
   z-index: 100;
}
.set_custom_label {
   min-height: 32px;
   box-sizing: border-box;
   // background-color: #f2f4f8;
   border-bottom: 1px solid #eee;
   .chanel-tags {
      width: 100%;
      position: relative;
      overflow: hidden;
      font-size: 12px;
      display: flex;
      flex-wrap: wrap;
      line-height: 26px;
      color: #999;
      cursor: pointer;
      box-sizing: border-box;
      height: 100%;
      .set-group-name {
         margin: 0px 7px;
         white-space: nowrap;
         // border: 1px solid #999;
         // border-radius: 0.75rem;
         // padding: 0 6px;
         // line-height: 22px;
      }
   }
}
.retract_icon {
   width: 20px;
   height: 48px;
   background: rgba(0, 0, 0, 0.2);
   position: absolute;
   top: 50%;
   left: 0;
   transform: translate(calc(100% - 40px), -50%);
   z-index: 9;
   cursor: pointer;
   display: flex;
   -webkit-box-align: center;
   -ms-flex-align: center;
   align-items: center;
   -webkit-box-pack: center;
   -ms-flex-pack: center;
   justify-content: center;
   padding: 0 3px;
   border-radius: 5px 5px 0px;
}
.pc-mouldboard {
   width: 354px;
   font-size: 14px;
   -webkit-animation: right-c650b75e 0.2s;
   animation: right-c650b75e 0.2s;
   overflow: hidden;
   .header {
      box-sizing: border-box;
      width: 100%;
@@ -359,7 +341,7 @@
      position: relative;
   }
   .tabbar-item {
      width: 108px;
      width: 78px;
      height: 30px;
      font-size: 13px;
      font-weight: 400;
@@ -405,9 +387,12 @@
   .set-expand-active {
      max-height: 180px;
   }
   .set-group-label-active {
      border-color: #161616;
      color: #000;
   }
   .set-label-active {
      border-color: #1c86ff;
      background-color: #ccdcfb;
      // background-color: #ccdcfb;
      color: #1c86ff;
   }
   .example-body {
@@ -433,6 +418,7 @@
      -webkit-box-pack: justify;
      -ms-flex-pack: justify;
      justify-content: space-between;
      overflow: hidden;
      .example-item {
         background-color: #f0f2f3;
         color: #8b939e;
@@ -464,5 +450,9 @@
   -webkit-box-direction: normal;
   -ms-flex-direction: column;
   flex-direction: column;
   overflow: hidden;
}
.set-height-custom {
   height: calc(100% - 120px);
}
</style>