zodiac-api.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * 属相测算 API 调用版本
  3. * 建议接入第三方专业API或自建专业数据库
  4. */
  5. import api from './api.js'
  6. /**
  7. * 方案一:接入第三方专业命理API
  8. * 推荐API提供商:
  9. * 1. 聚合数据 https://www.juhe.cn/ (有生肖运势、星座等API)
  10. * 2. 阿凡提 https://www.avatardata.cn/ (命理测算API)
  11. * 3. 天行数据 https://www.tianapi.com/ (运势查询API)
  12. */
  13. /**
  14. * 获取今日运势(接入专业API)
  15. */
  16. export async function getTodayFortuneFromAPI(zodiac, birthday) {
  17. try {
  18. // 示例:调用第三方API
  19. const response = await uni.request({
  20. url: 'https://api.example.com/fortune/daily',
  21. method: 'GET',
  22. data: {
  23. zodiac: zodiac,
  24. date: new Date().toISOString().split('T')[0],
  25. apiKey: 'YOUR_API_KEY'
  26. }
  27. })
  28. if (response.data.code === 200) {
  29. return {
  30. overall: response.data.result.level,
  31. love: response.data.result.love_score,
  32. career: response.data.result.career_score,
  33. wealth: response.data.result.wealth_score,
  34. health: response.data.result.health_score,
  35. tips: response.data.result.suggestion,
  36. luckyColor: response.data.result.lucky_color,
  37. luckyNumber: response.data.result.lucky_number,
  38. source: 'professional_api' // 标记数据来源
  39. }
  40. }
  41. } catch (error) {
  42. console.error('获取运势失败:', error)
  43. // 降级到本地数据
  44. return getLocalFortune(zodiac)
  45. }
  46. }
  47. /**
  48. * 方案二:后端自建专业数据库
  49. * 聘请专业命理师录入数据
  50. */
  51. export async function getFortuneFromBackend(zodiac, birthday) {
  52. try {
  53. const response = await api.astrology.getDailyFortune({
  54. zodiac: zodiac,
  55. birthday: birthday,
  56. date: new Date().toISOString().split('T')[0]
  57. })
  58. return {
  59. ...response.data,
  60. source: 'expert_database', // 专家数据库
  61. expertInfo: {
  62. name: response.data.expert_name,
  63. title: response.data.expert_title,
  64. certified: true
  65. }
  66. }
  67. } catch (error) {
  68. console.error('获取运势失败:', error)
  69. return getLocalFortune(zodiac)
  70. }
  71. }
  72. /**
  73. * 本地降级数据(当API失败时使用)
  74. */
  75. function getLocalFortune(zodiac) {
  76. // 基础数据,标记为娱乐性质
  77. return {
  78. overall: '中吉',
  79. love: 75,
  80. career: 80,
  81. wealth: 70,
  82. health: 85,
  83. tips: '今日运势仅供参考',
  84. source: 'local_fallback',
  85. disclaimer: '此数据仅供娱乐,不作为决策依据'
  86. }
  87. }
  88. /**
  89. * 方案三:结合多个数据源
  90. * 提高准确性和可信度
  91. */
  92. export async function getComprehensiveFortune(zodiac, birthday) {
  93. try {
  94. // 1. 获取专业API数据
  95. const apiData = await getTodayFortuneFromAPI(zodiac, birthday)
  96. // 2. 获取后端专家数据
  97. const expertData = await getFortuneFromBackend(zodiac, birthday)
  98. // 3. 综合计算
  99. return {
  100. love: Math.round((apiData.love + expertData.love) / 2),
  101. career: Math.round((apiData.career + expertData.career) / 2),
  102. wealth: Math.round((apiData.wealth + expertData.wealth) / 2),
  103. health: Math.round((apiData.health + expertData.health) / 2),
  104. tips: expertData.tips, // 优先使用专家建议
  105. dataSources: ['专业API', '命理专家'],
  106. reliability: 'high' // 可信度标记
  107. }
  108. } catch (error) {
  109. return getLocalFortune(zodiac)
  110. }
  111. }
  112. export default {
  113. getTodayFortuneFromAPI,
  114. getFortuneFromBackend,
  115. getComprehensiveFortune
  116. }