Login.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const API = require("../../../lib/API")
  2. const { v4: uuidv4 } = require('uuid')
  3. const db = require("../../../plugin/DataBase/db")
  4. const Redis = require('../../../plugin/DataBase/Redis')
  5. const { BaseStdResponse } = require("../../../BaseStdResponse")
  6. const { fetchUniLoginProfile, normalizeSocialType } = require('../../../lib/UniLoginClient')
  7. const {
  8. getBindingByIdentity,
  9. getLegacyUserByIdentity,
  10. insertSocialBinding,
  11. updateSocialBindingProfile,
  12. syncLegacySocialMirror
  13. } = require('../../../lib/UserSocialBinding')
  14. class Login extends API {
  15. constructor() {
  16. super()
  17. this.setPath('/UniLogin/Login')
  18. this.setMethod('POST')
  19. }
  20. async onRequest(req, res) {
  21. let { type, code } = req.body
  22. type = normalizeSocialType(type)
  23. if ([code].some(value => value === '' || value === null || value === undefined))
  24. return res.json({
  25. ...BaseStdResponse.MISSING_PARAMETER
  26. })
  27. if (!type)
  28. return res.json({
  29. ...BaseStdResponse.ERR,
  30. msg: '不支持的第三方登录类型'
  31. })
  32. try {
  33. let { social_uid, nickname, faceimg, ip } = await fetchUniLoginProfile(type, code)
  34. const session = uuidv4()
  35. const time = new Date().getTime()
  36. let binding = await getBindingByIdentity(type, social_uid)
  37. let uuid = binding?.user_uuid
  38. let selectRows = []
  39. if (uuid) {
  40. selectRows = await db.query('SELECT * FROM users WHERE uuid = ? LIMIT 1', [uuid])
  41. } else {
  42. const legacyUser = await getLegacyUserByIdentity(type, social_uid)
  43. if (legacyUser?.uuid) {
  44. uuid = legacyUser.uuid
  45. await insertSocialBinding(uuid, type, social_uid, nickname, faceimg)
  46. selectRows = await db.query('SELECT * FROM users WHERE uuid = ? LIMIT 1', [uuid])
  47. }
  48. }
  49. if (!uuid) {
  50. uuid = uuidv4()
  51. const username = `用户${uuid.slice(0, 8)}`
  52. let regSql = 'INSERT INTO users (uuid, username, registTime, social_uid, social_type, nickname, avatar, email) VALUES (?,?,?,?,?,?,?,?) '
  53. let regRows = await db.query(regSql, [uuid, username, time, social_uid, type, nickname, faceimg, '未设置'])
  54. if (!regRows || regRows.affectedRows !== 1) {
  55. this.logger.error(`聚合登录用户注册失败!数据库错误`)
  56. return res.json({
  57. ...BaseStdResponse.ERR,
  58. msg: '用户注册失败!'
  59. })
  60. }
  61. await insertSocialBinding(uuid, type, social_uid, nickname, faceimg)
  62. selectRows = await db.query('SELECT * FROM users WHERE uuid = ? LIMIT 1', [uuid])
  63. }
  64. if (!selectRows || selectRows.length === 0)
  65. return res.json({
  66. ...BaseStdResponse.ERR,
  67. msg: '用户登录失败!请稍后再试'
  68. })
  69. const user = selectRows[0]
  70. if (Number(user.is_banned) === 1)
  71. return res.json({
  72. ...BaseStdResponse.ERR,
  73. msg: '账号已被封禁,如有疑问请邮件联系:service@xxoo365.top'
  74. })
  75. await Redis.set(`userSession:${uuid}`, session, {
  76. EX: 2592000
  77. })
  78. await db.query(
  79. 'UPDATE users SET lastTime = ?, nickname = ? WHERE uuid = ?',
  80. [time, nickname, uuid]
  81. )
  82. await updateSocialBindingProfile(type, social_uid, nickname, faceimg)
  83. await syncLegacySocialMirror(uuid, type)
  84. res.json({
  85. ...BaseStdResponse.OK,
  86. data: {
  87. uuid,
  88. username: user.username,
  89. session,
  90. nickname,
  91. type,
  92. roles: user.permission || [],
  93. vip: user.vip,
  94. ic_count: user.ic_count,
  95. lepao_count: user.lepao_count,
  96. crouse_count: user.crouse_count,
  97. avatar: faceimg || user.avatar,
  98. email: user.email
  99. }
  100. })
  101. // 增加登录记录
  102. try {
  103. const userAgent = req.headers['user-agent']
  104. let insertSql = 'INSERT INTO login_history (uuid, time, deviceInfo, type, ip) VALUES (?, ?, ?, ?, ?)'
  105. await db.query(insertSql, [uuid, time, { 'ua': userAgent }, type, ip])
  106. } catch (error) {
  107. this.logger.error(`写入登录记录失败!${error}`)
  108. }
  109. } catch (error) {
  110. this.logger.error(`获取用户信息失败!${error.message || 'api接口错误'}`)
  111. return res.json({
  112. ...BaseStdResponse.ERR,
  113. msg: '获取用户信息失败!'
  114. })
  115. }
  116. }
  117. }
  118. module.exports.Login = Login