* 直播插件状态与筛选工具(与 UniApp pages/plugins/live 对齐)
* - 列表展示:status + status_name
* - 自动筛选:live_status(逗号分隔)、live_is_recommended(number,未选不传)
*/
export const LIVE_ROOM_STATUS_LIST = [
{ type: '0', name: '离线' },
{ type: '1', name: '直播中' },
{ type: '2', name: '离开' },
{ type: '3', name: '封禁' },
] as const;
export interface LiveRoomDataLike {
status?: string | number;
live_status?: string | number;
status_name?: string;
online_count?: string | number;
viewer_count?: string | number;
access_count?: string | number;
seller_name?: string;
}
export const get_live_room_status = (data: LiveRoomDataLike | undefined | null): number | null => {
if (!data) return null;
const raw = data.status ?? data.live_status;
if (raw === '' || raw === undefined || raw === null) return null;
const n = Number(raw);
return Number.isNaN(n) ? null : n;
};
export const should_show_live_status = (data: LiveRoomDataLike | undefined | null): boolean => {
return get_live_room_status(data) !== null;
};
export const get_live_room_status_name = (data: LiveRoomDataLike | undefined | null): string => {
if (data?.status_name) return String(data.status_name);
const s = get_live_room_status(data);
if (s === null) return '';
const item = LIVE_ROOM_STATUS_LIST.find((it) => String(it.type) === String(s));
return item?.name ?? '';
};
export const get_live_status_filter_list = (plugins: Record<string, unknown> | undefined) => {
const live = plugins?.live as { status_list?: { type: string; name: string }[] } | undefined;
const list = live?.status_list;
if (Array.isArray(list) && list.length > 0) {
return list;
}
return [...LIVE_ROOM_STATUS_LIST];
};
export const get_live_online_count = (data: LiveRoomDataLike | undefined | null): string | number => {
if (!data) return '0';
return data.online_count ?? data.viewer_count ?? data.access_count ?? '0';
};
const LIVE_IS_RECOMMENDED_FALLBACK = [
{ type: '1', name: '是' },
{ type: '0', name: '否' },
];
export const get_live_is_recommended_list = (plugins: Record<string, unknown> | undefined) => {
const live = plugins?.live as { is_recommended_list?: { type: string; name: string }[] } | undefined;
const list = live?.is_recommended_list;
if (Array.isArray(list) && list.length > 0) {
return list;
}
return [...LIVE_IS_RECOMMENDED_FALLBACK];
};
* 是否推荐转 autolivelist 参数 live_is_recommended(number)
* 未选择时返回 undefined,请求体不包含该字段
*/
export const format_live_is_recommended = (val: string | number | null | undefined): number | undefined => {
if (val === '' || val === null || val === undefined) return undefined;
const n = Number(val);
return Number.isNaN(n) ? undefined : n;
};