BindSocial.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const API = require("../../../lib/API")
  2. const db = require("../../../plugin/DataBase/db")
  3. const AccessControl = require("../../../lib/AccessControl")
  4. const { BaseStdResponse } = require("../../../BaseStdResponse")
  5. const { fetchUniLoginProfile, normalizeSocialType } = require('../../../lib/UniLoginClient')
  6. const {
  7. getBindingByIdentity,
  8. getUserSocialBindings,
  9. insertSocialBinding,
  10. updateSocialBindingProfile,
  11. syncLegacySocialMirror,
  12. toSocialBindingSummary
  13. } = require('../../../lib/UserSocialBinding')
  14. class BindSocial extends API {
  15. constructor() {
  16. super()
  17. this.setPath('/UniLogin/BindSocial')
  18. this.setMethod('POST')
  19. }
  20. async onRequest(req, res) {
  21. let { uuid, session, type, code } = req.body
  22. type = normalizeSocialType(type)
  23. if ([uuid, session, code].some(value => value === '' || value === null || value === undefined))
  24. return res.json({
  25. ...BaseStdResponse.MISSING_PARAMETER
  26. })
  27. if (!type)
  28. return res.json({
  29. ...BaseStdResponse.ERR,
  30. msg: '不支持的第三方账号类型'
  31. })
  32. if (!await AccessControl.checkSession(uuid, session))
  33. return res.status(401).json({
  34. ...BaseStdResponse.ACCESS_DENIED
  35. })
  36. try {
  37. const { social_uid, nickname, faceimg } = await fetchUniLoginProfile(type, code)
  38. const identityBinding = await getBindingByIdentity(type, social_uid)
  39. if (identityBinding && identityBinding.user_uuid !== uuid)
  40. return res.json({
  41. ...BaseStdResponse.ERR,
  42. msg: '该第三方账号已绑定其他用户'
  43. })
  44. const bindings = await getUserSocialBindings(uuid)
  45. if (bindings.some(binding => binding.social_type === type))
  46. return res.json({
  47. ...BaseStdResponse.ERR,
  48. msg: `当前账号已绑定${type === 'qq' ? 'QQ' : '微信'}`
  49. })
  50. const result = await insertSocialBinding(uuid, type, social_uid, nickname, faceimg)
  51. if (!result || result.affectedRows !== 1)
  52. return res.json({
  53. ...BaseStdResponse.ERR,
  54. msg: '绑定失败,请稍后再试'
  55. })
  56. await updateSocialBindingProfile(type, social_uid, nickname, faceimg)
  57. await db.query(
  58. 'UPDATE users SET nickname = COALESCE(NULLIF(nickname, ""), ?), avatar = COALESCE(NULLIF(avatar, ""), ?) WHERE uuid = ?',
  59. [nickname, faceimg, uuid]
  60. )
  61. await syncLegacySocialMirror(uuid, type)
  62. const newBindings = await getUserSocialBindings(uuid)
  63. return res.json({
  64. ...BaseStdResponse.OK,
  65. data: {
  66. socialBindings: toSocialBindingSummary(newBindings),
  67. boundTypes: newBindings.map(binding => binding.social_type)
  68. }
  69. })
  70. } catch (error) {
  71. this.logger.error(`绑定第三方账号失败!${error.message || error}`)
  72. return res.json({
  73. ...BaseStdResponse.ERR,
  74. msg: '绑定失败,请稍后再试'
  75. })
  76. }
  77. }
  78. }
  79. module.exports.BindSocial = BindSocial