Login.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const API = require("../../../lib/API")
  2. const axios = require('axios')
  3. const { v4: uuidv4 } = require('uuid')
  4. const db = require("../../../plugin/DataBase/db")
  5. const Redis = require('../../../plugin/DataBase/Redis')
  6. const config = require('../../../config.json')
  7. const { BaseStdResponse } = require("../../../BaseStdResponse")
  8. class Login extends API {
  9. constructor() {
  10. super()
  11. this.setPath('/UniLogin/Login')
  12. this.setMethod('POST')
  13. }
  14. async onRequest(req, res) {
  15. let { type, code } = req.body
  16. if ([code].some(value => value === '' || value === null || value === undefined))
  17. return res.json({
  18. ...BaseStdResponse.MISSING_PARAMETER
  19. })
  20. const uniConfig = config.unilogin
  21. let url = `${uniConfig.url}/connect.php?act=callback&appid=${uniConfig.appid}&appkey=${uniConfig.appkey}&type=${type || 'qq'}&code=${code}`
  22. try {
  23. const agent = new https.Agent({
  24. rejectUnauthorized: false
  25. })
  26. const r = await axios.get(url, {
  27. httpsAgent: agent,
  28. proxy: false
  29. })
  30. if (!r || r.data?.code !== 0) {
  31. this.logger.error(`获取用户信息失败!${r.data?.msg || 'api接口错误'}`)
  32. return res.json({
  33. ...BaseStdResponse.ERR,
  34. msg: '获取用户信息失败!'
  35. })
  36. }
  37. let { social_uid, nickname, faceimg, ip } = r.data
  38. const session = uuidv4()
  39. const time = new Date().getTime()
  40. let selectSql = 'SELECT uuid, username, permission FROM users WHERE social_uid = ? AND social_type = ?'
  41. let selectRows = await db.query(selectSql, [social_uid, type || 'qq'])
  42. let uuid, username, permission
  43. // 用户不存在 执行注册操作
  44. if (selectRows.length == 0) {
  45. uuid = uuidv4()
  46. username = `用户${uuid.slice(0, 8)}`
  47. let regSql = 'INSERT INTO users (uuid, username, registTime, social_uid, social_type, nickname, avatar, email) VALUES (?,?,?,?,?,?,?,?) '
  48. let regRows = await db.query(regSql, [uuid, username, time, social_uid, type || 'qq', nickname, faceimg, '未设置'])
  49. if (!regRows || regRows.affectedRows !== 1) {
  50. this.logger.error(`聚合登录用户注册失败!数据库错误`)
  51. return res.json({
  52. ...BaseStdResponse.ERR,
  53. msg: '用户注册失败!'
  54. })
  55. }
  56. }
  57. else {
  58. uuid = selectRows[0].uuid
  59. username = selectRows[0].username
  60. permission = selectRows[0].permission
  61. }
  62. await Redis.set(`userSession:${uuid}`, session, {
  63. EX: 2592000
  64. })
  65. res.json({
  66. ...BaseStdResponse.OK,
  67. data: {
  68. uuid,
  69. username,
  70. session,
  71. nickname,
  72. type: type || 'qq',
  73. roles: permission || [],
  74. avatar: faceimg,
  75. }
  76. })
  77. // 增加登录记录
  78. try {
  79. if (selectRows.length !== 0) {
  80. let updateSql = 'UPDATE users SET lastTime = ?, avatar = ?, nickname = ? WHERE social_uid = ? AND social_type = ?'
  81. await db.query(updateSql, [time, faceimg, nickname, social_uid, type || 'qq'])
  82. }
  83. const userAgent = req.headers['user-agent']
  84. let insertSql = 'INSERT INTO login_history (uuid, time, deviceInfo, type, ip) VALUES (?, ?, ?, ?, ?)'
  85. await db.query(insertSql, [uuid, time, { 'ua': userAgent }, type || 'qq', ip])
  86. } catch (error) {
  87. this.logger.error(`写入登录记录失败!${error}`)
  88. }
  89. } catch (error) {
  90. this.logger.error(`获取用户信息失败!${error.message || 'api接口错误'}`)
  91. return res.json({
  92. ...BaseStdResponse.ERR,
  93. msg: '获取用户信息失败!'
  94. })
  95. }
  96. }
  97. }
  98. module.exports.Login = Login