-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
266 lines (241 loc) · 7.57 KB
/
index.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
auto.waitFor();
importClass(android.content.Intent);
importClass(android.net.Uri);
toast("点我啦");
var fns = {
// 检测是否安装微信
checkIsWx() {
var packageName = "com.tencent.mm";
var isWechatInstalled = app.getAppName(packageName);
console.log("🚀 ~ file: index.js:14 ~ checkIsWx ~ isWechatInstalled:", isWechatInstalled);
if (isWechatInstalled) {
return true;
} else {
return false;
}
},
// 删除download文件夹下的图片文件
delDown() {
try {
var downPath = files.join(files.getSdcardPath(), "Download");
var fileList = files.listDir(downPath);
// 遍历文件,删除图片
fileList
.filter(function (file) {
return file.endsWith(".jpg") || file.endsWith(".png");
})
.forEach(function (name) {
var imgeFile = files.join(downPath, name);
files.remove(imgeFile);
});
var msg = "已删除download文件夹下的图片文件";
return errorFn(true, msg);
} catch (error) {
return errorFn(false, error);
}
},
/**
* 将二维码图片保存到download文件夹下
*/
getQrSave() {
try {
var downPath = files.join(files.getSdcardPath(), "Download", "hotel-qr.jpg");
var imgRes = http.get("https://picb.zhimg.com/v2-8bd693cc12eeb749ba0ae5fb3062d5f9_b.png"); // 发送http请求
if (imgRes.statusCode != 200) return errorFn(false, "图片请求失败");
var imgBytes = imgRes.body.bytes(); // 获取byte类型的图片数据
files.writeBytes(downPath, imgBytes); // 将图片数据保存到文件中
//把图片加入相册
media.scanFile(downPath);
var msg = "图片已保存到:" + downPath;
return errorFn(true, msg);
} catch (error) {
return errorFn(false, error);
}
},
/**
* 打开微信-扫一扫
* @returns {boolean} true-成功;false-失败
*/
openWxSys: function () {
try {
toast("正在打开微信扫一扫…");
//利用Intent打开微信
app.startActivity({
action: "VIEW",
packageName: "com.tencent.mm",
className: "com.tencent.mm.ui.LauncherUI",
extras: {
"LauncherUI.From.Scaner.Shortcut": true
}
});
sleep(3000);
while (!click("相册"));
toast("选择第一张照片");
sleep(3000);
var oneX = className("android.widget.RelativeLayout").find()[1].bounds().centerX();
var oneY = className("android.widget.RelativeLayout").find()[1].bounds().centerY();
var isClickSuccess = click(oneX, oneY);
console.log("🚀 ~ file: index.js:85 ~ isClickSuccess:", isClickSuccess);
if (isClickSuccess) {
var msg = "第一张照片,已成功选择";
return errorFn(isClickSuccess, msg);
} else {
var msg = "第一张照片,选择失败";
return errorFn(isClickSuccess, msg);
}
} catch (e) {
console.log("🚀 ~ file: actions.js:89 ~ e:", e);
return errorFn(false, e);
}
},
/**
* 打开微信小程序(只能打开已打开的微信小程序)
*/
openWxXcx: () => {
var packageName = "com.tencent.mm";
var launchActivity = "com.tencent.mm.plugin.appbrand.ui.AppBrandUI";
// 使用 adb 命令启动微信小程序
shell("am start -n " + packageName + "/" + launchActivity, true);
},
// 模拟随机滑动
sml_move(qx, qy, zx, zy, time) {
var xxy = [time];
var point = [];
var dx0 = {
x: qx,
y: qy
};
var dx1 = {
x: random(qx - 100, qx + 100),
y: random(qy, qy + 50)
};
var dx2 = {
x: random(zx - 100, zx + 100),
y: random(zy, zy + 50)
};
var dx3 = {
x: zx,
y: zy
};
for (var i = 0; i < 4; i++) {
eval("point.push(dx" + i + ")");
}
log(point[3].x);
for (let i = 0; i < 1; i += 0.08) {
xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)];
xxy.push(xxyy);
}
log(xxy);
gesture.apply(null, xxy);
function bezier_curves(cp, t) {
cx = 3.0 * (cp[1].x - cp[0].x);
bx = 3.0 * (cp[2].x - cp[1].x) - cx;
ax = cp[3].x - cp[0].x - cx - bx;
cy = 3.0 * (cp[1].y - cp[0].y);
by = 3.0 * (cp[2].y - cp[1].y) - cy;
ay = cp[3].y - cp[0].y - cy - by;
tSquared = t * t;
tCubed = tSquared * t;
result = {
x: 0,
y: 0
};
result.x = ax * tCubed + bx * tSquared + cx * t + cp[0].x;
result.y = ay * tCubed + by * tSquared + cy * t + cp[0].y;
return result;
}
},
record(timeLimit) {
return new Promise((resolve, reject) => {
const defaultOption = {
timeLimit: 10
};
if (!timeLimit) {
timeLimit = defaultOption.timeLimit;
}
//adb 录屏
const IS_ROOT = files.exists("/sbin/su") || files.exists("/system/xbin/su") || files.exists("/system/bin/su");
if (!IS_ROOT) {
toast("没有root权限,无法执行");
reject("没有root权限,无法执行");
exit;
}
// 推流: https://shu1shu2.com/article/2022/4/22/38.html
// adb -s 9305ac03 shell screenrecord --bit-rate 8000000 --size 1280x720 --time-limit 30 /sdcard/download/demo.mp4 --verbose
// https://blog.csdn.net/shenfengchen/article/details/111364422
var savePath = `/sdcard/download/${new Date().getTime()}_hotel.mp4`; // 保存路径
var bitRate = "8000000";
var cmd = `screenrecord --bit-rate ${bitRate} --time-limit ${timeLimit} ${savePath}`;
try {
toast(`开始录制,录制时间${timeLimit}秒,保存目录:${savePath}`);
var result = shell(cmd, true); //第二个参数: 是否以root权限运行,默认为false。
// 4.1 随机滑动
fns.sml_move(400, 1800, 800, 230, timeLimit);
if (result.code == 0) {
sleep(3000);
var killCmd = "pkill -l SIGINT -f screenrecord"; // 停止录屏的命令
shell(killCmd, true); // 异步执行命令
var msg = "录屏结束,保存在Download目录" + savePath;
resolve({ savePath });
return errorFn(true, msg, { savePath });
} else {
reject(result.error);
var msg = `执行失败~: ${result.error}`;
return errorFn(false, msg);
}
} catch (error) {
reject(error);
return errorFn(false, error);
}
});
},
// 文件上传
uploadFile(filePath) {
return new Promise((resolve, reject) => {
var url = `http://192.168.2.194:8080/fileupload`;
threads.start(function () {
var res = http.postMultipart(url, {
imei: "",
upload: open(filePath) // 要上传的字段名称和路径
});
if (res.statusCode != 200) {
errorFn(true, `上传失败: ${res.statusCode} ${res.statusMessage}`);
reject(res);
}
let r = res.body.string();
errorFn(true, `上传成功: ${r}`);
resolve(r);
});
});
}
};
function errorFn(status, msg, data) {
toast(msg);
// exit();
return {
status,
msg,
data
};
}
// 1、检测是否安装微信
if (!fns.checkIsWx()) {
toast("微信未安装, 请安装微信后再试~");
exit();
}
// 2、 请求接口,获取远程二维码图片
fns.getQrSave();
// 3、打开微信-扫一扫
fns.openWxSys();
// 4、开始录屏
fns
.record(10)
.then((res) => {
return fns.uploadFile(res.savePath);
})
.then((res) => {
console.log("🚀 ~ file: index.js:271 ~ fns.record ~ res:", res);
})
.catch((err) => {
console.log("🚀 ~ file: index.js:273 ~ err:", err);
});