GetUserInfo.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const API = require("../../lib/API")
  2. const db = require("../../plugin/DataBase/db")
  3. const AccessControl = require("../../lib/AccessControl")
  4. const Redis = require('../../plugin/DataBase/Redis')
  5. const { BaseStdResponse } = require("../../BaseStdResponse")
  6. const { getUserSocialBindings, toSocialBindingSummary } = require("../../lib/UserSocialBinding")
  7. class GetRepoList extends API {
  8. constructor() {
  9. super();
  10. this.setPath('/User/Info')
  11. this.setMethod('GET')
  12. }
  13. async onRequest(req, res) {
  14. let {
  15. uuid,
  16. session
  17. } = req.query
  18. if ([uuid, session].some(value => value === '' || value === null || value === undefined))
  19. return res.json({
  20. ...BaseStdResponse.MISSING_PARAMETER
  21. })
  22. // 检查 session
  23. if (!await AccessControl.checkSession(uuid, session))
  24. return res.status(401).json({
  25. ...BaseStdResponse.ACCESS_DENIED
  26. })
  27. let sql = 'SELECT uuid, username, permission AS roles, avatar, email, registTime FROM users WHERE uuid = ?'
  28. let rows = await db.query(sql, [uuid])
  29. if (!rows || rows.length === 0)
  30. return res.json({
  31. ...BaseStdResponse.MISSING_FILE,
  32. msg: '获取用户信息失败!'
  33. })
  34. const userSession = await Redis.get(`userSession:${uuid}`)
  35. const bindings = await getUserSocialBindings(uuid)
  36. res.json({
  37. ...BaseStdResponse.OK,
  38. data: {
  39. ...rows[0],
  40. session: userSession,
  41. socialBindings: toSocialBindingSummary(bindings),
  42. boundTypes: bindings.map(binding => binding.social_type)
  43. }
  44. })
  45. }
  46. }
  47. module.exports.GetRepoList = GetRepoList;