misc.go 5.1 KB

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