misc.go 6.0 KB

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