refactor: parseRawTab 合并到 Api 层
This commit is contained in:
@@ -1,9 +1,17 @@
|
|||||||
// AI选期接口层:负责从网络获取原始数据
|
// AI选期接口层:负责从网络获取原始数据并解析为视图数据
|
||||||
// 对应 Vue 端 index.vue 里的 getData()(http() 封装 + __GLOBAL__.api.API_HOST 拼接)
|
// 对应 Vue 端 index.vue 里的 getData()(http() 封装 + 解析流程)
|
||||||
// 目前网络请求未接入,直接返回 Mock 数据;接入时替换 fetchAiPickTabs 内部实现即可,
|
// 目前网络请求未接入,直接返回 Mock 数据;接入时替换 fetchAiPickTabs 内部实现即可。
|
||||||
// 上层 AiPickModel 的调用方式不需要改动。
|
import {
|
||||||
import { RawPickTabItem } from './AiPickTypes';
|
PickContractItem,
|
||||||
|
PickStrategy,
|
||||||
|
PickTabData,
|
||||||
|
RawPickTabItem,
|
||||||
|
RawStrategyDetail,
|
||||||
|
RawStrategyItem,
|
||||||
|
} from './AiPickTypes';
|
||||||
|
import { getResData, getTableList, ResData } from './AiPickUtils';
|
||||||
import { MOCK_RAW_TABS } from './AiPickMock';
|
import { MOCK_RAW_TABS } from './AiPickMock';
|
||||||
|
import { MAX_ITEMS, MAX_TABS, MAX_TAGS } from './AiPickConstant';
|
||||||
|
|
||||||
// 新版接口地址(isCustomVersion=true):
|
// 新版接口地址(isCustomVersion=true):
|
||||||
// {API_HOST}futgwapi/api/ai_diagnosis/home_strategy/v1/user_data
|
// {API_HOST}futgwapi/api/ai_diagnosis/home_strategy/v1/user_data
|
||||||
@@ -13,13 +21,65 @@ import { MOCK_RAW_TABS } from './AiPickMock';
|
|||||||
// 模拟网络延迟(毫秒),验证 loading 状态和异步流程用;接入真实接口后删除
|
// 模拟网络延迟(毫秒),验证 loading 状态和异步流程用;接入真实接口后删除
|
||||||
const MOCK_DELAY_MS = 300;
|
const MOCK_DELAY_MS = 300;
|
||||||
|
|
||||||
// 获取 AI 选期原始数据
|
// 对应 Vue 端 index.vue 中 formatTabData 的单项处理(新版 isCustomVersion=true)
|
||||||
|
function parseRawTab(item: RawPickTabItem): PickTabData {
|
||||||
|
const isSystemPeriod = item.type === 'system_period';
|
||||||
|
const strategyItem: RawStrategyItem = (
|
||||||
|
isSystemPeriod ? item.system_strategy : item.custom_strategy
|
||||||
|
) ?? {};
|
||||||
|
|
||||||
|
const strategyDetail: RawStrategyDetail = (
|
||||||
|
isSystemPeriod
|
||||||
|
? strategyItem.system_strategy
|
||||||
|
: strategyItem.custom_strategy
|
||||||
|
) ?? { id: '', strategy_name: '', strategy_type: '' };
|
||||||
|
|
||||||
|
const resData: ResData = getResData(strategyItem);
|
||||||
|
const allItems: PickContractItem[] = getTableList(resData.list, resData.resultValueList);
|
||||||
|
// count 取自 strategyItem 层级(与 detail_list/field_list 同级),
|
||||||
|
// 对应 Vue 端 strategyItem['detail_count'],不是嵌套的策略详情对象里的字段
|
||||||
|
const count = strategyItem.detail_count ?? 0;
|
||||||
|
|
||||||
|
// Tab 标题:策略名 + "(count个)"(isCustomVersion 全局为 true 时,系统策略 Tab 也会拼此后缀)
|
||||||
|
const title = `${strategyDetail.strategy_name}(${count}个)`;
|
||||||
|
|
||||||
|
// 图片:系统策略有 white_img/black_img,自定义策略为空
|
||||||
|
// 保留两个位置(哪怕为空字符串),对应 Vue 端 [white_img, black_img] 不做过滤
|
||||||
|
const imgs: string[] = isSystemPeriod
|
||||||
|
? [strategyDetail.white_img ?? '', strategyDetail.black_img ?? '']
|
||||||
|
: [];
|
||||||
|
|
||||||
|
const strategy: PickStrategy = {
|
||||||
|
id: strategyDetail.id,
|
||||||
|
name: strategyDetail.strategy_name,
|
||||||
|
// 对应 Vue 端 user_question || content:空字符串也会 fallback
|
||||||
|
detail: strategyDetail.user_question || strategyDetail.content || '',
|
||||||
|
// 对应 Vue 端 getDefaultValue(strategy_type, 'custom_strategy')
|
||||||
|
type: strategyDetail.strategy_type || 'custom_strategy',
|
||||||
|
periodType: item.type,
|
||||||
|
// 对应 Vue 端默认值 ['自编'](非空数组不会被替换,因为空数组本身是 truthy)
|
||||||
|
tagList: (strategyDetail.tag_list ?? ['自编']).slice(0, MAX_TAGS),
|
||||||
|
imgs,
|
||||||
|
count,
|
||||||
|
};
|
||||||
|
|
||||||
|
const tabData: PickTabData = {
|
||||||
|
title,
|
||||||
|
strategy,
|
||||||
|
items: allItems.slice(0, MAX_ITEMS),
|
||||||
|
keysList: resData.showKeyList,
|
||||||
|
fieldList: resData.fieldList,
|
||||||
|
};
|
||||||
|
return tabData;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取 AI 选期视图数据(原始网络数据 + 解析为 PickTabData[])
|
||||||
// TODO: 接入真实接口后,改为调用 @ohos.net.http 请求上述 URL,
|
// TODO: 接入真实接口后,改为调用 @ohos.net.http 请求上述 URL,
|
||||||
// 解析 JSON 响应体为 RawPickTabItem[] 返回
|
// 解析 JSON 响应体为 RawPickTabItem[] 后传入 parseRawTab
|
||||||
export function fetchAiPickTabs(): Promise<RawPickTabItem[]> {
|
export function fetchAiPickTabs(): Promise<PickTabData[]> {
|
||||||
return new Promise<RawPickTabItem[]>((resolve) => {
|
return new Promise<PickTabData[]>((resolve) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
resolve(MOCK_RAW_TABS);
|
resolve(MOCK_RAW_TABS.slice(0, MAX_TABS).map(parseRawTab));
|
||||||
}, MOCK_DELAY_MS);
|
}, MOCK_DELAY_MS);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,69 +1,6 @@
|
|||||||
import router from '@ohos.router';
|
import router from '@ohos.router';
|
||||||
import {
|
import { PickTabData } from './AiPickTypes';
|
||||||
PickContractItem,
|
|
||||||
PickStrategy,
|
|
||||||
PickTabData,
|
|
||||||
RawPickTabItem,
|
|
||||||
RawStrategyDetail,
|
|
||||||
RawStrategyItem,
|
|
||||||
} from './AiPickTypes';
|
|
||||||
import { getResData, getTableList, ResData } from './AiPickUtils';
|
|
||||||
import { fetchAiPickTabs } from './AiPickApi';
|
import { fetchAiPickTabs } from './AiPickApi';
|
||||||
import { MAX_ITEMS, MAX_TABS, MAX_TAGS } from './AiPickConstant';
|
|
||||||
|
|
||||||
// 对应 Vue 端 index.vue 中 formatTabData 的单项处理(新版 isCustomVersion=true)
|
|
||||||
function parseRawTab(item: RawPickTabItem): PickTabData {
|
|
||||||
const isSystemPeriod = item.type === 'system_period';
|
|
||||||
const strategyItem: RawStrategyItem = (
|
|
||||||
isSystemPeriod ? item.system_strategy : item.custom_strategy
|
|
||||||
) ?? {};
|
|
||||||
|
|
||||||
const strategyDetail: RawStrategyDetail = (
|
|
||||||
isSystemPeriod
|
|
||||||
? strategyItem.system_strategy
|
|
||||||
: strategyItem.custom_strategy
|
|
||||||
) ?? { id: '', strategy_name: '', strategy_type: '' };
|
|
||||||
|
|
||||||
const resData: ResData = getResData(strategyItem);
|
|
||||||
const allItems: PickContractItem[] = getTableList(resData.list, resData.resultValueList);
|
|
||||||
// count 取自 strategyItem 层级(与 detail_list/field_list 同级),
|
|
||||||
// 对应 Vue 端 strategyItem['detail_count'],不是嵌套的策略详情对象里的字段
|
|
||||||
const count = strategyItem.detail_count ?? 0;
|
|
||||||
|
|
||||||
// Tab 标题:策略名 + "(count个)"(isCustomVersion 全局为 true 时,系统策略 Tab 也会拼此后缀)
|
|
||||||
const title = `${strategyDetail.strategy_name}(${count}个)`;
|
|
||||||
|
|
||||||
// 图片:系统策略有 white_img/black_img,自定义策略为空
|
|
||||||
// 保留两个位置(哪怕为空字符串),对应 Vue 端 [white_img, black_img] 不做过滤
|
|
||||||
const imgs: string[] = isSystemPeriod
|
|
||||||
? [strategyDetail.white_img ?? '', strategyDetail.black_img ?? '']
|
|
||||||
: [];
|
|
||||||
|
|
||||||
const strategy: PickStrategy = {
|
|
||||||
id: strategyDetail.id,
|
|
||||||
name: strategyDetail.strategy_name,
|
|
||||||
// 对应 Vue 端 user_question || content:空字符串也会 fallback
|
|
||||||
detail: strategyDetail.user_question || strategyDetail.content || '',
|
|
||||||
// 对应 Vue 端 getDefaultValue(strategy_type, 'custom_strategy')
|
|
||||||
type: strategyDetail.strategy_type || 'custom_strategy',
|
|
||||||
periodType: item.type,
|
|
||||||
// 对应 Vue 端默认值 ['自编'](非空数组不会被替换,因为空数组本身是 truthy)
|
|
||||||
tagList: (strategyDetail.tag_list ?? ['自编']).slice(0, MAX_TAGS),
|
|
||||||
imgs,
|
|
||||||
count,
|
|
||||||
};
|
|
||||||
|
|
||||||
const tabData: PickTabData = {
|
|
||||||
title,
|
|
||||||
strategy,
|
|
||||||
items: allItems.slice(0, MAX_ITEMS),
|
|
||||||
keysList: resData.showKeyList,
|
|
||||||
fieldList: resData.fieldList,
|
|
||||||
};
|
|
||||||
return tabData;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── Model ─────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
@Observed
|
@Observed
|
||||||
export class AiPickModel {
|
export class AiPickModel {
|
||||||
@@ -73,11 +10,10 @@ export class AiPickModel {
|
|||||||
// 请求是否失败(对应 Vue 端 dataError)
|
// 请求是否失败(对应 Vue 端 dataError)
|
||||||
dataError: boolean = false;
|
dataError: boolean = false;
|
||||||
|
|
||||||
// 拉取数据并解析为视图数据(对应 Vue 端 getData() → formatTabData())
|
// 拉取并解析视图数据
|
||||||
async loadData(): Promise<void> {
|
async loadData(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const rawTabs = await fetchAiPickTabs();
|
this.tabs = await fetchAiPickTabs();
|
||||||
this.tabs = rawTabs.slice(0, MAX_TABS).map(parseRawTab);
|
|
||||||
this.dataError = false;
|
this.dataError = false;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.tabs = [];
|
this.tabs = [];
|
||||||
|
|||||||
Reference in New Issue
Block a user