BindSocial.js 3.2 KB

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