UserSocialBinding.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const db = require('../plugin/DataBase/db')
  2. const { normalizeSocialType } = require('./UniLoginClient')
  3. async function getBindingByIdentity(socialType, socialUid) {
  4. const type = normalizeSocialType(socialType)
  5. if (!type || !socialUid)
  6. return null
  7. const rows = await db.query(
  8. 'SELECT user_uuid, social_type, social_uid, social_nickname, social_avatar FROM user_social_bindings WHERE social_type = ? AND social_uid = ? LIMIT 1',
  9. [type, socialUid]
  10. )
  11. return rows?.[0] || null
  12. }
  13. async function getLegacyUserByIdentity(socialType, socialUid) {
  14. const type = normalizeSocialType(socialType)
  15. if (!type || !socialUid)
  16. return null
  17. const rows = await db.query(
  18. 'SELECT uuid, username, permission, avatar, nickname FROM users WHERE social_uid = ? AND social_type = ? LIMIT 1',
  19. [socialUid, type]
  20. )
  21. return rows?.[0] || null
  22. }
  23. async function getUserSocialBindings(userUuid) {
  24. const rows = await db.query(
  25. 'SELECT social_type, social_uid, social_nickname, social_avatar FROM user_social_bindings WHERE user_uuid = ? ORDER BY FIELD(social_type, "qq", "wx")',
  26. [userUuid]
  27. )
  28. return rows || []
  29. }
  30. async function insertSocialBinding(userUuid, socialType, socialUid, socialNickname = null, socialAvatar = null) {
  31. const type = normalizeSocialType(socialType)
  32. if (!type || !socialUid)
  33. return null
  34. const now = new Date().getTime()
  35. return await db.query(
  36. 'INSERT IGNORE INTO user_social_bindings (user_uuid, social_type, social_uid, social_nickname, social_avatar, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)',
  37. [userUuid, type, socialUid, socialNickname, socialAvatar, now, now]
  38. )
  39. }
  40. async function updateSocialBindingProfile(socialType, socialUid, socialNickname = null, socialAvatar = null) {
  41. const type = normalizeSocialType(socialType)
  42. if (!type || !socialUid)
  43. return null
  44. const now = new Date().getTime()
  45. return await db.query(
  46. 'UPDATE user_social_bindings SET social_nickname = ?, social_avatar = ?, updated_at = ? WHERE social_type = ? AND social_uid = ?',
  47. [socialNickname, socialAvatar, now, type, socialUid]
  48. )
  49. }
  50. async function syncLegacySocialMirror(userUuid, preferredSocialType = null) {
  51. const bindings = await getUserSocialBindings(userUuid)
  52. const preferredType = normalizeSocialType(preferredSocialType)
  53. const binding = bindings.find(item => item.social_type === preferredType) || bindings[0]
  54. if (!binding) {
  55. return await db.query(
  56. 'UPDATE users SET social_uid = NULL, social_type = NULL WHERE uuid = ?',
  57. [userUuid]
  58. )
  59. }
  60. return await db.query(
  61. 'UPDATE users SET social_uid = ?, social_type = ? WHERE uuid = ?',
  62. [binding.social_uid, binding.social_type, userUuid]
  63. )
  64. }
  65. async function removeSocialBinding(userUuid, socialType) {
  66. const type = normalizeSocialType(socialType)
  67. if (!type)
  68. return null
  69. return await db.query(
  70. 'DELETE FROM user_social_bindings WHERE user_uuid = ? AND social_type = ?',
  71. [userUuid, type]
  72. )
  73. }
  74. function toSocialBindingSummary(bindings = []) {
  75. return ['qq', 'wx'].map(type => {
  76. const binding = bindings.find(item => item.social_type === type)
  77. return {
  78. type,
  79. bound: !!binding,
  80. social_uid: binding?.social_uid || '',
  81. nickname: binding?.social_nickname || '',
  82. avatar: binding?.social_avatar || ''
  83. }
  84. })
  85. }
  86. module.exports = {
  87. getBindingByIdentity,
  88. getLegacyUserByIdentity,
  89. getUserSocialBindings,
  90. insertSocialBinding,
  91. updateSocialBindingProfile,
  92. syncLegacySocialMirror,
  93. removeSocialBinding,
  94. toSocialBindingSummary
  95. }