-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMinaExprUsersCommand.js
104 lines (93 loc) · 3.04 KB
/
MinaExprUsersCommand.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"use strict";
/**
* @description 小程序体验成员管理(获取所有体验成员列表,添加体验成员,删除体验成员)
*/
const querystring = require("../utils/querystring");
const Command = require("../command");
const PAGE_LIMIT = 90;
class MinaExprUsersCommand extends Command {
async exec() {
const opt = this.args.type;
switch (opt) {
case "list":
return this.listExprUsers();
case "add":
return this.addExprUser();
case "remove":
return this.removeExprUser();
default:
throw new Error(`${opt} not support`);
}
}
async listExprUsers() {
this.logger.info("get all expr users...");
const userInfo = this.admin.getUser();
const params = {
path: `/wxopen/authprofile?action=get_auth_list&use_role=1&is_expr=1&f=json&offset_of_expr=0&limit_of_expr=${PAGE_LIMIT}&token=${userInfo.token}&lang=zh_CN`,
token: "",
random: String(Math.random())
};
let resp = await this.fetch(
`https://mp.weixin.qq.com/wxamp/cgi/route?${querystring.encode(params)}`
);
let content = await resp.json();
let users = JSON.parse(content.auth_list_of_expr).items;
if (content.biz_expr_limit > PAGE_LIMIT) {
params.path = `/wxopen/authprofile?action=get_auth_list&use_role=1&is_expr=1&f=json&offset_of_expr=${PAGE_LIMIT}&limit_of_expr=${content.biz_expr_limit -
PAGE_LIMIT}&token=${userInfo.token}&lang=zh_CN`;
resp = await this.fetch(
`https://mp.weixin.qq.com/wxamp/cgi/route?${querystring.encode(params)}`
);
content = await resp.json();
users = users.concat(JSON.parse(content.auth_list_of_expr).items);
}
return users;
}
async addExprUser() {
this.logger.info("add expr users...");
const resp = await this.fetch(
`https://mp.weixin.qq.com/wxamp/cgi/route?path=%2Fwxopen%2Fauthprofile%3Faction%3Dadd_user%26use_role%3D1%26is_expr%3D1`,
{
method: "POST",
headers: {
"content-type": "application/x-www-form-urlencoded"
},
body: `check_ticket=1&auth_list=${encodeURIComponent(
JSON.stringify({
items: this.args.users
})
)}`
}
);
const content = await resp.json();
this.logger.info("result", content);
if (content.ret === 0) {
return true;
}
return false;
}
async removeExprUser() {
this.logger.info("remove expr users...");
const resp = await this.fetch(
`https://mp.weixin.qq.com/wxamp/cgi/route?path=%2Fwxopen%2Fauthprofile%3Faction%3Dsave_auth`,
{
method: "POST",
headers: {
"content-type": "application/x-www-form-urlencoded"
},
body: `check_ticket=1&use_role=1&is_expr=1&auth_list=${encodeURIComponent(
JSON.stringify({
items: this.args.users
})
)}`
}
);
const content = await resp.json();
this.logger.info("result", content);
if (content.ret === 0) {
return true;
}
return false;
}
}
module.exports = MinaExprUsersCommand;