misc.go 4.6 KB

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