user.go 17 KB

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