UserSocialBinding.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. try {
  18. const rows = await db.query(
  19. 'SELECT uuid, username, permission, avatar, nickname FROM users WHERE social_uid = ? AND social_type = ? LIMIT 1',
  20. [socialUid, type]
  21. )
  22. return rows?.[0] || null
  23. } catch (error) {
  24. if (error?.code === 'ER_BAD_FIELD_ERROR')
  25. return null
  26. throw error
  27. }
  28. }
  29. async function getUserSocialBindings(userUuid) {
  30. const rows = await db.query(
  31. 'SELECT social_type, social_uid, social_nickname, social_avatar FROM user_social_bindings WHERE user_uuid = ? ORDER BY FIELD(social_type, "qq", "wx")',
  32. [userUuid]
  33. )
  34. return rows || []
  35. }
  36. async function insertSocialBinding(userUuid, socialType, socialUid, socialNickname = null, socialAvatar = null) {
  37. const type = normalizeSocialType(socialType)
  38. if (!type || !socialUid)
  39. return null
  40. const now = new Date().getTime()
  41. return await db.query(
  42. 'INSERT IGNORE INTO user_social_bindings (user_uuid, social_type, social_uid, social_nickname, social_avatar, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)',
  43. [userUuid, type, socialUid, socialNickname, socialAvatar, now, now]
  44. )
  45. }
  46. async function updateSocialBindingProfile(socialType, socialUid, socialNickname = null, socialAvatar = null) {
  47. const type = normalizeSocialType(socialType)
  48. if (!type || !socialUid)
  49. return null
  50. const now = new Date().getTime()
  51. return await db.query(
  52. 'UPDATE user_social_bindings SET social_nickname = ?, social_avatar = ?, updated_at = ? WHERE social_type = ? AND social_uid = ?',
  53. [socialNickname, socialAvatar, now, type, socialUid]
  54. )
  55. }
  56. async function removeSocialBinding(userUuid, socialType) {
  57. const type = normalizeSocialType(socialType)
  58. if (!type)
  59. return null
  60. return await db.query(
  61. 'DELETE FROM user_social_bindings WHERE user_uuid = ? AND social_type = ?',
  62. [userUuid, type]
  63. )
  64. }
  65. function toSocialBindingSummary(bindings = []) {
  66. return ['qq', 'wx'].map(type => {
  67. const binding = bindings.find(item => item.social_type === type)
  68. return {
  69. type,
  70. bound: !!binding,
  71. social_uid: binding?.social_uid || '',
  72. nickname: binding?.social_nickname || '',
  73. avatar: binding?.social_avatar || ''
  74. }
  75. })
  76. }
  77. module.exports = {
  78. getBindingByIdentity,
  79. getLegacyUserByIdentity,
  80. getUserSocialBindings,
  81. insertSocialBinding,
  82. updateSocialBindingProfile,
  83. removeSocialBinding,
  84. toSocialBindingSummary
  85. }