misc.go 6.7 KB

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