Login.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, 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. username = selectRows[0].username
  63. permission = selectRows[0].permission
  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. } catch (error) {
  79. this.logger.error(`获取用户信息失败!${error.message || 'api接口错误'}`)
  80. return res.json({
  81. ...BaseStdResponse.ERR,
  82. msg: '获取用户信息失败!'
  83. })
  84. }
  85. }
  86. }
  87. module.exports.Login = Login;