mirror of
https://github.com/beilunyang/moemail.git
synced 2026-08-28 19:36:38 +08:00
fix: Resolve issue with email domain environment variable not working
This commit is contained in:
@@ -0,0 +1,25 @@
|
|||||||
|
import { EMAIL_CONFIG } from "@/config"
|
||||||
|
import { NextResponse } from "next/server"
|
||||||
|
|
||||||
|
export const runtime = "edge"
|
||||||
|
|
||||||
|
export async function GET() {
|
||||||
|
try {
|
||||||
|
const domains = EMAIL_CONFIG.DOMAINS
|
||||||
|
|
||||||
|
if (domains.length === 0) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "无效的域名列表" },
|
||||||
|
{ status: 400 }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({ domains })
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to fetch domains:', error)
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "获取域名列表失败" },
|
||||||
|
{ status: 500 }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import { useState } from "react"
|
import { useEffect, useState } from "react"
|
||||||
import { Button } from "@/components/ui/button"
|
import { Button } from "@/components/ui/button"
|
||||||
import { Input } from "@/components/ui/input"
|
import { Input } from "@/components/ui/input"
|
||||||
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"
|
||||||
@@ -11,17 +11,21 @@ import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
|
|||||||
import { Label } from "@/components/ui/label"
|
import { Label } from "@/components/ui/label"
|
||||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
|
||||||
import { EXPIRY_OPTIONS } from "@/types/email"
|
import { EXPIRY_OPTIONS } from "@/types/email"
|
||||||
import { EMAIL_CONFIG } from "@/config"
|
|
||||||
|
|
||||||
interface CreateDialogProps {
|
interface CreateDialogProps {
|
||||||
onEmailCreated: () => void
|
onEmailCreated: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface DomainResponse {
|
||||||
|
domains: string[]
|
||||||
|
}
|
||||||
|
|
||||||
export function CreateDialog({ onEmailCreated }: CreateDialogProps) {
|
export function CreateDialog({ onEmailCreated }: CreateDialogProps) {
|
||||||
const [open, setOpen] = useState(false)
|
const [open, setOpen] = useState(false)
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
const [emailName, setEmailName] = useState("")
|
const [emailName, setEmailName] = useState("")
|
||||||
const [domain, setDomain] = useState(EMAIL_CONFIG.DOMAINS[0])
|
const [domains, setDomains] = useState<string[]>([])
|
||||||
|
const [currentDomain, setCurrentDomain] = useState("")
|
||||||
const [expiryTime, setExpiryTime] = useState(EXPIRY_OPTIONS[1].value.toString())
|
const [expiryTime, setExpiryTime] = useState(EXPIRY_OPTIONS[1].value.toString())
|
||||||
const { toast } = useToast()
|
const { toast } = useToast()
|
||||||
|
|
||||||
@@ -42,13 +46,13 @@ export function CreateDialog({ onEmailCreated }: CreateDialogProps) {
|
|||||||
const response = await fetch("/api/emails/generate", {
|
const response = await fetch("/api/emails/generate", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
name: emailName,
|
name: emailName,
|
||||||
domain,
|
domain: currentDomain,
|
||||||
expiryTime: parseInt(expiryTime)
|
expiryTime: parseInt(expiryTime)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const data = await response.json()
|
const data = await response.json()
|
||||||
toast({
|
toast({
|
||||||
@@ -77,6 +81,17 @@ export function CreateDialog({ onEmailCreated }: CreateDialogProps) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fetchDomains = async () => {
|
||||||
|
const response = await fetch("/api/emails/domains");
|
||||||
|
const data = (await response.json()) as DomainResponse;
|
||||||
|
setDomains(data.domains);
|
||||||
|
setCurrentDomain(data.domains[0]);
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fetchDomains()
|
||||||
|
}, [])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog open={open} onOpenChange={setOpen}>
|
<Dialog open={open} onOpenChange={setOpen}>
|
||||||
<DialogTrigger asChild>
|
<DialogTrigger asChild>
|
||||||
@@ -97,13 +112,13 @@ export function CreateDialog({ onEmailCreated }: CreateDialogProps) {
|
|||||||
placeholder="输入邮箱名"
|
placeholder="输入邮箱名"
|
||||||
className="flex-1"
|
className="flex-1"
|
||||||
/>
|
/>
|
||||||
{EMAIL_CONFIG.DOMAINS.length > 1 && (
|
{domains.length > 1 && (
|
||||||
<Select value={domain} onValueChange={setDomain}>
|
<Select value={currentDomain} onValueChange={setCurrentDomain}>
|
||||||
<SelectTrigger className="w-[180px]">
|
<SelectTrigger className="w-[180px]">
|
||||||
<SelectValue />
|
<SelectValue />
|
||||||
</SelectTrigger>
|
</SelectTrigger>
|
||||||
<SelectContent>
|
<SelectContent>
|
||||||
{EMAIL_CONFIG.DOMAINS.map(d => (
|
{domains.map(d => (
|
||||||
<SelectItem key={d} value={d}>@{d}</SelectItem>
|
<SelectItem key={d} value={d}>@{d}</SelectItem>
|
||||||
))}
|
))}
|
||||||
</SelectContent>
|
</SelectContent>
|
||||||
@@ -118,7 +133,7 @@ export function CreateDialog({ onEmailCreated }: CreateDialogProps) {
|
|||||||
<RefreshCw className="w-4 h-4" />
|
<RefreshCw className="w-4 h-4" />
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex items-center gap-4">
|
<div className="flex items-center gap-4">
|
||||||
<Label className="shrink-0 text-muted-foreground">过期时间</Label>
|
<Label className="shrink-0 text-muted-foreground">过期时间</Label>
|
||||||
<RadioGroup
|
<RadioGroup
|
||||||
@@ -138,7 +153,7 @@ export function CreateDialog({ onEmailCreated }: CreateDialogProps) {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="text-sm text-gray-500">
|
<div className="text-sm text-gray-500">
|
||||||
完整邮箱地址将为: {emailName ? `${emailName}@${domain}` : "..."}
|
完整邮箱地址将为: {emailName ? `${emailName}@${currentDomain}` : "..."}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex justify-end gap-2">
|
<div className="flex justify-end gap-2">
|
||||||
|
|||||||
Reference in New Issue
Block a user