misc.go 6.2 KB

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