qqLoginStep1.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 qqLoginStep1 extends API {
  8. constructor() {
  9. super()
  10. this.setPath('/User/qqLoginStep1')
  11. this.setMethod('POST')
  12. }
  13. async onRequest(req, res) {
  14. let { qq, location, deviceInfo } = req.body
  15. if ([qq, deviceInfo].some(value => value === '' || value === null || value === undefined))
  16. return res.json({
  17. ...BaseStdResponse.MISSING_PARAMETER
  18. })
  19. if(String(qq).length > 10 || String(qq).length < 5)
  20. return res.json({
  21. ...BaseStdResponse.ERR,
  22. msg: '请输入正确的QQ号'
  23. })
  24. const uniConfig = config.unilogin
  25. let url = `${uniConfig.url}/connect.php?act=callback&appid=${uniConfig.appid}&appkey=${uniConfig.appkey}&type=${type || 'qq'}&code=${code}`
  26. try {
  27. const r = await axios.get(url, {
  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, 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, session, registTime, social_uid, social_type, nickname, avatar, email) VALUES (?,?,?,?,?,?,?,?,?) '
  48. let regRows = await db.query(regSql, [uuid, username, session, 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. let updateSql = 'UPDATE users SET session = ?, lastTime = ?, avatar = ?, nickname = ? WHERE social_uid = ? AND login_type = ?'
  59. let updateRows = await db.query(updateSql, [session, time, faceimg, nickname, social_uid, type || 'qq'])
  60. if (!updateRows || updateRows.affectedRows !== 1) {
  61. this.logger.error(`聚合登录用户登录失败!数据库错误`)
  62. return res.json({
  63. ...BaseStdResponse.ERR,
  64. msg: '用户登录失败!请稍后再试'
  65. })
  66. }
  67. username = selectRows[0].username
  68. permission = selectRows[0].permission
  69. }
  70. res.json({
  71. ...BaseStdResponse.OK,
  72. data: {
  73. uuid,
  74. username,
  75. session,
  76. nickname,
  77. type: type || 'qq',
  78. roles: permission || [],
  79. avatar: faceimg,
  80. }
  81. })
  82. // 增加登录记录
  83. } catch (error) {
  84. this.logger.error(`获取用户信息失败!${error.message || 'api接口错误'}`)
  85. return res.json({
  86. ...BaseStdResponse.ERR,
  87. msg: '获取用户信息失败!'
  88. })
  89. }
  90. }
  91. }
  92. module.exports.qqLoginStep1 = qqLoginStep1