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:
145
supabase/functions/bazi-analyzer/index.ts
Normal file
145
supabase/functions/bazi-analyzer/index.ts
Normal file
@@ -0,0 +1,145 @@
|
||||
// Supabase Edge Function: Bazi Analyzer
|
||||
// This function analyzes Chinese Four Pillars (Bazi) 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 BaziRequest {
|
||||
birthDate: string
|
||||
birthTime: string
|
||||
gender: 'male' | 'female'
|
||||
location?: string
|
||||
}
|
||||
|
||||
interface BaziResponse {
|
||||
success: boolean
|
||||
data?: {
|
||||
fourPillars: {
|
||||
year: { heavenlyStem: string; earthlyBranch: string }
|
||||
month: { heavenlyStem: string; earthlyBranch: string }
|
||||
day: { heavenlyStem: string; earthlyBranch: string }
|
||||
hour: { heavenlyStem: string; earthlyBranch: string }
|
||||
}
|
||||
elements: {
|
||||
wood: number
|
||||
fire: number
|
||||
earth: number
|
||||
metal: number
|
||||
water: number
|
||||
}
|
||||
analysis: {
|
||||
personality: string
|
||||
career: string
|
||||
health: string
|
||||
relationships: 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 }: BaziRequest = 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 Bazi calculation logic
|
||||
// For now, return mock data
|
||||
const mockAnalysis: BaziResponse = {
|
||||
success: true,
|
||||
data: {
|
||||
fourPillars: {
|
||||
year: { heavenlyStem: '甲', earthlyBranch: '子' },
|
||||
month: { heavenlyStem: '乙', earthlyBranch: '丑' },
|
||||
day: { heavenlyStem: '丙', earthlyBranch: '寅' },
|
||||
hour: { heavenlyStem: '丁', earthlyBranch: '卯' },
|
||||
},
|
||||
elements: {
|
||||
wood: 2,
|
||||
fire: 1,
|
||||
earth: 1,
|
||||
metal: 0,
|
||||
water: 1,
|
||||
},
|
||||
analysis: {
|
||||
personality: '性格温和,具有创造力,善于沟通。',
|
||||
career: '适合从事创意、教育或咨询相关工作。',
|
||||
health: '注意肝胆和心血管健康。',
|
||||
relationships: '人际关系良好,容易获得他人信任。',
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Save analysis to database
|
||||
const { error: insertError } = await supabaseClient
|
||||
.from('analysis_history')
|
||||
.insert({
|
||||
user_id: user.id,
|
||||
analysis_type: 'bazi',
|
||||
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 bazi-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