misc.go 4.4 KB

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