misc.go 4.8 KB

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