Login.js 4.9 KB

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