feat: workspace to store (#3261)

This commit is contained in:
shaohuzhang1 2025-06-16 12:08:09 +08:00 committed by GitHub
parent cbcad3cc83
commit 9e24e8c467
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 491 additions and 25 deletions

View File

@ -1,10 +1,15 @@
import { Result } from '@/request/Result'
import { get, post, del, put } from '@/request/index'
import useStore from '@/stores'
import { type Ref } from 'vue'
const prefix = '/workspace/' + localStorage.getItem('workspace_id') + '/application'
const prefix: any = { _value: '/workspace/' }
Object.defineProperty(prefix, 'value', {
get: function () {
const { user } = useStore()
return this._value + user.getWorkspaceId() + '/application'
},
})
/**
* API_KEY列表
* @param application_id
@ -13,7 +18,7 @@ const getAPIKey: (application_id: string, loading?: Ref<boolean>) => Promise<Res
application_id,
loading,
) => {
return get(`${prefix}/${application_id}/application`, undefined, loading)
return get(`${prefix.value}/${application_id}/application`, undefined, loading)
}
/**
@ -24,7 +29,7 @@ const postAPIKey: (application_id: string, loading?: Ref<boolean>) => Promise<Re
application_id,
loading,
) => {
return post(`${prefix}/${application_id}/application`, {}, undefined, loading)
return post(`${prefix.value}/${application_id}/application`, {}, undefined, loading)
}
/**
@ -36,7 +41,12 @@ const delAPIKey: (
api_key_id: string,
loading?: Ref<boolean>,
) => Promise<Result<boolean>> = (application_id, api_key_id, loading) => {
return del(`${prefix}/${application_id}/application/${api_key_id}`, undefined, undefined, loading)
return del(
`${prefix.value}/${application_id}/application/${api_key_id}`,
undefined,
undefined,
loading,
)
}
/**
@ -52,7 +62,12 @@ const putAPIKey: (
data: any,
loading?: Ref<boolean>,
) => Promise<Result<any>> = (application_id, api_key_id, data, loading) => {
return put(`${prefix}/${application_id}/application/${api_key_id}`, data, undefined, loading)
return put(
`${prefix.value}/${application_id}/application/${api_key_id}`,
data,
undefined,
loading,
)
}
export default {

View File

@ -3,9 +3,15 @@ import { get, post, postStream, del, put, request, download, exportFile } from '
import type { pageRequest } from '@/api/type/common'
import type { ApplicationFormType } from '@/api/type/application'
import { type Ref } from 'vue'
import useStore from '@/stores'
const prefix = '/workspace/' + localStorage.getItem('workspace_id') + '/application'
const prefix: any = { _value: '/workspace/' }
Object.defineProperty(prefix, 'value', {
get: function () {
const { user } = useStore()
return this._value + user.getWorkspaceId() + '/application'
},
})
/**
*
* @param
@ -14,7 +20,7 @@ const getAllApplication: (param?: any, loading?: Ref<boolean>) => Promise<Result
param,
loading,
) => {
return get(`${prefix}`, param, loading)
return get(`${prefix.value}`, param, loading)
}
/**
@ -28,7 +34,7 @@ const getApplication: (
param: any,
loading?: Ref<boolean>,
) => Promise<Result<any>> = (page, param, loading) => {
return get(`${prefix}/${page.current_page}/${page.page_size}`, param, loading)
return get(`${prefix.value}/${page.current_page}/${page.page_size}`, param, loading)
}
/**
@ -39,7 +45,7 @@ const postApplication: (
data: ApplicationFormType,
loading?: Ref<boolean>,
) => Promise<Result<any>> = (data, loading) => {
return post(`${prefix}`, data, undefined, loading)
return post(`${prefix.value}`, data, undefined, loading)
}
/**
@ -51,7 +57,7 @@ const putApplication: (
data: ApplicationFormType,
loading?: Ref<boolean>,
) => Promise<Result<any>> = (application_id, data, loading) => {
return put(`${prefix}/${application_id}`, data, undefined, loading)
return put(`${prefix.value}/${application_id}`, data, undefined, loading)
}
/**
@ -62,7 +68,7 @@ const delApplication: (
application_id: string,
loading?: Ref<boolean>,
) => Promise<Result<boolean>> = (application_id, loading) => {
return del(`${prefix}/${application_id}`, undefined, {}, loading)
return del(`${prefix.value}/${application_id}`, undefined, {}, loading)
}
/**
@ -73,7 +79,7 @@ const getApplicationDetail: (
application_id: string,
loading?: Ref<boolean>,
) => Promise<Result<any>> = (application_id, loading) => {
return get(`${prefix}/${application_id}`, undefined, loading)
return get(`${prefix.value}/${application_id}`, undefined, loading)
}
/**
@ -84,7 +90,7 @@ const getAccessToken: (application_id: string, loading?: Ref<boolean>) => Promis
application_id,
loading,
) => {
return get(`${prefix}/${application_id}/access_token`, undefined, loading)
return get(`${prefix.value}/${application_id}/access_token`, undefined, loading)
}
/**
@ -99,7 +105,7 @@ const putAccessToken: (
data: any,
loading?: Ref<boolean>,
) => Promise<Result<any>> = (application_id, data, loading) => {
return put(`${prefix}/${application_id}/access_token`, data, undefined, loading)
return put(`${prefix.value}/${application_id}/access_token`, data, undefined, loading)
}
/**
@ -113,7 +119,7 @@ const exportApplication = (
) => {
return exportFile(
application_name + '.mk',
`${prefix}/${application_id}/export`,
`${prefix.value}/${application_id}/export`,
undefined,
loading,
)
@ -126,7 +132,7 @@ const importApplication: (data: any, loading?: Ref<boolean>) => Promise<Result<a
data,
loading,
) => {
return post(`${prefix}/import`, data, undefined, loading)
return post(`${prefix.value}/import`, data, undefined, loading)
}
/**
@ -138,7 +144,7 @@ const getStatistics: (
data: any,
loading?: Ref<boolean>,
) => Promise<Result<any>> = (application_id, data, loading) => {
return get(`${prefix}/${application_id}/application_stats`, data, loading)
return get(`${prefix.value}/${application_id}/application_stats`, data, loading)
}
/**
* id
@ -150,7 +156,7 @@ const open: (application_id: string, loading?: Ref<boolean>) => Promise<Result<s
application_id,
loading,
) => {
return get(`${prefix}/${application_id}/open`, {}, loading)
return get(`${prefix.value}/${application_id}/open`, {}, loading)
}
/**
*

54
ui/src/api/chat/chat.ts Normal file
View File

@ -0,0 +1,54 @@
import { Result } from '@/request/Result'
import {
get,
post,
postStream,
del,
put,
request,
download,
exportFile,
} from '@/request/chat/index'
import { type Ref } from 'vue'
const prefix = '/workspace/' + localStorage.getItem('workspace_id') + '/application'
/**
* id
* @param application_id id
* @param loading
* @returns
*/
const open: (application_id: string, loading?: Ref<boolean>) => Promise<Result<string>> = (
application_id,
loading,
) => {
return get(`${prefix}/${application_id}/open`, {}, loading)
}
/**
*
* @param
* chat_id: string
* data
*/
const chat: (chat_id: string, data: any) => Promise<any> = (chat_id, data) => {
return postStream(`/api/chat_message/${chat_id}`, data)
}
const chatProfile: (assessToken: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
assessToken,
loading,
) => {
return get('/auth/profile', { access_token: assessToken }, loading)
}
const applicationProfile: (assessToken: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
assessToken,
loading,
) => {
return get('/chat/api/profile')
}
export default {
open,
chat,
chatProfile,
}

View File

@ -0,0 +1,19 @@
export class Result<T> {
message: string
code: number
data: T
constructor(message: string, code: number, data: T) {
this.message = message
this.code = code
this.data = data
}
static success(data: any) {
return new Result('请求成功', 200, data)
}
static error(message: string, code: number) {
return new Result(message, code, null)
}
}
export default Result

View File

@ -0,0 +1,349 @@
import axios, { type InternalAxiosRequestConfig, AxiosHeaders } from 'axios'
import { MsgError } from '@/utils/message'
import type { NProgress } from 'nprogress'
import type { Ref } from 'vue'
import type { Result } from '@/request/Result'
import useStore from '@/stores'
import router from '@/router'
import { ref, type WritableComputedRef } from 'vue'
const axiosConfig = {
baseURL: '/chat/api',
withCredentials: false,
timeout: 600000,
headers: {},
}
const instance = axios.create(axiosConfig)
/* 设置请求拦截器 */
instance.interceptors.request.use(
(config: InternalAxiosRequestConfig) => {
if (config.headers === undefined) {
config.headers = new AxiosHeaders()
}
const { user, login } = useStore()
const token = login.getToken()
const language = user.getLanguage()
config.headers['Accept-Language'] = `${language}`
if (token) {
config.headers['AUTHORIZATION'] = `Bearer ${token}`
}
return config
},
(err: any) => {
return Promise.reject(err)
},
)
//设置响应拦截器
instance.interceptors.response.use(
(response: any) => {
if (response.data) {
if (response.data.code !== 200 && !(response.data instanceof Blob)) {
if (response.config.url.includes('/application/authentication')) {
return Promise.reject(response.data)
}
if (
!response.config.url.includes('/valid') &&
!response.config.url.includes('/tool/debug')
) {
MsgError(response.data.message)
return Promise.reject(response.data)
}
}
}
return response
},
(err: any) => {
if (err.code === 'ECONNABORTED') {
MsgError(err.message)
console.error(err)
}
if (err.response?.status === 404) {
if (!err.response.config.url.includes('/application/authentication')) {
router.push('/404 ')
}
}
if (err.response?.status === 401) {
if (
!err.response.config.url.includes('chat/open') &&
!err.response.config.url.includes('application/profile')
) {
router.push({ name: 'login' })
}
}
if (err.response?.status === 403 && !err.response.config.url.includes('chat/open')) {
MsgError(
err.response.data && err.response.data.message ? err.response.data.message : '没有权限访问',
)
}
return Promise.reject(err)
},
)
export const request = instance
/* 简化请求方法统一处理返回结果并增加loading处理这里以{success,data,message}格式的返回值为例,具体项目根据实际需求修改 */
const promise: (
request: Promise<any>,
loading?: NProgress | Ref<boolean> | WritableComputedRef<boolean>,
) => Promise<Result<any>> = (request, loading = ref(false)) => {
return new Promise((resolve, reject) => {
if ((loading as NProgress).start) {
;(loading as NProgress).start()
} else {
;(loading as Ref).value = true
}
request
.then((response) => {
// blob类型的返回状态是response.status
if (response.status === 200) {
resolve(response?.data || response)
} else {
reject(response?.data || response)
}
})
.catch((error) => {
reject(error)
})
.finally(() => {
if ((loading as NProgress).start) {
;(loading as NProgress).done()
} else {
;(loading as Ref).value = false
}
})
})
}
/**
* get请求
* @param url url
* @param params
* @param loading loading
* @returns promise对象
*/
export const get: (
url: string,
params?: unknown,
loading?: NProgress | Ref<boolean>,
timeout?: number,
) => Promise<Result<any>> = (
url: string,
params: unknown,
loading?: NProgress | Ref<boolean>,
timeout?: number,
) => {
return promise(request({ url: url, method: 'get', params, timeout: timeout }), loading)
}
/**
* faso post请求
* @param url url
* @param params
* @param data
* @param loading loading
* @returns promise对象
*/
export const post: (
url: string,
data?: unknown,
params?: unknown,
loading?: NProgress | Ref<boolean>,
timeout?: number,
) => Promise<Result<any> | any> = (url, data, params, loading, timeout) => {
return promise(request({ url: url, method: 'post', data, params, timeout }), loading)
}
/**|
* put请求
* @param url
* @param params params参数地址
* @param data
* @param loading
* @returns
*/
export const put: (
url: string,
data?: unknown,
params?: unknown,
loading?: NProgress | Ref<boolean>,
timeout?: number,
) => Promise<Result<any>> = (url, data, params, loading, timeout) => {
return promise(request({ url: url, method: 'put', data, params, timeout }), loading)
}
/**
*
* @param url url
* @param params params参数
* @param loading
* @returns
*/
export const del: (
url: string,
params?: unknown,
data?: unknown,
loading?: NProgress | Ref<boolean>,
timeout?: number,
) => Promise<Result<any>> = (url, params, data, loading, timeout) => {
return promise(request({ url: url, method: 'delete', params, data, timeout }), loading)
}
/**
*
* @param url url地址
* @param data body
* @returns
*/
export const postStream: (url: string, data?: unknown) => Promise<Result<any> | any> = (
url,
data,
) => {
const { user, login } = useStore()
const token = login.getToken()
const language = user.getLanguage()
const headers: HeadersInit = { 'Content-Type': 'application/json' }
if (token) {
headers['AUTHORIZATION'] = `Bearer ${token}`
}
headers['Accept-Language'] = `${language}`
return fetch(url, {
method: 'POST',
body: data ? JSON.stringify(data) : undefined,
headers: headers,
})
}
export const exportExcel: (
fileName: string,
url: string,
params: any,
loading?: NProgress | Ref<boolean>,
) => Promise<any> = (
fileName: string,
url: string,
params: any,
loading?: NProgress | Ref<boolean>,
) => {
return promise(request({ url: url, method: 'get', params, responseType: 'blob' }), loading).then(
(res: any) => {
if (res) {
const blob = new Blob([res], {
type: 'application/vnd.ms-excel',
})
const link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = fileName
link.click()
//释放内存
window.URL.revokeObjectURL(link.href)
}
return true
},
)
}
export const exportFile: (
fileName: string,
url: string,
params: any,
loading?: NProgress | Ref<boolean>,
) => Promise<any> = (
fileName: string,
url: string,
params: any,
loading?: NProgress | Ref<boolean>,
) => {
return promise(request({ url: url, method: 'get', params, responseType: 'blob' }), loading).then(
(res: any) => {
if (res) {
const blob = new Blob([res], {
type: 'application/octet-stream',
})
const link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = fileName
link.click()
//释放内存
window.URL.revokeObjectURL(link.href)
}
return true
},
)
}
export const exportExcelPost: (
fileName: string,
url: string,
params: any,
data: any,
loading?: NProgress | Ref<boolean>,
) => Promise<any> = (
fileName: string,
url: string,
params: any,
data: any,
loading?: NProgress | Ref<boolean>,
) => {
return promise(
request({
url: url,
method: 'post',
params, // 查询字符串参数
data, // 请求体数据
responseType: 'blob',
}),
loading,
).then((res: any) => {
if (res) {
const blob = new Blob([res], {
type: 'application/vnd.ms-excel',
})
const link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = fileName
link.click()
// 释放内存
window.URL.revokeObjectURL(link.href)
}
return true
})
}
export const download: (
url: string,
method: string,
data?: any,
params?: any,
loading?: NProgress | Ref<boolean>,
) => Promise<any> = (
url: string,
method: string,
data?: any,
params?: any,
loading?: NProgress | Ref<boolean>,
) => {
return promise(request({ url: url, method: method, data, params, responseType: 'blob' }), loading)
}
/**
* ws链接
* @param url websocket路径
* @returns websocket实例
*/
export const socket = (url: string) => {
let protocol = 'ws://'
if (window.location.protocol === 'https:') {
protocol = 'wss://'
}
let uri = protocol + window.location.host + url
if (!import.meta.env.DEV) {
uri = protocol + window.location.host + import.meta.env.VITE_BASE_PATH + url
}
return new WebSocket(uri)
}
export default instance

View File

@ -21,6 +21,7 @@ export interface userStateTypes {
license_is_valid: boolean
edition: 'CE' | 'PE' | 'EE'
themeInfo: any
workspace_id: string
}
const useUserStore = defineStore('user', {
@ -31,6 +32,7 @@ const useUserStore = defineStore('user', {
license_is_valid: false,
edition: 'CE',
themeInfo: null,
workspace_id: 'default',
}),
actions: {
getLanguage() {
@ -56,6 +58,16 @@ const useUserStore = defineStore('user', {
return this.asyncGetProfile()
})
},
getWorkspaceId(): string | null {
if (this.workspace_id) {
return this.workspace_id
}
const workspace_id = localStorage.getItem('workspace_id')
if (workspace_id) {
this.workspace_id = workspace_id
}
return workspace_id
},
async asyncGetProfile() {
return new Promise((resolve, reject) => {
UserApi.getProfile()

View File

@ -14,7 +14,7 @@
v-model="is_auth"
:style="{
'--el-color-primary': application_profile?.custom_theme?.theme_color,
'--el-color-primary-light-9': hexToRgba(application_profile?.custom_theme?.theme_color, 0.1)
'--el-color-primary-light-9': hexToRgba(application_profile?.custom_theme?.theme_color, 0.1),
}"
></Auth>
</template>
@ -26,17 +26,18 @@ import Auth from '@/views/chat/auth/index.vue'
import { hexToRgba } from '@/utils/theme'
import { useI18n } from 'vue-i18n'
import { getBrowserLang } from '@/locales/index'
import ChatAPI from '@/api/chat/chat.ts'
const { locale } = useI18n({ useScope: 'global' })
const route = useRoute()
const { application, user } = useStore()
const components: any = import.meta.glob('@/views/chat/**/index.vue', {
eager: true
eager: true,
})
const {
query: { mode },
params: { accessToken }
params: { accessToken },
} = route as any
const is_auth = ref<boolean>(false)
const currentTemplate = computed(() => {
@ -91,9 +92,14 @@ function getAccessToken(token: string) {
return getAppProfile()
})
}
const getChatProfile = () => {
return ChatAPI.chatProfile(accessToken).then((ok: any) => {
console.log(ok)
})
}
onBeforeMount(() => {
user.changeUserType(2, accessToken)
Promise.all([user.asyncGetProfile(), getAccessToken(accessToken)])
Promise.all([user.asyncGetProfile(), getChatProfile()])
.catch(() => {
applicationAvailable.value = false
})

View File

@ -25,6 +25,11 @@ export default defineConfig(({ mode }) => {
changeOrigin: true,
rewrite: (path: string) => path.replace(ENV.VITE_BASE_PATH, '/'),
}
proxyConf['/chat'] = {
target: 'http://127.0.0.1:8080',
changeOrigin: true,
rewrite: (path: string) => path.replace(ENV.VITE_BASE_PATH, '/'),
}
proxyConf['/doc'] = {
target: 'http://127.0.0.1:8080',
changeOrigin: true,