222 lines
6.2 KiB
TypeScript
222 lines
6.2 KiB
TypeScript
/**
|
||
* 八字择吉 API
|
||
*/
|
||
|
||
// 请求参数类型
|
||
export interface BaziZejiCalculateRequest {
|
||
name: string;
|
||
gender: 'male' | 'female';
|
||
birth_date: string; // 显示格式:1999年11月15日 子时
|
||
birth_date_api: string; // API格式:1999-11-15 00:30:00
|
||
birth_place: string; // 出生地
|
||
zeji_type: 'wedding' | 'business' | 'move' | 'travel' | 'investment' | 'surgery' | 'contract' | 'other';
|
||
zeji_purpose: string; // 择吉目的描述
|
||
date_range_start: string; // 期望日期范围开始 YYYY-MM-DD
|
||
date_range_end: string; // 期望日期范围结束 YYYY-MM-DD
|
||
}
|
||
|
||
// 响应数据类型
|
||
export interface BaziZejiDate {
|
||
date: string; // 日期 YYYY-MM-DD
|
||
lunar: string; // 农历日期
|
||
desc: string; // 日期描述
|
||
score: number; // 吉运指数
|
||
hours: string; // 吉时
|
||
clash: string; // 冲煞
|
||
suitable: string[]; // 宜
|
||
avoid: string[]; // 忌
|
||
}
|
||
|
||
export interface BaziZejiCalculateResponse {
|
||
name: string;
|
||
gender: string;
|
||
birth_date: string;
|
||
birth_place: string;
|
||
zeji_type: string;
|
||
zeji_purpose: string;
|
||
date_range_start: string;
|
||
date_range_end: string;
|
||
advice: string; // 择吉建议
|
||
dates: BaziZejiDate[]; // 吉日列表
|
||
}
|
||
|
||
// 列表查询相关类型
|
||
export interface BaziZejiListRequest {
|
||
page_no?: number;
|
||
page_size?: number;
|
||
}
|
||
|
||
export interface BaziZejiListItem {
|
||
id: number;
|
||
name: string;
|
||
gender: string;
|
||
birth_date: string;
|
||
zeji_type: string;
|
||
zeji_type_label: string;
|
||
zeji_purpose: string;
|
||
date_range_start: string;
|
||
date_range_end: string;
|
||
createdAt: string;
|
||
}
|
||
|
||
export interface BaziZejiListResponse {
|
||
code: number;
|
||
msg: string;
|
||
data: {
|
||
page_no: number;
|
||
page_size: number;
|
||
total: number;
|
||
has_next: boolean;
|
||
data: BaziZejiListItem[];
|
||
};
|
||
status_code: number;
|
||
success: boolean;
|
||
}
|
||
|
||
/**
|
||
* 八字择吉测算
|
||
*/
|
||
export async function calculateBaziZeji(params: BaziZejiCalculateRequest): Promise<BaziZejiCalculateResponse> {
|
||
// 获取token(避免JSON解析错误)
|
||
let token = '';
|
||
try {
|
||
// 尝试从localStorage获取(Web环境)
|
||
if (typeof localStorage !== 'undefined') {
|
||
token = localStorage.getItem('token') || '';
|
||
// 如果token是JSON字符串,解析它
|
||
if (token.startsWith('"') && token.endsWith('"')) {
|
||
token = JSON.parse(token);
|
||
}
|
||
} else {
|
||
// uni-app环境,直接获取字符串
|
||
token = uni.getStorageSync('token') || '';
|
||
}
|
||
} catch (e) {
|
||
console.error('获取token失败:', e);
|
||
}
|
||
|
||
const response = await uni.request({
|
||
url: 'https://yifan.action-ai.cn/api/v1/yifan/yifan_bazi_zeji/calculate',
|
||
method: 'POST',
|
||
header: {
|
||
'Content-Type': 'application/json',
|
||
'Authorization': token ? `Bearer ${token}` : '',
|
||
},
|
||
data: params,
|
||
});
|
||
|
||
if (response.status_code !== 200) {
|
||
throw new Error(response.data?.msg || '网络请求失败');
|
||
}
|
||
|
||
// 处理认证失败,跳转到登录页
|
||
if (response.data?.msg === '认证失败,请登录后再试') {
|
||
uni.showToast({
|
||
title: '请先登录',
|
||
icon: 'none',
|
||
duration: 2000,
|
||
});
|
||
|
||
setTimeout(() => {
|
||
uni.reLaunch({
|
||
url: '/pages/login/login',
|
||
});
|
||
}, 2000);
|
||
|
||
throw new Error(response.data.msg);
|
||
}
|
||
|
||
if (response.data.code !== 200) {
|
||
throw new Error(response.data.msg || '测算失败');
|
||
}
|
||
|
||
return response.data.data;
|
||
}
|
||
|
||
/**
|
||
* 查询八字择吉列表
|
||
*/
|
||
export async function getBaziZejiList(params: BaziZejiListRequest = {}): Promise<BaziZejiListResponse | null> {
|
||
// 获取token(避免JSON解析错误)
|
||
let token = '';
|
||
if (typeof localStorage !== 'undefined') {
|
||
token = localStorage.getItem('token') || '';
|
||
// 如果token是JSON字符串,解析它
|
||
if (token.startsWith('"') && token.endsWith('"')) {
|
||
token = token.slice(1, -1);
|
||
}
|
||
} else {
|
||
// uni-app环境,直接获取字符串
|
||
token = uni.getStorageSync('token') || '';
|
||
}
|
||
|
||
const { page_no = 1, page_size = 10 } = params;
|
||
|
||
const response = await uni.request({
|
||
url: `https://yifan.action-ai.cn/api/v1/yifan/yifan_bazi_zeji/list?page_no=${page_no}&page_size=${page_size}`,
|
||
method: 'GET',
|
||
header: {
|
||
'Content-Type': 'application/json',
|
||
'Authorization': token ? `Bearer ${token}` : '',
|
||
},
|
||
});
|
||
|
||
if (response.data?.msg === '认证失败,请登录后再试') {
|
||
uni.showToast({ title: '请先登录', icon: 'none', duration: 2000 });
|
||
setTimeout(() => {
|
||
uni.reLaunch({ url: '/pages/login/login' });
|
||
}, 2000);
|
||
return null;
|
||
}
|
||
|
||
if (response.data.code == 0) {
|
||
return response.data;
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 根据id获取八字择吉详情
|
||
*/
|
||
export async function getBaziZejiDetail(id: number) {
|
||
let token = '';
|
||
if (typeof localStorage !== 'undefined') {
|
||
token = localStorage.getItem('token') || '';
|
||
if (token.startsWith('"') && token.endsWith('"')) {
|
||
token = token.slice(1, -1);
|
||
}
|
||
} else {
|
||
token = uni.getStorageSync('token') || '';
|
||
}
|
||
|
||
const response = await uni.request({
|
||
url: `https://yifan.action-ai.cn/api/v1/yifan/yifan_bazi_zeji/${id}`,
|
||
method: 'GET',
|
||
header: {
|
||
'Content-Type': 'application/json',
|
||
'Authorization': token ? `Bearer ${token}` : '',
|
||
},
|
||
});
|
||
|
||
if (response.data?.msg === '认证失败,请登录后再试') {
|
||
uni.showToast({ title: '请先登录', icon: 'none', duration: 2000 });
|
||
setTimeout(() => {
|
||
uni.reLaunch({ url: '/pages/login/login' });
|
||
}, 2000);
|
||
return null;
|
||
}
|
||
|
||
if (response.data.code == 0) {
|
||
return response.data.data;
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
export const baziZejiApi = {
|
||
calculateBaziZeji,
|
||
getBaziZejiList,
|
||
getBaziZejiDetail,
|
||
};
|