misc.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "net/http"
  7. "one-api/common"
  8. "one-api/model"
  9. )
  10. func GetStatus(c *gin.Context) {
  11. c.JSON(http.StatusOK, gin.H{
  12. "success": true,
  13. "message": "",
  14. "data": gin.H{
  15. "version": common.Version,
  16. "start_time": common.StartTime,
  17. "email_verification": common.EmailVerificationEnabled,
  18. "github_oauth": common.GitHubOAuthEnabled,
  19. "github_client_id": common.GitHubClientId,
  20. "system_name": common.SystemName,
  21. "footer_html": common.Footer,
  22. "wechat_qrcode": common.WeChatAccountQRCodeImageURL,
  23. "wechat_login": common.WeChatAuthEnabled,
  24. "server_address": common.ServerAddress,
  25. "turnstile_check": common.TurnstileCheckEnabled,
  26. "turnstile_site_key": common.TurnstileSiteKey,
  27. "top_up_link": common.TopUpLink,
  28. },
  29. })
  30. return
  31. }
  32. func GetNotice(c *gin.Context) {
  33. common.OptionMapRWMutex.RLock()
  34. defer common.OptionMapRWMutex.RUnlock()
  35. c.JSON(http.StatusOK, gin.H{
  36. "success": true,
  37. "message": "",
  38. "data": common.OptionMap["Notice"],
  39. })
  40. return
  41. }
  42. func GetAbout(c *gin.Context) {
  43. common.OptionMapRWMutex.RLock()
  44. defer common.OptionMapRWMutex.RUnlock()
  45. c.JSON(http.StatusOK, gin.H{
  46. "success": true,
  47. "message": "",
  48. "data": common.OptionMap["About"],
  49. })
  50. return
  51. }
  52. func SendEmailVerification(c *gin.Context) {
  53. email := c.Query("email")
  54. if err := common.Validate.Var(email, "required,email"); err != nil {
  55. c.JSON(http.StatusOK, gin.H{
  56. "success": false,
  57. "message": "无效的参数",
  58. })
  59. return
  60. }
  61. if model.IsEmailAlreadyTaken(email) {
  62. c.JSON(http.StatusOK, gin.H{
  63. "success": false,
  64. "message": "邮箱地址已被占用",
  65. })
  66. return
  67. }
  68. code := common.GenerateVerificationCode(6)
  69. common.RegisterVerificationCodeWithKey(email, code, common.EmailVerificationPurpose)
  70. subject := fmt.Sprintf("%s邮箱验证邮件", common.SystemName)
  71. content := fmt.Sprintf("<p>您好,你正在进行%s邮箱验证。</p>"+
  72. "<p>您的验证码为: <strong>%s</strong></p>"+
  73. "<p>验证码 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, code, common.VerificationValidMinutes)
  74. err := common.SendEmail(subject, email, content)
  75. if err != nil {
  76. c.JSON(http.StatusOK, gin.H{
  77. "success": false,
  78. "message": err.Error(),
  79. })
  80. return
  81. }
  82. c.JSON(http.StatusOK, gin.H{
  83. "success": true,
  84. "message": "",
  85. })
  86. return
  87. }
  88. func SendPasswordResetEmail(c *gin.Context) {
  89. email := c.Query("email")
  90. if err := common.Validate.Var(email, "required,email"); err != nil {
  91. c.JSON(http.StatusOK, gin.H{
  92. "success": false,
  93. "message": "无效的参数",
  94. })
  95. return
  96. }
  97. if !model.IsEmailAlreadyTaken(email) {
  98. c.JSON(http.StatusOK, gin.H{
  99. "success": false,
  100. "message": "该邮箱地址未注册",
  101. })
  102. return
  103. }
  104. code := common.GenerateVerificationCode(0)
  105. common.RegisterVerificationCodeWithKey(email, code, common.PasswordResetPurpose)
  106. link := fmt.Sprintf("%s/user/reset?email=%s&token=%s", common.ServerAddress, email, code)
  107. subject := fmt.Sprintf("%s密码重置", common.SystemName)
  108. content := fmt.Sprintf("<p>您好,你正在进行%s密码重置。</p>"+
  109. "<p>点击<a href='%s'>此处</a>进行密码重置。</p>"+
  110. "<p>重置链接 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, link, common.VerificationValidMinutes)
  111. err := common.SendEmail(subject, email, content)
  112. if err != nil {
  113. c.JSON(http.StatusOK, gin.H{
  114. "success": false,
  115. "message": err.Error(),
  116. })
  117. return
  118. }
  119. c.JSON(http.StatusOK, gin.H{
  120. "success": true,
  121. "message": "",
  122. })
  123. return
  124. }
  125. type PasswordResetRequest struct {
  126. Email string `json:"email"`
  127. Token string `json:"token"`
  128. }
  129. func ResetPassword(c *gin.Context) {
  130. var req PasswordResetRequest
  131. err := json.NewDecoder(c.Request.Body).Decode(&req)
  132. if req.Email == "" || req.Token == "" {
  133. c.JSON(http.StatusOK, gin.H{
  134. "success": false,
  135. "message": "无效的参数",
  136. })
  137. return
  138. }
  139. if !common.VerifyCodeWithKey(req.Email, req.Token, common.PasswordResetPurpose) {
  140. c.JSON(http.StatusOK, gin.H{
  141. "success": false,
  142. "message": "重置链接非法或已过期",
  143. })
  144. return
  145. }
  146. password := common.GenerateVerificationCode(12)
  147. err = model.ResetUserPasswordByEmail(req.Email, password)
  148. if err != nil {
  149. c.JSON(http.StatusOK, gin.H{
  150. "success": false,
  151. "message": err.Error(),
  152. })
  153. return
  154. }
  155. common.DeleteKey(req.Email, common.PasswordResetPurpose)
  156. c.JSON(http.StatusOK, gin.H{
  157. "success": true,
  158. "message": "",
  159. "data": password,
  160. })
  161. return
  162. }