mirror of
https://github.com/hicccc77/WeFlow.git
synced 2026-04-03 15:08:25 +00:00
Merge branch 'dev' into dev
This commit is contained in:
119
electron/main.ts
119
electron/main.ts
@@ -30,16 +30,105 @@ import { cloudControlService } from './services/cloudControlService'
|
||||
import { destroyNotificationWindow, registerNotificationHandlers, showNotification } from './windows/notificationWindow'
|
||||
import { httpService } from './services/httpService'
|
||||
import { messagePushService } from './services/messagePushService'
|
||||
|
||||
import { bizService } from './services/bizService'
|
||||
|
||||
// 配置自动更新
|
||||
autoUpdater.autoDownload = false
|
||||
autoUpdater.autoInstallOnAppQuit = true
|
||||
autoUpdater.disableDifferentialDownload = true // 禁用差分更新,强制全量下载
|
||||
// Windows x64 与 arm64 使用不同更新通道,避免 latest.yml 互相覆盖导致下错架构安装包。
|
||||
if (process.platform === 'win32' && process.arch === 'arm64') {
|
||||
autoUpdater.channel = 'latest-arm64'
|
||||
// 更新通道策略:
|
||||
// - 稳定版(如 4.3.0)默认走 latest
|
||||
// - 预览版(如 4.3.0-preview.26.1)默认走 preview
|
||||
// - 开发版(如 4.3.0-dev.26.3.4)默认走 dev
|
||||
// - 用户可在设置页切换稳定/预览/开发,切换后即时生效
|
||||
// 同时区分 Windows x64 / arm64,避免更新清单互相覆盖。
|
||||
const appVersion = app.getVersion()
|
||||
const defaultUpdateTrack: 'stable' | 'preview' | 'dev' = (() => {
|
||||
if (/-preview\.\d+\.\d+$/i.test(appVersion)) return 'preview'
|
||||
if (/-dev\.\d+\.\d+\.\d+$/i.test(appVersion)) return 'dev'
|
||||
if (/(alpha|beta|rc)/i.test(appVersion)) return 'dev'
|
||||
return 'stable'
|
||||
})()
|
||||
const isPrereleaseBuild = defaultUpdateTrack !== 'stable'
|
||||
let configService: ConfigService | null = null
|
||||
|
||||
const normalizeUpdateTrack = (raw: unknown): 'stable' | 'preview' | 'dev' | null => {
|
||||
if (raw === 'stable' || raw === 'preview' || raw === 'dev') return raw
|
||||
return null
|
||||
}
|
||||
|
||||
const getEffectiveUpdateTrack = (): 'stable' | 'preview' | 'dev' => {
|
||||
const configuredTrack = normalizeUpdateTrack(configService?.get('updateChannel'))
|
||||
return configuredTrack || defaultUpdateTrack
|
||||
}
|
||||
|
||||
const isRemoteVersionNewer = (latestVersion: string, currentVersion: string): boolean => {
|
||||
const latest = String(latestVersion || '').trim()
|
||||
const current = String(currentVersion || '').trim()
|
||||
if (!latest || !current) return false
|
||||
|
||||
const parseVersion = (version: string) => {
|
||||
const normalized = version.replace(/^v/i, '')
|
||||
const [main, pre = ''] = normalized.split('-', 2)
|
||||
const core = main.split('.').map((segment) => Number.parseInt(segment, 10) || 0)
|
||||
const prerelease = pre ? pre.split('.').map((segment) => /^\d+$/.test(segment) ? Number.parseInt(segment, 10) : segment) : []
|
||||
return { core, prerelease }
|
||||
}
|
||||
|
||||
const compareParsedVersion = (a: ReturnType<typeof parseVersion>, b: ReturnType<typeof parseVersion>): number => {
|
||||
const maxLen = Math.max(a.core.length, b.core.length)
|
||||
for (let i = 0; i < maxLen; i += 1) {
|
||||
const left = a.core[i] || 0
|
||||
const right = b.core[i] || 0
|
||||
if (left > right) return 1
|
||||
if (left < right) return -1
|
||||
}
|
||||
|
||||
const aPre = a.prerelease
|
||||
const bPre = b.prerelease
|
||||
if (aPre.length === 0 && bPre.length === 0) return 0
|
||||
if (aPre.length === 0) return 1
|
||||
if (bPre.length === 0) return -1
|
||||
|
||||
const preMaxLen = Math.max(aPre.length, bPre.length)
|
||||
for (let i = 0; i < preMaxLen; i += 1) {
|
||||
const left = aPre[i]
|
||||
const right = bPre[i]
|
||||
if (left === undefined) return -1
|
||||
if (right === undefined) return 1
|
||||
if (left === right) continue
|
||||
|
||||
const leftNum = typeof left === 'number'
|
||||
const rightNum = typeof right === 'number'
|
||||
if (leftNum && rightNum) return left > right ? 1 : -1
|
||||
if (leftNum) return -1
|
||||
if (rightNum) return 1
|
||||
return String(left) > String(right) ? 1 : -1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
try {
|
||||
return autoUpdater.currentVersion.compare(latest) < 0
|
||||
} catch {
|
||||
return compareParsedVersion(parseVersion(latest), parseVersion(current)) > 0
|
||||
}
|
||||
}
|
||||
|
||||
const applyAutoUpdateChannel = (reason: 'startup' | 'settings' = 'startup') => {
|
||||
const track = getEffectiveUpdateTrack()
|
||||
const baseUpdateChannel = track === 'stable' ? 'latest' : track
|
||||
autoUpdater.allowPrerelease = track !== 'stable'
|
||||
autoUpdater.allowDowngrade = isPrereleaseBuild && track === 'stable'
|
||||
autoUpdater.channel =
|
||||
process.platform === 'win32' && process.arch === 'arm64'
|
||||
? `${baseUpdateChannel}-arm64`
|
||||
: baseUpdateChannel
|
||||
console.log(`[Update](${reason}) 当前版本 ${appVersion},渠道偏好: ${track},更新通道: ${autoUpdater.channel}`)
|
||||
}
|
||||
|
||||
applyAutoUpdateChannel('startup')
|
||||
const AUTO_UPDATE_ENABLED =
|
||||
process.env.AUTO_UPDATE_ENABLED === 'true' ||
|
||||
process.env.AUTO_UPDATE_ENABLED === '1' ||
|
||||
@@ -87,7 +176,6 @@ function sanitizePathEnv() {
|
||||
sanitizePathEnv()
|
||||
|
||||
// 单例服务
|
||||
let configService: ConfigService | null = null
|
||||
|
||||
// 协议窗口实例
|
||||
let agreementWindow: BrowserWindow | null = null
|
||||
@@ -243,6 +331,14 @@ const normalizeReleaseNotes = (rawReleaseNotes: unknown): string => {
|
||||
return cleaned
|
||||
}
|
||||
|
||||
const getDialogReleaseNotes = (rawReleaseNotes: unknown): string => {
|
||||
const track = getEffectiveUpdateTrack()
|
||||
if (track !== 'stable') {
|
||||
return '修复了一些已知问题'
|
||||
}
|
||||
return normalizeReleaseNotes(rawReleaseNotes)
|
||||
}
|
||||
|
||||
type AnnualReportYearsLoadStrategy = 'cache' | 'native' | 'hybrid'
|
||||
type AnnualReportYearsLoadPhase = 'cache' | 'native' | 'scan' | 'done'
|
||||
|
||||
@@ -1110,6 +1206,7 @@ const removeMatchedEntriesInDir = async (
|
||||
// 注册 IPC 处理器
|
||||
function registerIpcHandlers() {
|
||||
registerNotificationHandlers()
|
||||
bizService.registerHandlers()
|
||||
// 配置相关
|
||||
ipcMain.handle('config:get', async (_, key: string) => {
|
||||
return configService?.get(key as any)
|
||||
@@ -1117,6 +1214,9 @@ function registerIpcHandlers() {
|
||||
|
||||
ipcMain.handle('config:set', async (_, key: string, value: any) => {
|
||||
const result = configService?.set(key as any, value)
|
||||
if (key === 'updateChannel') {
|
||||
applyAutoUpdateChannel('settings')
|
||||
}
|
||||
void messagePushService.handleConfigChanged(key)
|
||||
return result
|
||||
})
|
||||
@@ -1238,11 +1338,11 @@ function registerIpcHandlers() {
|
||||
if (result && result.updateInfo) {
|
||||
const currentVersion = app.getVersion()
|
||||
const latestVersion = result.updateInfo.version
|
||||
if (latestVersion !== currentVersion) {
|
||||
if (isRemoteVersionNewer(latestVersion, currentVersion)) {
|
||||
return {
|
||||
hasUpdate: true,
|
||||
version: latestVersion,
|
||||
releaseNotes: normalizeReleaseNotes(result.updateInfo.releaseNotes),
|
||||
releaseNotes: getDialogReleaseNotes(result.updateInfo.releaseNotes),
|
||||
minimumVersion: (result.updateInfo as any).minimumVersion
|
||||
}
|
||||
}
|
||||
@@ -2696,7 +2796,7 @@ function checkForUpdatesOnStartup() {
|
||||
const latestVersion = result.updateInfo.version
|
||||
|
||||
// 检查是否有新版本
|
||||
if (latestVersion !== currentVersion && mainWindow) {
|
||||
if (isRemoteVersionNewer(latestVersion, currentVersion) && mainWindow) {
|
||||
// 检查该版本是否被用户忽略
|
||||
const ignoredVersion = configService?.get('ignoredUpdateVersion')
|
||||
if (ignoredVersion === latestVersion) {
|
||||
@@ -2707,7 +2807,7 @@ function checkForUpdatesOnStartup() {
|
||||
// 通知渲染进程有新版本
|
||||
mainWindow.webContents.send('app:updateAvailable', {
|
||||
version: latestVersion,
|
||||
releaseNotes: normalizeReleaseNotes(result.updateInfo.releaseNotes),
|
||||
releaseNotes: getDialogReleaseNotes(result.updateInfo.releaseNotes),
|
||||
minimumVersion: (result.updateInfo as any).minimumVersion
|
||||
})
|
||||
}
|
||||
@@ -2741,6 +2841,7 @@ app.whenReady().then(async () => {
|
||||
// 初始化配置服务
|
||||
updateSplashProgress(5, '正在加载配置...')
|
||||
configService = new ConfigService()
|
||||
applyAutoUpdateChannel('startup')
|
||||
|
||||
// 将用户主题配置推送给 Splash 窗口
|
||||
if (splashWindow && !splashWindow.isDestroyed()) {
|
||||
|
||||
@@ -413,6 +413,14 @@ contextBridge.exposeInMainWorld('electronAPI', {
|
||||
downloadEmoji: (params: { url: string; encryptUrl?: string; aesKey?: string }) => ipcRenderer.invoke('sns:downloadEmoji', params)
|
||||
},
|
||||
|
||||
biz: {
|
||||
listAccounts: (account?: string) => ipcRenderer.invoke('biz:listAccounts', account),
|
||||
listMessages: (username: string, account?: string, limit?: number, offset?: number) =>
|
||||
ipcRenderer.invoke('biz:listMessages', username, account, limit, offset),
|
||||
listPayRecords: (account?: string, limit?: number, offset?: number) =>
|
||||
ipcRenderer.invoke('biz:listPayRecords', account, limit, offset)
|
||||
},
|
||||
|
||||
|
||||
// 数据收集
|
||||
cloud: {
|
||||
|
||||
221
electron/services/bizService.ts
Normal file
221
electron/services/bizService.ts
Normal file
@@ -0,0 +1,221 @@
|
||||
import { join } from 'path'
|
||||
import { readdirSync, existsSync } from 'fs'
|
||||
import { wcdbService } from './wcdbService'
|
||||
import { ConfigService } from './config'
|
||||
import { chatService, Message } from './chatService'
|
||||
import { ipcMain } from 'electron'
|
||||
import { createHash } from 'crypto'
|
||||
|
||||
export interface BizAccount {
|
||||
username: string
|
||||
name: string
|
||||
avatar: string
|
||||
type: number
|
||||
last_time: number
|
||||
formatted_last_time: string
|
||||
}
|
||||
|
||||
export interface BizMessage {
|
||||
local_id: number
|
||||
create_time: number
|
||||
title: string
|
||||
des: string
|
||||
url: string
|
||||
cover: string
|
||||
content_list: any[]
|
||||
}
|
||||
|
||||
export interface BizPayRecord {
|
||||
local_id: number
|
||||
create_time: number
|
||||
title: string
|
||||
description: string
|
||||
merchant_name: string
|
||||
merchant_icon: string
|
||||
timestamp: number
|
||||
formatted_time: string
|
||||
}
|
||||
|
||||
export class BizService {
|
||||
private configService: ConfigService
|
||||
|
||||
constructor() {
|
||||
this.configService = new ConfigService()
|
||||
}
|
||||
|
||||
private extractXmlValue(xml: string, tagName: string): string {
|
||||
const regex = new RegExp(`<${tagName}>([\\s\\S]*?)</${tagName}>`, 'i')
|
||||
const match = regex.exec(xml)
|
||||
if (match) {
|
||||
return match[1].replace(/<!\[CDATA\[/g, '').replace(/\]\]>/g, '').trim()
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
private parseBizContentList(xmlStr: string): any[] {
|
||||
if (!xmlStr) return []
|
||||
const contentList: any[] = []
|
||||
try {
|
||||
const itemRegex = /<item>([\s\S]*?)<\/item>/gi
|
||||
let match: RegExpExecArray | null
|
||||
while ((match = itemRegex.exec(xmlStr)) !== null) {
|
||||
const itemXml = match[1]
|
||||
const itemStruct = {
|
||||
title: this.extractXmlValue(itemXml, 'title'),
|
||||
url: this.extractXmlValue(itemXml, 'url'),
|
||||
cover: this.extractXmlValue(itemXml, 'cover') || this.extractXmlValue(itemXml, 'thumburl'),
|
||||
summary: this.extractXmlValue(itemXml, 'summary') || this.extractXmlValue(itemXml, 'digest')
|
||||
}
|
||||
if (itemStruct.title) contentList.push(itemStruct)
|
||||
}
|
||||
} catch (e) { }
|
||||
return contentList
|
||||
}
|
||||
|
||||
private parsePayXml(xmlStr: string): any {
|
||||
if (!xmlStr) return null
|
||||
try {
|
||||
const title = this.extractXmlValue(xmlStr, 'title')
|
||||
const description = this.extractXmlValue(xmlStr, 'des')
|
||||
const merchantName = this.extractXmlValue(xmlStr, 'display_name') || '微信支付'
|
||||
const merchantIcon = this.extractXmlValue(xmlStr, 'icon_url')
|
||||
const pubTime = parseInt(this.extractXmlValue(xmlStr, 'pub_time') || '0')
|
||||
if (!title && !description) return null
|
||||
return { title, description, merchant_name: merchantName, merchant_icon: merchantIcon, timestamp: pubTime }
|
||||
} catch (e) { return null }
|
||||
}
|
||||
|
||||
async listAccounts(account?: string): Promise<BizAccount[]> {
|
||||
try {
|
||||
const contactsResult = await chatService.getContacts({ lite: true })
|
||||
if (!contactsResult.success || !contactsResult.contacts) return []
|
||||
|
||||
const officialContacts = contactsResult.contacts.filter(c => c.type === 'official')
|
||||
const usernames = officialContacts.map(c => c.username)
|
||||
const enrichment = await chatService.enrichSessionsContactInfo(usernames)
|
||||
const contactInfoMap = enrichment.success && enrichment.contacts ? enrichment.contacts : {}
|
||||
|
||||
const root = this.configService.get('dbPath')
|
||||
const myWxid = this.configService.get('myWxid')
|
||||
const accountWxid = account || myWxid
|
||||
if (!root || !accountWxid) return []
|
||||
|
||||
const dbDir = join(root, accountWxid, 'db_storage', 'message')
|
||||
const bizLatestTime: Record<string, number> = {}
|
||||
|
||||
if (existsSync(dbDir)) {
|
||||
const bizDbFiles = readdirSync(dbDir).filter(f => f.startsWith('biz_message') && f.endsWith('.db'))
|
||||
for (const file of bizDbFiles) {
|
||||
const dbPath = join(dbDir, file)
|
||||
const name2idRes = await wcdbService.execQuery('message', dbPath, 'SELECT username FROM Name2Id')
|
||||
if (name2idRes.success && name2idRes.rows) {
|
||||
for (const row of name2idRes.rows) {
|
||||
const uname = row.username || row.user_name
|
||||
if (uname) {
|
||||
const md5 = createHash('md5').update(uname).digest('hex').toLowerCase()
|
||||
const tName = `Msg_${md5}`
|
||||
const timeRes = await wcdbService.execQuery('message', dbPath, `SELECT MAX(create_time) as max_time FROM ${tName}`)
|
||||
if (timeRes.success && timeRes.rows && timeRes.rows[0]?.max_time) {
|
||||
const t = parseInt(timeRes.rows[0].max_time)
|
||||
if (!isNaN(t)) bizLatestTime[uname] = Math.max(bizLatestTime[uname] || 0, t)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result: BizAccount[] = officialContacts.map(contact => {
|
||||
const uname = contact.username
|
||||
const info = contactInfoMap[uname]
|
||||
const lastTime = bizLatestTime[uname] || 0
|
||||
return {
|
||||
username: uname,
|
||||
name: info?.displayName || contact.displayName || uname,
|
||||
avatar: info?.avatarUrl || '',
|
||||
type: 0,
|
||||
last_time: lastTime,
|
||||
formatted_last_time: lastTime ? new Date(lastTime * 1000).toISOString().split('T')[0] : ''
|
||||
}
|
||||
})
|
||||
|
||||
const contactDbPath = join(root, accountWxid, 'contact.db')
|
||||
if (existsSync(contactDbPath)) {
|
||||
const bizInfoRes = await wcdbService.execQuery('contact', contactDbPath, 'SELECT username, type FROM biz_info')
|
||||
if (bizInfoRes.success && bizInfoRes.rows) {
|
||||
const typeMap: Record<string, number> = {}
|
||||
for (const r of bizInfoRes.rows) typeMap[r.username] = r.type
|
||||
for (const acc of result) if (typeMap[acc.username] !== undefined) acc.type = typeMap[acc.username]
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
.filter(acc => !acc.name.includes('朋友圈广告'))
|
||||
.sort((a, b) => {
|
||||
if (a.username === 'gh_3dfda90e39d6') return -1
|
||||
if (b.username === 'gh_3dfda90e39d6') return 1
|
||||
return b.last_time - a.last_time
|
||||
})
|
||||
} catch (e) { return [] }
|
||||
}
|
||||
|
||||
async listMessages(username: string, account?: string, limit: number = 20, offset: number = 0): Promise<BizMessage[]> {
|
||||
try {
|
||||
// 仅保留核心路径:利用 chatService 的自动路由能力
|
||||
const res = await chatService.getMessages(username, offset, limit)
|
||||
if (!res.success || !res.messages) return []
|
||||
|
||||
return res.messages.map(msg => {
|
||||
const bizMsg: BizMessage = {
|
||||
local_id: msg.localId,
|
||||
create_time: msg.createTime,
|
||||
title: msg.linkTitle || msg.parsedContent || '',
|
||||
des: msg.appMsgDesc || '',
|
||||
url: msg.linkUrl || '',
|
||||
cover: msg.linkThumb || msg.appMsgThumbUrl || '',
|
||||
content_list: []
|
||||
}
|
||||
if (msg.rawContent) {
|
||||
bizMsg.content_list = this.parseBizContentList(msg.rawContent)
|
||||
if (bizMsg.content_list.length > 0 && !bizMsg.title) {
|
||||
bizMsg.title = bizMsg.content_list[0].title
|
||||
bizMsg.cover = bizMsg.cover || bizMsg.content_list[0].cover
|
||||
}
|
||||
}
|
||||
return bizMsg
|
||||
})
|
||||
} catch (e) { return [] }
|
||||
}
|
||||
|
||||
async listPayRecords(account?: string, limit: number = 20, offset: number = 0): Promise<BizPayRecord[]> {
|
||||
const username = 'gh_3dfda90e39d6'
|
||||
try {
|
||||
const res = await chatService.getMessages(username, offset, limit)
|
||||
if (!res.success || !res.messages) return []
|
||||
|
||||
const records: BizPayRecord[] = []
|
||||
for (const msg of res.messages) {
|
||||
if (!msg.rawContent) continue
|
||||
const parsedData = this.parsePayXml(msg.rawContent)
|
||||
if (parsedData) {
|
||||
records.push({
|
||||
local_id: msg.localId,
|
||||
create_time: msg.createTime,
|
||||
...parsedData,
|
||||
timestamp: parsedData.timestamp || msg.createTime,
|
||||
formatted_time: new Date((parsedData.timestamp || msg.createTime) * 1000).toLocaleString()
|
||||
})
|
||||
}
|
||||
}
|
||||
return records
|
||||
} catch (e) { return [] }
|
||||
}
|
||||
|
||||
registerHandlers() {
|
||||
ipcMain.handle('biz:listAccounts', (_, account) => this.listAccounts(account))
|
||||
ipcMain.handle('biz:listMessages', (_, username, account, limit, offset) => this.listMessages(username, account, limit, offset))
|
||||
ipcMain.handle('biz:listPayRecords', (_, account, limit, offset) => this.listPayRecords(account, limit, offset))
|
||||
}
|
||||
}
|
||||
|
||||
export const bizService = new BizService()
|
||||
@@ -45,6 +45,7 @@ interface ConfigSchema {
|
||||
|
||||
// 更新相关
|
||||
ignoredUpdateVersion: string
|
||||
updateChannel: 'auto' | 'stable' | 'preview' | 'dev'
|
||||
|
||||
// 通知
|
||||
notificationEnabled: boolean
|
||||
@@ -119,6 +120,7 @@ export class ConfigService {
|
||||
authUseHello: false,
|
||||
authHelloSecret: '',
|
||||
ignoredUpdateVersion: '',
|
||||
updateChannel: 'auto',
|
||||
notificationEnabled: true,
|
||||
notificationPosition: 'top-right',
|
||||
notificationFilterMode: 'all',
|
||||
|
||||
Reference in New Issue
Block a user