one-api/controller/misc.go

195 lines
5.3 KiB
Go
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controller
import (
"encoding/json"
"fmt"
"net/http"
"one-api/common"
"one-api/model"
"github.com/gin-gonic/gin"
)
func GetStatus(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": gin.H{
"version": common.Version,
"start_time": common.StartTime,
"email_verification": common.EmailVerificationEnabled,
"github_oauth": common.GitHubOAuthEnabled,
"github_client_id": common.GitHubClientId,
"system_name": common.SystemName,
"logo": common.Logo,
"footer_html": common.Footer,
"wechat_qrcode": common.WeChatAccountQRCodeImageURL,
"wechat_login": common.WeChatAuthEnabled,
"server_address": common.ServerAddress,
"turnstile_check": common.TurnstileCheckEnabled,
"turnstile_site_key": common.TurnstileSiteKey,
"top_up_link": common.TopUpLink,
"chat_link": common.ChatLink,
"quota_per_unit": common.QuotaPerUnit,
"display_in_currency": common.DisplayInCurrencyEnabled,
},
})
return
}
func GetNotice(c *gin.Context) {
common.OptionMapRWMutex.RLock()
defer common.OptionMapRWMutex.RUnlock()
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": common.OptionMap["Notice"],
})
return
}
func GetAbout(c *gin.Context) {
common.OptionMapRWMutex.RLock()
defer common.OptionMapRWMutex.RUnlock()
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": common.OptionMap["About"],
})
return
}
func GetHomePageContent(c *gin.Context) {
common.OptionMapRWMutex.RLock()
defer common.OptionMapRWMutex.RUnlock()
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": common.OptionMap["HomePageContent"],
})
return
}
func SendEmailVerification(c *gin.Context, bypassRegisterEnabledCheck bool) {
email := c.Query("email")
if err := common.Validate.Var(email, "required,email"); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无效的å<E2809E>æ•°",
})
return
}
if model.IsEmailAlreadyTaken(email) {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": ®ç®±åœ°å<C2B0>€å·²è¢«å<C2AB> ç”¨",
})
return
}
if !common.RegisterEnabled && !bypassRegisterEnabledCheck {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "管ç<C2A1>†å˜å…³é—­äº†æ°ç”¨æˆ·æ³¨å†Œ",
})
return
}
code := common.GenerateVerificationCode(6)
common.RegisterVerificationCodeWithKey(email, code, common.EmailVerificationPurpose)
subject := fmt.Sprintf("%sé®ç®±éªŒè¯<C3A8>é®ä»¶", common.SystemName)
content := fmt.Sprintf("<p>您好,你正在进行%sé®ç®±éªŒè¯<C3A8>ã€</p>"+
"<p>æ¨çš„验è¯<C3A8>ç <C3A7>为: <strong>%s</strong></p>"+
"<p>验è¯<C3A8>ç <C3A7> %d 分éŸå†…æœ‰æ•ˆï¼Œå¦æžœä¸<C3A4>是本人æ“<C3A6>作,请忽略ã€</p>", common.SystemName, code, common.VerificationValidMinutes)
err := common.SendEmail(subject, email, content)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
})
return
}
func SendPasswordResetEmail(c *gin.Context) {
email := c.Query("email")
if err := common.Validate.Var(email, "required,email"); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无效的å<E2809E>æ•°",
})
return
}
if !model.IsEmailAlreadyTaken(email) {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "该é®ç®±åœ°å<C2B0>€æœªæ³¨å†Œ",
})
return
}
code := common.GenerateVerificationCode(0)
common.RegisterVerificationCodeWithKey(email, code, common.PasswordResetPurpose)
link := fmt.Sprintf("%s/user/reset?email=%s&token=%s", common.ServerAddress, email, code)
subject := fmt.Sprintf("%s密ç <C3A7>é‡<C3A9>ç½®", common.SystemName)
content := fmt.Sprintf("<p>您好,你正在进行%s密ç <C3A7>é‡<C3A9>ç½®ã€</p>"+
"<p>点击 <a href='%s'>此处</a> è¿è¡Œå¯†ç <C3A7>é‡<C3A9>ç½®ã€</p>"+
"<p>妿žœé“¾æŽ¥æ— æ³•ç¹å‡»ï¼Œè¯·å°<C3A5>试ç¹å‡»ä¸é<E280B9>¢çš„链接æˆå°†å…¶å¤<C3A5>制到æµ<C3A6>览器中打开:<br> %s </p>"+
"<p>é‡<C3A9>置链接 %d 分éŸå†…æœ‰æ•ˆï¼Œå¦æžœä¸<C3A4>是本人æ“<C3A6>作,请忽略ã€</p>", common.SystemName, link, link, common.VerificationValidMinutes)
err := common.SendEmail(subject, email, content)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
})
return
}
type PasswordResetRequest struct {
Email string `json:"email"`
Token string `json:"token"`
}
func ResetPassword(c *gin.Context) {
var req PasswordResetRequest
err := json.NewDecoder(c.Request.Body).Decode(&req)
if req.Email == "" || req.Token == "" {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无效的å<E2809E>æ•°",
})
return
}
if !common.VerifyCodeWithKey(req.Email, req.Token, common.PasswordResetPurpose) {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "é‡<C3A9>置链接é<C2A5>žæ³•æˆå·²è¿‡æœŸ",
})
return
}
password := common.GenerateVerificationCode(12)
err = model.ResetUserPasswordByEmail(req.Email, password)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
common.DeleteKey(req.Email, common.PasswordResetPurpose)
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": password,
})
return
}