mirror of
https://github.com/patdelphi/suanming.git
synced 2026-02-28 05:33:11 +08:00
feat: 添加Supabase本地开发环境配置和Edge Functions
This commit is contained in:
155
supabase/functions/ziwei-analyzer/index.ts
Normal file
155
supabase/functions/ziwei-analyzer/index.ts
Normal file
@@ -0,0 +1,155 @@
|
||||
// Supabase Edge Function: Ziwei Analyzer
|
||||
// This function analyzes Ziwei Doushu (Purple Star Astrology)
|
||||
|
||||
import { serve } from 'https://deno.land/std@0.168.0/http/server.ts'
|
||||
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
|
||||
|
||||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
||||
}
|
||||
|
||||
interface ZiweiRequest {
|
||||
birthDate: string
|
||||
birthTime: string
|
||||
gender: 'male' | 'female'
|
||||
location?: string
|
||||
}
|
||||
|
||||
interface ZiweiResponse {
|
||||
success: boolean
|
||||
data?: {
|
||||
palaces: {
|
||||
ming: { position: string; stars: string[] }
|
||||
xiong: { position: string; stars: string[] }
|
||||
cai: { position: string; stars: string[] }
|
||||
guan: { position: string; stars: string[] }
|
||||
tian: { position: string; stars: string[] }
|
||||
fu: { position: string; stars: string[] }
|
||||
zi: { position: string; stars: string[] }
|
||||
nu: { position: string; stars: string[] }
|
||||
qian: { position: string; stars: string[] }
|
||||
ji: { position: string; stars: string[] }
|
||||
tian2: { position: string; stars: string[] }
|
||||
xiang: { position: string; stars: string[] }
|
||||
}
|
||||
mainStars: string[]
|
||||
luckyStars: string[]
|
||||
unluckyStars: string[]
|
||||
analysis: {
|
||||
personality: string
|
||||
career: string
|
||||
wealth: string
|
||||
relationships: string
|
||||
health: string
|
||||
}
|
||||
}
|
||||
error?: string
|
||||
}
|
||||
|
||||
serve(async (req) => {
|
||||
// Handle CORS preflight requests
|
||||
if (req.method === 'OPTIONS') {
|
||||
return new Response('ok', { headers: corsHeaders })
|
||||
}
|
||||
|
||||
try {
|
||||
// Initialize Supabase client
|
||||
const supabaseClient = createClient(
|
||||
Deno.env.get('SUPABASE_URL') ?? '',
|
||||
Deno.env.get('SUPABASE_ANON_KEY') ?? '',
|
||||
{
|
||||
global: {
|
||||
headers: { Authorization: req.headers.get('Authorization')! },
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// Get the current user
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabaseClient.auth.getUser()
|
||||
|
||||
if (!user) {
|
||||
return new Response(
|
||||
JSON.stringify({ success: false, error: 'Unauthorized' }),
|
||||
{
|
||||
status: 401,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// Parse request body
|
||||
const { birthDate, birthTime, gender, location }: ZiweiRequest = await req.json()
|
||||
|
||||
if (!birthDate || !birthTime || !gender) {
|
||||
return new Response(
|
||||
JSON.stringify({ success: false, error: 'Missing required fields' }),
|
||||
{
|
||||
status: 400,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// TODO: Implement actual Ziwei calculation logic
|
||||
// For now, return mock data
|
||||
const mockAnalysis: ZiweiResponse = {
|
||||
success: true,
|
||||
data: {
|
||||
palaces: {
|
||||
ming: { position: '子', stars: ['紫微', '天府'] },
|
||||
xiong: { position: '丑', stars: ['太阳', '巨门'] },
|
||||
cai: { position: '寅', stars: ['天机', '太阴'] },
|
||||
guan: { position: '卯', stars: ['天同', '天梁'] },
|
||||
tian: { position: '辰', stars: ['七杀'] },
|
||||
fu: { position: '巳', stars: ['破军'] },
|
||||
zi: { position: '午', stars: ['廉贞', '贪狼'] },
|
||||
nu: { position: '未', stars: ['天相'] },
|
||||
qian: { position: '申', stars: ['天马'] },
|
||||
ji: { position: '酉', stars: ['文昌'] },
|
||||
tian2: { position: '戌', stars: ['文曲'] },
|
||||
xiang: { position: '亥', stars: ['左辅', '右弼'] },
|
||||
},
|
||||
mainStars: ['紫微', '天府', '太阳', '巨门', '天机', '太阴'],
|
||||
luckyStars: ['文昌', '文曲', '左辅', '右弼', '天马'],
|
||||
unluckyStars: ['擎羊', '陀罗', '火星', '铃星'],
|
||||
analysis: {
|
||||
personality: '性格高贵,具有领导才能,喜欢掌控全局。',
|
||||
career: '适合从事管理、政治或高端服务业。',
|
||||
wealth: '财运稳定,有贵人相助,投资需谨慎。',
|
||||
relationships: '人际关系复杂,需要平衡各方利益。',
|
||||
health: '注意心脏和血压问题,保持规律作息。',
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Save analysis to database
|
||||
const { error: insertError } = await supabaseClient
|
||||
.from('analysis_history')
|
||||
.insert({
|
||||
user_id: user.id,
|
||||
analysis_type: 'ziwei',
|
||||
input_data: { birthDate, birthTime, gender, location },
|
||||
result_data: mockAnalysis.data,
|
||||
})
|
||||
|
||||
if (insertError) {
|
||||
console.error('Error saving analysis:', insertError)
|
||||
}
|
||||
|
||||
return new Response(JSON.stringify(mockAnalysis), {
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error in ziwei-analyzer:', error)
|
||||
return new Response(
|
||||
JSON.stringify({ success: false, error: 'Internal server error' }),
|
||||
{
|
||||
status: 500,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
}
|
||||
)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user