Merge branch 'upstream/main'

This commit is contained in:
Laisky.Cai
2025-02-03 12:25:00 +00:00
70 changed files with 4032 additions and 2781 deletions

View File

@@ -9,6 +9,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/i18n"
"github.com/songquanpeng/one-api/common/message"
"github.com/songquanpeng/one-api/model"
)
@@ -85,7 +86,7 @@ func SendEmailVerification(c *gin.Context) {
if err := common.Validate.Var(email, "required,email"); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "Invalid parameter",
"message": i18n.Translate(c, "invalid_parameter"),
})
return
}
@@ -114,10 +115,17 @@ func SendEmailVerification(c *gin.Context) {
}
code := common.GenerateVerificationCode(6)
common.RegisterVerificationCodeWithKey(email, code, common.EmailVerificationPurpose)
subject := fmt.Sprintf("%s Email verification email", config.SystemName)
content := fmt.Sprintf("<p>Hello, you are verifying %s email.</p>"+
"<p>Your verification code is: <strong>%s</strong></p>"+
"<p>The verification code is valid within %d minutes. If it is not your operation, please ignore it.</p>", config.SystemName, code, common.VerificationValidMinutes)
subject := fmt.Sprintf("%s 邮箱验证邮件", config.SystemName)
content := message.EmailTemplate(
subject,
fmt.Sprintf(`
<p>Hello!</p>
<p>You are verifying your email for %s.</p>
<p>Your verification code is:</p>
<p style="font-size: 24px; font-weight: bold; color: #333; background-color: #f8f8f8; padding: 10px; text-align: center; border-radius: 4px;">%s</p>
<p style="color: #666;">The verification code is valid for %d minutes. If you did not request this, please ignore.</p>
`, config.SystemName, code, common.VerificationValidMinutes),
)
err := message.SendEmail(subject, email, content)
if err != nil {
c.JSON(http.StatusOK, gin.H{
@@ -138,7 +146,7 @@ func SendPasswordResetEmail(c *gin.Context) {
if err := common.Validate.Var(email, "required,email"); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "Invalid parameter",
"message": i18n.Translate(c, "invalid_parameter"),
})
return
}
@@ -152,16 +160,26 @@ func SendPasswordResetEmail(c *gin.Context) {
code := common.GenerateVerificationCode(0)
common.RegisterVerificationCodeWithKey(email, code, common.PasswordResetPurpose)
link := fmt.Sprintf("%s/user/reset?email=%s&token=%s", config.ServerAddress, email, code)
subject := fmt.Sprintf("%s Password reset", config.SystemName)
content := fmt.Sprintf("<p>Hello, you are resetting %s password.</p>"+
"<p>Click <a href='%s'>here</a> to reset your password.</p>"+
"<p>If the link cannot be clicked, please try clicking the link below or copy it to your browser to open:<br> %s </p>"+
"<p>The reset link is valid within %d minutes. If it is not your operation, please ignore it.</p>", config.SystemName, link, link, common.VerificationValidMinutes)
subject := fmt.Sprintf("%s 密码重置", config.SystemName)
content := message.EmailTemplate(
subject,
fmt.Sprintf(`
<p>Hello!</p>
<p>You are resetting your password for %s.</p>
<p>Please click the button below to reset your password:</p>
<p style="text-align: center; margin: 30px 0;">
<a href="%s" style="background-color: #007bff; color: white; padding: 12px 24px; text-decoration: none; border-radius: 4px; display: inline-block;">Reset Password</a>
</p>
<p style="color: #666;">If the button doesn't work, please copy the following link and paste it into your browser:</p>
<p style="background-color: #f8f8f8; padding: 10px; border-radius: 4px; word-break: break-all;">%s</p>
<p style="color: #666;">The reset link is valid for %d minutes. If you didn't request this, please ignore.</p>
`, config.SystemName, link, link, common.VerificationValidMinutes),
)
err := message.SendEmail(subject, email, content)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
"message": fmt.Sprintf("%s%s", i18n.Translate(c, "send_email_failed"), err.Error()),
})
return
}
@@ -183,7 +201,7 @@ func ResetPassword(c *gin.Context) {
if req.Email == "" || req.Token == "" {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "Invalid parameter",
"message": i18n.Translate(c, "invalid_parameter"),
})
return
}