common.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. var common = {};
  2. common.index = 0;
  3. common.random = function (min, max) {
  4. return Math.floor(Math.random() * (max - min + 1)) + min;
  5. }
  6. common.resetConsole = function (x, y, w, h) {
  7. console.show(true);
  8. x = x || 0;
  9. y = y || 0;
  10. w = w || device.width * 0.7;
  11. h = h || device.height * 0.2;
  12. sleep(1000);
  13. console.setPosition(x, y);
  14. sleep(1000);
  15. ui.run(() => {
  16. console.setSize(w, h);
  17. console.setCanInput(false);
  18. console.setTitle("", "#ff11ee00", 30);
  19. console.setLogSize(8);
  20. })
  21. }
  22. common.setConsoleTitle = function (title, color, size) {
  23. console.setTitle(title, color, size);
  24. }
  25. common.getRadioIndex = function (radios) {
  26. for (let index = 0; index < radios.length; index++) {
  27. const element = radios[index];
  28. if (element == true) {
  29. return index;
  30. }
  31. }
  32. return 0;
  33. }
  34. common.waitTime = function (seconds, txt) {
  35. var i = seconds;
  36. var msg = txt || "倒计时";
  37. if (1 > seconds && seconds > 0) {
  38. common.index++;
  39. log(msg);
  40. sleep(seconds * 1000)
  41. return;
  42. }
  43. var show = true;
  44. while (i >= 0) {
  45. show = true;
  46. if (i > 100 && i % 5 != 0) {
  47. show = false;
  48. }
  49. if (show) {
  50. log(msg + "-->" + i);
  51. }
  52. if (i % 5 == 0) {
  53. common.index++;
  54. }
  55. if (i != 0) {
  56. sleep(1000)
  57. }
  58. i--;
  59. }
  60. }
  61. common.openApp = function (name) {
  62. launchApp(name);
  63. }
  64. common.backHomeReloadApp = function (name) {
  65. common.closeApp(name)
  66. sleep(1000);
  67. common.openApp(name);
  68. }
  69. common.swipeToRight = function () {
  70. // eslint-disable-next-line no-undef
  71. swipe(device.width * 0.8 + random(-20, 10), device.height * 0.5 + random(-20, 10),
  72. device.width * 0.2 + random(-20, 10), device.height * 0.5 + random(-20, 10), 1);
  73. }
  74. common.swipeToLeft = function () {
  75. // eslint-disable-next-line no-undef
  76. swipe(device.width * 0.2 + random(-20, 10), device.height * 0.5 + random(-20, 10),
  77. device.width * 0.8 + random(-20, 10), device.height * 0.5 + random(-20, 10), 1);
  78. }
  79. common.closeApp = function (name) {
  80. // launchApp(name);
  81. var i = 0;
  82. while (i < 5) {
  83. back();
  84. sleep(1000);
  85. i++;
  86. }
  87. back();
  88. sleep(1000);
  89. back();
  90. }
  91. //短距离测试
  92. //sml_move(400, 1000, 800, 600, 1000);
  93. //此代码由飞云脚本圈整理提供(www.feiyunjs.com)
  94. function bezier_curves(cp, t) {
  95. var cx = 3.0 * (cp[1].x - cp[0].x);
  96. var bx = 3.0 * (cp[2].x - cp[1].x) - cx;
  97. var ax = cp[3].x - cp[0].x - cx - bx;
  98. var cy = 3.0 * (cp[1].y - cp[0].y);
  99. var by = 3.0 * (cp[2].y - cp[1].y) - cy;
  100. var ay = cp[3].y - cp[0].y - cy - by;
  101. var tSquared = t * t;
  102. var tCubed = tSquared * t;
  103. var result = {
  104. "x": 0,
  105. "y": 0
  106. };
  107. result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
  108. result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
  109. return result;
  110. }
  111. //仿真随机带曲线滑动
  112. //qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
  113. common.swipeRandom = function (qx, qy, zx, zy, time) {
  114. var xxy = [time];
  115. var point = [];
  116. var dx0 = {
  117. "x": qx,
  118. "y": qy
  119. };
  120. var dx1 = {
  121. "x": random(qx - 100, qx + 100),
  122. "y": random(qy, qy + 50)
  123. };
  124. var dx2 = {
  125. "x": random(zx - 100, zx + 100),
  126. "y": random(zy, zy + 50),
  127. };
  128. var dx3 = {
  129. "x": zx,
  130. "y": zy
  131. };
  132. point.push(dx0);
  133. point.push(dx1);
  134. point.push(dx2);
  135. point.push(dx3);
  136. // log(point[3].x)
  137. for (let i = 0; i < 1.2; i += 0.08) {
  138. var xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]
  139. xxy.push(xxyy);
  140. }
  141. // log(xxy);
  142. gesture.apply(null, xxy);
  143. };
  144. common.readlines = function (dir) {
  145. files.ensureDir(dir);
  146. if (!files.exists(dir)) {
  147. files.create(dir);
  148. }
  149. var m = files.open(dir, "r").readlines();
  150. return m;
  151. }
  152. common.deviceHeight = function () {
  153. return device.height - device.getVirtualBarHeigh();
  154. }
  155. common.clickDesc = function (desc) {
  156. // eslint-disable-next-line no-constant-condition
  157. while (true) {
  158. desc("desc").findOnce();
  159. break;
  160. }
  161. }
  162. common.getRandomMsg = function (msgs) {
  163. var index = random(0, msgs.length - 1);
  164. return msgs[index];
  165. }
  166. common.timeToSec = function (time) {
  167. var min = time.split(':')[0]
  168. var sec = time.split(':')[1]
  169. var s = Number(min * 60) + Number(sec)
  170. return s
  171. }
  172. common.timestampToHMS = function (timestamp) {
  173. var date = new Date(Number(timestamp));//时间戳为10位需*1000,时间戳为13位的话不需乘1000
  174. var h = (date.getHours() < 10 ? '0' + date.getHours() : "" + date.getHours()) + ':';
  175. var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : "" + date.getMinutes()) + ':';
  176. var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : "" + date.getSeconds());
  177. return h + m + s;
  178. }
  179. common.textToList = function (str) {
  180. var snsArr = str.split('\n');
  181. snsArr = snsArr.filter(function (s) {
  182. return s && s.trim();
  183. });
  184. return snsArr;
  185. }
  186. var storageKey = "cslcommon";
  187. common.saveObj = function (key, obj) {
  188. var storage = storages.create(storageKey);
  189. storage.put(key, obj);
  190. }
  191. common.getObj = function (key) {
  192. var storage = storages.create(storageKey);
  193. return storage.get(key);
  194. }
  195. common.contains = function (key) {
  196. var storage = storages.create(storageKey);
  197. return storage.contains(key);
  198. }
  199. common.remove = function (key) {
  200. var storage = storages.create(storageKey);
  201. storage.remove(key)
  202. }
  203. common.clear = function () {
  204. var storage = storages.create(storageKey);
  205. storage.clear();
  206. }
  207. common.httptext = function (id) {
  208. try {
  209. var r = http.get(common.origin + "/material/getText?id=" + id);
  210. return r.body.json();
  211. } catch (error) {
  212. console.log(error)
  213. }
  214. return {};
  215. }
  216. common.dateFormat = function (fmt, date) {
  217. var o = {
  218. "M+": date.getMonth() + 1, //月份
  219. "d+": date.getDate(), //日
  220. "h+": date.getHours(), //小时
  221. "m+": date.getMinutes(), //分
  222. "s+": date.getSeconds(), //秒
  223. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  224. "S": date.getMilliseconds() //毫秒
  225. };
  226. if (/(y+)/.test(fmt)) {
  227. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  228. }
  229. for (var k in o) {
  230. if (new RegExp("(" + k + ")").test(fmt)) {
  231. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  232. }
  233. }
  234. return fmt;
  235. }
  236. common.init = function (options) {
  237. common.userCode = options.userCode || 2;
  238. common.origin = options.origin || 'http://112.74.161.35:9317';
  239. common.deviceCode = this.getDeviceCode();
  240. }
  241. common.strToNumber = function (str) {
  242. var dw = 1;
  243. if (str.indexOf("w") != -1) {
  244. dw = 10000;
  245. }
  246. return parseInt(str.replace('w', "")) * parseInt(dw);
  247. }
  248. common.getDeviceCode = function () {
  249. if (!common.deviceCode) {
  250. common.deviceCode = device.getIMEI() || device.getAndroidId();
  251. }
  252. return common.deviceCode;
  253. }
  254. common.stringIsBlank = function (string) {
  255. if (string == null || string == undefined || string == "" || string == 'undefined') {
  256. return true;
  257. }
  258. return false;
  259. }
  260. /**
  261. *
  262. * @param {*} status 1在线(空闲),0离线,3停用,-1删除,4未激活,2任务中
  263. */
  264. common.updateDeviceStatus = function (status = status || 1) {
  265. var qurl = common.origin + "/device/updateStatusByImei?imei" + this.getDeviceCode() + "&status=" + status;
  266. var r = http.get(qurl);
  267. var msg = r.body.json();
  268. console.log(msg);
  269. }
  270. common.forceStopApp = function (e) {
  271. try {
  272. shell("am force-stop " + e, true)
  273. } catch (e) {
  274. console.log(e)
  275. }
  276. }
  277. common.noResponse = function () {
  278. var e;
  279. if (text("等待").findOnce() && (e = text("关闭应用").findOnce())) {
  280. e.click();
  281. sleep(1e3);
  282. return true
  283. }
  284. return false
  285. }
  286. common.topClickedView = function (e) {
  287. while (e && !e.clickable()) {
  288. e = e.parent()
  289. }
  290. return e
  291. }
  292. common.xs_控件坐标点击 = function (UiObject, M) {
  293. if (UiObject) {
  294. if (M) {
  295. var rect = UiObject.bounds();
  296. return click(rect.centerX(), rect.centerY());
  297. } else {
  298. if (UiObject.clickable()) {
  299. return UiObject.click();
  300. } else {
  301. return arguments.callee(UiObject.parent());
  302. };
  303. };
  304. };
  305. };
  306. common.xs_控件点击 = function (nr1, nr2) {
  307. if (nr1 == "text") {
  308. text(nr2).findOne(1000).click();
  309. }
  310. if (nr1 == "name") {
  311. className(nr2).findOne(1000).click();
  312. }
  313. if (nr1 == "desc") {
  314. desc(nr2).findOne(1000).click();
  315. }
  316. if (nr1 == "id") {
  317. id(nr2).findOne(1000).click();
  318. }
  319. };
  320. common.xs_控件匹配点击 = function (nr1) {
  321. if (nr1) {
  322. var rect = textContains(nr1).findOne(1000)
  323. if (rect != null) {
  324. rect = rect.text();
  325. return click(rect);
  326. } else {
  327. return false
  328. }
  329. }
  330. };
  331. common.xs_删除写入文本 = function (nr1, nr2) {
  332. file = open(nr1, "w");
  333. file.write(nr2);
  334. file.close(); //关闭文件
  335. };
  336. common.xs_叠加写入文本 = function (nr1, nr2) {
  337. file = open(nr1, "a");
  338. file.writeline(nr2);
  339. file.close(); //关闭文件
  340. };
  341. common.xs_读取txt第一行 = function (nr1, nr2) { //nr1是路径,,nr2是编码 常用编码(utf-8)(gb2312)
  342. var text = open(nr1, "r", nr2);
  343. var t = text.readlines();
  344. text.close();
  345. for (a in t) {
  346. if (a == 0) {
  347. // log("txt第一行:" + t[i])
  348. return t[a]
  349. }
  350. }
  351. };
  352. common.xs_读取txt全部内容 = function (nr1, nr2) { //nr1是路径,,nr2是编码 常用编码(utf-8)(gb2312)
  353. var text = open(nr1, "r", nr2);
  354. var t = text.read();
  355. text.close();
  356. return t
  357. };
  358. common.xs_读取txt数组内容 = function (nr1, nr2) { //nr1是路径,,nr2是编码 常用编码(utf-8)(gb2312)
  359. var text = open(nr1, "r", nr2);
  360. var t = text.readlines();
  361. text.close();
  362. return t
  363. };
  364. common.xs_删除txt第一行 = function (nr1) {
  365. var text = open(nr1, "r", "utf-8")
  366. var t = text.readlines();
  367. text.close();
  368. files.remove(nr1)
  369. files.createWithDirs(nr1)
  370. var Text = open(nr1, "w", "utf-8");
  371. for (a in t) {
  372. if (a != 0) {
  373. Text.write(t[a] + "\r\n")
  374. }
  375. }
  376. Text.close()
  377. return t[1]
  378. };
  379. common.xs_控件是否存在 = function (nr1, nr2) {
  380. if (nr1 == "text") {
  381. if (text(nr2).exists()) {
  382. return true
  383. } else {
  384. return false
  385. }
  386. }
  387. if (nr1 == "name") {
  388. if (className(nr2).exists()) {
  389. return true
  390. } else {
  391. return false
  392. }
  393. }
  394. if (nr1 == "desc") {
  395. if (desc(nr2).exists()) {
  396. return true
  397. } else {
  398. return false
  399. }
  400. }
  401. if (nr1 == "id") {
  402. if (id(nr2).exists()) {
  403. return true
  404. } else {
  405. return false
  406. }
  407. }
  408. };
  409. common.xs_控件匹配是否存在 = function (nr1, nr2) {
  410. if (nr1 == "text") {
  411. if (textContains(nr2).exists()) {
  412. return true;
  413. } else {
  414. return false;
  415. }
  416. }
  417. if (nr1 == "desc") {
  418. if (descContains(nr2).exists()) {
  419. return true;
  420. } else {
  421. return false;
  422. }
  423. }
  424. };
  425. common.xs_全屏正则匹配是否存在 = function (reg) {
  426. if (descMatches(reg).boundsInside(0,0,device.width,device.height).exists()) {
  427. console.log("文本-->",reg, descMatches(reg).findOne().desc());
  428. return true;
  429. }
  430. if (textMatches(reg).boundsInside(0,0,device.width,device.height).exists()) {
  431. console.log("文本-->",reg, textMatches(reg).findOne().text());
  432. return true;
  433. }
  434. return false;
  435. };
  436. common.findNode = function (selecter, isclick) { //text("在线") textContains("在线") desc("在线") descContains("在线")
  437. let g_ret = null;
  438. try {
  439. g_ret = selecter.visibleToUser(true).findOne(10);
  440. if (g_ret != null) {
  441. if (isclick) {
  442. g_ret.click();
  443. }
  444. return true;
  445. }
  446. } catch (e) {
  447. log(e);
  448. }
  449. return false;
  450. };
  451. common.findNodeXY = function (selecter, isclick) { //text("在线") textContains("在线") desc("在线") descContains("在线")
  452. let g_ret = null;
  453. try {
  454. g_ret = selecter.visibleToUser(true).findOne(10);
  455. if (g_ret != null) {
  456. if (isclick) {
  457. g_ret = g_ret.bounds(); sleep(20);
  458. click(g_ret.centerX(), g_ret.centerY());
  459. }
  460. return true;
  461. }
  462. } catch (e) {
  463. log(e);
  464. }
  465. return false;
  466. };
  467. common.foreachClick = function (node) {
  468. if (!node) return "";
  469. if (node.clickable()) {
  470. return node;
  471. } else {
  472. if (!node.parent()) return "";
  473. return foreachClick(node.parent());
  474. }
  475. };
  476. /**
  477. *
  478. * @param {*} userInfo
  479. * userInfo.huozanNumber //喜欢数
  480. userInfo.concernNumber = //点赞数
  481. userInfo.friendNumber = //粉丝数
  482. userInfo.name //名字 唯一标识
  483. userInfo.account //账号唯一标识
  484. userInfo.synopsis //概要
  485. userInfo.gender // 性别
  486. userInfo.age //年龄
  487. userInfo.address //地址 infos[5]
  488. userInfo.school // 学习 infos[6]
  489. */
  490. common.saveInfo =function (userInfo){
  491. var qurl = common.origin + "/appuser/update";
  492. userInfo.userId = common.userCode;
  493. userInfo.deviceCode=common.getDeviceCode();
  494. try {
  495. var r = http.postJson(qurl, userInfo);
  496. console.log(r.body.json());
  497. } catch (error) {
  498. console.log(error);
  499. }
  500. }
  501. common.checkFile = function(dirPath){
  502. files.ensureDir(dirPath);
  503. if (!files.exists(dirPath)) {
  504. files.create(dirPath);
  505. }
  506. }
  507. module.exports = common