设置云触手默认登录设备
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import {
|
||||
DEFAULT_CLOUDTENTACLES_DEVICE_ID,
|
||||
DEFAULT_CLOUDTENTACLES_DEVICE_TYPE,
|
||||
normalizeCloudtentaclesDeviceId,
|
||||
normalizeCloudtentaclesDeviceType,
|
||||
} from './defaults.js'
|
||||
|
||||
test('normalizeCloudtentaclesDeviceId maps empty and legacy placeholder to default device', () => {
|
||||
assert.equal(normalizeCloudtentaclesDeviceId(''), DEFAULT_CLOUDTENTACLES_DEVICE_ID)
|
||||
assert.equal(normalizeCloudtentaclesDeviceId('-'), DEFAULT_CLOUDTENTACLES_DEVICE_ID)
|
||||
assert.equal(normalizeCloudtentaclesDeviceId(' custom-device '), 'custom-device')
|
||||
})
|
||||
|
||||
test('normalizeCloudtentaclesDeviceType maps empty and legacy zero to default type', () => {
|
||||
assert.equal(normalizeCloudtentaclesDeviceType(undefined), DEFAULT_CLOUDTENTACLES_DEVICE_TYPE)
|
||||
assert.equal(normalizeCloudtentaclesDeviceType(0), DEFAULT_CLOUDTENTACLES_DEVICE_TYPE)
|
||||
assert.equal(normalizeCloudtentaclesDeviceType('2'), 2)
|
||||
})
|
||||
@@ -0,0 +1,17 @@
|
||||
export const DEFAULT_CLOUDTENTACLES_DEVICE_ID =
|
||||
"08bc9d8c-fd15-48ea-bc00-8d754076cafc";
|
||||
export const DEFAULT_CLOUDTENTACLES_DEVICE_TYPE = 1;
|
||||
|
||||
export function normalizeCloudtentaclesDeviceId(value: unknown) {
|
||||
const normalized = String(value || "").trim();
|
||||
return normalized && normalized !== "-"
|
||||
? normalized
|
||||
: DEFAULT_CLOUDTENTACLES_DEVICE_ID;
|
||||
}
|
||||
|
||||
export function normalizeCloudtentaclesDeviceType(value: unknown) {
|
||||
const parsed = Number(value);
|
||||
return Number.isInteger(parsed) && parsed > 0
|
||||
? parsed
|
||||
: DEFAULT_CLOUDTENTACLES_DEVICE_TYPE;
|
||||
}
|
||||
@@ -1,6 +1,12 @@
|
||||
import { runtimeConfig } from '../../../config/runtime.js'
|
||||
|
||||
import type { RuntimeConfig } from '../../../types/runtime-config.js'
|
||||
import {
|
||||
DEFAULT_CLOUDTENTACLES_DEVICE_ID,
|
||||
DEFAULT_CLOUDTENTACLES_DEVICE_TYPE,
|
||||
normalizeCloudtentaclesDeviceId,
|
||||
normalizeCloudtentaclesDeviceType,
|
||||
} from './defaults.js'
|
||||
|
||||
type CloudtentaclesRuntimeConfig = RuntimeConfig['platforms']['cloudtentacles']
|
||||
type JsonObject = Record<string, any>
|
||||
@@ -40,8 +46,8 @@ export function resolveCloudtentaclesConfig(overrides: Partial<CloudtentaclesRun
|
||||
bindUrlProbeExtraCookie: '',
|
||||
publicKeyPem: '',
|
||||
clientSource: 'ct-client',
|
||||
deviceId: '-',
|
||||
deviceType: 0,
|
||||
deviceId: DEFAULT_CLOUDTENTACLES_DEVICE_ID,
|
||||
deviceType: DEFAULT_CLOUDTENTACLES_DEVICE_TYPE,
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -87,8 +93,8 @@ export function resolveCloudtentaclesConfig(overrides: Partial<CloudtentaclesRun
|
||||
bindUrlProbeExtraCookie: String(overrides.bindUrlProbeExtraCookie || baseConfig.bindUrlProbeExtraCookie || '').trim(),
|
||||
publicKeyPem: normalizePem(overrides.publicKeyPem || baseConfig.publicKeyPem),
|
||||
clientSource: String(overrides.clientSource || baseConfig.clientSource || 'ct-client').trim() || 'ct-client',
|
||||
deviceId: String(overrides.deviceId ?? baseConfig.deviceId ?? '-').trim() || '-',
|
||||
deviceType: normalizeInteger(overrides.deviceType ?? baseConfig.deviceType, 0),
|
||||
deviceId: normalizeCloudtentaclesDeviceId(overrides.deviceId ?? baseConfig.deviceId),
|
||||
deviceType: normalizeCloudtentaclesDeviceType(overrides.deviceType ?? baseConfig.deviceType),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,8 +120,8 @@ export function buildCloudtentaclesUrl(
|
||||
export function buildCloudtentaclesHeaders({
|
||||
token = '',
|
||||
contentType = 'application/json',
|
||||
deviceId = '-',
|
||||
deviceType = 0,
|
||||
deviceId = DEFAULT_CLOUDTENTACLES_DEVICE_ID,
|
||||
deviceType = DEFAULT_CLOUDTENTACLES_DEVICE_TYPE,
|
||||
extra = {},
|
||||
}: {
|
||||
token?: unknown
|
||||
@@ -127,8 +133,8 @@ export function buildCloudtentaclesHeaders({
|
||||
const headers: JsonObject = {
|
||||
accept: 'application/json, text/plain, */*',
|
||||
...(contentType ? { 'content-type': contentType } : {}),
|
||||
deviceid: String(deviceId || '-').trim() || '-',
|
||||
devicetype: String(Number(deviceType || 0)),
|
||||
deviceid: normalizeCloudtentaclesDeviceId(deviceId),
|
||||
devicetype: String(normalizeCloudtentaclesDeviceType(deviceType)),
|
||||
...normalizeHeaderMap(extra),
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,12 @@ import path from 'node:path'
|
||||
|
||||
import { PROJECT_ROOT } from '../../../config/runtime.js'
|
||||
import { createHttpError } from '../../../utils/http.js'
|
||||
import {
|
||||
DEFAULT_CLOUDTENTACLES_DEVICE_ID,
|
||||
DEFAULT_CLOUDTENTACLES_DEVICE_TYPE,
|
||||
normalizeCloudtentaclesDeviceId,
|
||||
normalizeCloudtentaclesDeviceType,
|
||||
} from './defaults.js'
|
||||
|
||||
const CLOUDTENTACLES_SESSION_FILE_PATH = path.join(PROJECT_ROOT, 'data', 'cloudtentacles-session.json')
|
||||
|
||||
@@ -166,8 +172,8 @@ function normalizeCloudtentaclesSessionState(rawValue: unknown) {
|
||||
username: String(source.username || '').trim(),
|
||||
phone: String(source.phone || '').trim(),
|
||||
loggedInAt: String(source.loggedInAt || '').trim(),
|
||||
deviceId: String(source.deviceId || '-').trim() || '-',
|
||||
deviceType: normalizeInteger(source.deviceType, 0),
|
||||
deviceId: normalizeCloudtentaclesDeviceId(source.deviceId),
|
||||
deviceType: normalizeCloudtentaclesDeviceType(source.deviceType),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,8 +209,8 @@ function createDefaultCloudtentaclesSessionState() {
|
||||
username: '',
|
||||
phone: '',
|
||||
loggedInAt: '',
|
||||
deviceId: '-',
|
||||
deviceType: 0,
|
||||
deviceId: DEFAULT_CLOUDTENTACLES_DEVICE_ID,
|
||||
deviceType: DEFAULT_CLOUDTENTACLES_DEVICE_TYPE,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,11 +222,6 @@ function createDefaultCloudtentaclesSessionStates(): CloudtentaclesSessionStates
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeInteger(value: unknown, fallback: number) {
|
||||
const parsed = Number(value)
|
||||
return Number.isInteger(parsed) ? parsed : fallback
|
||||
}
|
||||
|
||||
function isPlainObject(value: unknown): value is JsonObject {
|
||||
return Object.prototype.toString.call(value) === '[object Object]'
|
||||
}
|
||||
|
||||
@@ -3,6 +3,10 @@ import path from 'node:path'
|
||||
|
||||
import { PROJECT_ROOT } from '../../../config/runtime.js'
|
||||
import { createHttpError } from '../../../utils/http.js'
|
||||
import {
|
||||
normalizeCloudtentaclesDeviceId,
|
||||
normalizeCloudtentaclesDeviceType,
|
||||
} from './defaults.js'
|
||||
|
||||
const CLOUDTENTACLES_SOURCES_FILE_PATH = path.join(PROJECT_ROOT, 'data', 'cloudtentacles-sources.json')
|
||||
|
||||
@@ -142,8 +146,8 @@ function normalizeCloudtentaclesSourceItem(rawValue: unknown) {
|
||||
username: String(source.username || '').trim(),
|
||||
password: String(source.password || '').trim(),
|
||||
phone: String(source.phone || '').trim(),
|
||||
deviceId: String(source.deviceId || '-').trim() || '-',
|
||||
deviceType: normalizeInteger(source.deviceType, 0),
|
||||
deviceId: normalizeCloudtentaclesDeviceId(source.deviceId),
|
||||
deviceType: normalizeCloudtentaclesDeviceType(source.deviceType),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,11 +188,6 @@ function createDefaultCloudtentaclesSourcesConfig() {
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeInteger(value: unknown, fallback: number) {
|
||||
const parsed = Number(value)
|
||||
return Number.isInteger(parsed) ? parsed : fallback
|
||||
}
|
||||
|
||||
function isPlainObject(value: unknown): value is JsonObject {
|
||||
return Object.prototype.toString.call(value) === '[object Object]'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user