Skip to content

Commit

Permalink
feat: 优化登录二维码加载机制,提升获取概率
Browse files Browse the repository at this point in the history
  • Loading branch information
phk422 committed Dec 27, 2023
1 parent 2df2735 commit 7660555
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"dist"
],
"scripts": {
"start": "pnpm dev && cross-env NODE_ENV=development node ./index.js",
"start:pro": "pnpm dev && node ./index.js",
"start": "cross-env NODE_ENV=development node ./index.js",
"start:pro": "pnpm build && node ./index.js",
"dev": "unbuild --stub",
"build": "unbuild",
"publish:ci": "npm publish --access public",
Expand Down
3 changes: 3 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import process from 'node:process'
import type { Viewport } from 'puppeteer'

export { description, version, name } from '../../package.json'
Expand All @@ -15,3 +16,5 @@ export enum ACTION {
}

export const VIEWPORT: Viewport = { width: 1920, height: 1080, deviceScaleFactor: 1.5 }

export const __DEV__ = process.env.NODE_ENV === 'development'
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function pathResolve(...paths: string[]) {
* @param targetPath - 目标路径
* @returns 返回一个Promise,resolve时为生成的QR码对象,reject时为Error对象
*/
export async function showQrCodeToTerminal(targetPath: string) {
export async function showQrCodeToTerminal(targetPath: string): Promise<string> {
return new Promise((resolve, reject) => {
const data = UPNG.decode(fs.readFileSync(targetPath))
const code = jsqr(new Uint8ClampedArray(UPNG.toRGBA8(data)[0]), data.width, data.height)
Expand Down
21 changes: 14 additions & 7 deletions src/weixin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Browser, Page } from 'puppeteer'
import puppeteer from 'puppeteer'
import type { Ora } from 'ora'
import ora from 'ora'
import { ACTION, VIEWPORT, WEIXIN_URL } from '../constants'
import { ACTION, VIEWPORT, WEIXIN_URL, __DEV__ } from '../constants'
import { pathResolve, showQrCodeToTerminal, sleep } from '../utils'

let browser: Browser
Expand All @@ -27,8 +27,6 @@ export async function getLoginScanCode() {
return new Promise<void>((resolve, reject) => {
const el = document.querySelector<HTMLImageElement>('.login__type__container__scan__qrcode')
if (el) {
if (el.complete)
return resolve()
el.onload = () => resolve()
el.onerror = reject
}
Expand All @@ -38,9 +36,17 @@ export async function getLoginScanCode() {
})
})
const loginCodeImagePath = pathResolve('../cache/login-qr.png')
await sleep(1000)
await loginCode?.screenshot({ path: loginCodeImagePath, type: 'png' })
const scanCode = await showQrCodeToTerminal(loginCodeImagePath)
const getScanCode = async (): Promise<string> => {
await loginCode?.screenshot({ path: loginCodeImagePath, type: 'png' })
try {
return await showQrCodeToTerminal(loginCodeImagePath)
}
catch (e) {
await sleep()
return getScanCode()
}
}
const scanCode = await getScanCode()
spinner.succeed(yellow('请使用微信扫描二维码登录微信公众平台'))
console.log(scanCode)
await page.waitForSelector('.weui-desktop-icon.weui-desktop-icon__success.weui-desktop-icon__large', { timeout: 0 })
Expand Down Expand Up @@ -158,7 +164,8 @@ export default async function weixinRobot(action: ACTION) {
process.exit(0)
}
catch (err) {
if (process.env.NODE_ENV === 'development') {
console.log(__DEV__)
if (__DEV__) {
console.error(err)
return
}
Expand Down

0 comments on commit 7660555

Please sign in to comment.