Files
----/前端源码/uni-app/api/affinity.ts

305 lines
7.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 缘分合盘 API
*/
// 请求参数类型
export interface AffinityPersonInfo {
name: string;
gender: 'male' | 'female';
birth_date: string; // 显示格式1990年1月1日 子时
birth_date_api: string; // API格式1990-01-01 00:30:00
}
export interface AffinityCalculateRequest {
relationship: 'couple' | 'married' | 'crush' | 'partner' | 'friend' | 'family';
person1: AffinityPersonInfo;
person2: AffinityPersonInfo;
}
// 响应数据类型
export interface AffinitySixDimension {
personality: number; // 性格
emotion: number; // 情感
family: number; // 家庭
wealth: number; // 财运
career: number; // 事业
health: number; // 健康
}
export interface AffinityAnalysisCard {
id: string;
icon: string;
iconBg: string;
title: string;
score: string;
summary: string;
content: string;
}
export interface AffinityPastLife {
description: string;
depth: string;
relationship: string;
}
export interface AffinityMatchItem {
label: string;
match: number;
desc: string;
}
export interface AffinityGuide {
realNeeds: string;
redZone: string;
tips: string[];
}
export interface AffinityMonthlyFortune {
heats: number[];
heatLabels: string[];
highlights: string;
warnings: string;
}
export interface AffinityTimelineItem {
year: string;
title: string;
desc: string;
highlight: boolean;
}
export interface AffinityMasterAdvice {
message: string;
tips: string[];
}
export interface AffinityUnlockedContent {
pastLife: AffinityPastLife;
matchItems: AffinityMatchItem[];
guide: AffinityGuide;
monthlyFortune: AffinityMonthlyFortune;
timeline: AffinityTimelineItem[];
masterAdvice: AffinityMasterAdvice;
}
export interface AffinityCalculateResponse {
relationship: string;
relationshipLabel: string;
person1: {
name: string;
gender: string;
birthDate: string;
};
person2: {
name: string;
gender: string;
birthDate: string;
};
score: number;
scoreBadge: string;
sixDimension: AffinitySixDimension;
radarDesc: string;
analysisCards: AffinityAnalysisCard[];
unlocked: AffinityUnlockedContent;
isUnlocked: boolean;
unlockPrice: number;
unlockStats: {
unlockCount: number;
accuracy: string;
};
}
// 列表查询相关类型
export interface AffinityListRequest {
page_no?: number;
page_size?: number;
}
export interface AffinityListItem {
id: number;
uuid: string;
user_id: number;
relationship: string;
person1_name: string;
person1_gender: string;
person1_birth_date: string;
person1_birth_date_api: string;
person2_name: string;
person2_gender: string;
person2_birth_date: string;
person2_birth_date_api: string;
score: number | null;
score_badge: string | null;
six_dimension: string | null;
radar_desc: string | null;
analysis_cards: string | null;
is_unlocked: number;
unlock_price: number;
unlocked_content: string | null;
task_status: number;
error_message: string | null;
created_time: string;
updated_time: string;
}
export interface AffinityListResponse {
code: number;
msg: string;
data: {
page_no: number;
page_size: number;
total: number;
has_next: boolean;
data: AffinityListItem[];
};
status_code: number;
success: boolean;
}
/**
* 缘分合盘测算
*/
export async function calculateAffinity(params: AffinityCalculateRequest): Promise<AffinityCalculateResponse> {
// 获取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_affinity/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 || '网络请求失败');
}
// 处理认证失败跳转到登录页必须在检查code之前
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 getAffinityList(params: AffinityListRequest = {}): Promise<AffinityListResponse | 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_affinity/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 getAffinityDetail(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_affinity/${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 affinityApi = {
calculateAffinity,
getAffinityList,
getAffinityDetail,
};