misc.go 6.4 KB

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