后端迁移会话微信登录模块
This commit is contained in:
+67
-15
@@ -1,12 +1,64 @@
|
||||
// @ts-nocheck
|
||||
/// <reference lib="dom" />
|
||||
|
||||
import fs from 'node:fs/promises'
|
||||
|
||||
import { downloadRemoteQrImage, logBrowserSessionDebug, waitForFrame } from './session-login-shared.js'
|
||||
|
||||
export async function ensureWxLoginReady(page) {
|
||||
type LocatorLike = {
|
||||
first: () => LocatorLike
|
||||
waitFor: (options?: { state?: string; timeout?: number }) => Promise<void>
|
||||
screenshot: (options: { path: string }) => Promise<void>
|
||||
isVisible: () => Promise<boolean>
|
||||
click: (options?: { timeout?: number }) => Promise<void>
|
||||
evaluate: <T = unknown>(callback: (element: Element) => T | Promise<T>) => Promise<T>
|
||||
}
|
||||
|
||||
type FrameLike = {
|
||||
url: () => string
|
||||
locator: (selector: string) => LocatorLike
|
||||
waitForTimeout: (milliseconds: number) => Promise<void>
|
||||
evaluate: <T = any>(callback: any) => Promise<T>
|
||||
}
|
||||
|
||||
type PageLike = {
|
||||
frames: () => FrameLike[]
|
||||
waitForTimeout: (milliseconds: number) => Promise<void>
|
||||
context: () => {
|
||||
request: {
|
||||
get: (url: string, options?: any) => Promise<{
|
||||
ok: () => boolean
|
||||
status: () => number
|
||||
body: () => Promise<Buffer>
|
||||
}>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type WxLoginViewState = {
|
||||
qrVisible: boolean
|
||||
quickLoginVisible: boolean
|
||||
switchToNormalVisible: boolean
|
||||
qrCandidates: Array<{
|
||||
index: number
|
||||
visible: boolean
|
||||
src: string
|
||||
naturalWidth: number
|
||||
}>
|
||||
bodyText: string
|
||||
}
|
||||
|
||||
type WxQrState = {
|
||||
visible: boolean
|
||||
qrVisible?: boolean
|
||||
scanned: boolean
|
||||
expired: boolean
|
||||
message: string
|
||||
frameUrl?: string
|
||||
}
|
||||
|
||||
export async function ensureWxLoginReady(page: PageLike): Promise<void> {
|
||||
try {
|
||||
const frame = await waitForFrame(
|
||||
const frame = await waitForFrame<FrameLike>(
|
||||
page,
|
||||
(item) => item.url().includes('open.weixin.qq.com/connect/qrconnect'),
|
||||
10_000,
|
||||
@@ -52,9 +104,9 @@ export async function ensureWxLoginReady(page) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function captureWxQrImage(page, qrImagePath) {
|
||||
export async function captureWxQrImage(page: PageLike, qrImagePath: string): Promise<void> {
|
||||
const qrLocator = await findWxQrLocator(page)
|
||||
const frame = await waitForFrame(
|
||||
const frame = await waitForFrame<FrameLike>(
|
||||
page,
|
||||
(item) => item.url().includes('open.weixin.qq.com/connect/qrconnect'),
|
||||
10_000,
|
||||
@@ -70,11 +122,11 @@ export async function captureWxQrImage(page, qrImagePath) {
|
||||
const downloaded = await downloadRemoteQrImage(page, qrUrl, qrImagePath, frame.url(), 'wx')
|
||||
logBrowserSessionDebug('wx.capture.downloadResult', {
|
||||
qrUrl,
|
||||
downloaded: Boolean(downloaded?.length),
|
||||
downloaded: Buffer.isBuffer(downloaded) && downloaded.length > 0,
|
||||
qrImagePath,
|
||||
})
|
||||
|
||||
if (downloaded?.length) {
|
||||
if (Buffer.isBuffer(downloaded) && downloaded.length > 0) {
|
||||
await fs.writeFile(qrImagePath, downloaded)
|
||||
return
|
||||
}
|
||||
@@ -86,10 +138,10 @@ export async function captureWxQrImage(page, qrImagePath) {
|
||||
await qrLocator.screenshot({ path: qrImagePath })
|
||||
}
|
||||
|
||||
export async function findWxQrLocator(page) {
|
||||
export async function findWxQrLocator(page: PageLike): Promise<LocatorLike> {
|
||||
await ensureWxLoginReady(page)
|
||||
|
||||
const frame = await waitForFrame(
|
||||
const frame = await waitForFrame<FrameLike>(
|
||||
page,
|
||||
(item) => item.url().includes('open.weixin.qq.com/connect/qrconnect'),
|
||||
)
|
||||
@@ -109,7 +161,7 @@ export async function findWxQrLocator(page) {
|
||||
return locator
|
||||
}
|
||||
|
||||
export async function extractWxQrState(page) {
|
||||
export async function extractWxQrState(page: Pick<PageLike, 'frames'>): Promise<WxQrState> {
|
||||
const frame = page.frames().find((item) => item.url().includes('open.weixin.qq.com/connect/qrconnect'))
|
||||
|
||||
if (!frame) {
|
||||
@@ -146,10 +198,10 @@ export async function extractWxQrState(page) {
|
||||
}
|
||||
}
|
||||
|
||||
async function inspectWxLoginView(frame) {
|
||||
async function inspectWxLoginView(frame: FrameLike): Promise<WxLoginViewState> {
|
||||
return frame
|
||||
.evaluate(() => {
|
||||
const isVisible = (element) => {
|
||||
const isVisible = (element: Element | null) => {
|
||||
if (!(element instanceof HTMLElement)) {
|
||||
return false
|
||||
}
|
||||
@@ -203,7 +255,7 @@ async function inspectWxLoginView(frame) {
|
||||
}))
|
||||
}
|
||||
|
||||
async function switchWxQuickLoginToQr(frame) {
|
||||
async function switchWxQuickLoginToQr(frame: FrameLike): Promise<boolean> {
|
||||
const switcherCandidates = [
|
||||
frame.locator('.js_switchToNormal:visible').first(),
|
||||
frame.locator('.js_switchToNormal').first(),
|
||||
@@ -279,10 +331,10 @@ async function switchWxQuickLoginToQr(frame) {
|
||||
.catch(() => false)
|
||||
}
|
||||
|
||||
async function resolveWxQrImageUrl(frame) {
|
||||
async function resolveWxQrImageUrl(frame: FrameLike): Promise<string> {
|
||||
return frame
|
||||
.evaluate(() => {
|
||||
const isVisible = (element) => {
|
||||
const isVisible = (element: Element | null) => {
|
||||
if (!(element instanceof HTMLElement)) {
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user