Login.js 4.3 KB

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