123 lines
4.3 KiB
Plaintext
123 lines
4.3 KiB
Plaintext
import { http } from '@kit.NetworkKit';
|
|
import { GlobalContext, HXLog } from 'biz_common';
|
|
import { buildHeader } from '../util/Header';
|
|
import { FirstPageCardsConfigLoader } from '../node/datacenter/FirstPageCardsConfigLoader';
|
|
import { AllCardsCard, ModDataItem } from '../node/model/AiDiagnosisNodeModel';
|
|
import {
|
|
PickContractItem,
|
|
PickStrategy,
|
|
PickTabData,
|
|
RawPickResponse,
|
|
RawPickTabItem,
|
|
RawStrategyDetail,
|
|
RawStrategyItem,
|
|
} from './AiPickModels';
|
|
import { getResData, getTableList, ResData } from './AiPickUtils';
|
|
import { MAX_ITEMS, MAX_TABS, MAX_TAGS } from './AiPickConstant';
|
|
|
|
const EMPTY_RAW_STRATEGY_ITEM: RawStrategyItem = {};
|
|
const EMPTY_RAW_STRATEGY_DETAIL: RawStrategyDetail = {
|
|
id: '',
|
|
strategy_name: '',
|
|
strategy_type: '',
|
|
};
|
|
|
|
function parseRawTab(item: RawPickTabItem): PickTabData {
|
|
const isSystemPeriod: boolean = item.type === 'system_period';
|
|
const strategyItem: RawStrategyItem = (
|
|
isSystemPeriod ? item.system_strategy : item.custom_strategy
|
|
) ?? EMPTY_RAW_STRATEGY_ITEM;
|
|
const strategyDetail: RawStrategyDetail = (
|
|
isSystemPeriod ? strategyItem.system_strategy : strategyItem.custom_strategy
|
|
) ?? EMPTY_RAW_STRATEGY_DETAIL;
|
|
const resData: ResData = getResData(strategyItem);
|
|
const allItems: PickContractItem[] =
|
|
getTableList(resData.list, resData.resultValueList);
|
|
const strategy: PickStrategy = {
|
|
id: strategyDetail.id,
|
|
name: strategyDetail.strategy_name,
|
|
detail: strategyDetail.user_question || strategyDetail.content || '',
|
|
type: strategyDetail.strategy_type || 'custom_strategy',
|
|
periodType: item.type,
|
|
tagList: (strategyDetail.tag_list ??
|
|
[GlobalContext.get().resourceManager
|
|
.getStringSync($r('app.string.ai_pick_custom_tag'))])
|
|
.slice(0, MAX_TAGS),
|
|
imgs: isSystemPeriod
|
|
? [strategyDetail.white_img ?? '', strategyDetail.black_img ?? '']
|
|
: [],
|
|
count: strategyItem.detail_count ?? 0,
|
|
};
|
|
const tabData: PickTabData = {
|
|
strategy: strategy,
|
|
items: allItems.slice(0, MAX_ITEMS),
|
|
keysList: resData.showKeyList,
|
|
fieldList: resData.fieldList,
|
|
};
|
|
return tabData;
|
|
}
|
|
|
|
function parseRawTabs(rawTabs: RawPickTabItem[]): PickTabData[] {
|
|
return rawTabs
|
|
.slice(0, MAX_TABS)
|
|
.map((item: RawPickTabItem): PickTabData => parseRawTab(item));
|
|
}
|
|
|
|
export class AiPickDataFetcher {
|
|
private static readonly instance: AiPickDataFetcher = new AiPickDataFetcher();
|
|
|
|
private constructor() {
|
|
}
|
|
|
|
static getInstance(): AiPickDataFetcher {
|
|
return AiPickDataFetcher.instance;
|
|
}
|
|
|
|
fetchCardConfig(): AllCardsCard | undefined {
|
|
return FirstPageCardsConfigLoader.getConfig()?.aiPick;
|
|
}
|
|
|
|
async fetchCardData(): Promise<PickTabData[]> {
|
|
const cardConfig: AllCardsCard | undefined = this.fetchCardConfig();
|
|
const modData: ModDataItem | undefined = cardConfig?.mod_data[0];
|
|
const configuredUrl: string = modData?.url ?? '';
|
|
if (configuredUrl === '') {
|
|
return [];
|
|
}
|
|
|
|
const requestOptions: http.HttpRequestOptions = {
|
|
method: http.RequestMethod.GET,
|
|
header: buildHeader(),
|
|
connectTimeout: 60000,
|
|
readTimeout: 60000,
|
|
};
|
|
const httpRequest: http.HttpRequest = http.createHttp();
|
|
try {
|
|
const requestUrl: string = this.buildRequestUrl(configuredUrl, Date.now());
|
|
const response: http.HttpResponse =
|
|
await httpRequest.request(requestUrl, requestOptions);
|
|
if (response.responseCode !== http.ResponseCode.OK) {
|
|
throw new Error('request failed: ' + response.responseCode);
|
|
}
|
|
const responseText: string = response.result as string;
|
|
const result: RawPickResponse = JSON.parse(responseText) as RawPickResponse;
|
|
const responseCode: number = result.code ?? result.status_code ?? 0;
|
|
if (responseCode !== 0 || result.data === undefined) {
|
|
throw new Error('response code: ' + responseCode);
|
|
}
|
|
return parseRawTabs(result.data);
|
|
} catch (requestError) {
|
|
const errorInfo: Error = requestError as Error;
|
|
HXLog.e('AiPickDataFetcher', 'fetchCardData error: ' + errorInfo.message);
|
|
throw new Error(errorInfo.message);
|
|
} finally {
|
|
httpRequest.destroy();
|
|
}
|
|
}
|
|
|
|
private buildRequestUrl(configuredUrl: string, timestamp: number): string {
|
|
const separator: string = configuredUrl.includes('?') ? '&' : '?';
|
|
return `${configuredUrl}${separator}_=${timestamp}`;
|
|
}
|
|
}
|