1
0

misc.go 4.5 KB

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