user.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. c.JSON(http.StatusOK, gin.H{
  183. "success": true,
  184. "message": "",
  185. })
  186. return
  187. }
  188. func GetAllUsers(c *gin.Context) {
  189. p, _ := strconv.Atoi(c.Query("p"))
  190. if p < 0 {
  191. p = 0
  192. }
  193. users, err := model.GetAllUsers(p*common.ItemsPerPage, common.ItemsPerPage)
  194. if err != nil {
  195. c.JSON(http.StatusOK, gin.H{
  196. "success": false,
  197. "message": err.Error(),
  198. })
  199. return
  200. }
  201. c.JSON(http.StatusOK, gin.H{
  202. "success": true,
  203. "message": "",
  204. "data": users,
  205. })
  206. return
  207. }
  208. func SearchUsers(c *gin.Context) {
  209. keyword := c.Query("keyword")
  210. group := c.Query("group")
  211. users, err := model.SearchUsers(keyword, group)
  212. if err != nil {
  213. c.JSON(http.StatusOK, gin.H{
  214. "success": false,
  215. "message": err.Error(),
  216. })
  217. return
  218. }
  219. c.JSON(http.StatusOK, gin.H{
  220. "success": true,
  221. "message": "",
  222. "data": users,
  223. })
  224. return
  225. }
  226. func GetUser(c *gin.Context) {
  227. id, err := strconv.Atoi(c.Param("id"))
  228. if err != nil {
  229. c.JSON(http.StatusOK, gin.H{
  230. "success": false,
  231. "message": err.Error(),
  232. })
  233. return
  234. }
  235. user, err := model.GetUserById(id, false)
  236. if err != nil {
  237. c.JSON(http.StatusOK, gin.H{
  238. "success": false,
  239. "message": err.Error(),
  240. })
  241. return
  242. }
  243. myRole := c.GetInt("role")
  244. if myRole <= user.Role && myRole != common.RoleRootUser {
  245. c.JSON(http.StatusOK, gin.H{
  246. "success": false,
  247. "message": "无权获取同级或更高等级用户的信息",
  248. })
  249. return
  250. }
  251. c.JSON(http.StatusOK, gin.H{
  252. "success": true,
  253. "message": "",
  254. "data": user,
  255. })
  256. return
  257. }
  258. func GenerateAccessToken(c *gin.Context) {
  259. id := c.GetInt("id")
  260. user, err := model.GetUserById(id, true)
  261. if err != nil {
  262. c.JSON(http.StatusOK, gin.H{
  263. "success": false,
  264. "message": err.Error(),
  265. })
  266. return
  267. }
  268. user.AccessToken = common.GetUUID()
  269. if model.DB.Where("access_token = ?", user.AccessToken).First(user).RowsAffected != 0 {
  270. c.JSON(http.StatusOK, gin.H{
  271. "success": false,
  272. "message": "请重试,系统生成的 UUID 竟然重复了!",
  273. })
  274. return
  275. }
  276. if err := user.Update(false); err != nil {
  277. c.JSON(http.StatusOK, gin.H{
  278. "success": false,
  279. "message": err.Error(),
  280. })
  281. return
  282. }
  283. c.JSON(http.StatusOK, gin.H{
  284. "success": true,
  285. "message": "",
  286. "data": user.AccessToken,
  287. })
  288. return
  289. }
  290. type TransferAffQuotaRequest struct {
  291. Quota int `json:"quota" binding:"required"`
  292. }
  293. func TransferAffQuota(c *gin.Context) {
  294. id := c.GetInt("id")
  295. user, err := model.GetUserById(id, true)
  296. if err != nil {
  297. c.JSON(http.StatusOK, gin.H{
  298. "success": false,
  299. "message": err.Error(),
  300. })
  301. return
  302. }
  303. tran := TransferAffQuotaRequest{}
  304. if err := c.ShouldBindJSON(&tran); err != nil {
  305. c.JSON(http.StatusOK, gin.H{
  306. "success": false,
  307. "message": err.Error(),
  308. })
  309. return
  310. }
  311. err = user.TransferAffQuotaToQuota(tran.Quota)
  312. if err != nil {
  313. c.JSON(http.StatusOK, gin.H{
  314. "success": false,
  315. "message": "划转失败 " + err.Error(),
  316. })
  317. return
  318. }
  319. c.JSON(http.StatusOK, gin.H{
  320. "success": true,
  321. "message": "划转成功",
  322. })
  323. }
  324. func GetAffCode(c *gin.Context) {
  325. id := c.GetInt("id")
  326. user, err := model.GetUserById(id, true)
  327. if err != nil {
  328. c.JSON(http.StatusOK, gin.H{
  329. "success": false,
  330. "message": err.Error(),
  331. })
  332. return
  333. }
  334. if user.AffCode == "" {
  335. user.AffCode = common.GetRandomString(4)
  336. if err := user.Update(false); err != nil {
  337. c.JSON(http.StatusOK, gin.H{
  338. "success": false,
  339. "message": err.Error(),
  340. })
  341. return
  342. }
  343. }
  344. c.JSON(http.StatusOK, gin.H{
  345. "success": true,
  346. "message": "",
  347. "data": user.AffCode,
  348. })
  349. return
  350. }
  351. func GetSelf(c *gin.Context) {
  352. id := c.GetInt("id")
  353. user, err := model.GetUserById(id, false)
  354. if err != nil {
  355. c.JSON(http.StatusOK, gin.H{
  356. "success": false,
  357. "message": err.Error(),
  358. })
  359. return
  360. }
  361. c.JSON(http.StatusOK, gin.H{
  362. "success": true,
  363. "message": "",
  364. "data": user,
  365. })
  366. return
  367. }
  368. func GetUserModels(c *gin.Context) {
  369. id, err := strconv.Atoi(c.Param("id"))
  370. if err != nil {
  371. id = c.GetInt("id")
  372. }
  373. user, err := model.GetUserById(id, true)
  374. if err != nil {
  375. c.JSON(http.StatusOK, gin.H{
  376. "success": false,
  377. "message": err.Error(),
  378. })
  379. return
  380. }
  381. models := model.GetGroupModels(user.Group)
  382. c.JSON(http.StatusOK, gin.H{
  383. "success": true,
  384. "message": "",
  385. "data": models,
  386. })
  387. return
  388. }
  389. func UpdateUser(c *gin.Context) {
  390. var updatedUser model.User
  391. err := json.NewDecoder(c.Request.Body).Decode(&updatedUser)
  392. if err != nil || updatedUser.Id == 0 {
  393. c.JSON(http.StatusOK, gin.H{
  394. "success": false,
  395. "message": "无效的参数",
  396. })
  397. return
  398. }
  399. if updatedUser.Password == "" {
  400. updatedUser.Password = "$I_LOVE_U" // make Validator happy :)
  401. }
  402. if err := common.Validate.Struct(&updatedUser); err != nil {
  403. c.JSON(http.StatusOK, gin.H{
  404. "success": false,
  405. "message": "输入不合法 " + err.Error(),
  406. })
  407. return
  408. }
  409. originUser, err := model.GetUserById(updatedUser.Id, false)
  410. if err != nil {
  411. c.JSON(http.StatusOK, gin.H{
  412. "success": false,
  413. "message": err.Error(),
  414. })
  415. return
  416. }
  417. myRole := c.GetInt("role")
  418. if myRole <= originUser.Role && myRole != common.RoleRootUser {
  419. c.JSON(http.StatusOK, gin.H{
  420. "success": false,
  421. "message": "无权更新同权限等级或更高权限等级的用户信息",
  422. })
  423. return
  424. }
  425. if myRole <= updatedUser.Role && myRole != common.RoleRootUser {
  426. c.JSON(http.StatusOK, gin.H{
  427. "success": false,
  428. "message": "无权将其他用户权限等级提升到大于等于自己的权限等级",
  429. })
  430. return
  431. }
  432. if updatedUser.Password == "$I_LOVE_U" {
  433. updatedUser.Password = "" // rollback to what it should be
  434. }
  435. updatePassword := updatedUser.Password != ""
  436. if err := updatedUser.Edit(updatePassword); err != nil {
  437. c.JSON(http.StatusOK, gin.H{
  438. "success": false,
  439. "message": err.Error(),
  440. })
  441. return
  442. }
  443. if originUser.Quota != updatedUser.Quota {
  444. model.RecordLog(originUser.Id, model.LogTypeManage, fmt.Sprintf("管理员将用户额度从 %s修改为 %s", common.LogQuota(originUser.Quota), common.LogQuota(updatedUser.Quota)))
  445. }
  446. c.JSON(http.StatusOK, gin.H{
  447. "success": true,
  448. "message": "",
  449. })
  450. return
  451. }
  452. func UpdateSelf(c *gin.Context) {
  453. var user model.User
  454. err := json.NewDecoder(c.Request.Body).Decode(&user)
  455. if err != nil {
  456. c.JSON(http.StatusOK, gin.H{
  457. "success": false,
  458. "message": "无效的参数",
  459. })
  460. return
  461. }
  462. if user.Password == "" {
  463. user.Password = "$I_LOVE_U" // make Validator happy :)
  464. }
  465. if err := common.Validate.Struct(&user); err != nil {
  466. c.JSON(http.StatusOK, gin.H{
  467. "success": false,
  468. "message": "输入不合法 " + err.Error(),
  469. })
  470. return
  471. }
  472. cleanUser := model.User{
  473. Id: c.GetInt("id"),
  474. Username: user.Username,
  475. Password: user.Password,
  476. DisplayName: user.DisplayName,
  477. }
  478. if user.Password == "$I_LOVE_U" {
  479. user.Password = "" // rollback to what it should be
  480. cleanUser.Password = ""
  481. }
  482. updatePassword := user.Password != ""
  483. if err := cleanUser.Update(updatePassword); err != nil {
  484. c.JSON(http.StatusOK, gin.H{
  485. "success": false,
  486. "message": err.Error(),
  487. })
  488. return
  489. }
  490. c.JSON(http.StatusOK, gin.H{
  491. "success": true,
  492. "message": "",
  493. })
  494. return
  495. }
  496. func DeleteUser(c *gin.Context) {
  497. id, err := strconv.Atoi(c.Param("id"))
  498. if err != nil {
  499. c.JSON(http.StatusOK, gin.H{
  500. "success": false,
  501. "message": err.Error(),
  502. })
  503. return
  504. }
  505. originUser, err := model.GetUserById(id, false)
  506. if err != nil {
  507. c.JSON(http.StatusOK, gin.H{
  508. "success": false,
  509. "message": err.Error(),
  510. })
  511. return
  512. }
  513. myRole := c.GetInt("role")
  514. if myRole <= originUser.Role {
  515. c.JSON(http.StatusOK, gin.H{
  516. "success": false,
  517. "message": "无权删除同权限等级或更高权限等级的用户",
  518. })
  519. return
  520. }
  521. err = model.HardDeleteUserById(id)
  522. if err != nil {
  523. c.JSON(http.StatusOK, gin.H{
  524. "success": true,
  525. "message": "",
  526. })
  527. return
  528. }
  529. }
  530. func DeleteSelf(c *gin.Context) {
  531. id := c.GetInt("id")
  532. user, _ := model.GetUserById(id, false)
  533. if user.Role == common.RoleRootUser {
  534. c.JSON(http.StatusOK, gin.H{
  535. "success": false,
  536. "message": "不能删除超级管理员账户",
  537. })
  538. return
  539. }
  540. err := model.DeleteUserById(id)
  541. if err != nil {
  542. c.JSON(http.StatusOK, gin.H{
  543. "success": false,
  544. "message": err.Error(),
  545. })
  546. return
  547. }
  548. c.JSON(http.StatusOK, gin.H{
  549. "success": true,
  550. "message": "",
  551. })
  552. return
  553. }
  554. func CreateUser(c *gin.Context) {
  555. var user model.User
  556. err := json.NewDecoder(c.Request.Body).Decode(&user)
  557. if err != nil || user.Username == "" || user.Password == "" {
  558. c.JSON(http.StatusOK, gin.H{
  559. "success": false,
  560. "message": "无效的参数",
  561. })
  562. return
  563. }
  564. if err := common.Validate.Struct(&user); err != nil {
  565. c.JSON(http.StatusOK, gin.H{
  566. "success": false,
  567. "message": "输入不合法 " + err.Error(),
  568. })
  569. return
  570. }
  571. if user.DisplayName == "" {
  572. user.DisplayName = user.Username
  573. }
  574. myRole := c.GetInt("role")
  575. if user.Role >= myRole {
  576. c.JSON(http.StatusOK, gin.H{
  577. "success": false,
  578. "message": "无法创建权限大于等于自己的用户",
  579. })
  580. return
  581. }
  582. // Even for admin users, we cannot fully trust them!
  583. cleanUser := model.User{
  584. Username: user.Username,
  585. Password: user.Password,
  586. DisplayName: user.DisplayName,
  587. }
  588. if err := cleanUser.Insert(0); err != nil {
  589. c.JSON(http.StatusOK, gin.H{
  590. "success": false,
  591. "message": err.Error(),
  592. })
  593. return
  594. }
  595. c.JSON(http.StatusOK, gin.H{
  596. "success": true,
  597. "message": "",
  598. })
  599. return
  600. }
  601. type ManageRequest struct {
  602. Username string `json:"username"`
  603. Action string `json:"action"`
  604. }
  605. // ManageUser Only admin user can do this
  606. func ManageUser(c *gin.Context) {
  607. var req ManageRequest
  608. err := json.NewDecoder(c.Request.Body).Decode(&req)
  609. if err != nil {
  610. c.JSON(http.StatusOK, gin.H{
  611. "success": false,
  612. "message": "无效的参数",
  613. })
  614. return
  615. }
  616. user := model.User{
  617. Username: req.Username,
  618. }
  619. // Fill attributes
  620. model.DB.Unscoped().Where(&user).First(&user)
  621. if user.Id == 0 {
  622. c.JSON(http.StatusOK, gin.H{
  623. "success": false,
  624. "message": "用户不存在",
  625. })
  626. return
  627. }
  628. myRole := c.GetInt("role")
  629. if myRole <= user.Role && myRole != common.RoleRootUser {
  630. c.JSON(http.StatusOK, gin.H{
  631. "success": false,
  632. "message": "无权更新同权限等级或更高权限等级的用户信息",
  633. })
  634. return
  635. }
  636. switch req.Action {
  637. case "disable":
  638. user.Status = common.UserStatusDisabled
  639. if user.Role == common.RoleRootUser {
  640. c.JSON(http.StatusOK, gin.H{
  641. "success": false,
  642. "message": "无法禁用超级管理员用户",
  643. })
  644. return
  645. }
  646. case "enable":
  647. user.Status = common.UserStatusEnabled
  648. case "delete":
  649. if user.Role == common.RoleRootUser {
  650. c.JSON(http.StatusOK, gin.H{
  651. "success": false,
  652. "message": "无法删除超级管理员用户",
  653. })
  654. return
  655. }
  656. if err := user.Delete(); err != nil {
  657. c.JSON(http.StatusOK, gin.H{
  658. "success": false,
  659. "message": err.Error(),
  660. })
  661. return
  662. }
  663. case "promote":
  664. if myRole != common.RoleRootUser {
  665. c.JSON(http.StatusOK, gin.H{
  666. "success": false,
  667. "message": "普通管理员用户无法提升其他用户为管理员",
  668. })
  669. return
  670. }
  671. if user.Role >= common.RoleAdminUser {
  672. c.JSON(http.StatusOK, gin.H{
  673. "success": false,
  674. "message": "该用户已经是管理员",
  675. })
  676. return
  677. }
  678. user.Role = common.RoleAdminUser
  679. case "demote":
  680. if user.Role == common.RoleRootUser {
  681. c.JSON(http.StatusOK, gin.H{
  682. "success": false,
  683. "message": "无法降级超级管理员用户",
  684. })
  685. return
  686. }
  687. if user.Role == common.RoleCommonUser {
  688. c.JSON(http.StatusOK, gin.H{
  689. "success": false,
  690. "message": "该用户已经是普通用户",
  691. })
  692. return
  693. }
  694. user.Role = common.RoleCommonUser
  695. }
  696. if err := user.Update(false); err != nil {
  697. c.JSON(http.StatusOK, gin.H{
  698. "success": false,
  699. "message": err.Error(),
  700. })
  701. return
  702. }
  703. clearUser := model.User{
  704. Role: user.Role,
  705. Status: user.Status,
  706. }
  707. c.JSON(http.StatusOK, gin.H{
  708. "success": true,
  709. "message": "",
  710. "data": clearUser,
  711. })
  712. return
  713. }
  714. func EmailBind(c *gin.Context) {
  715. email := c.Query("email")
  716. code := c.Query("code")
  717. if !common.VerifyCodeWithKey(email, code, common.EmailVerificationPurpose) {
  718. c.JSON(http.StatusOK, gin.H{
  719. "success": false,
  720. "message": "验证码错误或已过期",
  721. })
  722. return
  723. }
  724. id := c.GetInt("id")
  725. user := model.User{
  726. Id: id,
  727. }
  728. err := user.FillUserById()
  729. if err != nil {
  730. c.JSON(http.StatusOK, gin.H{
  731. "success": false,
  732. "message": err.Error(),
  733. })
  734. return
  735. }
  736. user.Email = email
  737. // no need to check if this email already taken, because we have used verification code to check it
  738. err = user.Update(false)
  739. if err != nil {
  740. c.JSON(http.StatusOK, gin.H{
  741. "success": false,
  742. "message": err.Error(),
  743. })
  744. return
  745. }
  746. if user.Role == common.RoleRootUser {
  747. common.RootUserEmail = email
  748. }
  749. c.JSON(http.StatusOK, gin.H{
  750. "success": true,
  751. "message": "",
  752. })
  753. return
  754. }
  755. type topUpRequest struct {
  756. Key string `json:"key"`
  757. }
  758. var lock = sync.Mutex{}
  759. func TopUp(c *gin.Context) {
  760. lock.Lock()
  761. defer lock.Unlock()
  762. req := topUpRequest{}
  763. err := c.ShouldBindJSON(&req)
  764. if err != nil {
  765. c.JSON(http.StatusOK, gin.H{
  766. "success": false,
  767. "message": err.Error(),
  768. })
  769. return
  770. }
  771. id := c.GetInt("id")
  772. quota, err := model.Redeem(req.Key, id)
  773. if err != nil {
  774. c.JSON(http.StatusOK, gin.H{
  775. "success": false,
  776. "message": err.Error(),
  777. })
  778. return
  779. }
  780. c.JSON(http.StatusOK, gin.H{
  781. "success": true,
  782. "message": "",
  783. "data": quota,
  784. })
  785. return
  786. }