mirror of
https://github.com/coaidev/coai.git
synced 2025-06-05 13:20:15 +09:00
feat: smtp test feature
This commit is contained in:
parent
d278f242a5
commit
d19ce5eb28
@ -23,7 +23,7 @@ import { Separator } from "@/components/ui/separator.tsx";
|
|||||||
import { Badge } from "@/components/ui/badge.tsx";
|
import { Badge } from "@/components/ui/badge.tsx";
|
||||||
import { useEffectAsync } from "@/utils/hook.ts";
|
import { useEffectAsync } from "@/utils/hook.ts";
|
||||||
import { selectAuthenticated } from "@/store/auth.ts";
|
import { selectAuthenticated } from "@/store/auth.ts";
|
||||||
import { deeptrainEndpoint } from "@/utils/env.ts";
|
import { deeptrainEndpoint, useDeeptrain } from "@/utils/env.ts";
|
||||||
|
|
||||||
function PackageDialog() {
|
function PackageDialog() {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@ -33,13 +33,14 @@ function PackageDialog() {
|
|||||||
const teenager = useSelector(teenagerSelector);
|
const teenager = useSelector(teenagerSelector);
|
||||||
const auth = useSelector(selectAuthenticated);
|
const auth = useSelector(selectAuthenticated);
|
||||||
|
|
||||||
useEffectAsync(async () => {
|
useDeeptrain &&
|
||||||
if (!auth) return;
|
useEffectAsync(async () => {
|
||||||
const task = setInterval(() => refreshPackage(dispatch), 20000);
|
if (!auth) return;
|
||||||
await refreshPackage(dispatch);
|
const task = setInterval(() => refreshPackage(dispatch), 20000);
|
||||||
|
await refreshPackage(dispatch);
|
||||||
|
|
||||||
return () => clearInterval(task);
|
return () => clearInterval(task);
|
||||||
}, [auth]);
|
}, [auth]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog open={open} onOpenChange={(open) => dispatch(setDialog(open))}>
|
<Dialog open={open} onOpenChange={(open) => dispatch(setDialog(open))}>
|
||||||
|
@ -464,6 +464,7 @@
|
|||||||
"search": "联网搜索",
|
"search": "联网搜索",
|
||||||
"mail": "SMTP 发件设置",
|
"mail": "SMTP 发件设置",
|
||||||
"save": "保存",
|
"save": "保存",
|
||||||
|
"test": "测试发件",
|
||||||
"backend": "后端域名",
|
"backend": "后端域名",
|
||||||
"backendTip": "后端回调域名(docker 安装默认路径为 /api),接收回调参数。",
|
"backendTip": "后端回调域名(docker 安装默认路径为 /api),接收回调参数。",
|
||||||
"mailHost": "发件域名",
|
"mailHost": "发件域名",
|
||||||
|
@ -13,7 +13,7 @@ import Paragraph, {
|
|||||||
import { Button } from "@/components/ui/button.tsx";
|
import { Button } from "@/components/ui/button.tsx";
|
||||||
import { Label } from "@/components/ui/label.tsx";
|
import { Label } from "@/components/ui/label.tsx";
|
||||||
import { Input } from "@/components/ui/input.tsx";
|
import { Input } from "@/components/ui/input.tsx";
|
||||||
import { useReducer } from "react";
|
import { useReducer, useState } from "react";
|
||||||
import { formReducer } from "@/utils/form.ts";
|
import { formReducer } from "@/utils/form.ts";
|
||||||
import { NumberInput } from "@/components/ui/number-input.tsx";
|
import { NumberInput } from "@/components/ui/number-input.tsx";
|
||||||
import {
|
import {
|
||||||
@ -27,12 +27,22 @@ import {
|
|||||||
} from "@/admin/api/system.ts";
|
} from "@/admin/api/system.ts";
|
||||||
import { useEffectAsync } from "@/utils/hook.ts";
|
import { useEffectAsync } from "@/utils/hook.ts";
|
||||||
import { toastState } from "@/admin/utils.ts";
|
import { toastState } from "@/admin/utils.ts";
|
||||||
import { useToast } from "@/components/ui/use-toast.ts";
|
import { toast, useToast } from "@/components/ui/use-toast.ts";
|
||||||
|
import { doVerify } from "@/api/auth.ts";
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogDescription,
|
||||||
|
DialogFooter,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTrigger,
|
||||||
|
} from "@/components/ui/dialog.tsx";
|
||||||
|
import { DialogTitle } from "@radix-ui/react-dialog";
|
||||||
|
|
||||||
type CompProps<T> = {
|
type CompProps<T> = {
|
||||||
data: T;
|
data: T;
|
||||||
dispatch: (action: any) => void;
|
dispatch: (action: any) => void;
|
||||||
onChange: () => void;
|
onChange: (doToast?: boolean) => Promise<void>;
|
||||||
};
|
};
|
||||||
|
|
||||||
function General({ data, dispatch, onChange }: CompProps<GeneralState>) {
|
function General({ data, dispatch, onChange }: CompProps<GeneralState>) {
|
||||||
@ -72,6 +82,18 @@ function General({ data, dispatch, onChange }: CompProps<GeneralState>) {
|
|||||||
|
|
||||||
function Mail({ data, dispatch, onChange }: CompProps<MailState>) {
|
function Mail({ data, dispatch, onChange }: CompProps<MailState>) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
const [email, setEmail] = useState<string>("");
|
||||||
|
|
||||||
|
const [mailDialog, setMailDialog] = useState<boolean>(false);
|
||||||
|
|
||||||
|
const onTest = async () => {
|
||||||
|
if (!email.trim()) return;
|
||||||
|
await onChange(false);
|
||||||
|
const res = await doVerify(email);
|
||||||
|
toastState(toast, t, res, true);
|
||||||
|
|
||||||
|
if (res.status) setMailDialog(false);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Paragraph
|
<Paragraph
|
||||||
@ -145,6 +167,39 @@ function Mail({ data, dispatch, onChange }: CompProps<MailState>) {
|
|||||||
</ParagraphItem>
|
</ParagraphItem>
|
||||||
<ParagraphFooter>
|
<ParagraphFooter>
|
||||||
<div className={`grow`} />
|
<div className={`grow`} />
|
||||||
|
<Dialog open={mailDialog} onOpenChange={setMailDialog}>
|
||||||
|
<DialogTrigger>
|
||||||
|
<Button variant={`outline`} size={`sm`} loading={true}>
|
||||||
|
{t("admin.system.test")}
|
||||||
|
</Button>
|
||||||
|
</DialogTrigger>
|
||||||
|
<DialogContent>
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>{t("admin.system.test")}</DialogTitle>
|
||||||
|
<DialogDescription className={`pt-2`}>
|
||||||
|
<Input
|
||||||
|
placeholder={t("auth.email-placeholder")}
|
||||||
|
value={email}
|
||||||
|
onChange={(e) => setEmail(e.target.value)}
|
||||||
|
/>
|
||||||
|
</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
<DialogFooter>
|
||||||
|
<Button
|
||||||
|
variant={`outline`}
|
||||||
|
onClick={() => {
|
||||||
|
setEmail("");
|
||||||
|
setMailDialog(false);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{t("admin.cancel")}
|
||||||
|
</Button>
|
||||||
|
<Button variant={`default`} loading={true} onClick={onTest}>
|
||||||
|
{t("admin.confirm")}
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
<Button size={`sm`} loading={true} onClick={onChange}>
|
<Button size={`sm`} loading={true} onClick={onChange}>
|
||||||
{t("admin.system.save")}
|
{t("admin.system.save")}
|
||||||
</Button>
|
</Button>
|
||||||
@ -206,9 +261,10 @@ function System() {
|
|||||||
initialSystemState,
|
initialSystemState,
|
||||||
);
|
);
|
||||||
|
|
||||||
const save = async () => {
|
const doSaving = async (doToast?: boolean) => {
|
||||||
const res = await setConfig(data);
|
const res = await setConfig(data);
|
||||||
toastState(toast, t, res, true);
|
|
||||||
|
if (doToast !== false) toastState(toast, t, res, true);
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffectAsync(async () => {
|
useEffectAsync(async () => {
|
||||||
@ -226,9 +282,9 @@ function System() {
|
|||||||
<CardTitle>{t("admin.settings")}</CardTitle>
|
<CardTitle>{t("admin.settings")}</CardTitle>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent className={`flex flex-col gap-1`}>
|
<CardContent className={`flex flex-col gap-1`}>
|
||||||
<General data={data.general} dispatch={setData} onChange={save} />
|
<General data={data.general} dispatch={setData} onChange={doSaving} />
|
||||||
<Mail data={data.mail} dispatch={setData} onChange={save} />
|
<Mail data={data.mail} dispatch={setData} onChange={doSaving} />
|
||||||
<Search data={data.search} dispatch={setData} onChange={save} />
|
<Search data={data.search} dispatch={setData} onChange={doSaving} />
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
|
12
auth/auth.go
12
auth/auth.go
@ -95,17 +95,15 @@ func Verify(c *gin.Context, email string) error {
|
|||||||
|
|
||||||
provider := channel.SystemInstance.GetMail()
|
provider := channel.SystemInstance.GetMail()
|
||||||
|
|
||||||
template, err := provider.RenderTemplate("code.html", struct {
|
type Temp struct {
|
||||||
Code string
|
Code string `json:"code"`
|
||||||
}{Code: code})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return provider.SendMail(
|
return provider.RenderMail(
|
||||||
|
"code.html",
|
||||||
|
Temp{Code: code},
|
||||||
email,
|
email,
|
||||||
"Chat Nio | OTP Verification",
|
"Chat Nio | OTP Verification",
|
||||||
template,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,6 +28,10 @@ func NewSmtpPoster(host string, port int, username string, password string, from
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SmtpPoster) SendMail(to string, subject string, body string) error {
|
func (s *SmtpPoster) SendMail(to string, subject string, body string) error {
|
||||||
|
if s.Host == "" || s.Port <= 0 || s.Port > 65535 || s.Username == "" || s.Password == "" || s.From == "" {
|
||||||
|
return fmt.Errorf("smtp not configured properly")
|
||||||
|
}
|
||||||
|
|
||||||
addr := fmt.Sprintf("%s:%d", s.Host, s.Port)
|
addr := fmt.Sprintf("%s:%d", s.Host, s.Port)
|
||||||
auth := smtp.PlainAuth("", s.From, s.Password, s.Host)
|
auth := smtp.PlainAuth("", s.From, s.Password, s.Host)
|
||||||
|
|
||||||
@ -54,6 +58,15 @@ func (s *SmtpPoster) RenderTemplate(filename string, data interface{}) (string,
|
|||||||
return buf.String(), nil
|
return buf.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SmtpPoster) RenderMail(filename string, data interface{}, to string, subject string) error {
|
||||||
|
body, err := s.RenderTemplate(filename, data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.SendMail(to, subject, body)
|
||||||
|
}
|
||||||
|
|
||||||
func dial(addr string) (*smtp.Client, error) {
|
func dial(addr string) (*smtp.Client, error) {
|
||||||
conn, err := tls.Dial("tcp", addr, nil)
|
conn, err := tls.Dial("tcp", addr, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user