misc.go 5.4 KB

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