| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- const db = require('../plugin/DataBase/db')
- const { normalizeSocialType } = require('./UniLoginClient')
- async function getBindingByIdentity(socialType, socialUid) {
- const type = normalizeSocialType(socialType)
- if (!type || !socialUid)
- return null
- const rows = await db.query(
- 'SELECT user_uuid, social_type, social_uid, social_nickname, social_avatar FROM user_social_bindings WHERE social_type = ? AND social_uid = ? LIMIT 1',
- [type, socialUid]
- )
- return rows?.[0] || null
- }
- async function getLegacyUserByIdentity(socialType, socialUid) {
- const type = normalizeSocialType(socialType)
- if (!type || !socialUid)
- return null
- try {
- const rows = await db.query(
- 'SELECT uuid, username, permission, avatar, nickname FROM users WHERE social_uid = ? AND social_type = ? LIMIT 1',
- [socialUid, type]
- )
- return rows?.[0] || null
- } catch (error) {
- if (error?.code === 'ER_BAD_FIELD_ERROR')
- return null
- throw error
- }
- }
- async function getUserSocialBindings(userUuid) {
- const rows = await db.query(
- 'SELECT social_type, social_uid, social_nickname, social_avatar FROM user_social_bindings WHERE user_uuid = ? ORDER BY FIELD(social_type, "qq", "wx")',
- [userUuid]
- )
- return rows || []
- }
- async function insertSocialBinding(userUuid, socialType, socialUid, socialNickname = null, socialAvatar = null) {
- const type = normalizeSocialType(socialType)
- if (!type || !socialUid)
- return null
- const now = new Date().getTime()
- return await db.query(
- 'INSERT IGNORE INTO user_social_bindings (user_uuid, social_type, social_uid, social_nickname, social_avatar, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)',
- [userUuid, type, socialUid, socialNickname, socialAvatar, now, now]
- )
- }
- async function updateSocialBindingProfile(socialType, socialUid, socialNickname = null, socialAvatar = null) {
- const type = normalizeSocialType(socialType)
- if (!type || !socialUid)
- return null
- const now = new Date().getTime()
- return await db.query(
- 'UPDATE user_social_bindings SET social_nickname = ?, social_avatar = ?, updated_at = ? WHERE social_type = ? AND social_uid = ?',
- [socialNickname, socialAvatar, now, type, socialUid]
- )
- }
- async function removeSocialBinding(userUuid, socialType) {
- const type = normalizeSocialType(socialType)
- if (!type)
- return null
- return await db.query(
- 'DELETE FROM user_social_bindings WHERE user_uuid = ? AND social_type = ?',
- [userUuid, type]
- )
- }
- function toSocialBindingSummary(bindings = []) {
- return ['qq', 'wx'].map(type => {
- const binding = bindings.find(item => item.social_type === type)
- return {
- type,
- bound: !!binding,
- social_uid: binding?.social_uid || '',
- nickname: binding?.social_nickname || '',
- avatar: binding?.social_avatar || ''
- }
- })
- }
- module.exports = {
- getBindingByIdentity,
- getLegacyUserByIdentity,
- getUserSocialBindings,
- insertSocialBinding,
- updateSocialBindingProfile,
- removeSocialBinding,
- toSocialBindingSummary
- }
|