Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【WIP】完成支付宝自动提交审核 #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/alipay/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* eslint-disable ts/no-unsafe-assignment */
/* eslint-disable ts/no-unsafe-member-access */

import { yellow } from 'kolorist'
import type { Browser, ElementHandle, Page } from 'puppeteer'
import puppeteer from 'puppeteer'
import type { Ora } from 'ora'
import ora from 'ora'
import prompts from 'prompts'
import { ALIPAY_URL, VIEWPORT, __DEV__ } from '../constants'
import { pathResolve, showQrCodeToTerminal } from '../utils'
import type { IBrand } from './type'

let browser: Browser
let page: Page

let spinner: Ora
let options: InputOptions

async function getLoginScanCode() {
spinner = ora('正在获取登录二维码...').start()
browser = await puppeteer.launch({ headless: __DEV__ ? false : options.headless })
page = await browser.newPage()
await page.setViewport(VIEWPORT)
await page.goto(ALIPAY_URL)
const qrWrapper = await page.waitForSelector('.authcenter-body-login')
const qrCodePath = pathResolve('../cache/login-qr-alipay.png')
await qrWrapper?.screenshot({ path: qrCodePath })
spinner.succeed(yellow('请使用支付宝扫描二维码登录'))
console.log(await showQrCodeToTerminal(qrCodePath))
await page.waitForSelector('.title___UNcmz')
spinner.succeed('支付宝登录成功')
}

async function getBrandList(): Promise<IBrand[]> {
const brandList: IBrand[] = []
let nextBtn: ElementHandle | null = null
spinner.start('获取品牌列表')
return new Promise((resolve) => {
// eslint-disable-next-line ts/no-misused-promises
page.on('response', async (response) => {
const url = response.url()
if (url.includes('https://developerportal.alipay.com/app/appPageQuery.json') && url.includes('appType=TINYAPP_NORMAL')) {
const result: any = await response.json()
const list: IBrand[] = result.data.rows
brandList.push(...list)
if (result.data.pageInfo.pageNum * result.data.pageInfo.pageSize < result.data.pageInfo.total) {
nextBtn = nextBtn ?? await page.waitForSelector('#rc-tabs-0-panel-TINYAPP_NORMAL > div > div.ant-table-wrapper > div > div > ul > li.ant-pagination-next > button')
void nextBtn?.click()
}
else {
page.off('response')
resolve(brandList)
spinner.succeed('获取品牌列表成功')
}
}
})
})
}

export async function alipayRobot(opts: InputOptions) {
options = opts
await getLoginScanCode()
const brandList = await getBrandList()
const result = await prompts({
type: 'multiselect',
name: 'brand',
message: '请选择要登录的支付宝账号',
choices: brandList.map((item) => {
return {
title: item.appName,
value: item.appId,
description: item.lastVersion,
}
}),
})
console.log(result)
}
23 changes: 23 additions & 0 deletions src/alipay/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export interface IBrand {
hasAlipayOnline: boolean
showIteration: boolean
gmtCreate: number
gmtModified: number
appId: string
appName: string
appDesc: string
appType: string
logoUrl: string
oid: string
pid: string
status: string
masterName: string
masterAccount: string
lastVersion?: string
lastVersionStatus?: string
allowBind: boolean
bindStatus: string
currentRole: {
[index: string]: string
}
}
Binary file added src/cache/login-qr-alipay.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Viewport } from 'puppeteer'
export { description, version, name } from '../../package.json'

export const WEIXIN_URL = 'https://mp.weixin.qq.com/'
export const ALIPAY_URL = 'https://auth.alipay.com/login/ant_sso_index.htm?goto=https%3A%2F%2Fopen.alipay.com%2Fdevelop%2Fmanage'

export enum PLATFORM {
WEIXIN = 'weixin',
Expand Down
5 changes: 3 additions & 2 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { bgGreen, blue, green, red, reset } from 'kolorist'
import { blue, green, red, reset } from 'kolorist'
import prompts from 'prompts'
import { ACTION, PLATFORM } from '../constants'
import weixinRobot from '../weixin'
import { isEmpty } from '../utils'
import { alipayRobot } from '../alipay'

export default async function main(options: InputOptions) {
const result: prompts.Answers<
Expand Down Expand Up @@ -43,5 +44,5 @@ export default async function main(options: InputOptions) {
if (opts.platform === PLATFORM.WEIXIN)
await weixinRobot(opts)
else
console.log(bgGreen('正在开发中...'))
await alipayRobot(opts)
}
Loading