misc.go 4.7 KB

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