misc.go 4.8 KB

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