misc.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 GetHomePageContent(c *gin.Context) {
  53. common.OptionMapRWMutex.RLock()
  54. defer common.OptionMapRWMutex.RUnlock()
  55. c.JSON(http.StatusOK, gin.H{
  56. "success": true,
  57. "message": "",
  58. "data": common.OptionMap["HomePageContent"],
  59. })
  60. return
  61. }
  62. func SendEmailVerification(c *gin.Context) {
  63. email := c.Query("email")
  64. if err := common.Validate.Var(email, "required,email"); err != nil {
  65. c.JSON(http.StatusOK, gin.H{
  66. "success": false,
  67. "message": "无效的参数",
  68. })
  69. return
  70. }
  71. if model.IsEmailAlreadyTaken(email) {
  72. c.JSON(http.StatusOK, gin.H{
  73. "success": false,
  74. "message": "邮箱地址已被占用",
  75. })
  76. return
  77. }
  78. code := common.GenerateVerificationCode(6)
  79. common.RegisterVerificationCodeWithKey(email, code, common.EmailVerificationPurpose)
  80. subject := fmt.Sprintf("%s邮箱验证邮件", common.SystemName)
  81. content := fmt.Sprintf("<p>您好,你正在进行%s邮箱验证。</p>"+
  82. "<p>您的验证码为: <strong>%s</strong></p>"+
  83. "<p>验证码 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, code, common.VerificationValidMinutes)
  84. err := common.SendEmail(subject, email, content)
  85. if err != nil {
  86. c.JSON(http.StatusOK, gin.H{
  87. "success": false,
  88. "message": err.Error(),
  89. })
  90. return
  91. }
  92. c.JSON(http.StatusOK, gin.H{
  93. "success": true,
  94. "message": "",
  95. })
  96. return
  97. }
  98. func SendPasswordResetEmail(c *gin.Context) {
  99. email := c.Query("email")
  100. if err := common.Validate.Var(email, "required,email"); err != nil {
  101. c.JSON(http.StatusOK, gin.H{
  102. "success": false,
  103. "message": "无效的参数",
  104. })
  105. return
  106. }
  107. if !model.IsEmailAlreadyTaken(email) {
  108. c.JSON(http.StatusOK, gin.H{
  109. "success": false,
  110. "message": "该邮箱地址未注册",
  111. })
  112. return
  113. }
  114. code := common.GenerateVerificationCode(0)
  115. common.RegisterVerificationCodeWithKey(email, code, common.PasswordResetPurpose)
  116. link := fmt.Sprintf("%s/user/reset?email=%s&token=%s", common.ServerAddress, email, code)
  117. subject := fmt.Sprintf("%s密码重置", common.SystemName)
  118. content := fmt.Sprintf("<p>您好,你正在进行%s密码重置。</p>"+
  119. "<p>点击<a href='%s'>此处</a>进行密码重置。</p>"+
  120. "<p>重置链接 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, link, common.VerificationValidMinutes)
  121. err := common.SendEmail(subject, email, content)
  122. if err != nil {
  123. c.JSON(http.StatusOK, gin.H{
  124. "success": false,
  125. "message": err.Error(),
  126. })
  127. return
  128. }
  129. c.JSON(http.StatusOK, gin.H{
  130. "success": true,
  131. "message": "",
  132. })
  133. return
  134. }
  135. type PasswordResetRequest struct {
  136. Email string `json:"email"`
  137. Token string `json:"token"`
  138. }
  139. func ResetPassword(c *gin.Context) {
  140. var req PasswordResetRequest
  141. err := json.NewDecoder(c.Request.Body).Decode(&req)
  142. if req.Email == "" || req.Token == "" {
  143. c.JSON(http.StatusOK, gin.H{
  144. "success": false,
  145. "message": "无效的参数",
  146. })
  147. return
  148. }
  149. if !common.VerifyCodeWithKey(req.Email, req.Token, common.PasswordResetPurpose) {
  150. c.JSON(http.StatusOK, gin.H{
  151. "success": false,
  152. "message": "重置链接非法或已过期",
  153. })
  154. return
  155. }
  156. password := common.GenerateVerificationCode(12)
  157. err = model.ResetUserPasswordByEmail(req.Email, password)
  158. if err != nil {
  159. c.JSON(http.StatusOK, gin.H{
  160. "success": false,
  161. "message": err.Error(),
  162. })
  163. return
  164. }
  165. common.DeleteKey(req.Email, common.PasswordResetPurpose)
  166. c.JSON(http.StatusOK, gin.H{
  167. "success": true,
  168. "message": "",
  169. "data": password,
  170. })
  171. return
  172. }