misc.go 5.7 KB

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