user.go 18 KB

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