Commit 643d1d7d by weiw

fix:处理文件预览

parent 0859f43d
// src/api/common.ts
import http from '@/utils/request' import http from '@/utils/request'
/** /**
* 登录 * 登录
* @returns
*/ */
export function fetchLoginByUid(uid: string) { export function fetchLoginByUid(uid: string) {
return http.post('/user/api/login/mobile/v1/guest/login', { return http.post('/user/api/login/mobile/v1/guest/login', {
...@@ -12,7 +12,6 @@ export function fetchLoginByUid(uid: string) { ...@@ -12,7 +12,6 @@ export function fetchLoginByUid(uid: string) {
/** /**
* 第三方登录 * 第三方登录
* @returns
*/ */
export function fetchLoginByToken(loginCode: any) { export function fetchLoginByToken(loginCode: any) {
return http.post('/user/api/login/mobile/v1/sso_login', { return http.post('/user/api/login/mobile/v1/sso_login', {
...@@ -22,16 +21,25 @@ export function fetchLoginByToken(loginCode: any) { ...@@ -22,16 +21,25 @@ export function fetchLoginByToken(loginCode: any) {
/** /**
* 获取协议列表 * 获取协议列表
* @returns
*/ */
export function fetchGetAgreementList() { export function fetchGetAgreementList() {
return http.post('/config-center/api/commonconfig/mobile/v1/query_agreement_list') return http.post('/config-center/api/commonconfig/mobile/v1/query_agreement_list')
} }
/** /**
* 获取文档链接s * 获取文档链接
* @param recordId * @param docId
* @returns */
export function fetchGetDocumentLink(docId: string) {
return http.post('/conversation/api/conversation/mobile/v1/get_document', {
ossType: 'private',
docId,
})
}
/**
* 批量获取文档链接
* @param docIdList
*/ */
export function fetchGetDocumentLinks(docIdList: string[]) { export function fetchGetDocumentLinks(docIdList: string[]) {
return http.post('/conversation/api/conversation/mobile/v1/query_batch_document', { return http.post('/conversation/api/conversation/mobile/v1/query_batch_document', {
...@@ -41,13 +49,14 @@ export function fetchGetDocumentLinks(docIdList: string[]) { ...@@ -41,13 +49,14 @@ export function fetchGetDocumentLinks(docIdList: string[]) {
} }
/** /**
* 获取文档链接 * 下载文件
* @param recordId * @param docId
* @returns
*/ */
export function fetchGetDocumentLink(docId: string) { export function fetchDownloadFile(docId: string) {
return http.post('/conversation/api/conversation/mobile/v1/get_document', { return http.post('/conversation/api/conversation/mobile/v1/download_file', {
ossType: 'private', ossType: 'private',
docId, docId,
}, {
responseType: 'blob',
}) })
} }
...@@ -9,7 +9,7 @@ import CardCalculation from '@/assets/card-calculation.png' ...@@ -9,7 +9,7 @@ import CardCalculation from '@/assets/card-calculation.png'
import CardDetailImg from '@/assets/card-detail.png' import CardDetailImg from '@/assets/card-detail.png'
import CardPlansImg from '@/assets/card-book1111.png' import CardPlansImg from '@/assets/card-book1111.png'
import CardProductCompareImg from '@/assets/card-product2222.png' import CardProductCompareImg from '@/assets/card-product2222.png'
import { fetchGetDocumentLink } from '@/api/common' import { fetchDownloadFile } from '@/api/common'
import { FilePreviewModal } from '@/components/FilePreviewModal' import { FilePreviewModal } from '@/components/FilePreviewModal'
interface ChatAnswerAttachmentProps { interface ChatAnswerAttachmentProps {
...@@ -40,22 +40,48 @@ export const ChatAnswerAttachment: React.FC<ChatAnswerAttachmentProps> = ({ ...@@ -40,22 +40,48 @@ export const ChatAnswerAttachment: React.FC<ChatAnswerAttachmentProps> = ({
const handleClickDocLink = async (doc: any) => { const handleClickDocLink = async (doc: any) => {
try { try {
const docId = `${doc.knowledgeName}/${doc.documentStoreKey}` const docId = `${doc.knowledgeName}/${doc.documentStoreKey}`
const res = await fetchGetDocumentLink(docId)
if (res.data) { const resBlob: any = await fetchDownloadFile(docId)
setCurrentDoc(doc) const mimeType = blobType(doc.documentAlias) // 修正参数
setDocUrl(res.data.docUrl) const blob = new Blob([resBlob.data], { type: mimeType })
setPreviewModalOpen(true)
} // 创建 fileBlob URL
const fileBlobUrl = URL.createObjectURL(blob)
setCurrentDoc(doc)
setDocUrl(fileBlobUrl) // 传递 blob URL
setPreviewModalOpen(true)
} }
catch (error) { catch (error) {
console.error('获取文档链接失败:', error) console.error('获取文档链接失败:', error)
// 如果获取预览链接失败,直接打开新窗口 }
const docId = `${doc.knowledgeName}/${doc.documentStoreKey}` }
const res = await fetchGetDocumentLink(docId)
if (res.data) { function blobType(fileName: string) {
window.open(res.data.docUrl, '_blank') const ext = fileName.substring(fileName.lastIndexOf('.')).toLowerCase()
} switch (ext) {
case '.pdf':
return 'application/pdf'
case '.doc':
return 'application/msword'
case '.docx':
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
case '.xls':
return 'application/vnd.ms-excel'
case '.xlsx':
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
case '.ppt':
return 'application/vnd.ms-powerpoint'
case '.pptx':
return 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
case '.txt':
return 'text/plain;charset=utf-8'
case '.wps':
return 'application/wps-office.wps'
case '.ett':
return 'application/wps-office.ett'
default:
return 'application/octet-stream'
} }
} }
......
// src/utils/request.ts
import axios from 'axios' import axios from 'axios'
import type { import type {
AxiosError, AxiosError,
...@@ -33,13 +34,10 @@ service.interceptors.request.use( ...@@ -33,13 +34,10 @@ service.interceptors.request.use(
'X-Token': JSON.parse(token), 'X-Token': JSON.parse(token),
'X-Request-Id': `${Date.now()}${Math.random().toString(36).substring(2)}`, 'X-Request-Id': `${Date.now()}${Math.random().toString(36).substring(2)}`,
'X-Request-By': config.url, 'X-Request-By': config.url,
// 'X-App-Type': getAppType() || '',
// 'X-Channel-Code': getChannelCode() || '',
} }
return config return config
}, },
(error: AxiosError) => { (error: AxiosError) => {
// showToast(error.message)
return Promise.reject(error) return Promise.reject(error)
}, },
) )
...@@ -47,8 +45,15 @@ service.interceptors.request.use( ...@@ -47,8 +45,15 @@ service.interceptors.request.use(
service.interceptors.response.use( service.interceptors.response.use(
(response: AxiosResponse<any, Response<ResponseData>>) => { (response: AxiosResponse<any, Response<ResponseData>>) => {
const { code, message } = response.data const { code, message } = response.data
if (response.config.responseType === 'stream')
// 处理 blob 响应类型
if (response.config.responseType === 'blob') {
return response
}
if (response.config.responseType === 'stream') {
return response return response
}
if (code === '00000000') { if (code === '00000000') {
return response.data return response.data
...@@ -56,16 +61,12 @@ service.interceptors.response.use( ...@@ -56,16 +61,12 @@ service.interceptors.response.use(
else if (code === '00000005') { else if (code === '00000005') {
// 处理登录失效 // 处理登录失效
window.localStorage.clear() window.localStorage.clear()
// window.location.href = '/sdream-ai'
// window.location.reload()
} }
else { else {
// showToast(message)
return Promise.reject(new Error(message)) return Promise.reject(new Error(message))
} }
}, },
(error: AxiosError) => { (error: AxiosError) => {
// showToast('网络请求异常')
return Promise.reject(error) return Promise.reject(error)
}, },
) )
...@@ -95,6 +96,7 @@ export const http = { ...@@ -95,6 +96,7 @@ export const http = {
delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> { delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
return service.delete(url, config) return service.delete(url, config)
}, },
stream<T = any>(url: string, data: T) { stream<T = any>(url: string, data: T) {
return service.post(url, data, { return service.post(url, data, {
responseType: 'stream', responseType: 'stream',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment