user.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "one-api/common"
  7. "one-api/model"
  8. "strconv"
  9. "sync"
  10. "github.com/gin-contrib/sessions"
  11. "github.com/gin-gonic/gin"
  12. )
  13. type LoginRequest struct {
  14. Username string `json:"username"`
  15. Password string `json:"password"`
  16. }
  17. func Login(c *gin.Context) {
  18. if !common.PasswordLoginEnabled {
  19. c.JSON(http.StatusOK, gin.H{
  20. "message": "管理员关闭了密码登录",
  21. "success": false,
  22. })
  23. return
  24. }
  25. var loginRequest LoginRequest
  26. err := json.NewDecoder(c.Request.Body).Decode(&loginRequest)
  27. if err != nil {
  28. c.JSON(http.StatusOK, gin.H{
  29. "message": "无效的参数",
  30. "success": false,
  31. })
  32. return
  33. }
  34. username := loginRequest.Username
  35. password := loginRequest.Password
  36. if username == "" || password == "" {
  37. c.JSON(http.StatusOK, gin.H{
  38. "message": "无效的参数",
  39. "success": false,
  40. })
  41. return
  42. }
  43. user := model.User{
  44. Username: username,
  45. Password: password,
  46. }
  47. err = user.ValidateAndFill()
  48. if err != nil {
  49. c.JSON(http.StatusOK, gin.H{
  50. "message": err.Error(),
  51. "success": false,
  52. })
  53. return
  54. }
  55. setupLogin(&user, c)
  56. }
  57. // setup session & cookies and then return user info
  58. func setupLogin(user *model.User, c *gin.Context) {
  59. session := sessions.Default(c)
  60. session.Set("id", user.Id)
  61. session.Set("username", user.Username)
  62. session.Set("role", user.Role)
  63. session.Set("status", user.Status)
  64. err := session.Save()
  65. if err != nil {
  66. c.JSON(http.StatusOK, gin.H{
  67. "message": "无法保存会话信息,请重试",
  68. "success": false,
  69. })
  70. return
  71. }
  72. cleanUser := model.User{
  73. Id: user.Id,
  74. Username: user.Username,
  75. DisplayName: user.DisplayName,
  76. Role: user.Role,
  77. Status: user.Status,
  78. Group: user.Group,
  79. }
  80. c.JSON(http.StatusOK, gin.H{
  81. "message": "",
  82. "success": true,
  83. "data": cleanUser,
  84. })
  85. }
  86. func Logout(c *gin.Context) {
  87. session := sessions.Default(c)
  88. session.Clear()
  89. err := session.Save()
  90. if err != nil {
  91. c.JSON(http.StatusOK, gin.H{
  92. "message": err.Error(),
  93. "success": false,
  94. })
  95. return
  96. }
  97. c.JSON(http.StatusOK, gin.H{
  98. "message": "",
  99. "success": true,
  100. })
  101. }
  102. func Register(c *gin.Context) {
  103. if !common.RegisterEnabled {
  104. c.JSON(http.StatusOK, gin.H{
  105. "message": "管理员关闭了新用户注册",
  106. "success": false,
  107. })
  108. return
  109. }
  110. if !common.PasswordRegisterEnabled {
  111. c.JSON(http.StatusOK, gin.H{
  112. "message": "管理员关闭了通过密码进行注册,请使用第三方账户验证的形式进行注册",
  113. "success": false,
  114. })
  115. return
  116. }
  117. var user model.User
  118. err := json.NewDecoder(c.Request.Body).Decode(&user)
  119. if err != nil {
  120. c.JSON(http.StatusOK, gin.H{
  121. "success": false,
  122. "message": "无效的参数",
  123. })
  124. return
  125. }
  126. if err := common.Validate.Struct(&user); err != nil {
  127. c.JSON(http.StatusOK, gin.H{
  128. "success": false,
  129. "message": "输入不合法 " + err.Error(),
  130. })
  131. return
  132. }
  133. if common.EmailVerificationEnabled {
  134. if user.Email == "" || user.VerificationCode == "" {
  135. c.JSON(http.StatusOK, gin.H{
  136. "success": false,
  137. "message": "管理员开启了邮箱验证,请输入邮箱地址和验证码",
  138. })
  139. return
  140. }
  141. if !common.VerifyCodeWithKey(user.Email, user.VerificationCode, common.EmailVerificationPurpose) {
  142. c.JSON(http.StatusOK, gin.H{
  143. "success": false,
  144. "message": "验证码错误或已过期",
  145. })
  146. return
  147. }
  148. }
  149. exist, err := model.CheckUserExistOrDeleted(user.Username, user.Email)
  150. if err != nil {
  151. c.JSON(http.StatusOK, gin.H{
  152. "success": false,
  153. "message": err.Error(),
  154. })
  155. return
  156. }
  157. if exist {
  158. c.JSON(http.StatusOK, gin.H{
  159. "success": false,
  160. "message": "用户名已存在,或已注销",
  161. })
  162. return
  163. }
  164. affCode := user.AffCode // this code is the inviter's code, not the user's own code
  165. inviterId, _ := model.GetUserIdByAffCode(affCode)
  166. cleanUser := model.User{
  167. Username: user.Username,
  168. Password: user.Password,
  169. DisplayName: user.Username,
  170. InviterId: inviterId,
  171. }
  172. if common.EmailVerificationEnabled {
  173. cleanUser.Email = user.Email
  174. }
  175. if err := cleanUser.Insert(inviterId); err != nil {
  176. c.JSON(http.StatusOK, gin.H{
  177. "success": false,
  178. "message": err.Error(),
  179. })
  180. return
  181. }
  182. // 获取插入后的用户ID
  183. var insertedUser model.User
  184. if err := model.DB.Where("username = ?", cleanUser.Username).First(&insertedUser).Error; err != nil {
  185. c.JSON(http.StatusOK, gin.H{
  186. "success": false,
  187. "message": "用户注册失败或用户ID获取失败",
  188. })
  189. return
  190. }
  191. // 生成默认令牌
  192. // tokenName := cleanUser.Username + "的初始令牌"
  193. token := model.Token{
  194. UserId: insertedUser.Id, // 使用插入后的用户ID
  195. Name: cleanUser.Username + "的初始令牌",
  196. Key: common.GenerateKey(),
  197. CreatedTime: common.GetTimestamp(),
  198. AccessedTime: common.GetTimestamp(),
  199. ExpiredTime: -1, // 永不过期
  200. RemainQuota: 500000, // 示例额度
  201. UnlimitedQuota: true,
  202. ModelLimitsEnabled: false,
  203. }
  204. if err := token.Insert(); err != nil {
  205. c.JSON(http.StatusOK, gin.H{
  206. "success": false,
  207. "message": "创建默认令牌失败",
  208. })
  209. return
  210. }
  211. c.JSON(http.StatusOK, gin.H{
  212. "success": true,
  213. "message": "",
  214. })
  215. return
  216. }
  217. func GetAllUsers(c *gin.Context) {
  218. p, _ := strconv.Atoi(c.Query("p"))
  219. if p < 0 {
  220. p = 0
  221. }
  222. users, err := model.GetAllUsers(p*common.ItemsPerPage, common.ItemsPerPage)
  223. if err != nil {
  224. c.JSON(http.StatusOK, gin.H{
  225. "success": false,
  226. "message": err.Error(),
  227. })
  228. return
  229. }
  230. c.JSON(http.StatusOK, gin.H{
  231. "success": true,
  232. "message": "",
  233. "data": users,
  234. })
  235. return
  236. }
  237. func SearchUsers(c *gin.Context) {
  238. keyword := c.Query("keyword")
  239. group := c.Query("group")
  240. users, err := model.SearchUsers(keyword, group)
  241. if err != nil {
  242. c.JSON(http.StatusOK, gin.H{
  243. "success": false,
  244. "message": err.Error(),
  245. })
  246. return
  247. }
  248. c.JSON(http.StatusOK, gin.H{
  249. "success": true,
  250. "message": "",
  251. "data": users,
  252. })
  253. return
  254. }
  255. func GetUser(c *gin.Context) {
  256. id, err := strconv.Atoi(c.Param("id"))
  257. if err != nil {
  258. c.JSON(http.StatusOK, gin.H{
  259. "success": false,
  260. "message": err.Error(),
  261. })
  262. return
  263. }
  264. user, err := model.GetUserById(id, false)
  265. if err != nil {
  266. c.JSON(http.StatusOK, gin.H{
  267. "success": false,
  268. "message": err.Error(),
  269. })
  270. return
  271. }
  272. myRole := c.GetInt("role")
  273. if myRole <= user.Role && myRole != common.RoleRootUser {
  274. c.JSON(http.StatusOK, gin.H{
  275. "success": false,
  276. "message": "无权获取同级或更高等级用户的信息",
  277. })
  278. return
  279. }
  280. c.JSON(http.StatusOK, gin.H{
  281. "success": true,
  282. "message": "",
  283. "data": user,
  284. })
  285. return
  286. }
  287. func GenerateAccessToken(c *gin.Context) {
  288. id := c.GetInt("id")
  289. user, err := model.GetUserById(id, true)
  290. if err != nil {
  291. c.JSON(http.StatusOK, gin.H{
  292. "success": false,
  293. "message": err.Error(),
  294. })
  295. return
  296. }
  297. user.AccessToken = common.GetUUID()
  298. if model.DB.Where("access_token = ?", user.AccessToken).First(user).RowsAffected != 0 {
  299. c.JSON(http.StatusOK, gin.H{
  300. "success": false,
  301. "message": "请重试,系统生成的 UUID 竟然重复了!",
  302. })
  303. return
  304. }
  305. if err := user.Update(false); err != nil {
  306. c.JSON(http.StatusOK, gin.H{
  307. "success": false,
  308. "message": err.Error(),
  309. })
  310. return
  311. }
  312. c.JSON(http.StatusOK, gin.H{
  313. "success": true,
  314. "message": "",
  315. "data": user.AccessToken,
  316. })
  317. return
  318. }
  319. type TransferAffQuotaRequest struct {
  320. Quota int `json:"quota" binding:"required"`
  321. }
  322. func TransferAffQuota(c *gin.Context) {
  323. id := c.GetInt("id")
  324. user, err := model.GetUserById(id, true)
  325. if err != nil {
  326. c.JSON(http.StatusOK, gin.H{
  327. "success": false,
  328. "message": err.Error(),
  329. })
  330. return
  331. }
  332. tran := TransferAffQuotaRequest{}
  333. if err := c.ShouldBindJSON(&tran); err != nil {
  334. c.JSON(http.StatusOK, gin.H{
  335. "success": false,
  336. "message": err.Error(),
  337. })
  338. return
  339. }
  340. err = user.TransferAffQuotaToQuota(tran.Quota)
  341. if err != nil {
  342. c.JSON(http.StatusOK, gin.H{
  343. "success": false,
  344. "message": "划转失败 " + err.Error(),
  345. })
  346. return
  347. }
  348. c.JSON(http.StatusOK, gin.H{
  349. "success": true,
  350. "message": "划转成功",
  351. })
  352. }
  353. func GetAffCode(c *gin.Context) {
  354. id := c.GetInt("id")
  355. user, err := model.GetUserById(id, true)
  356. if err != nil {
  357. c.JSON(http.StatusOK, gin.H{
  358. "success": false,
  359. "message": err.Error(),
  360. })
  361. return
  362. }
  363. if user.AffCode == "" {
  364. user.AffCode = common.GetRandomString(4)
  365. if err := user.Update(false); err != nil {
  366. c.JSON(http.StatusOK, gin.H{
  367. "success": false,
  368. "message": err.Error(),
  369. })
  370. return
  371. }
  372. }
  373. c.JSON(http.StatusOK, gin.H{
  374. "success": true,
  375. "message": "",
  376. "data": user.AffCode,
  377. })
  378. return
  379. }
  380. func GetSelf(c *gin.Context) {
  381. id := c.GetInt("id")
  382. user, err := model.GetUserById(id, false)
  383. if err != nil {
  384. c.JSON(http.StatusOK, gin.H{
  385. "success": false,
  386. "message": err.Error(),
  387. })
  388. return
  389. }
  390. c.JSON(http.StatusOK, gin.H{
  391. "success": true,
  392. "message": "",
  393. "data": user,
  394. })
  395. return
  396. }
  397. func GetUserModels(c *gin.Context) {
  398. id, err := strconv.Atoi(c.Param("id"))
  399. if err != nil {
  400. id = c.GetInt("id")
  401. }
  402. user, err := model.GetUserById(id, true)
  403. if err != nil {
  404. c.JSON(http.StatusOK, gin.H{
  405. "success": false,
  406. "message": err.Error(),
  407. })
  408. return
  409. }
  410. models := model.GetGroupModels(user.Group)
  411. c.JSON(http.StatusOK, gin.H{
  412. "success": true,
  413. "message": "",
  414. "data": models,
  415. })
  416. return
  417. }
  418. func UpdateUser(c *gin.Context) {
  419. var updatedUser model.User
  420. err := json.NewDecoder(c.Request.Body).Decode(&updatedUser)
  421. if err != nil || updatedUser.Id == 0 {
  422. c.JSON(http.StatusOK, gin.H{
  423. "success": false,
  424. "message": "无效的参数",
  425. })
  426. return
  427. }
  428. if updatedUser.Password == "" {
  429. updatedUser.Password = "$I_LOVE_U" // make Validator happy :)
  430. }
  431. if err := common.Validate.Struct(&updatedUser); err != nil {
  432. c.JSON(http.StatusOK, gin.H{
  433. "success": false,
  434. "message": "输入不合法 " + err.Error(),
  435. })
  436. return
  437. }
  438. originUser, err := model.GetUserById(updatedUser.Id, false)
  439. if err != nil {
  440. c.JSON(http.StatusOK, gin.H{
  441. "success": false,
  442. "message": err.Error(),
  443. })
  444. return
  445. }
  446. myRole := c.GetInt("role")
  447. if myRole <= originUser.Role && myRole != common.RoleRootUser {
  448. c.JSON(http.StatusOK, gin.H{
  449. "success": false,
  450. "message": "无权更新同权限等级或更高权限等级的用户信息",
  451. })
  452. return
  453. }
  454. if myRole <= updatedUser.Role && myRole != common.RoleRootUser {
  455. c.JSON(http.StatusOK, gin.H{
  456. "success": false,
  457. "message": "无权将其他用户权限等级提升到大于等于自己的权限等级",
  458. })
  459. return
  460. }
  461. if updatedUser.Password == "$I_LOVE_U" {
  462. updatedUser.Password = "" // rollback to what it should be
  463. }
  464. updatePassword := updatedUser.Password != ""
  465. if err := updatedUser.Edit(updatePassword); err != nil {
  466. c.JSON(http.StatusOK, gin.H{
  467. "success": false,
  468. "message": err.Error(),
  469. })
  470. return
  471. }
  472. if originUser.Quota != updatedUser.Quota {
  473. model.RecordLog(originUser.Id, model.LogTypeManage, fmt.Sprintf("管理员将用户额度从 %s修改为 %s", common.LogQuota(originUser.Quota), common.LogQuota(updatedUser.Quota)))
  474. }
  475. c.JSON(http.StatusOK, gin.H{
  476. "success": true,
  477. "message": "",
  478. })
  479. return
  480. }
  481. func UpdateSelf(c *gin.Context) {
  482. var user model.User
  483. err := json.NewDecoder(c.Request.Body).Decode(&user)
  484. if err != nil {
  485. c.JSON(http.StatusOK, gin.H{
  486. "success": false,
  487. "message": "无效的参数",
  488. })
  489. return
  490. }
  491. if user.Password == "" {
  492. user.Password = "$I_LOVE_U" // make Validator happy :)
  493. }
  494. if err := common.Validate.Struct(&user); err != nil {
  495. c.JSON(http.StatusOK, gin.H{
  496. "success": false,
  497. "message": "输入不合法 " + err.Error(),
  498. })
  499. return
  500. }
  501. cleanUser := model.User{
  502. Id: c.GetInt("id"),
  503. Username: user.Username,
  504. Password: user.Password,
  505. DisplayName: user.DisplayName,
  506. }
  507. if user.Password == "$I_LOVE_U" {
  508. user.Password = "" // rollback to what it should be
  509. cleanUser.Password = ""
  510. }
  511. updatePassword := user.Password != ""
  512. if err := cleanUser.Update(updatePassword); err != nil {
  513. c.JSON(http.StatusOK, gin.H{
  514. "success": false,
  515. "message": err.Error(),
  516. })
  517. return
  518. }
  519. c.JSON(http.StatusOK, gin.H{
  520. "success": true,
  521. "message": "",
  522. })
  523. return
  524. }
  525. func DeleteUser(c *gin.Context) {
  526. id, err := strconv.Atoi(c.Param("id"))
  527. if err != nil {
  528. c.JSON(http.StatusOK, gin.H{
  529. "success": false,
  530. "message": err.Error(),
  531. })
  532. return
  533. }
  534. originUser, err := model.GetUserById(id, false)
  535. if err != nil {
  536. c.JSON(http.StatusOK, gin.H{
  537. "success": false,
  538. "message": err.Error(),
  539. })
  540. return
  541. }
  542. myRole := c.GetInt("role")
  543. if myRole <= originUser.Role {
  544. c.JSON(http.StatusOK, gin.H{
  545. "success": false,
  546. "message": "无权删除同权限等级或更高权限等级的用户",
  547. })
  548. return
  549. }
  550. err = model.HardDeleteUserById(id)
  551. if err != nil {
  552. c.JSON(http.StatusOK, gin.H{
  553. "success": true,
  554. "message": "",
  555. })
  556. return
  557. }
  558. }
  559. func DeleteSelf(c *gin.Context) {
  560. id := c.GetInt("id")
  561. user, _ := model.GetUserById(id, false)
  562. if user.Role == common.RoleRootUser {
  563. c.JSON(http.StatusOK, gin.H{
  564. "success": false,
  565. "message": "不能删除超级管理员账户",
  566. })
  567. return
  568. }
  569. err := model.DeleteUserById(id)
  570. if err != nil {
  571. c.JSON(http.StatusOK, gin.H{
  572. "success": false,
  573. "message": err.Error(),
  574. })
  575. return
  576. }
  577. c.JSON(http.StatusOK, gin.H{
  578. "success": true,
  579. "message": "",
  580. })
  581. return
  582. }
  583. func CreateUser(c *gin.Context) {
  584. var user model.User
  585. err := json.NewDecoder(c.Request.Body).Decode(&user)
  586. if err != nil || user.Username == "" || user.Password == "" {
  587. c.JSON(http.StatusOK, gin.H{
  588. "success": false,
  589. "message": "无效的参数",
  590. })
  591. return
  592. }
  593. if err := common.Validate.Struct(&user); err != nil {
  594. c.JSON(http.StatusOK, gin.H{
  595. "success": false,
  596. "message": "输入不合法 " + err.Error(),
  597. })
  598. return
  599. }
  600. if user.DisplayName == "" {
  601. user.DisplayName = user.Username
  602. }
  603. myRole := c.GetInt("role")
  604. if user.Role >= myRole {
  605. c.JSON(http.StatusOK, gin.H{
  606. "success": false,
  607. "message": "无法创建权限大于等于自己的用户",
  608. })
  609. return
  610. }
  611. // Even for admin users, we cannot fully trust them!
  612. cleanUser := model.User{
  613. Username: user.Username,
  614. Password: user.Password,
  615. DisplayName: user.DisplayName,
  616. }
  617. if err := cleanUser.Insert(0); err != nil {
  618. c.JSON(http.StatusOK, gin.H{
  619. "success": false,
  620. "message": err.Error(),
  621. })
  622. return
  623. }
  624. c.JSON(http.StatusOK, gin.H{
  625. "success": true,
  626. "message": "",
  627. })
  628. return
  629. }
  630. type ManageRequest struct {
  631. Username string `json:"username"`
  632. Action string `json:"action"`
  633. }
  634. // ManageUser Only admin user can do this
  635. func ManageUser(c *gin.Context) {
  636. var req ManageRequest
  637. err := json.NewDecoder(c.Request.Body).Decode(&req)
  638. if err != nil {
  639. c.JSON(http.StatusOK, gin.H{
  640. "success": false,
  641. "message": "无效的参数",
  642. })
  643. return
  644. }
  645. user := model.User{
  646. Username: req.Username,
  647. }
  648. // Fill attributes
  649. model.DB.Unscoped().Where(&user).First(&user)
  650. if user.Id == 0 {
  651. c.JSON(http.StatusOK, gin.H{
  652. "success": false,
  653. "message": "用户不存在",
  654. })
  655. return
  656. }
  657. myRole := c.GetInt("role")
  658. if myRole <= user.Role && myRole != common.RoleRootUser {
  659. c.JSON(http.StatusOK, gin.H{
  660. "success": false,
  661. "message": "无权更新同权限等级或更高权限等级的用户信息",
  662. })
  663. return
  664. }
  665. switch req.Action {
  666. case "disable":
  667. user.Status = common.UserStatusDisabled
  668. if user.Role == common.RoleRootUser {
  669. c.JSON(http.StatusOK, gin.H{
  670. "success": false,
  671. "message": "无法禁用超级管理员用户",
  672. })
  673. return
  674. }
  675. case "enable":
  676. user.Status = common.UserStatusEnabled
  677. case "delete":
  678. if user.Role == common.RoleRootUser {
  679. c.JSON(http.StatusOK, gin.H{
  680. "success": false,
  681. "message": "无法删除超级管理员用户",
  682. })
  683. return
  684. }
  685. if err := user.Delete(); err != nil {
  686. c.JSON(http.StatusOK, gin.H{
  687. "success": false,
  688. "message": err.Error(),
  689. })
  690. return
  691. }
  692. case "promote":
  693. if myRole != common.RoleRootUser {
  694. c.JSON(http.StatusOK, gin.H{
  695. "success": false,
  696. "message": "普通管理员用户无法提升其他用户为管理员",
  697. })
  698. return
  699. }
  700. if user.Role >= common.RoleAdminUser {
  701. c.JSON(http.StatusOK, gin.H{
  702. "success": false,
  703. "message": "该用户已经是管理员",
  704. })
  705. return
  706. }
  707. user.Role = common.RoleAdminUser
  708. case "demote":
  709. if user.Role == common.RoleRootUser {
  710. c.JSON(http.StatusOK, gin.H{
  711. "success": false,
  712. "message": "无法降级超级管理员用户",
  713. })
  714. return
  715. }
  716. if user.Role == common.RoleCommonUser {
  717. c.JSON(http.StatusOK, gin.H{
  718. "success": false,
  719. "message": "该用户已经是普通用户",
  720. })
  721. return
  722. }
  723. user.Role = common.RoleCommonUser
  724. }
  725. if err := user.Update(false); err != nil {
  726. c.JSON(http.StatusOK, gin.H{
  727. "success": false,
  728. "message": err.Error(),
  729. })
  730. return
  731. }
  732. clearUser := model.User{
  733. Role: user.Role,
  734. Status: user.Status,
  735. }
  736. c.JSON(http.StatusOK, gin.H{
  737. "success": true,
  738. "message": "",
  739. "data": clearUser,
  740. })
  741. return
  742. }
  743. func EmailBind(c *gin.Context) {
  744. email := c.Query("email")
  745. code := c.Query("code")
  746. if !common.VerifyCodeWithKey(email, code, common.EmailVerificationPurpose) {
  747. c.JSON(http.StatusOK, gin.H{
  748. "success": false,
  749. "message": "验证码错误或已过期",
  750. })
  751. return
  752. }
  753. id := c.GetInt("id")
  754. user := model.User{
  755. Id: id,
  756. }
  757. err := user.FillUserById()
  758. if err != nil {
  759. c.JSON(http.StatusOK, gin.H{
  760. "success": false,
  761. "message": err.Error(),
  762. })
  763. return
  764. }
  765. user.Email = email
  766. // no need to check if this email already taken, because we have used verification code to check it
  767. err = user.Update(false)
  768. if err != nil {
  769. c.JSON(http.StatusOK, gin.H{
  770. "success": false,
  771. "message": err.Error(),
  772. })
  773. return
  774. }
  775. if user.Role == common.RoleRootUser {
  776. common.RootUserEmail = email
  777. }
  778. c.JSON(http.StatusOK, gin.H{
  779. "success": true,
  780. "message": "",
  781. })
  782. return
  783. }
  784. type topUpRequest struct {
  785. Key string `json:"key"`
  786. }
  787. var topUpLock = sync.Mutex{}
  788. func TopUp(c *gin.Context) {
  789. topUpLock.Lock()
  790. defer topUpLock.Unlock()
  791. req := topUpRequest{}
  792. err := c.ShouldBindJSON(&req)
  793. if err != nil {
  794. c.JSON(http.StatusOK, gin.H{
  795. "success": false,
  796. "message": err.Error(),
  797. })
  798. return
  799. }
  800. id := c.GetInt("id")
  801. quota, err := model.Redeem(req.Key, id)
  802. if err != nil {
  803. c.JSON(http.StatusOK, gin.H{
  804. "success": false,
  805. "message": err.Error(),
  806. })
  807. return
  808. }
  809. c.JSON(http.StatusOK, gin.H{
  810. "success": true,
  811. "message": "",
  812. "data": quota,
  813. })
  814. return
  815. }