misc.go 6.2 KB

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