Login.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const API = require("../../../lib/API")
  2. const axios = require('axios')
  3. const { v4: uuidv4 } = require('uuid')
  4. const UAParser = require('ua-parser-js')
  5. const db = require("../../../plugin/DataBase/db")
  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 r = await axios.get(url, {
  24. proxy: false
  25. })
  26. if (!r || r.data?.code !== 0) {
  27. this.logger.error(`获取用户信息失败!${r.data?.msg || 'api接口错误'}`)
  28. return res.json({
  29. ...BaseStdResponse.ERR,
  30. msg: '获取用户信息失败!'
  31. })
  32. }
  33. let { social_uid, nickname, faceimg, ip } = r.data
  34. const session = uuidv4()
  35. const time = new Date().getTime()
  36. let selectSql = 'SELECT uuid, username, permission FROM users WHERE social_uid = ? AND social_type = ?'
  37. let selectRows = await db.query(selectSql, [social_uid, type || 'qq'])
  38. let uuid, username, permission
  39. // 用户不存在 执行注册操作
  40. if (selectRows.length == 0) {
  41. uuid = uuidv4()
  42. username = `用户${uuid.slice(0, 8)}`
  43. let regSql = 'INSERT INTO users (uuid, username, session, registTime, social_uid, social_type, nickname, avatar, email) VALUES (?,?,?,?,?,?,?,?,?) '
  44. let regRows = await db.query(regSql, [uuid, username, session, time, social_uid, type || 'qq', nickname, faceimg, '未设置'])
  45. if (!regRows || regRows.affectedRows !== 1) {
  46. this.logger.error(`聚合登录用户注册失败!数据库错误`)
  47. return res.json({
  48. ...BaseStdResponse.ERR,
  49. msg: '用户注册失败!'
  50. })
  51. }
  52. }
  53. else {
  54. let updateSql = 'UPDATE users SET session = ?, lastTime = ?, avatar = ?, nickname = ? WHERE social_uid = ? AND social_type = ?'
  55. let updateRows = await db.query(updateSql, [session, time, faceimg, nickname, social_uid, type || 'qq'])
  56. if (!updateRows || updateRows.affectedRows !== 1) {
  57. this.logger.error(`聚合登录用户登录失败!数据库错误`)
  58. return res.json({
  59. ...BaseStdResponse.ERR,
  60. msg: '用户登录失败!请稍后再试'
  61. })
  62. }
  63. uuid = selectRows[0].uuid
  64. username = selectRows[0].username
  65. permission = selectRows[0].permission
  66. }
  67. res.json({
  68. ...BaseStdResponse.OK,
  69. data: {
  70. uuid,
  71. username,
  72. session,
  73. nickname,
  74. type: type || 'qq',
  75. roles: permission || [],
  76. avatar: faceimg,
  77. }
  78. })
  79. // 增加登录记录
  80. try {
  81. const userAgent = req.headers['user-agent']
  82. const parser = new UAParser(userAgent)
  83. const uaResult = parser.getResult()
  84. console.log(uaResult)
  85. let insertSql = 'INSERT INTO login_history (uuid, time, deviceInfo, type, ip) VALUES (?, ?, ?, ?, ?)'
  86. await db.query(insertSql, [uuid, time, uaResult, type || 'qq', ip])
  87. } catch (error) {
  88. this.logger.error(`写入登录记录失败!${error}`)
  89. }
  90. } catch (error) {
  91. this.logger.error(`获取用户信息失败!${error.message || 'api接口错误'}`)
  92. return res.json({
  93. ...BaseStdResponse.ERR,
  94. msg: '获取用户信息失败!'
  95. })
  96. }
  97. }
  98. }
  99. module.exports.Login = Login;