wangrong
2026-08-30 761e5c5fa871959f60e15c971c8d83f2b53d0af5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<template>
  <div
    id="dataease-v2-embedded-assistant-sqlbot"
    class="dataease-v2-embedded-assistant-sqlbot"
  ></div>
</template>
 
<script lang="ts" setup>
import { createApp, onMounted, onUnmounted, reactive, ref } from 'vue'
import request from '@/config/axios'
import { useUserStoreWithOut } from '@/store/modules/user'
import SQDatasetSelect from '@/views/sqlbot/SQDatasetSelect.vue'
import AssistantHead from '@/views/sqlbot/AssistantHead.vue'
const userStore = useUserStoreWithOut()
const loading = ref(true)
const state = reactive({
  domain: '',
  id: '',
  enabled: false,
  valid: false,
  historyShow: false,
  sqlbotScript: null
})
const sqlbotExist = ref(false)
const timer = ref()
 
const loadSqlbotInfo = () => {
  const url = '/sysParameter/sqlbot'
  request.get({ url }).then(res => {
    if (res && res.data) {
      const { domain, id, enabled, valid } = res.data
      if (!enabled) {
        console.error('sqlbot embedded disabled')
      }
      if (!valid) {
        console.error('sqlbot embedded invalid')
      }
      state.domain = domain
      state.id = id
      state.enabled = enabled
      state.valid = valid
      loadSqlbotPage()
    }
  })
}
 
const loadSqlbotPage = () => {
  const scriptId = `sqlbot-assistant-float-script-${state.id}`
  const exitsScript = document.getElementById(scriptId)
  if (exitsScript && window['sqlbot_assistant_handler']) {
    mountedEmbeddedPage()
    return
  }
  const script = document.createElement('script')
  script.defer = true
  script.async = true
  script.id = scriptId
  let sqlbotDomain = state.domain
  if (sqlbotDomain.endsWith('/')) {
    sqlbotDomain = sqlbotDomain.slice(0, -1)
  }
  script.src = `${sqlbotDomain}/assistant.js?id=${state.id}&online=true&userFlag=${
    userStore.getUid
  }&t=${new Date().getTime()}`
  script.onload = () => {
    mountedEmbeddedPage()
  }
  document.head.appendChild(script)
  state.sqlbotScript = script
}
const mountedEmbeddedPage = () => {
  if (sqlbotExist.value) {
    return
  }
  const tempTimer = setTimeout(() => {
    if (window['sqlbot_assistant_handler']) {
      const container = document.getElementById('sqlbot-assistant-chat-container')
      if (container) {
        // 数据集选择
        const mountPoint = document.createElement('div')
        mountPoint.id = 'chat-component-mount-point'
        container.appendChild(mountPoint)
        const chatApp = createApp(SQDatasetSelect, {
          // 在这里传递 props
          assistantId: state.id
        })
        chatApp.mount(mountPoint)
 
        // 头部样式
        const mountPointHead = document.createElement('div')
        mountPointHead.id = 'chat-component-mount-point-head'
        container.appendChild(mountPointHead)
        const chatHeadApp = createApp(AssistantHead)
        chatHeadApp.mount(mountPointHead)
      }
      loading.value = false
      sqlbotExist.value = true
      if (tempTimer) {
        clearTimeout(tempTimer)
      }
    }
  }, 2000)
}
 
onMounted(() => {
  loadSqlbotInfo()
  timer.value = setInterval(() => {
    loadSqlbotInfo()
  }, 30000)
})
 
onUnmounted(() => {
  if (timer.value) {
    clearInterval(timer.value)
    timer.value = null
  }
 
  // 移除 script 标签
  if (state.sqlbotScript && state.sqlbotScript.parentNode) {
    state.sqlbotScript.parentNode.removeChild(state.sqlbotScript)
  }
 
  // 可选:清理全局变量
  if (window['sqlbot_assistant_handler']) {
    delete window['sqlbot_assistant_handler']
  }
})
</script>
 
<style lang="less">
#sqlbot-assistant-chat-container {
  z-index: 200;
}
</style>
 
<style lang="less" scoped>
.dataease-v2-embedded-assistant-sqlbot {
  width: 20px;
  height: 20px;
  overflow: hidden;
  position: absolute;
  display: flex;
}
</style>