mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-12-01 01:05:30 +08:00
feat: windows自动化下统一逻辑
This commit is contained in:
1 parent
915ee73f95
commit
450668f5e6
14 files changed
+1907
-64
No files matched your search
@@ -0,0 +1,78 @@
|
||||
import type { MessageInsertCapability } from './msg-insert-capability-service'
|
||||
|
||||
/** 退群内提示开关的判定:授权或组件不成立时必须显示关闭;库没连只压 effective。 */
|
||||
|
||||
export interface ExitNoticeGateDecision {
|
||||
/** 界面开关值 */
|
||||
desiredEnabled: boolean
|
||||
/** 是否真的在生效 */
|
||||
effectiveEnabled: boolean
|
||||
/** 只在明确否定时落盘;探测失败不落盘,避免误关正常用户的开关。 */
|
||||
persistDisable: boolean
|
||||
}
|
||||
|
||||
/** 授权维度是否成立(不含微信库连接)。 */
|
||||
export function isEntitlementBlocked(capability: MessageInsertCapability): boolean {
|
||||
return capability.entitlementState !== 'valid' || capability.nativeComponentState !== 'ready'
|
||||
}
|
||||
|
||||
/** 明确否定(不含 unavailable:那是「不知道」,不是「不行」)。 */
|
||||
export function isDefinitelyBlocked(capability: MessageInsertCapability): boolean {
|
||||
return (
|
||||
capability.entitlementState === 'invalid' ||
|
||||
capability.entitlementState === 'unsupported' ||
|
||||
capability.nativeComponentState === 'missing' ||
|
||||
capability.nativeComponentState === 'invalid' ||
|
||||
capability.nativeComponentState === 'incompatible'
|
||||
)
|
||||
}
|
||||
|
||||
export function resolveExitNoticeGate(
|
||||
capability: MessageInsertCapability,
|
||||
runtimeEnabled: boolean
|
||||
): ExitNoticeGateDecision {
|
||||
if (isEntitlementBlocked(capability)) {
|
||||
return {
|
||||
desiredEnabled: false,
|
||||
effectiveEnabled: false,
|
||||
persistDisable: isDefinitelyBlocked(capability) && runtimeEnabled
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 授权与组件都就绪,只是微信库没连:用户意愿保留(连上自动恢复),
|
||||
* 但此刻没有真的在跑。
|
||||
*/
|
||||
if (runtimeEnabled && !capability.canEnable) {
|
||||
return { desiredEnabled: true, effectiveEnabled: false, persistDisable: false }
|
||||
}
|
||||
return {
|
||||
desiredEnabled: runtimeEnabled,
|
||||
effectiveEnabled: runtimeEnabled && capability.canEnable,
|
||||
persistDisable: false
|
||||
}
|
||||
}
|
||||
|
||||
/** 界面呈现结果(只有开关的两个布尔值,不含落盘决策)。 */
|
||||
export interface SwitchPresentation {
|
||||
desiredEnabled: boolean
|
||||
effectiveEnabled: boolean
|
||||
}
|
||||
|
||||
/** 两个开关共用这套呈现判定:授权不成立时都显示关闭,但关闭操作永远放行。 */
|
||||
export function resolveSwitchPresentation(
|
||||
kind: 'recall_notice' | 'group_exit_notice',
|
||||
currentDesired: boolean,
|
||||
capability: MessageInsertCapability
|
||||
): SwitchPresentation {
|
||||
if (kind === 'group_exit_notice') {
|
||||
const gate = resolveExitNoticeGate(capability, currentDesired)
|
||||
return { desiredEnabled: gate.desiredEnabled, effectiveEnabled: gate.effectiveEnabled }
|
||||
}
|
||||
if (isEntitlementBlocked(capability)) {
|
||||
return { desiredEnabled: false, effectiveEnabled: false }
|
||||
}
|
||||
return {
|
||||
desiredEnabled: currentDesired,
|
||||
effectiveEnabled: currentDesired && capability.canEnable
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
import crypto from 'node:crypto'
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
|
||||
/**
|
||||
* 发送能力组件定位:读 current.json 找目录,按 manifest 校验 size/sha256,不过当没装。
|
||||
* 组件放用户目录,避免被自动更新清掉。
|
||||
*/
|
||||
|
||||
export interface ComponentLocation {
|
||||
dir: string
|
||||
helperPath: string
|
||||
version: string
|
||||
/** 打包时工作区是否 dirty(内部调试构建标识,正式发布不应出现) */
|
||||
dirty: boolean
|
||||
}
|
||||
|
||||
interface ManifestFile {
|
||||
path?: unknown
|
||||
size?: unknown
|
||||
sha256?: unknown
|
||||
}
|
||||
|
||||
const HELPER_NAME = 'tm-wcdb-config-helper.exe'
|
||||
|
||||
const verifyCache = new Map<string, { stamp: string; ok: boolean; dirty: boolean }>()
|
||||
|
||||
function sha256File(filePath: string): string | null {
|
||||
try {
|
||||
const hash = crypto.createHash('sha256')
|
||||
hash.update(fs.readFileSync(filePath))
|
||||
return hash.digest('hex')
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function verifyManifest(dir: string): { ok: boolean; dirty: boolean } {
|
||||
const manifestPath = path.join(dir, 'manifest.json')
|
||||
let stat: fs.Stats
|
||||
try {
|
||||
stat = fs.statSync(manifestPath)
|
||||
} catch {
|
||||
return { ok: false, dirty: false }
|
||||
}
|
||||
const stamp = `${dir}|${stat.mtimeMs}|${stat.size}`
|
||||
const cached = verifyCache.get(dir)
|
||||
if (cached && cached.stamp === stamp) return { ok: cached.ok, dirty: cached.dirty }
|
||||
|
||||
let ok = false
|
||||
let dirty = false
|
||||
try {
|
||||
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')) as {
|
||||
worktreeDirty?: unknown
|
||||
files?: ManifestFile[]
|
||||
}
|
||||
dirty = manifest.worktreeDirty === true
|
||||
const files = Array.isArray(manifest.files) ? manifest.files : []
|
||||
if (files.length === 0) {
|
||||
ok = false
|
||||
} else {
|
||||
ok = files.every((entry) => {
|
||||
const name = typeof entry.path === 'string' ? entry.path : ''
|
||||
if (!name) return false
|
||||
const target = path.join(dir, name)
|
||||
let size = -1
|
||||
try {
|
||||
size = fs.statSync(target).size
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
if (typeof entry.size === 'number' && size !== entry.size) return false
|
||||
if (typeof entry.sha256 === 'string' && entry.sha256.length === 64) {
|
||||
return sha256File(target) === entry.sha256
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
ok = false
|
||||
}
|
||||
verifyCache.set(dir, { stamp, ok, dirty })
|
||||
return { ok, dirty }
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回发送能力组件位置;不存在、校验失败、非 Windows 一律返回 null。
|
||||
* 调用方拿到 null 时应当把它当作「组件没装」,而不是「没授权」。
|
||||
*/
|
||||
export function resolveExternalComponent(
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
platform: NodeJS.Platform = process.platform
|
||||
): ComponentLocation | null {
|
||||
if (platform !== 'win32') return null
|
||||
const localAppData = env.LOCALAPPDATA
|
||||
if (!localAppData) return null
|
||||
|
||||
const root = path.join(localAppData, 'TraceMemo', 'Pro', 'windows-x64')
|
||||
const pointerPath = path.join(root, 'current.json')
|
||||
if (!fs.existsSync(pointerPath)) return null
|
||||
|
||||
let version = ''
|
||||
let dir = ''
|
||||
try {
|
||||
const pointer = JSON.parse(fs.readFileSync(pointerPath, 'utf8')) as {
|
||||
version?: unknown
|
||||
dir?: unknown
|
||||
}
|
||||
version = typeof pointer.version === 'string' ? pointer.version : ''
|
||||
dir = typeof pointer.dir === 'string' ? pointer.dir : ''
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
if (!version || !dir) return null
|
||||
try {
|
||||
if (!fs.statSync(dir).isDirectory()) return null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
|
||||
const helperPath = path.join(dir, HELPER_NAME)
|
||||
if (!fs.existsSync(helperPath)) return null
|
||||
|
||||
const verified = verifyManifest(dir)
|
||||
if (!verified.ok) return null
|
||||
|
||||
return { dir, helperPath, version, dirty: verified.dirty }
|
||||
}
|
||||
|
||||
/** 测试用:清掉校验缓存 */
|
||||
export function resetComponentCache(): void {
|
||||
verifyCache.clear()
|
||||
}
|
||||
@@ -0,0 +1,393 @@
|
||||
import fs from 'node:fs'
|
||||
import http from 'node:http'
|
||||
|
||||
import type { CapabilityReasonCode } from '../../shared/msg-insert-switch'
|
||||
import { hasMacNativeBundle, isMacNativeRuntimeUnavailable } from '../native-runtime-paths'
|
||||
import { resolveHelperPath } from './msg-insert-switch-service'
|
||||
|
||||
/**
|
||||
* 发送能力的唯一判定入口:授权、原生组件、库连接分开算,任一项不确定都不许开启。
|
||||
* 4290 的 JSON 本机可伪造,这里只是应用层门禁,真正的校验在原生 helper。
|
||||
*/
|
||||
|
||||
export type { CapabilityReasonCode }
|
||||
|
||||
export type EntitlementState = 'valid' | 'invalid' | 'pending' | 'unavailable' | 'unsupported'
|
||||
|
||||
export type NativeComponentState = 'ready' | 'missing' | 'invalid' | 'incompatible'
|
||||
|
||||
export interface MessageInsertCapability {
|
||||
/** 授权 */
|
||||
entitlementState: EntitlementState
|
||||
/** 发送能力组件 */
|
||||
nativeComponentState: NativeComponentState
|
||||
/** 微信数据库是否已连接(不代表发送能力) */
|
||||
databaseReady: boolean
|
||||
/** 是否允许「开启」发送能力 */
|
||||
canEnable: boolean
|
||||
/** 关闭永远不因授权被拒,否则用户没法把自己开过的功能关掉。 */
|
||||
canDisable: boolean
|
||||
reasonCode: CapabilityReasonCode
|
||||
/** 给人看的一句话 */
|
||||
message: string
|
||||
/** Hook 原始状态名,仅用于诊断展示 */
|
||||
licenseStatus?: string
|
||||
/** 判定时间戳 */
|
||||
checkedAt: number
|
||||
}
|
||||
|
||||
export interface CapabilityInput {
|
||||
/** 微信库是否已连接(由主进程按 chat 状态传入) */
|
||||
databaseReady: boolean
|
||||
}
|
||||
|
||||
export interface LicenseProbeResult {
|
||||
ok: boolean
|
||||
status?: string
|
||||
error?: string
|
||||
}
|
||||
|
||||
export interface CapabilityDeps {
|
||||
platform: NodeJS.Platform
|
||||
/** mac:是否 native 包 */
|
||||
hasMacNativeBundle: () => boolean
|
||||
/** mac:是否 public 包(拿不到 native 运行时) */
|
||||
isMacRuntimeUnavailable: () => boolean
|
||||
/** 发送能力组件路径;null = 缺失 */
|
||||
resolveNativeComponent: () => string | null
|
||||
/** 授权探测(Windows) */
|
||||
probeLicense: (timeoutMs: number) => Promise<LicenseProbeResult>
|
||||
/** 组件文件探测(便于测试注入) */
|
||||
statFile?: (filePath: string) => { size: number } | null
|
||||
now?: () => number
|
||||
}
|
||||
|
||||
const DEFAULT_TTL_MS = 30_000
|
||||
const PROBE_TIMEOUT_MS = 1_200
|
||||
|
||||
/** Hook 的 LicenseStatus → 授权状态。只有明确可接受的结果才算 valid。 */
|
||||
export function mapLicenseStatus(status: string | undefined): {
|
||||
entitlement: EntitlementState
|
||||
reasonCode: CapabilityReasonCode
|
||||
message: string
|
||||
} {
|
||||
switch (status) {
|
||||
case 'LICENSE_OK':
|
||||
return { entitlement: 'valid', reasonCode: 'CAPABILITY_READY', message: '授权有效' }
|
||||
case 'LICENSE_CHECKING':
|
||||
return {
|
||||
entitlement: 'pending',
|
||||
reasonCode: 'LICENSE_PENDING',
|
||||
message: '授权正在验证,请稍候'
|
||||
}
|
||||
case 'LICENSE_EXPIRED':
|
||||
return {
|
||||
entitlement: 'invalid',
|
||||
reasonCode: 'LICENSE_EXPIRED',
|
||||
message: '授权已过期,请重新激活后再开启'
|
||||
}
|
||||
case 'LICENSE_MACHINE_MISMATCH':
|
||||
return {
|
||||
entitlement: 'invalid',
|
||||
reasonCode: 'LICENSE_MACHINE_MISMATCH',
|
||||
message: '这份授权不属于本机'
|
||||
}
|
||||
case 'LICENSE_REVOKED':
|
||||
return {
|
||||
entitlement: 'invalid',
|
||||
reasonCode: 'LICENSE_REVOKED',
|
||||
message: '授权已被吊销'
|
||||
}
|
||||
case 'LICENSE_INVALID':
|
||||
case 'LICENSE_TAMPERED':
|
||||
return {
|
||||
entitlement: 'invalid',
|
||||
reasonCode: 'LICENSE_INVALID',
|
||||
message: '授权无效,请重新激活'
|
||||
}
|
||||
case 'LICENSE_NOT_FOUND':
|
||||
return {
|
||||
entitlement: 'invalid',
|
||||
reasonCode: 'LICENSE_REQUIRED',
|
||||
message: '未检测到有效授权'
|
||||
}
|
||||
case 'LICENSE_NETWORK_ERROR':
|
||||
case 'LICENSE_SERVER_ERROR':
|
||||
return {
|
||||
entitlement: 'unavailable',
|
||||
reasonCode: 'LICENSE_UNAVAILABLE',
|
||||
message: '授权服务暂时不可用,稍后再试'
|
||||
}
|
||||
default:
|
||||
// 未知状态一律 fail-closed,但要说清「是未知」而不是「你没买」
|
||||
return {
|
||||
entitlement: 'unavailable',
|
||||
reasonCode: 'LICENSE_UNAVAILABLE',
|
||||
message: '暂时无法确认授权状态'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 探测本机 Hook 的授权接口。失败只报失败,不臆测状态。 */
|
||||
export function probeLicenseStatus(timeoutMs = PROBE_TIMEOUT_MS): Promise<LicenseProbeResult> {
|
||||
return new Promise((resolve) => {
|
||||
let settled = false
|
||||
const done = (result: LicenseProbeResult): void => {
|
||||
if (settled) return
|
||||
settled = true
|
||||
resolve(result)
|
||||
}
|
||||
let req: http.ClientRequest
|
||||
try {
|
||||
req = http.get(
|
||||
{ host: '127.0.0.1', port: 4290, path: '/license/status', timeout: timeoutMs },
|
||||
(res) => {
|
||||
const chunks: Buffer[] = []
|
||||
res.on('data', (chunk: Buffer) => chunks.push(chunk))
|
||||
res.on('end', () => {
|
||||
try {
|
||||
const body = JSON.parse(Buffer.concat(chunks).toString('utf8')) as {
|
||||
status?: unknown
|
||||
}
|
||||
done({ ok: true, status: typeof body.status === 'string' ? body.status : '' })
|
||||
} catch {
|
||||
done({ ok: false, error: 'BAD_JSON' })
|
||||
}
|
||||
})
|
||||
res.on('error', () => done({ ok: false, error: 'RESPONSE_ERROR' }))
|
||||
}
|
||||
)
|
||||
} catch (error) {
|
||||
done({ ok: false, error: error instanceof Error ? error.message : String(error) })
|
||||
return
|
||||
}
|
||||
req.on('timeout', () => {
|
||||
req.destroy()
|
||||
done({ ok: false, error: 'TIMEOUT' })
|
||||
})
|
||||
req.on('error', (error: Error) => done({ ok: false, error: error.message }))
|
||||
})
|
||||
}
|
||||
|
||||
/** 粗检组件:非 PE 或架构不符判 invalid,不做哈希白名单。 */
|
||||
export function inspectNativeComponent(
|
||||
filePath: string | null,
|
||||
arch: string,
|
||||
statFile: (p: string) => { size: number } | null
|
||||
): NativeComponentState {
|
||||
if (!filePath) return 'missing'
|
||||
const stat = statFile(filePath)
|
||||
if (!stat || stat.size < 1024) return 'missing'
|
||||
try {
|
||||
const fd = fs.openSync(filePath, 'r')
|
||||
try {
|
||||
const head = Buffer.alloc(0x40)
|
||||
fs.readSync(fd, head, 0, 0x40, 0)
|
||||
if (head[0] !== 0x4d || head[1] !== 0x5a) return 'invalid' // "MZ"
|
||||
const peOffset = head.readUInt32LE(0x3c)
|
||||
const pe = Buffer.alloc(6)
|
||||
fs.readSync(fd, pe, 0, 6, peOffset)
|
||||
if (pe[0] !== 0x50 || pe[1] !== 0x45) return 'invalid' // "PE"
|
||||
const machine = pe.readUInt16LE(4)
|
||||
const expected = arch === 'arm64' ? 0xaa64 : 0x8664
|
||||
return machine === expected ? 'ready' : 'incompatible'
|
||||
} finally {
|
||||
fs.closeSync(fd)
|
||||
}
|
||||
} catch {
|
||||
return 'invalid'
|
||||
}
|
||||
}
|
||||
|
||||
export class MessageInsertCapabilityService {
|
||||
private cached: MessageInsertCapability | null = null
|
||||
private inflight: Promise<MessageInsertCapability> | null = null
|
||||
private readonly now: () => number
|
||||
|
||||
constructor(private readonly deps: CapabilityDeps) {
|
||||
this.now = deps.now || (() => Date.now())
|
||||
}
|
||||
|
||||
/** 读缓存(页面刷新用);过期或从未算过就重新评估。 */
|
||||
async getCapability(input: CapabilityInput): Promise<MessageInsertCapability> {
|
||||
const now = this.now()
|
||||
if (this.cached && now - this.cached.checkedAt < DEFAULT_TTL_MS) {
|
||||
/* 数据库连接状态可能瞬间变化,缓存命中时也要用最新入参重算这一项 */
|
||||
const merged = this.compose(input, this.cached.entitlementState, this.cached.licenseStatus)
|
||||
this.cached = merged
|
||||
return merged
|
||||
}
|
||||
return this.refresh(input)
|
||||
}
|
||||
|
||||
/** 强制重新评估(写库前必须调用,不能相信页面上那份缓存)。 */
|
||||
async refresh(input: CapabilityInput): Promise<MessageInsertCapability> {
|
||||
if (this.inflight) return this.inflight
|
||||
this.inflight = this.evaluate(input).finally(() => {
|
||||
this.inflight = null
|
||||
})
|
||||
const result = await this.inflight
|
||||
this.cached = result
|
||||
return result
|
||||
}
|
||||
|
||||
/** 供主进程做「开启」前的最终裁决。 */
|
||||
async ensureCanEnable(input: CapabilityInput): Promise<MessageInsertCapability> {
|
||||
return this.refresh(input)
|
||||
}
|
||||
|
||||
/** 测试与设置变化时清缓存。 */
|
||||
invalidate(): void {
|
||||
this.cached = null
|
||||
}
|
||||
|
||||
private async evaluate(input: CapabilityInput): Promise<MessageInsertCapability> {
|
||||
const platform = this.deps.platform
|
||||
|
||||
if (platform !== 'win32' && platform !== 'darwin') {
|
||||
return this.build({
|
||||
input,
|
||||
entitlement: 'unsupported',
|
||||
licenseStatus: undefined,
|
||||
reasonCode: 'UNSUPPORTED_PLATFORM',
|
||||
message: '微信内消息写入目前仅支持 Windows 与 macOS'
|
||||
})
|
||||
}
|
||||
|
||||
if (platform === 'darwin') {
|
||||
if (!this.deps.hasMacNativeBundle()) {
|
||||
// OSS 包:如实说不提供,不假装「未授权」
|
||||
return this.build({
|
||||
input,
|
||||
entitlement: 'unsupported',
|
||||
licenseStatus: undefined,
|
||||
reasonCode: 'UNSUPPORTED_PLATFORM',
|
||||
message: '当前版本不包含微信内消息发送能力'
|
||||
})
|
||||
}
|
||||
return this.build({
|
||||
input,
|
||||
entitlement: 'valid',
|
||||
licenseStatus: undefined,
|
||||
reasonCode: 'CAPABILITY_READY',
|
||||
message: '授权有效'
|
||||
})
|
||||
}
|
||||
|
||||
const probe = await this.deps.probeLicense(PROBE_TIMEOUT_MS)
|
||||
if (!probe.ok) {
|
||||
return this.build({
|
||||
input,
|
||||
entitlement: 'unavailable',
|
||||
licenseStatus: undefined,
|
||||
reasonCode: 'LICENSE_UNAVAILABLE',
|
||||
message: '暂时无法确认授权状态'
|
||||
})
|
||||
}
|
||||
const mapped = mapLicenseStatus(probe.status)
|
||||
return this.build({
|
||||
input,
|
||||
entitlement: mapped.entitlement,
|
||||
licenseStatus: probe.status,
|
||||
reasonCode: mapped.reasonCode,
|
||||
message: mapped.message
|
||||
})
|
||||
}
|
||||
|
||||
private build(args: {
|
||||
input: CapabilityInput
|
||||
entitlement: EntitlementState
|
||||
licenseStatus?: string
|
||||
reasonCode: CapabilityReasonCode
|
||||
message: string
|
||||
}): MessageInsertCapability {
|
||||
return this.compose(args.input, args.entitlement, args.licenseStatus, args.reasonCode, args.message)
|
||||
}
|
||||
|
||||
private compose(
|
||||
input: CapabilityInput,
|
||||
entitlement: EntitlementState,
|
||||
licenseStatus: string | undefined,
|
||||
reasonCode?: CapabilityReasonCode,
|
||||
message?: string
|
||||
): MessageInsertCapability {
|
||||
const platform = this.deps.platform
|
||||
const supportedPlatform = platform === 'win32' || platform === 'darwin'
|
||||
const component = supportedPlatform
|
||||
? inspectNativeComponent(
|
||||
this.deps.resolveNativeComponent(),
|
||||
process.arch,
|
||||
this.deps.statFile || defaultStat
|
||||
)
|
||||
: 'missing'
|
||||
const databaseReady = input.databaseReady === true
|
||||
|
||||
let code: CapabilityReasonCode = reasonCode || 'CAPABILITY_READY'
|
||||
let text = message || ''
|
||||
if (!code || code === 'CAPABILITY_READY') {
|
||||
if (entitlement === 'unsupported') {
|
||||
code = 'UNSUPPORTED_PLATFORM'
|
||||
text = text || '当前版本不包含微信内消息发送能力'
|
||||
} else if (entitlement === 'unavailable') {
|
||||
code = 'LICENSE_UNAVAILABLE'
|
||||
text = text || '暂时无法确认授权状态'
|
||||
} else if (entitlement === 'pending') {
|
||||
code = 'LICENSE_PENDING'
|
||||
text = text || '授权正在验证,请稍候'
|
||||
} else if (entitlement === 'invalid') {
|
||||
code = 'LICENSE_REQUIRED'
|
||||
text = text || '未检测到有效授权'
|
||||
} else if (component !== 'ready') {
|
||||
code = component === 'missing' ? 'COMPONENT_MISSING' : 'COMPONENT_INVALID'
|
||||
text =
|
||||
component === 'missing'
|
||||
? '未安装发送能力组件'
|
||||
: '发送能力组件不完整或版本不匹配'
|
||||
} else if (!databaseReady) {
|
||||
code = 'DATABASE_UNAVAILABLE'
|
||||
text = '微信数据库尚未连接'
|
||||
} else {
|
||||
code = 'CAPABILITY_READY'
|
||||
text = '授权与组件均已就绪'
|
||||
}
|
||||
}
|
||||
|
||||
const canEnable =
|
||||
supportedPlatform &&
|
||||
entitlement === 'valid' &&
|
||||
component === 'ready' &&
|
||||
databaseReady &&
|
||||
code === 'CAPABILITY_READY'
|
||||
/* 关闭不受授权限制:用户丢了授权也必须能把已有规则关掉 */
|
||||
const canDisable = supportedPlatform
|
||||
|
||||
return {
|
||||
entitlementState: entitlement,
|
||||
nativeComponentState: component,
|
||||
databaseReady,
|
||||
canEnable,
|
||||
canDisable,
|
||||
reasonCode: code,
|
||||
message: text,
|
||||
...(licenseStatus ? { licenseStatus } : {}),
|
||||
checkedAt: this.now()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function defaultStat(filePath: string): { size: number } | null {
|
||||
try {
|
||||
const stat = fs.statSync(filePath)
|
||||
return stat.isFile() ? { size: stat.size } : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export const msgInsertCapabilityService = new MessageInsertCapabilityService({
|
||||
platform: process.platform,
|
||||
hasMacNativeBundle,
|
||||
isMacRuntimeUnavailable: isMacNativeRuntimeUnavailable,
|
||||
resolveNativeComponent: resolveHelperPath,
|
||||
probeLicense: probeLicenseStatus
|
||||
})
|
||||
@@ -30,6 +30,7 @@ import path from 'node:path'
|
||||
|
||||
import { findResource } from '../resource-paths'
|
||||
import { isMacNativeRuntimeUnavailable, resolveWcdbConfigHelper } from '../native-runtime-paths'
|
||||
import { resolveExternalComponent } from './external-component-locator'
|
||||
import type { Wcdb4Client } from '../wcdb4-client'
|
||||
import {
|
||||
MSG_INSERT_SWITCHES,
|
||||
@@ -158,10 +159,13 @@ export function emptyMsgInsertStatus(
|
||||
*/
|
||||
export function resolveHelperPath(): string | null {
|
||||
if (process.platform === 'darwin') return resolveWcdbConfigHelper()
|
||||
/* 优先用外置目录里的发送能力组件,避免自动更新清掉;校验不过当没装。 */
|
||||
const external = resolveExternalComponent()
|
||||
if (external) return external.helperPath
|
||||
/*
|
||||
* 平台/架构都要分。Windows 交付在 `resources/wcdb/win32/x64/`(注意是 win32 不是 windows),
|
||||
* 文件名带 `.exe`。以前这里三个候选全写死 macos —— Windows 上会恒返回 null,
|
||||
* 表现为「缺少写入组件」+ 开关永久置灰。
|
||||
* 表现为「缺少发送能力组件」+ 开关永久置灰。
|
||||
*
|
||||
* 同目录还有 wcdb_api.dll / WCDB.dll / SDL2.dll,MSVC 运行库在 resources/runtime/win32/,
|
||||
* 两者都由 preload-env.ts 加进 DLL 搜索路径,所以 helper.exe 能直接加载。
|
||||
@@ -657,7 +661,7 @@ export async function applyMsgInsertSwitch(
|
||||
status: emptyMsgInsertStatus(input.name, 'HELPER_NOT_FOUND', '暂无法使用'),
|
||||
errorCode: 'HELPER_NOT_FOUND',
|
||||
/* 面向用户是能力语义;errorCode 仍是 HELPER_NOT_FOUND,门禁/授权层照旧能判。 */
|
||||
message: '暂无法使用:当前安装不包含微信内消息写入组件'
|
||||
message: '暂无法使用:当前安装不包含发送能力组件'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,8 +33,9 @@ import { app } from 'electron'
|
||||
import type { GroupMemberExitedEvent } from '../../shared/group-exit-event'
|
||||
import { renderLeaveNotificationText } from '../../shared/group-exit-event'
|
||||
import { GROUP_EXIT_NOTIFICATION_TEMPLATE } from '../../shared/group-exit-monitor'
|
||||
import { findResource } from '../resource-paths'
|
||||
import { isMacNativeRuntimeUnavailable, resolveWcdbConfigHelper } from '../native-runtime-paths'
|
||||
import { resolveWcdbConfigHelper } from '../native-runtime-paths'
|
||||
import { msgInsertCapabilityService } from './msg-insert-capability-service'
|
||||
import { resolveHelperPath } from './msg-insert-switch-service'
|
||||
|
||||
export type LocalExitNoticeOutcome =
|
||||
| 'inserted'
|
||||
@@ -58,28 +59,18 @@ interface StoredState {
|
||||
enabled?: boolean
|
||||
}
|
||||
|
||||
const HELPER_NAME = 'tm-wcdb-config-helper'
|
||||
const HELPER_TIMEOUT_MS = 20_000
|
||||
/** 最近若干次插入结果,只用于诊断展示,不参与判定 */
|
||||
const RECENT_LIMIT = 50
|
||||
|
||||
function helperPath(): string | null {
|
||||
if (process.platform === 'darwin') return resolveWcdbConfigHelper()
|
||||
/* 与 msg-insert-switch-service 的 resolveHelperPath 保持一致:
|
||||
* Windows 走 wcdb/win32/<arch>/ 且文件名带 .exe。 */
|
||||
const arch = process.arch === 'x64' ? 'x64' : 'arm64'
|
||||
const other = arch === 'arm64' ? 'x64' : 'arm64'
|
||||
const platformDir = process.platform === 'win32' ? 'win32' : 'macos'
|
||||
const suffix = process.platform === 'win32' ? '.exe' : ''
|
||||
for (const candidate of [
|
||||
`wcdb/${platformDir}/${arch}/${HELPER_NAME}${suffix}`,
|
||||
`wcdb/${platformDir}/${other}/${HELPER_NAME}${suffix}`,
|
||||
`wcdb/${platformDir}/${HELPER_NAME}${suffix}`
|
||||
]) {
|
||||
const resolved = findResource(candidate)
|
||||
if (resolved) return resolved
|
||||
}
|
||||
return null
|
||||
/*
|
||||
* 不在这里重复一份 resources 搜索逻辑。
|
||||
* Windows 上 helper 可能被部署到外置目录,
|
||||
* 只有一处解析入口,两条链路才不会各解析各的。
|
||||
*/
|
||||
return resolveHelperPath()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -151,10 +142,14 @@ export class WechatLocalExitNoticeService {
|
||||
}
|
||||
try {
|
||||
this.ensureLoaded()
|
||||
if (isMacNativeRuntimeUnavailable()) {
|
||||
return this.finish({ ...base, outcome: 'disabled', code: 'SEND_CAPABILITY_UNAVAILABLE' })
|
||||
}
|
||||
if (!this.enabled) return this.finish({ ...base, outcome: 'disabled', code: 'DISABLED' })
|
||||
/* 判定走统一服务;未授权或超时一律不写,普通退群检测不受影响。 */
|
||||
const capability = await msgInsertCapabilityService.getCapability({
|
||||
databaseReady: true
|
||||
})
|
||||
if (!capability.canEnable) {
|
||||
return this.finish({ ...base, outcome: 'disabled', code: capability.reasonCode })
|
||||
}
|
||||
if (!event.eventId || !event.conversationId) {
|
||||
return this.finish({ ...base, outcome: 'failed', code: 'EVENT_INCOMPLETE' })
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user