misc.go 6.6 KB

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