user.go 18 KB

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