From 79e0019953df3e1f41c74d15eee6a8b997a849f8 Mon Sep 17 00:00:00 2001 From: colin Date: Thu, 10 Oct 2024 15:19:48 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix(axios):=20=E7=A7=BB=E9=99=A4=E5=A4=9A?= =?UTF-8?q?=E4=BD=99=E7=9A=84api=E5=89=8D=E7=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/vue/.env.development | 2 +- apps/vue/.env.production | 2 +- apps/vue/build/vite/proxy.ts | 3 +- .../abp/application-configuration/index.ts | 6 +- .../api/oss-management/files/private/index.ts | 32 ++--- .../api/oss-management/files/public/index.ts | 32 ++--- .../src/api/oss-management/objects/index.ts | 22 ++-- apps/vue/src/api/sys/user.ts | 28 +++-- apps/vue/src/api/weather/index.ts | 114 ++++++++++-------- apps/vue/src/hooks/web/useSignalR.ts | 6 +- .../header/components/notify/useMessages.ts | 9 +- .../components/notify/useNotifications.ts | 17 +-- apps/vue/src/utils/http/axios/Axios.ts | 43 +++++-- apps/vue/src/utils/http/axios/index.ts | 7 +- apps/vue/vite.config.ts | 2 +- 15 files changed, 169 insertions(+), 156 deletions(-) diff --git a/apps/vue/.env.development b/apps/vue/.env.development index 323254092..271808509 100644 --- a/apps/vue/.env.development +++ b/apps/vue/.env.development @@ -13,7 +13,7 @@ VITE_PROXY=[["/connect","http://127.0.0.1:30000"],["/api","http://127.0.0.1:3000 VITE_DROP_CONSOLE=false # Basic interface address SPA -VITE_GLOB_API_URL=/api +VITE_GLOB_API_URL= # File upload address, optional VITE_GLOB_UPLOAD_URL=/upload diff --git a/apps/vue/.env.production b/apps/vue/.env.production index 035e63f6d..a2763938a 100644 --- a/apps/vue/.env.production +++ b/apps/vue/.env.production @@ -16,7 +16,7 @@ VITE_BUILD_COMPRESS='none' VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE=false # Basic interface address SPA -VITE_GLOB_API_URL=/api +VITE_GLOB_API_URL= # File upload address, optional # It can be forwarded by nginx or write the actual address directly diff --git a/apps/vue/build/vite/proxy.ts b/apps/vue/build/vite/proxy.ts index 8525397b6..a23c14a5a 100644 --- a/apps/vue/build/vite/proxy.ts +++ b/apps/vue/build/vite/proxy.ts @@ -25,7 +25,8 @@ export function createProxy(list: ProxyList = []) { target: target, changeOrigin: true, ws: true, - rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''), + rewrite: (path) => path, + // rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''), // https is require secure=false ...(isHttps ? { secure: false } : {}), }; diff --git a/apps/vue/src/api/abp/application-configuration/index.ts b/apps/vue/src/api/abp/application-configuration/index.ts index 409d9b716..258e614e2 100644 --- a/apps/vue/src/api/abp/application-configuration/index.ts +++ b/apps/vue/src/api/abp/application-configuration/index.ts @@ -1,10 +1,8 @@ import { defHttp } from '/@/utils/http/axios'; -export const GetAsyncByOptions = (options?: { - includeLocalizationResources?: boolean; -}) => { +export const GetAsyncByOptions = (options?: { includeLocalizationResources?: boolean }) => { return defHttp.get({ url: '/api/abp/application-configuration', params: options, }); -}; \ No newline at end of file +}; diff --git a/apps/vue/src/api/oss-management/files/private/index.ts b/apps/vue/src/api/oss-management/files/private/index.ts index 7b0b75636..534e14309 100644 --- a/apps/vue/src/api/oss-management/files/private/index.ts +++ b/apps/vue/src/api/oss-management/files/private/index.ts @@ -1,37 +1,27 @@ import { OssObject } from '../../objects/model'; import { FileShare, FileShareInput, MyFileShare } from '../share/model'; import { defHttp } from '/@/utils/http/axios'; -import { AxiosResponse } from 'axios'; export const formatUrl = (url: string) => { // 格式化路径为用户目录 - return `/api/api/files/static/users/p/${url}`; + return `/api/files/static/users/p/${url}`; }; export const upload = (file: Blob, path: string, name: string) => { - return new Promise>((resolve, reject) => { - defHttp - .uploadFile( - { - url: '/api/api/files/private', - }, - { - data: { path: path, object: name }, - file: file, - }, - ) - .then((res: AxiosResponse) => { - resolve(res); - }) - .catch((err) => { - reject(err); - }); - }); + return defHttp.uploadFile( + { + url: '/api/files/private', + }, + { + data: { path: path, object: name }, + file: file, + }, + ); }; export const get = (path: string, name: string) => { return defHttp.get({ - url: `/api/api/files/private/p/${path}/${name}`, + url: `/api/files/private/p/${path}/${name}`, }); }; diff --git a/apps/vue/src/api/oss-management/files/public/index.ts b/apps/vue/src/api/oss-management/files/public/index.ts index eddf27d5d..ef7f790da 100644 --- a/apps/vue/src/api/oss-management/files/public/index.ts +++ b/apps/vue/src/api/oss-management/files/public/index.ts @@ -1,36 +1,26 @@ import { OssObject } from '../../objects/model'; import { defHttp } from '/@/utils/http/axios'; -import { AxiosResponse } from 'axios'; export const formatUrl = (url: string) => { // 格式化路径为公共目录 - return `/api/api/files/static/public/p/${url}`; + return `/api/files/static/public/p/${url}`; }; export const upload = (file: Blob, path: string, name: string) => { - return new Promise>((resolve, reject) => { - defHttp - .uploadFile( - { - url: `/api/api/files/public/upload`, - }, - { - data: { path: path, object: name }, - file: file, - }, - ) - .then((res: AxiosResponse) => { - resolve(res); - }) - .catch((err) => { - reject(err); - }); - }); + return defHttp.uploadFile( + { + url: `/api/files/public/upload`, + }, + { + data: { path: path, object: name }, + file: file, + }, + ); }; export const get = (path: string, name: string) => { return defHttp.get({ - url: `/api/api/files/public/p/${path}/${name}`, + url: `/api/files/public/p/${path}/${name}`, }); }; diff --git a/apps/vue/src/api/oss-management/objects/index.ts b/apps/vue/src/api/oss-management/objects/index.ts index 9101a461a..3d9cf30d8 100644 --- a/apps/vue/src/api/oss-management/objects/index.ts +++ b/apps/vue/src/api/oss-management/objects/index.ts @@ -14,8 +14,8 @@ import { useAbpStoreWithOut } from '/@/store/modules/abp'; import { format } from '/@/utils/strings'; import { ContentTypeEnum } from '/@/enums/httpEnum'; -export const uploadUrl = '/api/api/oss-management/objects/upload'; -export const downloadUrl = '/api/api/files/static/{bucket}/p/{path}/{name}'; +export const uploadUrl = '/api/oss-management/objects/upload'; +export const downloadUrl = '/api/files/static/{bucket}/p/{path}/{name}'; export function generateOssUrl(bucket: string, path: string, object: string) { if (path) { @@ -35,8 +35,6 @@ export const downloadBlob = (bucket: string, path: string, object: string) => { accept: 'application/json', }, responseType: 'blob', - }, { - apiUrl: '/api' }); }; @@ -78,7 +76,7 @@ export const uploadObject = (params: UploadFileParams, event: any) => { const path = encodeURIComponent(params.data?.path); res.data = { url: format(formatUrl, { - bucket: params.data?.bucket, + bucket: params.data?.bucket, tenantId: currentTenant.id, path: path, name: fileName, @@ -112,7 +110,7 @@ export const uploadObject = (params: UploadFileParams, event: any) => { fileName: fileName, }; return defHttp - .uploadFile(requestConfig, { + .uploadFile(requestConfig, { data: requestData, file: fileData, }) @@ -157,12 +155,10 @@ export const createObject = (input: OssObjectCreate, file?: Blob) => { }; export const deleteObject = (input: GetOssObjectRequest) => { - return defHttp.delete( - { - url: '/api/oss-management/objects', - params: input, - }, - ); + return defHttp.delete({ + url: '/api/oss-management/objects', + params: input, + }); }; export const bulkDeleteObject = (input: OssObjectBulkDelete) => { @@ -170,7 +166,7 @@ export const bulkDeleteObject = (input: OssObjectBulkDelete) => { url: '/api/oss-management/objects/bulk-delete', data: input, }); -} +}; export const getObject = (input: GetOssObjectRequest) => { return defHttp.get({ diff --git a/apps/vue/src/api/sys/user.ts b/apps/vue/src/api/sys/user.ts index e8bdf0b40..4ac876c2e 100644 --- a/apps/vue/src/api/sys/user.ts +++ b/apps/vue/src/api/sys/user.ts @@ -25,7 +25,11 @@ enum Api { /** * @description: user login api */ -export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal', isPortalLogin: boolean = false) { +export function loginApi( + params: LoginParams, + mode: ErrorMessageMode = 'modal', + isPortalLogin = false, +) { const setting = useGlobSetting(); const tokenParams = { client_id: setting.clientId, @@ -48,7 +52,6 @@ export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal', }, { errorMessageMode: mode, - apiUrl: '/connect', withToken: false, }, ); @@ -79,7 +82,6 @@ export function loginPhoneApi(params: LoginByPhoneParams, mode: ErrorMessageMode }, { errorMessageMode: mode, - apiUrl: '/connect', }, ); } @@ -88,14 +90,16 @@ export function loginPhoneApi(params: LoginByPhoneParams, mode: ErrorMessageMode * @description: getUserInfo */ export function getUserInfo() { - return defHttp.get( - { - url: Api.GetUserInfo, - }, - { - errorMessageMode: 'none', - apiUrl: '/connect', - }).catch(() => { + return defHttp + .get( + { + url: Api.GetUserInfo, + }, + { + errorMessageMode: 'none', + }, + ) + .catch(() => { const userStore = useUserStoreWithOut(); createErrorModal({ title: t('sys.api.errorTip'), @@ -103,7 +107,7 @@ export function getUserInfo() { onOk: () => { userStore.setToken(undefined); userStore.logout(true); - } + }, }); }); } diff --git a/apps/vue/src/api/weather/index.ts b/apps/vue/src/api/weather/index.ts index 7e8976bb1..304ae8212 100644 --- a/apps/vue/src/api/weather/index.ts +++ b/apps/vue/src/api/weather/index.ts @@ -15,61 +15,73 @@ const Api = { }; export const getProvinces = () => { - return defHttp.get({ - url: Api.GetProvinces, - //baseURL: Host, - headers: { - 'X-Requested-With': 'XMLHttpRequest' - } - }, { - apiUrl: '', - joinTime: false, - withToken: false, - withAcceptLanguage: false, - }); + return defHttp.get( + { + url: Api.GetProvinces, + //baseURL: Host, + headers: { + 'X-Requested-With': 'XMLHttpRequest', + }, + }, + { + apiUrl: '', + joinTime: false, + withToken: false, + withAcceptLanguage: false, + }, + ); }; export const getPosition = () => { - return defHttp.get({ - url: Api.GetPosition, - //baseURL: Host, - headers: { - 'X-Requested-With': 'XMLHttpRequest', - } - }, { - apiUrl: '', - joinTime: false, - withToken: false, - withAcceptLanguage: false, - }); -} + return defHttp.get( + { + url: Api.GetPosition, + //baseURL: Host, + headers: { + 'X-Requested-With': 'XMLHttpRequest', + }, + }, + { + apiUrl: '', + joinTime: false, + withToken: false, + withAcceptLanguage: false, + }, + ); +}; export const getCitys = (provinceCode: string) => { - return defHttp.get({ - url: format(Api.GetCitys, {province: provinceCode}), - //baseURL: Host, - headers: { - 'X-Requested-With': 'XMLHttpRequest' - } - }, { - apiUrl: '', - joinTime: false, - withToken: false, - withAcceptLanguage: false, - }); -} + return defHttp.get( + { + url: format(Api.GetCitys, { province: provinceCode }), + //baseURL: Host, + headers: { + 'X-Requested-With': 'XMLHttpRequest', + }, + }, + { + apiUrl: '', + joinTime: false, + withToken: false, + withAcceptLanguage: false, + }, + ); +}; export const getWeather = (cityCode: string) => { - return defHttp.get({ - url: format(Api.GetWeather, {code: cityCode}), - //baseURL: Host, - headers: { - 'X-Requested-With': 'XMLHttpRequest' - } - }, { - apiUrl: '', - joinTime: false, - withToken: false, - withAcceptLanguage: false, - }); -} + return defHttp.get( + { + url: format(Api.GetWeather, { code: cityCode }), + //baseURL: Host, + headers: { + 'X-Requested-With': 'XMLHttpRequest', + }, + }, + { + apiUrl: '', + joinTime: false, + withToken: false, + withAcceptLanguage: false, + }, + ); +}; diff --git a/apps/vue/src/hooks/web/useSignalR.ts b/apps/vue/src/hooks/web/useSignalR.ts index 4eed635ce..a5f404a24 100644 --- a/apps/vue/src/hooks/web/useSignalR.ts +++ b/apps/vue/src/hooks/web/useSignalR.ts @@ -50,7 +50,7 @@ export function useSignalR(options: UseSignalR & SignalROptions) { httpOptions.accessTokenFactory = () => token.startsWith('Bearer ') ? token.substring(7) : token; } - var connectionBuilder = new HubConnectionBuilder() + const connectionBuilder = new HubConnectionBuilder() .withUrl(serverUrl, httpOptions) .configureLogging(LogLevel.Warning); if (automaticReconnect && nextRetryDelayInMilliseconds) { @@ -72,7 +72,7 @@ export function useSignalR(options: UseSignalR & SignalROptions) { try { await connection.start(); emitter.emit('signalR:onStart'); - } catch(error) { + } catch (error) { emitter.emit('signalR:onError', error); } } @@ -85,7 +85,7 @@ export function useSignalR(options: UseSignalR & SignalROptions) { try { await connection.stop(); emitter.emit('signalR:onStop'); - } catch(error) { + } catch (error) { emitter.emit('signalR:onError', error); } } diff --git a/apps/vue/src/layouts/default/header/components/notify/useMessages.ts b/apps/vue/src/layouts/default/header/components/notify/useMessages.ts index 6b553cf5d..8a4675ca5 100644 --- a/apps/vue/src/layouts/default/header/components/notify/useMessages.ts +++ b/apps/vue/src/layouts/default/header/components/notify/useMessages.ts @@ -29,7 +29,7 @@ export function useMessages() { const { createConfirm, createMessage } = useMessage(); const signalR = useSignalR({ autoStart: false, - serverUrl: '/signalr-hubs/signalr-hubs/messages', + serverUrl: '/signalr-hubs/messages', }); onMounted(() => { @@ -57,7 +57,7 @@ export function useMessages() { messageRef.value.list.length = 0; } - function refreshLastMessages(maxResultCount: number = 10) { + function refreshLastMessages(maxResultCount = 10) { getLastMessages({ sorting: '', state: MessageState.Send, @@ -69,7 +69,10 @@ export function useMessages() { function onMessageReceived(message: ChatMessage) { // 处理需要本地化的系统消息 - if (message.source === MessageSourceTye.System && (message.extraProperties.L === true || message.extraProperties.L === 'true')) { + if ( + message.source === MessageSourceTye.System && + (message.extraProperties.L === true || message.extraProperties.L === 'true') + ) { message.content = t( message.extraProperties.content.ResourceName + '.' + message.extraProperties.content.Name, message.extraProperties.content.Values as Recordable, diff --git a/apps/vue/src/layouts/default/header/components/notify/useNotifications.ts b/apps/vue/src/layouts/default/header/components/notify/useNotifications.ts index a8ca17b66..9646044b1 100644 --- a/apps/vue/src/layouts/default/header/components/notify/useNotifications.ts +++ b/apps/vue/src/layouts/default/header/components/notify/useNotifications.ts @@ -29,7 +29,7 @@ export function useNotifications() { list: [], }); const signalR = useSignalR({ - serverUrl: '/signalr-hubs/signalr-hubs/notifications', + serverUrl: '/signalr-hubs/notifications', }); onMounted(() => { @@ -49,17 +49,9 @@ export function useNotifications() { // 以通知名称来发起服务端回调 emitter.emit(notificationInfo.name, notificationInfo); return; - } - const { - title, - message, - description, - creationTime, - contentType, - severity, - type, - data, - } = deserialize(notificationInfo); + } + const { title, message, description, creationTime, contentType, severity, type, data } = + deserialize(notificationInfo); const notifier: Notification = { id: notificationInfo.id, avatar: data.avatar, @@ -76,7 +68,6 @@ export function useNotifications() { notifierRef.value.list.push(notifier); } - function _notification(notifier: Notification, severity: NotificationSeverity) { let message = notifier.description; switch (notifier.contentType) { diff --git a/apps/vue/src/utils/http/axios/Axios.ts b/apps/vue/src/utils/http/axios/Axios.ts index a37151d18..32c12922f 100644 --- a/apps/vue/src/utils/http/axios/Axios.ts +++ b/apps/vue/src/utils/http/axios/Axios.ts @@ -144,15 +144,40 @@ export class VAxios { }); } - return this.axiosInstance.request({ - ...config, - method: 'POST', - data: formData, - headers: { - 'Content-type': ContentTypeEnum.FORM_DATA, - // @ts-ignore - ignoreCancelToken: true, - }, + let conf: CreateAxiosOptions = cloneDeep(config); + const transform = this.getTransform(); + const { requestOptions } = this.options; + + const opt: RequestOptions = Object.assign({}, requestOptions); + const { beforeRequestHook, transformRequestHook } = transform || {}; + if (beforeRequestHook && isFunction(beforeRequestHook)) { + conf = beforeRequestHook(conf, opt); + } + + return new Promise((resolve, reject) => { + this.axiosInstance + .request({ + ...conf, + method: 'POST', + data: formData, + headers: { + 'Content-type': ContentTypeEnum.FORM_DATA, + // @ts-ignore + ignoreCancelToken: true, + }, + }) + .then((res) => { + if (transformRequestHook && isFunction(transformRequestHook)) { + try { + const ret = transformRequestHook(res as any, opt); + resolve(ret); + } catch (err) { + reject(err || new Error('request error!')); + } + return; + } + resolve(res as unknown as Promise); + }); }); } diff --git a/apps/vue/src/utils/http/axios/index.ts b/apps/vue/src/utils/http/axios/index.ts index c49e6ecb3..fd447951b 100644 --- a/apps/vue/src/utils/http/axios/index.ts +++ b/apps/vue/src/utils/http/axios/index.ts @@ -92,7 +92,10 @@ const transform: AxiosTransform = { } else { if (!isString(params)) { formatDate && formatRequestDate(params); - if (Reflect.has(config, 'data') && config.data && Object.keys(config.data).length > 0 || config.data instanceof FormData) { + if ( + (Reflect.has(config, 'data') && config.data && Object.keys(config.data).length > 0) || + config.data instanceof FormData + ) { config.data = data; config.params = params; } else if ( @@ -161,7 +164,7 @@ const transform: AxiosTransform = { if (axios.isCancel(error)) { return Promise.reject(error); } - + if (error.code && ['ECONNABORTED', 'ETIMEDOUT'].includes(error.code)) { const { t } = useI18n(); const timeout = t('sys.api.apiTimeoutMessage'); diff --git a/apps/vue/vite.config.ts b/apps/vue/vite.config.ts index 994b41f6d..8ecd736ea 100644 --- a/apps/vue/vite.config.ts +++ b/apps/vue/vite.config.ts @@ -18,7 +18,7 @@ function buildWeatherProxy(): Record { '/wapi': { target: 'http://www.nmc.cn', changeOrigin: true, - rewrite: (path) => path.replace('/wapi', ''), + rewrite: (path) => path, headers: { host: 'www.nmc.cn', referer: 'http://www.nmc.cn', From de064194c1117799cf1c5bbeb9293aabf41ae8f1 Mon Sep 17 00:00:00 2001 From: colin Date: Thu, 10 Oct 2024 15:20:54 +0800 Subject: [PATCH 2/4] =?UTF-8?q?fix(account):=20=E4=BF=AE=E5=A4=8D=E4=B8=AA?= =?UTF-8?q?=E4=BA=BA=E6=96=87=E6=A1=A3=E8=B7=AF=E5=BE=84=E6=8B=BC=E6=8E=A5?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/vue/src/views/account/center/Cloud.vue | 42 ++++++++++++++++----- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/apps/vue/src/views/account/center/Cloud.vue b/apps/vue/src/views/account/center/Cloud.vue index 0e0f4b73e..f860130dc 100644 --- a/apps/vue/src/views/account/center/Cloud.vue +++ b/apps/vue/src/views/account/center/Cloud.vue @@ -6,6 +6,7 @@