misc.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "one-api/common"
  7. "one-api/model"
  8. "strings"
  9. "github.com/gin-gonic/gin"
  10. )
  11. func GetStatus(c *gin.Context) {
  12. c.JSON(http.StatusOK, gin.H{
  13. "success": true,
  14. "message": "",
  15. "data": gin.H{
  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. "price": common.Price,
  27. "turnstile_check": common.TurnstileCheckEnabled,
  28. "turnstile_site_key": common.TurnstileSiteKey,
  29. "top_up_link": common.TopUpLink,
  30. "chat_link": common.ChatLink,
  31. "quota_per_unit": common.QuotaPerUnit,
  32. "display_in_currency": common.DisplayInCurrencyEnabled,
  33. "enable_batch_update": common.BatchUpdateEnabled,
  34. "enable_drawing": common.DrawingEnabled,
  35. "enable_data_export": common.DataExportEnabled,
  36. },
  37. })
  38. return
  39. }
  40. func GetNotice(c *gin.Context) {
  41. common.OptionMapRWMutex.RLock()
  42. defer common.OptionMapRWMutex.RUnlock()
  43. c.JSON(http.StatusOK, gin.H{
  44. "success": true,
  45. "message": "",
  46. "data": common.OptionMap["Notice"],
  47. })
  48. return
  49. }
  50. func GetAbout(c *gin.Context) {
  51. common.OptionMapRWMutex.RLock()
  52. defer common.OptionMapRWMutex.RUnlock()
  53. c.JSON(http.StatusOK, gin.H{
  54. "success": true,
  55. "message": "",
  56. "data": common.OptionMap["About"],
  57. })
  58. return
  59. }
  60. func GetMidjourney(c *gin.Context) {
  61. common.OptionMapRWMutex.RLock()
  62. defer common.OptionMapRWMutex.RUnlock()
  63. c.JSON(http.StatusOK, gin.H{
  64. "success": true,
  65. "message": "",
  66. "data": common.OptionMap["Midjourney"],
  67. })
  68. return
  69. }
  70. func GetHomePageContent(c *gin.Context) {
  71. common.OptionMapRWMutex.RLock()
  72. defer common.OptionMapRWMutex.RUnlock()
  73. c.JSON(http.StatusOK, gin.H{
  74. "success": true,
  75. "message": "",
  76. "data": common.OptionMap["HomePageContent"],
  77. })
  78. return
  79. }
  80. func SendEmailVerification(c *gin.Context) {
  81. email := c.Query("email")
  82. if err := common.Validate.Var(email, "required,email"); err != nil {
  83. c.JSON(http.StatusOK, gin.H{
  84. "success": false,
  85. "message": "无效的参数",
  86. })
  87. return
  88. }
  89. if common.EmailDomainRestrictionEnabled {
  90. allowed := false
  91. for _, domain := range common.EmailDomainWhitelist {
  92. if strings.HasSuffix(email, "@"+domain) {
  93. allowed = true
  94. break
  95. }
  96. }
  97. if !allowed {
  98. c.JSON(http.StatusOK, gin.H{
  99. "success": false,
  100. "message": "管理员启用了邮箱域名白名单,您的邮箱地址的域名不在白名单中",
  101. })
  102. return
  103. }
  104. }
  105. if model.IsEmailAlreadyTaken(email) {
  106. c.JSON(http.StatusOK, gin.H{
  107. "success": false,
  108. "message": "邮箱地址已被占用",
  109. })
  110. return
  111. }
  112. code := common.GenerateVerificationCode(6)
  113. common.RegisterVerificationCodeWithKey(email, code, common.EmailVerificationPurpose)
  114. subject := fmt.Sprintf("%s邮箱验证邮件", common.SystemName)
  115. content := fmt.Sprintf("<p>您好,你正在进行%s邮箱验证。</p>"+
  116. "<p>您的验证码为: <strong>%s</strong></p>"+
  117. "<p>验证码 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, code, common.VerificationValidMinutes)
  118. err := common.SendEmail(subject, email, content)
  119. if err != nil {
  120. c.JSON(http.StatusOK, gin.H{
  121. "success": false,
  122. "message": err.Error(),
  123. })
  124. return
  125. }
  126. c.JSON(http.StatusOK, gin.H{
  127. "success": true,
  128. "message": "",
  129. })
  130. return
  131. }
  132. func SendPasswordResetEmail(c *gin.Context) {
  133. email := c.Query("email")
  134. if err := common.Validate.Var(email, "required,email"); err != nil {
  135. c.JSON(http.StatusOK, gin.H{
  136. "success": false,
  137. "message": "无效的参数",
  138. })
  139. return
  140. }
  141. if !model.IsEmailAlreadyTaken(email) {
  142. c.JSON(http.StatusOK, gin.H{
  143. "success": false,
  144. "message": "该邮箱地址未注册",
  145. })
  146. return
  147. }
  148. code := common.GenerateVerificationCode(0)
  149. common.RegisterVerificationCodeWithKey(email, code, common.PasswordResetPurpose)
  150. link := fmt.Sprintf("%s/user/reset?email=%s&token=%s", common.ServerAddress, email, code)
  151. subject := fmt.Sprintf("%s密码重置", common.SystemName)
  152. content := fmt.Sprintf("<p>您好,你正在进行%s密码重置。</p>"+
  153. "<p>点击 <a href='%s'>此处</a> 进行密码重置。</p>"+
  154. "<p>如果链接无法点击,请尝试点击下面的链接或将其复制到浏览器中打开:<br> %s </p>"+
  155. "<p>重置链接 %d 分钟内有效,如果不是本人操作,请忽略。</p>", common.SystemName, link, link, common.VerificationValidMinutes)
  156. err := common.SendEmail(subject, email, content)
  157. if err != nil {
  158. c.JSON(http.StatusOK, gin.H{
  159. "success": false,
  160. "message": err.Error(),
  161. })
  162. return
  163. }
  164. c.JSON(http.StatusOK, gin.H{
  165. "success": true,
  166. "message": "",
  167. })
  168. return
  169. }
  170. type PasswordResetRequest struct {
  171. Email string `json:"email"`
  172. Token string `json:"token"`
  173. }
  174. func ResetPassword(c *gin.Context) {
  175. var req PasswordResetRequest
  176. err := json.NewDecoder(c.Request.Body).Decode(&req)
  177. if req.Email == "" || req.Token == "" {
  178. c.JSON(http.StatusOK, gin.H{
  179. "success": false,
  180. "message": "无效的参数",
  181. })
  182. return
  183. }
  184. if !common.VerifyCodeWithKey(req.Email, req.Token, common.PasswordResetPurpose) {
  185. c.JSON(http.StatusOK, gin.H{
  186. "success": false,
  187. "message": "重置链接非法或已过期",
  188. })
  189. return
  190. }
  191. password := common.GenerateVerificationCode(12)
  192. err = model.ResetUserPasswordByEmail(req.Email, password)
  193. if err != nil {
  194. c.JSON(http.StatusOK, gin.H{
  195. "success": false,
  196. "message": err.Error(),
  197. })
  198. return
  199. }
  200. common.DeleteKey(req.Email, common.PasswordResetPurpose)
  201. c.JSON(http.StatusOK, gin.H{
  202. "success": true,
  203. "message": "",
  204. "data": password,
  205. })
  206. return
  207. }