同步修改页面逻辑
This commit is contained in:
@@ -125,6 +125,21 @@ async function getDevicesFromSupabase({ user_id, active } = {}) {
|
||||
return data
|
||||
}
|
||||
|
||||
async function getMerchantDevicesFromSupabase({ merchant_id, active } = {}) {
|
||||
let q = 'push_devices'
|
||||
const params = []
|
||||
if (merchant_id) params.push(`merchant_id=eq.${encodeURIComponent(merchant_id)}`)
|
||||
if (active != null) params.push(`is_active=eq.${encodeURIComponent(active ? 'true' : 'false')}`)
|
||||
const suffix = params.length ? `?${params.join('&')}` : ''
|
||||
const resp = await supaFetch(`${q}${suffix}`, { method: 'GET' })
|
||||
if (!resp.ok) {
|
||||
const txt = await resp.text()
|
||||
throw new Error(`supabase get merchant devices failed ${resp.status} ${txt}`)
|
||||
}
|
||||
const data = await resp.json()
|
||||
return data
|
||||
}
|
||||
|
||||
async function start() {
|
||||
await ensureDataDir()
|
||||
const app = express()
|
||||
@@ -350,18 +365,31 @@ async function start() {
|
||||
|
||||
// 注册或更新设备
|
||||
app.post('/api/v1/push/register', async (req, res) => {
|
||||
const { cid, user_id, platform } = req.body || {}
|
||||
const { cid, user_id, merchant_id, platform, appid, registration_source } = req.body || {}
|
||||
if (!cid) return res.status(400).json({ error: 'cid required' })
|
||||
const devices = await readDevices()
|
||||
let found = devices.find(d => d.cid === cid)
|
||||
const now = new Date().toISOString()
|
||||
if (found) {
|
||||
found.user_id = user_id ?? found.user_id
|
||||
found.merchant_id = merchant_id ?? found.merchant_id
|
||||
found.platform = platform ?? found.platform
|
||||
found.appid = appid ?? found.appid
|
||||
found.registration_source = registration_source ?? found.registration_source
|
||||
found.updated_at = now
|
||||
found.active = true
|
||||
} else {
|
||||
found = { cid, user_id: user_id ?? null, platform: platform ?? null, created_at: now, updated_at: now, active: true }
|
||||
found = {
|
||||
cid,
|
||||
user_id: user_id ?? null,
|
||||
merchant_id: merchant_id ?? null,
|
||||
platform: platform ?? null,
|
||||
appid: appid ?? 'default',
|
||||
registration_source: registration_source ?? null,
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
active: true
|
||||
}
|
||||
devices.push(found)
|
||||
}
|
||||
await writeDevices(devices)
|
||||
@@ -372,12 +400,14 @@ async function start() {
|
||||
const body = {
|
||||
cid: found.cid,
|
||||
user_id: found.user_id,
|
||||
merchant_id: found.merchant_id,
|
||||
platform: found.platform,
|
||||
appid: found.appid || 'default',
|
||||
is_active: true,
|
||||
last_seen_at: found.updated_at
|
||||
}
|
||||
const resp = await supaFetch(`push_devices?on_conflict=cid`, {
|
||||
if (found.registration_source) body.registration_source = found.registration_source
|
||||
const resp = await supaFetch(`push_devices?on_conflict=appid,cid`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Prefer: 'return=representation' },
|
||||
body: JSON.stringify(body)
|
||||
@@ -392,18 +422,20 @@ async function start() {
|
||||
})()
|
||||
}
|
||||
|
||||
return res.json({ ok: true, cid })
|
||||
return res.json({ ok: true, cid, user_id: found.user_id || null, merchant_id: found.merchant_id || null })
|
||||
})
|
||||
|
||||
// 注销设备(可选移除或置为 inactive)
|
||||
app.post('/api/v1/push/unregister', async (req, res) => {
|
||||
const { cid, user_id } = req.body || {}
|
||||
if (!cid && !user_id) return res.status(400).json({ error: 'cid or user_id required' })
|
||||
const { cid, user_id, merchant_id } = req.body || {}
|
||||
if (!cid && !user_id && !merchant_id) return res.status(400).json({ error: 'cid or user_id or merchant_id required' })
|
||||
let devices = await readDevices()
|
||||
if (cid) {
|
||||
devices = devices.map(d => d.cid === cid ? Object.assign({}, d, { active: false, updated_at: new Date().toISOString() }) : d)
|
||||
} else if (user_id) {
|
||||
devices = devices.map(d => d.user_id === user_id ? Object.assign({}, d, { active: false, updated_at: new Date().toISOString() }) : d)
|
||||
} else if (merchant_id) {
|
||||
devices = devices.map(d => d.merchant_id === merchant_id ? Object.assign({}, d, { active: false, updated_at: new Date().toISOString() }) : d)
|
||||
}
|
||||
await writeDevices(devices)
|
||||
// 同步到 Supabase(如果可用)
|
||||
@@ -423,6 +455,12 @@ async function start() {
|
||||
headers: { 'Content-Type': 'application/json', Prefer: 'return=representation' },
|
||||
body: JSON.stringify({ is_active: false, updated_at: now })
|
||||
})
|
||||
} else if (merchant_id) {
|
||||
await supaFetch(`push_devices?merchant_id=eq.${encodeURIComponent(merchant_id)}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json', Prefer: 'return=representation' },
|
||||
body: JSON.stringify({ is_active: false, updated_at: now })
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Supabase unregister exception:', e)
|
||||
@@ -435,14 +473,17 @@ async function start() {
|
||||
|
||||
// 列出设备
|
||||
app.get('/api/v1/push/devices', async (req, res) => {
|
||||
const { user_id, active } = req.query
|
||||
const { user_id, merchant_id, active } = req.query
|
||||
try {
|
||||
if (SUPA_URL && SUPA_KEY) {
|
||||
const devices = await getDevicesFromSupabase({ user_id, active: active == null ? undefined : (active === 'true') })
|
||||
let devices = []
|
||||
if (merchant_id) devices = await getMerchantDevicesFromSupabase({ merchant_id, active: active == null ? undefined : (active === 'true') })
|
||||
else devices = await getDevicesFromSupabase({ user_id, active: active == null ? undefined : (active === 'true') })
|
||||
return res.json({ ok: true, total: devices.length, data: devices })
|
||||
}
|
||||
let devices = await readDevices()
|
||||
if (user_id) devices = devices.filter(d => String(d.user_id) === String(user_id))
|
||||
if (merchant_id) devices = devices.filter(d => String(d.merchant_id) === String(merchant_id))
|
||||
if (active != null) devices = devices.filter(d => String(!!d.active) === String(active === 'true'))
|
||||
res.json({ ok: true, total: devices.length, data: devices })
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user