misc.go 6.1 KB

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