wechat-mp-controller 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +3 -8
- package/dist/index.js +40 -4
- package/dist/type.d.ts +12 -0
- package/dist/utils.d.ts +2 -1
- package/package.json +2 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { LaunchOptions } from 'puppeteer-core';
|
|
2
|
-
import {
|
|
2
|
+
import { SubmitAuditOptions } from './type';
|
|
3
3
|
export type WeChatMpControllerOptions = Pick<LaunchOptions, 'headless' | 'userDataDir'>;
|
|
4
4
|
export declare class WeChatMpController {
|
|
5
5
|
static create(options?: WeChatMpControllerOptions): Promise<Controller>;
|
|
@@ -8,6 +8,7 @@ export declare class Controller {
|
|
|
8
8
|
private browser;
|
|
9
9
|
private page;
|
|
10
10
|
private _isLogin;
|
|
11
|
+
private getLoginQrCodeTask;
|
|
11
12
|
init(options?: WeChatMpControllerOptions): Promise<void>;
|
|
12
13
|
isLogin(): boolean;
|
|
13
14
|
close(): Promise<void>;
|
|
@@ -36,11 +37,5 @@ export declare class Controller {
|
|
|
36
37
|
isExperience: boolean;
|
|
37
38
|
}[];
|
|
38
39
|
}>;
|
|
39
|
-
submitAudit(options:
|
|
40
|
-
formData: SubmitAuditFormType;
|
|
41
|
-
uploader: {
|
|
42
|
-
name: string;
|
|
43
|
-
openId: string;
|
|
44
|
-
};
|
|
45
|
-
}): Promise<void>;
|
|
40
|
+
submitAudit(options: SubmitAuditOptions): Promise<void>;
|
|
46
41
|
}
|
package/dist/index.js
CHANGED
|
@@ -8,6 +8,7 @@ export class WeChatMpController {
|
|
|
8
8
|
return controller;
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
|
+
const WAIT_TIMEOUT = 30 * 1000;
|
|
11
12
|
export class Controller {
|
|
12
13
|
_isLogin = false;
|
|
13
14
|
async init(options) {
|
|
@@ -18,6 +19,18 @@ export class Controller {
|
|
|
18
19
|
...options
|
|
19
20
|
});
|
|
20
21
|
this.page = await this.browser.newPage();
|
|
22
|
+
this.getLoginQrCodeTask = new Promise(resolve => {
|
|
23
|
+
const listener = res => {
|
|
24
|
+
if (res.url().includes('/scanloginqrcode?action=getqrcode')) {
|
|
25
|
+
resolve(res);
|
|
26
|
+
this.page?.off('response', listener);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
this.page?.on('response', listener);
|
|
30
|
+
setTimeout(() => {
|
|
31
|
+
this.page?.off('response', listener);
|
|
32
|
+
}, WAIT_TIMEOUT);
|
|
33
|
+
});
|
|
21
34
|
await this.page.goto(`https://mp.weixin.qq.com/`);
|
|
22
35
|
this._isLogin = await Promise.race([this.page.waitForSelector(`.login__type__container__scan__qrcode`).then(() => false), this.page.waitForSelector('#menuBar .menu').then(() => true)]);
|
|
23
36
|
return;
|
|
@@ -43,8 +56,8 @@ export class Controller {
|
|
|
43
56
|
break;
|
|
44
57
|
}
|
|
45
58
|
}
|
|
46
|
-
const
|
|
47
|
-
const buffer = await
|
|
59
|
+
const loginQrCodeRes = await this.getLoginQrCodeTask;
|
|
60
|
+
const buffer = await loginQrCodeRes.buffer();
|
|
48
61
|
const loginLink = await analyzeQrCodeImgBuffer(buffer);
|
|
49
62
|
let _qrcode = '';
|
|
50
63
|
await new Promise(resolve => {
|
|
@@ -116,7 +129,8 @@ export class Controller {
|
|
|
116
129
|
}
|
|
117
130
|
const {
|
|
118
131
|
formData,
|
|
119
|
-
uploader
|
|
132
|
+
uploader,
|
|
133
|
+
onUpload
|
|
120
134
|
} = options;
|
|
121
135
|
const searchParams = new URLSearchParams({
|
|
122
136
|
action: 'get_class',
|
|
@@ -132,11 +146,33 @@ export class Controller {
|
|
|
132
146
|
}
|
|
133
147
|
if (formData.imageUrls?.length || formData.videoUrl) {
|
|
134
148
|
const [imageUploader, videoUploader] = await this.page.$$('.webuploader-container input');
|
|
149
|
+
const uploadResponseHandles = [];
|
|
135
150
|
if (formData.imageUrls?.length) {
|
|
136
|
-
|
|
151
|
+
for (let A = 0; A < formData.imageUrls.length; ++A) {
|
|
152
|
+
await imageUploader.uploadFile(formData.imageUrls[A]);
|
|
153
|
+
uploadResponseHandles.push(this.page.waitForResponse(res => {
|
|
154
|
+
return res.url().includes('https://mp.weixin.qq.com/cgi-bin/filetransfer');
|
|
155
|
+
}).then(response => {
|
|
156
|
+
return onUpload?.({
|
|
157
|
+
filePath: formData.imageUrls[A],
|
|
158
|
+
response
|
|
159
|
+
});
|
|
160
|
+
}));
|
|
161
|
+
}
|
|
137
162
|
}
|
|
138
163
|
if (formData.videoUrl) {
|
|
139
164
|
await videoUploader.uploadFile(formData.videoUrl);
|
|
165
|
+
uploadResponseHandles.push(this.page.waitForResponse(res => {
|
|
166
|
+
return res.url().includes('https://mp.weixin.qq.com/cgi-bin/filetransfer');
|
|
167
|
+
}).then(response => {
|
|
168
|
+
return onUpload?.({
|
|
169
|
+
filePath: formData.videoUrl,
|
|
170
|
+
response
|
|
171
|
+
});
|
|
172
|
+
}));
|
|
173
|
+
}
|
|
174
|
+
if (uploadResponseHandles.length) {
|
|
175
|
+
await Promise.all(uploadResponseHandles);
|
|
140
176
|
}
|
|
141
177
|
}
|
|
142
178
|
await this.page.locator('.btn.btn_primary[data-msgid="提交审核"]').click();
|
package/dist/type.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { HTTPResponse } from 'puppeteer-core';
|
|
1
2
|
export type VersionInfo = {
|
|
2
3
|
uploader: {
|
|
3
4
|
name: string;
|
|
@@ -64,3 +65,14 @@ export type SubmitAuditFormType = {
|
|
|
64
65
|
imageUrls?: string[];
|
|
65
66
|
videoUrl?: string;
|
|
66
67
|
};
|
|
68
|
+
export type SubmitAuditOptions = {
|
|
69
|
+
formData: SubmitAuditFormType;
|
|
70
|
+
uploader: {
|
|
71
|
+
name: string;
|
|
72
|
+
openId: string;
|
|
73
|
+
};
|
|
74
|
+
onUpload?: (data: {
|
|
75
|
+
filePath: string;
|
|
76
|
+
response: HTTPResponse;
|
|
77
|
+
}) => Promise<void>;
|
|
78
|
+
};
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
1
|
+
import { Jimp } from 'jimp';
|
|
2
|
+
export declare const analyzeQrCodeImgBuffer: (buffer: Parameters<typeof Jimp.read>[0]) => Promise<string>;
|
|
2
3
|
export declare const wait: (duration: number) => Promise<void>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wechat-mp-controller",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
"author": "ArcherCube",
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"jimp": "^1.6.0",
|
|
33
|
+
"node-fetch": "^3.3.2",
|
|
33
34
|
"puppeteer-core": "^24.22.0",
|
|
34
35
|
"qrcode-reader": "^1.0.4",
|
|
35
36
|
"qrcode-terminal": "^0.12.0"
|