From 77e1d92240a04819394cce3b753d9ad1d1bea7fb Mon Sep 17 00:00:00 2001 From: yangzy0603 Date: Mon, 21 Aug 2023 10:13:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BA=91=E7=AB=AF=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/gi-site/src/services/const.tsx | 10 ++- packages/gi-site/src/services/dataset.tsx | 23 ++++++- packages/gi-site/src/services/project.ts | 24 +++++-- packages/gi-site/src/services/user.tsx | 2 +- packages/gi-site/src/services/utils.ts | 83 ++++++++++++++++++++++- 5 files changed, 131 insertions(+), 11 deletions(-) diff --git a/packages/gi-site/src/services/const.tsx b/packages/gi-site/src/services/const.tsx index 611fc8a3f..51a7a9a87 100644 --- a/packages/gi-site/src/services/const.tsx +++ b/packages/gi-site/src/services/const.tsx @@ -1,3 +1,6 @@ +import { utils } from '@antv/gi-sdk'; + +const { getSiteContext } = utils; // GI 默认使用离线数据模式的站点 const GI_DEPOLY_OFFLINE_SITE = [ 'graphinsight.antv.vision', @@ -22,15 +25,18 @@ export const GI_SITE = { }, get SERVICE_URL() { const { hostname, protocol } = window.location; + const { GI_SITE_ID = 'DEFAULT' } = getSiteContext(); const port = 7001; let online = `${protocol}//${hostname}:${port}`; - if (!IS_DEV_ENV) { + if (GI_SITE_ID === 'DEFAULT') { + online = 'https://rplus-pre.alipay.com/api/function/gi'; + } else if (!IS_DEV_ENV){ online = window.location.origin + window.location.pathname; if (online.endsWith('/')) { online = online.slice(0, -1); } } - + return GI_SITE.IS_OFFLINE ? 'https://graphinsight.antgroup-inc.cn' // 'https://graphinsight-pre.alipay.com' : online; diff --git a/packages/gi-site/src/services/dataset.tsx b/packages/gi-site/src/services/dataset.tsx index 93a9a54a4..df5cd8ded 100644 --- a/packages/gi-site/src/services/dataset.tsx +++ b/packages/gi-site/src/services/dataset.tsx @@ -4,6 +4,9 @@ import DATASET_CASE from './initial.data/default.case'; import { IDataset } from './typing'; import { request } from './utils'; import { deepMix } from '@antv/util'; +import { utils } from '@antv/gi-sdk'; + +const { getSiteContext } = utils; /** * 更新或保存指定项目 @@ -15,9 +18,23 @@ export const queryDatasetInfo = async (id: string) => { if (GI_SITE.IS_OFFLINE) { return await GI_DATASET_DB.getItem(id); } else { - const response = await request(`${GI_SITE.SERVICE_URL}/dataset/${id}`, { - method: 'get', - }); + const { GI_SITE_ID = 'DEFAULT' } = getSiteContext(); + const requestParams: Record = { + url: `${GI_SITE.SERVICE_URL}/dataset/${id}`, + params: { + method: 'get', + } + } + if (GI_SITE_ID == 'DEFAULT') { + requestParams.url = `${GI_SITE.SERVICE_URL}/dataset/id`; + requestParams.params = { + method: 'post', + data: { + id: id + } + } + } + const response = await request(requestParams.url, requestParams.params); return response.data; } }; diff --git a/packages/gi-site/src/services/project.ts b/packages/gi-site/src/services/project.ts index 8d763b93d..62562c515 100644 --- a/packages/gi-site/src/services/project.ts +++ b/packages/gi-site/src/services/project.ts @@ -6,7 +6,9 @@ import { GI_SITE } from './const'; import { IProject } from './typing'; import { request } from './utils'; import $i18n from '../i18n'; +import { utils } from '@antv/gi-sdk'; +const { getSiteContext } = utils; /** * 获取所有项目 * @returns @@ -31,7 +33,6 @@ export const list = async (type: 'project' | 'case' | 'save'): Promise => { * @returns */ export const getById = async (id: string): Promise => { + const { GI_SITE_ID = 'DEFAULT' } = getSiteContext(); if (GI_SITE.IS_OFFLINE) { const project: any = await GI_PROJECT_DB.getItem(id); if (!project) { @@ -93,9 +95,23 @@ export const getById = async (id: string): Promise => { engineId: engineId || 'GI', }; } - const response = await request(`${GI_SITE.SERVICE_URL}/project/${id}`, { - method: 'get', - }); + const requestParams: Record = { + url: `${GI_SITE.SERVICE_URL}/project/${id}`, + params: { + method: 'get', + } + } + if (GI_SITE_ID == 'DEFAULT') { + requestParams.url = `${GI_SITE.SERVICE_URL}/project/id`; + requestParams.params = { + method: 'post', + data: { + id: id + } + } + } + const response = await request(requestParams.url, requestParams.params); + if (response.success) { const { projectConfig, engineId, ...others } = response.data; return { diff --git a/packages/gi-site/src/services/user.tsx b/packages/gi-site/src/services/user.tsx index bcbc3bf33..1e2e0c6d9 100644 --- a/packages/gi-site/src/services/user.tsx +++ b/packages/gi-site/src/services/user.tsx @@ -1,7 +1,7 @@ import { CrownTwoTone } from '@ant-design/icons'; import { Button, Space, notification } from 'antd'; import React from 'react'; -import request from 'umi-request'; +import { request } from './utils'; import $i18n from '../i18n'; import { GI_SITE } from './const'; diff --git a/packages/gi-site/src/services/utils.ts b/packages/gi-site/src/services/utils.ts index 6baf4ad09..05b16f229 100644 --- a/packages/gi-site/src/services/utils.ts +++ b/packages/gi-site/src/services/utils.ts @@ -1,6 +1,11 @@ import { notification } from 'antd'; import { extend } from 'umi-request'; +import type { RequestOptionsInit } from 'umi-request'; +import { utils } from '@antv/gi-sdk'; import $i18n from '../i18n'; + +const { getSiteContext } = utils; + const errorHandler = error => { console.log('error', JSON.stringify(error)); if (error.type === 'TypeError') { @@ -12,11 +17,87 @@ const errorHandler = error => { } throw error; }; -export const request = extend({ +const _request = extend({ headers: { 'Content-Type': 'application/json;charset=utf-8', + 'x-protocol-version': '1' }, credentials: 'include', withCredentials: true, // 携带cookie errorHandler, }); + +// 云端存储 入参转换逻辑 +const getCompatibleOptions = (url: string, options: RequestOptionsInit) => { + let finalUrl = url; + const finalOptions = { ...options }; + finalOptions.method = 'post'; + if (url.endsWith('delete')) { + // 兼容 云端存储服务 delete 关键字无法作为接口名 + finalUrl = url.replace('delete', 'remove'); + } + if (options.data) { + const { data } = options; + const { name, datasetId, engineId, desc, id, ...rest } = data; + const args = { + name, + datasetId, + engineId, + desc, + id, + } + if (Object.keys(rest).length) { + args.params = rest + } + finalOptions.data = { args: [args] }; + } + return { + url: finalUrl, + options: finalOptions + } +} + +// 云端存储 出参转换逻辑 +const getCompatibleResponse = (res: any) => { + const { result, success, error, ...rest } = res; + // 处理未登录逻辑 + if (!success) { + const { code, data } = error; + if (code === 'UNAUTHENTICATED_ERROR' && data.redirectUrl) { + window.location.replace(data.redirectUrl) + } + } + if (!result) { + return res; + } + // 兼容 云端服务返回 result 数据结构 + if (result.data) { + const data = Array.isArray(result.data) ? result.data.map(({ params, ...datumRest }) => ({ ...datumRest, ...params })) : result.data; + return { + data, + ...rest, + } + } else { + const { params, ...resultRest } = result; + return { + ...rest, + data: { + ...resultRest, + ...params + } + } + } +} + +export const request = (url: string, options: RequestOptionsInit = { method: 'get' }) => { + const { GI_SITE_ID } = getSiteContext(); + + if (GI_SITE_ID === 'DEFAULT') { + const { url: finalUrl, options: finalOptions } = getCompatibleOptions(url, options); + url = finalUrl; + options = finalOptions; + } + return _request(url, options).then(res => { + return getCompatibleResponse(res); + }); +}