-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMinaCodeCommand.js
230 lines (210 loc) · 6.61 KB
/
MinaCodeCommand.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"use strict";
/**
* @description 小程序版本管理(获取所有版本列表,版本设为体验版,版本提审,撤回提审,版本发布)
*/
const assert = require("assert");
const moment = require("moment");
const _ = require("lodash");
const querystring = require("../utils/querystring");
const Command = require("../command");
class MinaCodeCommand extends Command {
async exec() {
const opt = this.args.type;
switch (opt) {
case "list":
return this.listCodes();
case "expr":
return this.setCodeToExpr();
case "review":
return this.setCodeToReview();
case "cancel_review":
return this.cancelReview();
case "publish":
return this.publishCode();
default:
throw new Error(`${opt} not support`);
}
}
async listCodes() {
this.logger.info("list all codes...");
const params = {
path: `/wxopen/wacodepage?action=getcodepage&f=json&lang=zh_CN`,
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();
if (content.ret) {
this.logger.info("resp", content);
throw new Error("list code fail");
}
const codeData = JSON.parse(content.code_data);
const rtn = {
dev: codeData.develop_info.info_list.map(item => {
const code = item.basic_info;
code.is_exper = item.is_exper;
return code;
}),
exper: codeData.experience_info.basic_info,
online: codeData.online_info.basic_info
};
return rtn;
}
async setCodeToExpr() {
this.logger.info("set code to expr...", this.args.code);
const openid = _.get(this.args.code, "open_id", "");
const version = _.get(this.args.code, "version", "");
let resp;
let content;
assert(openid, "miss code.openid");
assert(version, "miss code.version");
const codes = await this.listCodes();
const devCodes = codes.dev;
const exprCode = _.find(devCodes, item => {
return item.is_exper;
});
const code = _.find(devCodes, item => {
return item.open_id === openid && version === item.version;
});
if (!code) {
throw new Error("code not found", openid, version);
}
if (exprCode) {
if (exprCode.open_id === openid) {
this.logger.info(`expr has exist`);
return true;
}
this.logger.info("delete exper", exprCode);
resp = await this.fetch(
`https://mp.weixin.qq.com/wxamp/cgi/route?path=%2Fwxopen%2Fwadevelopcode%3Faction%3Ddelete_exper&random=${Math.random()}`,
{
method: "POST",
headers: {
"content-type": "application/x-www-form-urlencoded"
},
body: `openid=${exprCode.open_id}&version=${exprCode.version}`
}
);
content = await resp.json();
this.logger.info(content);
if (content.ret) {
throw new Error("delete fail");
}
}
this.logger.info("submit exper", code);
resp = await this.fetch(
`https://mp.weixin.qq.com/wxamp/cgi/route?path=%2Fwxopen%2Fwadevelopcode%3Faction%3Dsumit_exper&random=${Math.random()}`,
{
method: "POST",
headers: {
"content-type": "application/x-www-form-urlencoded"
},
body: `openid=${code.open_id}&version=${code.version}`
}
);
content = await resp.json();
this.logger.info(content);
if (content.ret) {
throw new Error("submit fail");
}
return true;
}
async setCodeToReview() {
this.logger.info("set code to review...", this.args.code);
const openid = _.get(this.args.code, "open_id", "");
const version = _.get(this.args.code, "version", "");
const desc = _.get(this.args.code, "describe", "");
let resp;
let content;
assert(openid, "miss code.openid");
assert(version, "miss code.version");
const codes = await this.listCodes();
const devCodes = codes.dev;
const code = _.find(devCodes, item => {
return item.open_id === openid && version === item.version;
});
assert(code, "code not exist");
resp = await this.fetch(
`https://mp.weixin.qq.com/wxamp/cgi/route?path=%2Fwxopen%2Fwadevelopcode%3Faction%3Dsubmit_check&random=${Math.random()}`,
{
method: "POST",
headers: {
"content-type": "application/x-www-form-urlencoded"
},
body: `ticket=qrcheckTicket&openid=${openid}&auto_id=30&version_desc=${encodeURIComponent(
desc
)}&speedup_audit=0&speedup_type=&speedup_desc=&encrypted_username=&encrypted_password=&remark=&feedback_info=&feedbackList=`
}
);
content = await resp.json();
this.logger.info(content);
if (content.ret) {
throw new Error("submit code to review fail");
}
return true;
}
async cancelReview() {
this.logger.info("cancel review...");
let resp;
let content;
resp = await this.fetch(
`https://mp.weixin.qq.com/wxamp/cgi/route?path=%2Fwxopen%2Fwacodepage%3Faction%3Dundo_expr&random=${Math.random()}`,
{
method: "POST",
headers: {
"content-type": "application/x-www-form-urlencoded"
},
body: ""
}
);
content = await resp.json();
this.logger.info(content);
if (content.ret) {
throw new Error("cancel review fail");
}
return true;
}
async publishCode() {
this.logger.info("publish code...", this.args.code);
const code = this.args.code;
const openid = _.get(code, "open_id", "");
const version = _.get(code, "version", "");
let resp;
let content;
assert(openid, "miss code.openid");
assert(version, "miss code.version");
const ticket = await this.getTicket({
scene: 0,
auth_type: 5,
appid: "",
extra: "",
data: "",
info: {
version: version,
username: code.nick_name,
time: code.time,
describe: code.describe,
weapp_alias: this.admin.getUser().nickName,
timestr: moment(code.time).format("YYYY-MM-DD HH:mm:ss")
}
});
resp = await this.fetch(
`https://mp.weixin.qq.com/wxamp/cgi/route?path=%2Fwxopen%2Fwaexperiencecode%3Faction%3Drelease&random=${Math.random()}`,
{
method: "POST",
headers: {
"content-type": "application/x-www-form-urlencoded"
},
body: `openid=${openid}&ticket=${ticket}&version=${version}`
}
);
content = await resp.json();
this.logger.info(content);
if (content.ret) {
throw new Error("publish review fail");
}
return true;
}
}
module.exports = MinaCodeCommand;