misc.go 5.9 KB

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