Skip to content

Commit

Permalink
feat: 云端存储服务兼容
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzy0603 committed Aug 21, 2023
1 parent 8a2f24f commit 77e1d92
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 11 deletions.
10 changes: 8 additions & 2 deletions packages/gi-site/src/services/const.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { utils } from '@antv/gi-sdk';

const { getSiteContext } = utils;
// GI 默认使用离线数据模式的站点
const GI_DEPOLY_OFFLINE_SITE = [
'graphinsight.antv.vision',
Expand All @@ -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;
Expand Down
23 changes: 20 additions & 3 deletions packages/gi-site/src/services/dataset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* 更新或保存指定项目
Expand All @@ -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<string, any> = {
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;
}
};
Expand Down
24 changes: 20 additions & 4 deletions packages/gi-site/src/services/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,7 +33,6 @@ export const list = async (type: 'project' | 'case' | 'save'): Promise<IProject[
const response = await request(`${GI_SITE.SERVICE_URL}/project/list`, {
method: 'post',
});

if (response.success) {
return response.data;
}
Expand Down Expand Up @@ -75,6 +76,7 @@ export const create = async (param: any): Promise<string | undefined> => {
* @returns
*/
export const getById = async (id: string): Promise<IProject | undefined> => {
const { GI_SITE_ID = 'DEFAULT' } = getSiteContext();
if (GI_SITE.IS_OFFLINE) {
const project: any = await GI_PROJECT_DB.getItem(id);
if (!project) {
Expand All @@ -93,9 +95,23 @@ export const getById = async (id: string): Promise<IProject | undefined> => {
engineId: engineId || 'GI',
};
}
const response = await request(`${GI_SITE.SERVICE_URL}/project/${id}`, {
method: 'get',
});
const requestParams: Record<string, any> = {
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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/gi-site/src/services/user.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
83 changes: 82 additions & 1 deletion packages/gi-site/src/services/utils.ts
Original file line number Diff line number Diff line change
@@ -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') {
Expand All @@ -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);
});
}

0 comments on commit 77e1d92

Please sign in to comment.