nenMoScript.js 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /*
  2. 嫩模助手
  3. [Script]
  4. # > 嫩模助手
  5. ^https?:\/\/mo\.10coo\.com url script-request-body https://git.jojo21.top/shawenguan/Quantumult-X/raw/master/Scripts/mangguo/nenMoScript.js
  6. ^https?:\/\/mo\.10coo\.com url script-response-body https://git.jojo21.top/shawenguan/Quantumult-X/raw/master/Scripts/mangguo/nenMoScript.js
  7. [MITM]
  8. hostname = mo.10coo.com
  9. */
  10. const scriptName = `嫩模助手`;
  11. const magicJS = MagicJS(scriptName, "INFO");
  12. const MoConstKey = {
  13. Cookie: `mo_cookie`,
  14. OrderZfbPayUrlData: `mo_orderZfbPayUrlData`,
  15. OrderWxPayUrlData: `mo_orderWxPayUrlData`,
  16. };
  17. const gUserAgent = `Mozilla/5.0 (iPhone; CPU iPhone OS 16_6_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1`;
  18. const gHost = `mo.10coo.com`;
  19. const gCommonHeaders = {
  20. 'X-Requested-With': `XMLHttpRequest`,
  21. 'Sec-Fetch-Dest': `empty`,
  22. 'Connection': `keep-alive`,
  23. 'Accept-Encoding': `gzip, deflate, br`,
  24. 'Content-Type': `application/x-www-form-urlencoded`,
  25. 'Sec-Fetch-Site': `same-origin`,
  26. 'Origin': `https://mo.10coo.com`,
  27. 'User-Agent': gUserAgent,
  28. 'Sec-Fetch-Mode': `cors`,
  29. 'Host': gHost,
  30. 'Cookie': ``,
  31. 'Referer': `https://mo.10coo.com/`,
  32. 'Accept-Language': `zh-CN,zh-Hans;q=0.9`,
  33. 'Accept': `*/*`
  34. };
  35. let gRetBody;
  36. async function Main() {
  37. if (magicJS.isStrictRequest) {
  38. magicJS.checkRecordRequestBody();
  39. }
  40. if (magicJS.isRequest) {
  41. checkHandleRequest();
  42. } else {
  43. updateHeaders();
  44. await tryExecuteAfkTask();
  45. }
  46. magicJS.notification.msg('');
  47. if (gRetBody) {
  48. magicJS.done({
  49. body: JSON.stringify(gRetBody)
  50. });
  51. } else {
  52. magicJS.done();
  53. }
  54. }
  55. function checkHandleRequest() {
  56. const url = $request.url;
  57. const path = $request.path;
  58. magicJS.logger.info(`请求url=${url}#${$request.method}`);
  59. magicJS.logger.info(`请求body=${magicJS.getRequestBody()}`);
  60. if ($request && $request.method != 'OPTIONS') {
  61. switch (path) {
  62. case '/goLogin':
  63. handleLogin();
  64. break;
  65. case '/getConfig':
  66. handleConfig();
  67. break;
  68. case '/order/queryToBePaidStatusOrderList':
  69. break;
  70. case '/preStartTask':
  71. handlePreStartTask();
  72. break;
  73. case '/order/dashboard':
  74. handleOrderDashBoard();
  75. break;
  76. case '/order/list':
  77. handleOrderList();
  78. break;
  79. default:
  80. if (path.indexOf('/listValidXmAccount') != -1) {
  81. handleListValidXmAccount();
  82. }
  83. break;
  84. }
  85. }
  86. }
  87. function handleHeaders() {
  88. if ($request && $request.method != 'OPTIONS') {
  89. let headers = $request.headers;
  90. let newCookieStr = headers['Cookie'];
  91. let oldCookieStr = magicJS.data.read(MoConstKey.Cookie, null);
  92. if (oldCookieStr != newCookieStr) {
  93. magicJS.logger.info(`当前Cookie刷新到最新`);
  94. magicJS.notification.appendNotifyInfo(`🎉当前Cookie刷新到最新`);
  95. } else {
  96. magicJS.logger.info(`当前Cookie已经是最新无需更新`);
  97. }
  98. magicJS.data.write(MoConstKey.Cookie, newCookieStr);
  99. let username = getUserUserNameByCookie(newCookieStr);
  100. magicJS.data.write(`${MoConstKey.Cookie}#${username}`, newCookieStr);
  101. }
  102. }
  103. function getUserUserNameByCookie(cookieStr) {
  104. let cookieDict = magicJS.parseCookies(cookieStr);
  105. let jwtData = magicJS.parseJwt(cookieDict.au);
  106. if (jwtData) {
  107. return jwtData.username;
  108. }
  109. return '';
  110. }
  111. function updateHeaders() {
  112. let cookieStr = magicJS.data.read(MoConstKey.Cookie, ``);
  113. gCommonHeaders['Cookie'] = cookieStr;
  114. }
  115. function getResponsePlainData() {
  116. let data = magicJS.getResponseBody();
  117. if (!data) {
  118. return;
  119. }
  120. try {
  121. return JSON.parse(data);
  122. } catch (err) {
  123. magicJS.logger.error(err);
  124. }
  125. }
  126. function getRequestPlainData() {
  127. let data = magicJS.getRequestBody();
  128. let reqData = null;
  129. if (data) {
  130. reqData = magicJS.parseQueryStr(data);
  131. } else {
  132. reqData = magicJS.parseQueryStr($request.url);
  133. }
  134. return reqData;
  135. }
  136. function handleLogin() {
  137. let rspData = getResponsePlainData();
  138. if (!rspData) {
  139. return;
  140. }
  141. if (rspData.code != 0) {
  142. return;
  143. }
  144. }
  145. function handleConfig() {
  146. let rspData = getResponsePlainData();
  147. if (!rspData) {
  148. return;
  149. }
  150. if (rspData.code != 0) {
  151. return;
  152. }
  153. handleHeaders();
  154. }
  155. function handleListValidXmAccount() {
  156. let rspData = getResponsePlainData();
  157. if (!rspData) {
  158. return;
  159. }
  160. if (rspData.code != 0) {
  161. return;
  162. }
  163. handleHeaders();
  164. }
  165. function handlePreStartTask() {
  166. let rspData = getResponsePlainData();
  167. if (!rspData) {
  168. return;
  169. }
  170. rspData.code = 0;
  171. gRetBody = rspData;
  172. }
  173. function handleOrderDashBoard() {
  174. let rspData = getResponsePlainData();
  175. if (!rspData) {
  176. return;
  177. }
  178. if (rspData.code != 0) {
  179. return;
  180. }
  181. handleHeaders();
  182. }
  183. function handleOrderList() {
  184. let rspData = getResponsePlainData();
  185. if (!rspData) {
  186. return;
  187. }
  188. if (rspData.code != 0) {
  189. return;
  190. }
  191. handleHeaders();
  192. }
  193. async function tryExecuteAfkTask() {
  194. let username = '10191';
  195. let password = 'Sjojo510520';
  196. await doExecuteUserAfkTask(username, password);
  197. }
  198. async function doExecuteUserAfkTask(username, password) {
  199. let needReLogin = false;
  200. let cookieStr = magicJS.data.read(`${MoConstKey.Cookie}#${username}`, ``);
  201. // magicJS.logger.info(`cookieStr:${cookieStr}`);
  202. if (cookieStr && cookieStr.length > 0) {
  203. let cookieDict = magicJS.parseCookies(cookieStr);
  204. let jwtData = magicJS.parseJwt(cookieDict.au);
  205. // magicJS.logger.info(`token:${ookieDict.au}`);
  206. if (jwtData) {
  207. let payload = jwtData.payload;
  208. let expDate = new Date(payload.exp * 1000);
  209. let expTimeStr = magicJS.formatDate(expDate, 'yyyy-MM-dd HH:mm:ss');
  210. magicJS.logger.info(`token过期时间:${expTimeStr}`);
  211. if (Date.now() > expDate.getTime()) {
  212. needReLogin = true;
  213. }
  214. } else {
  215. needReLogin = true;
  216. }
  217. } else {
  218. needReLogin = true;
  219. }
  220. gCommonHeaders['Cookie'] = cookieStr;
  221. if (needReLogin) {
  222. let retLogin = await doLogin(username, password);
  223. if (!retLogin || retLogin.code != 0) {
  224. magicJS.logger.info(`登录失败!`);
  225. magicJS.notification.appendNotifyInfo(`登录失败!`);
  226. return;
  227. }
  228. }
  229. let retConfig = await getConfig();
  230. if (retConfig && retConfig.code == 4001) {//没有登录
  231. magicJS.logger.info(`没有登录,尝试重新登录`);
  232. let retLogin = await doLogin(username, password);
  233. if (!retLogin || retLogin.code != 0) {
  234. magicJS.logger.info(`登录失败!`);
  235. magicJS.notification.appendNotifyInfo(`登录失败!`);
  236. return;
  237. }
  238. retConfig = await getConfig();
  239. }
  240. if (!retConfig || retConfig.code != 0) {
  241. magicJS.logger.info(`获取配置数据失败,原因:${retConfig.message}`);
  242. magicJS.notification.appendNotifyInfo(`获取配置数据失败,原因:${retConfig.message}`);
  243. return;
  244. }
  245. let configData = retConfig.data;
  246. if (configData.hasNotice) {
  247. magicJS.notification.appendNotifyInfo(configData.noticeContent);
  248. }
  249. let monitorTip = ``;
  250. if (configData.runStatus == 1) {
  251. if (configData.maxQuantity && configData.maxQuantity > 0) {
  252. monitorTip = `当前${configData.occNodeNum}个账号正在监控中, 当前1号${configData.maxQuantity || 0}单`;
  253. } else {
  254. monitorTip = `当前${configData.occNodeNum}个账号正在监控中`;
  255. }
  256. magicJS.logger.info(monitorTip);
  257. if (configData.occNodeNum < configData.xmNodeNum) {
  258. magicJS.notification.appendNotifyInfo(monitorTip);
  259. }
  260. } else {
  261. monitorTip = `当前未开启监控`;
  262. magicJS.logger.info(monitorTip);
  263. }
  264. let nowTimeStr = magicJS.formatDate(new Date(), 'HH:mm:ss');
  265. if (configData.runStatus == 1 && (configData.occNodeNum >= configData.xmNodeNum || nowTimeStr >= configData.endTime)) {
  266. magicJS.logger.info(`正在检测是否存在待支付订单...`);
  267. await checkExistOrderNoPay(username);
  268. } else {
  269. magicJS.logger.info(`正在启动最大账号数监控任务...`);
  270. await checkAutoStartMonitorTask(username, configData);
  271. }
  272. }
  273. async function checkAutoStartMonitorTask(username, configData) {
  274. let retAccList = await listValidXmAccount();
  275. if (!retAccList || retAccList.code != 0) {
  276. magicJS.logger.info(`嫩模[${username}]获取账号列表数据失败!`);
  277. magicJS.notification.appendNotifyInfo(`嫩模[${username}]获取账号列表数据失败!`);
  278. return;
  279. }
  280. await checkAutoLoginTask();
  281. await checkAutoRefreshAddress();
  282. let accountList = retAccList.data;
  283. accountList.reverse();
  284. accountList.sort((a, b) => {
  285. return a.orderedQuantity - b.orderedQuantity;
  286. });
  287. magicJS.logger.info(`嫩模[${username}]下可用账号列表:\n` + JSON.stringify(accountList, null, 4));
  288. let taskAccIds = [];
  289. let maxTaskCount = configData.xmNodeNum || 3;
  290. let lastEndInex = Math.min(maxTaskCount, accountList.length);
  291. let startedLst = [];
  292. let willStartLst = [];
  293. let notLoingLst = [];
  294. for (let i = 0; i < lastEndInex; i++) {
  295. let accountInfo = accountList[i];
  296. if (accountInfo.realTimeStatus && accountInfo.oldLink != 1) {
  297. // oldLink: 选择了红色字体标记的账号,可能会没单,因为当前有这种账号最近下过单的老链接在,会被平台限购
  298. startedLst.push(accountInfo);
  299. } else {
  300. if (accountInfo.enable == 1 && (accountInfo.loginStatus != 1 && accountInfo.loginStatus != 2)) {
  301. // 未登录
  302. notLoingLst.push(accountInfo);
  303. }
  304. if (accountInfo.enable == 1 && accountInfo.loginStatus == 1) {
  305. // 已登录
  306. taskAccIds.push(accountInfo.id);
  307. willStartLst.push(accountInfo);
  308. }
  309. }
  310. }
  311. let msgTipsText = ``;
  312. let htmlContent = ``;
  313. if (taskAccIds.length > 0) {
  314. let retCheck = await preStartTask();
  315. if (!retCheck) {
  316. magicJS.notification.appendNotifyInfo(`嫩模[${username}]任务启动环境检查失败!`);
  317. return;
  318. }
  319. if (retCheck) {
  320. if (retCheck.code == 0) {
  321. magicJS.logger.info(`嫩模[${username}]任务启动环境检查成功!`);
  322. } else {
  323. magicJS.logger.info(`嫩模[${username}]${retCheck.message}`);
  324. magicJS.notification.appendNotifyInfo(`嫩模[${username}]${retCheck.message}`);
  325. return;
  326. }
  327. }
  328. htmlContent += `<h1><strong><span style="text-align: center;">嫩模[${username}]的监控信息</span></strong></h1>`;
  329. let nowTimeStr = magicJS.formatDate(new Date(), 'HH:mm:ss');
  330. if (nowTimeStr >= configData.startTime && nowTimeStr <= configData.endTime) {
  331. let retStart = await startTask(taskAccIds);
  332. if (retStart && retStart.code == 0) {
  333. startedLst = startedLst.concat(willStartLst);
  334. let resData = retStart.data;
  335. let taskRetText = `账号监控任务启动账号数量:${willStartLst.length},相关账号如下:\n`;
  336. htmlContent += `<p style="margin-bottom: 10px;">账号监控任务启动账号数量:${willStartLst.length},相关账号如下:</p>`;
  337. for (let i = 0; i < willStartLst.length; i++) {
  338. let accountInfo = willStartLst[i];
  339. taskRetText += `账号[${accountInfo.xmAccount}]启动...\n`;
  340. htmlContent += `<p style="margin-bottom: 10px;">账号[${accountInfo.xmAccount}]启动...</p>`;
  341. }
  342. taskRetText += `账号监控任务启动结果概览:\n`;
  343. taskRetText += `新增:${resData.addCount}\n`;
  344. taskRetText += `关闭:${resData.closeCount}\n`;
  345. taskRetText += `失败:${resData.failCount}\n`;
  346. taskRetText += `现存账号数:${resData.occNodeNum}\n`;
  347. magicJS.logger.info(taskRetText);
  348. msgTipsText += `嫩模[${username}]` + taskRetText + '\n';
  349. htmlContent += `<p style="margin-bottom: 10px;">账号监控任务启动结果概览:</p>`;
  350. htmlContent += `<p style="margin-bottom: 10px;">新增:${resData.addCount}</p>`;
  351. htmlContent += `<p style="margin-bottom: 10px;">关闭:${resData.closeCount}</p>`;
  352. htmlContent += `<p style="margin-bottom: 10px;">失败:${resData.failCount}</p>`;
  353. htmlContent += `<p style="margin-bottom: 10px;">现存账号数:${resData.occNodeNum}</p>`;
  354. }
  355. } else {
  356. msgTipsText += `当前时间不在运行时间段内,不执行任务!\n`;
  357. }
  358. }
  359. let retFastData = await getCommonFastTrackAccount();
  360. if (!retFastData || retFastData.code != 0) {
  361. magicJS.logger.info(`嫩模[${username}]获取神秘空间账号列表数据失败!`);
  362. return;
  363. }
  364. let left = retFastData.data.left;
  365. if (left > 0) {
  366. let fastAccountIds = [];
  367. if (retFastData.data.account) {
  368. fastAccountIds = retFastData.data.account.split(',');
  369. }
  370. msgTipsText += `嫩模[${username}]神秘空间剩余可用名额:${left},启动结果如下:\n`;
  371. htmlContent += `<p style="margin-bottom: 10px;">神秘空间剩余可用名额:${left},启动结果如下:</p>`;
  372. for (let i = 0; i < startedLst.length; i++) {
  373. let accountInfo = startedLst[i];
  374. if (fastAccountIds.includes(accountInfo.xmAccount)) {
  375. continue;
  376. }
  377. let retSet = await setCommonFastTrackAccount(accountInfo.xmAccount);
  378. if (retSet && retSet.code == 0) {
  379. msgTipsText += `账号[${accountInfo.xmAccount}]添加成功\n`;
  380. htmlContent += `<p style="margin-bottom: 10px;">账号[${accountInfo.xmAccount}]添加成功</p>`;
  381. left -= 1;
  382. if (left <= 0) {
  383. break;
  384. }
  385. } else {
  386. msgTipsText += `账号[${accountInfo.xmAccount}]添加失败,原因:${retSet?.message}\n`;
  387. htmlContent += `<p style="margin-bottom: 10px;">账号[${accountInfo.xmAccount}]添加失败,原因:${retSet?.message}</p>`;
  388. }
  389. }
  390. }
  391. retFastData = await getCommonFastTrackAccount();
  392. if (retFastData && retFastData.code == 0) {
  393. if (retFastData.data.compensateSeniority &&
  394. (!retFastData.data.compensateAccount || retFastData.data.compensateAccount.length === 0) &&
  395. retFastData.data.account) {
  396. magicJS.logger.info(`设置可添加补偿的账号`);
  397. msgTipsText += `光头补偿补偿设置结果如下:\n`;
  398. htmlContent += `<p style="margin-bottom: 10px;">光头补偿补偿设置结果如下:</p>`;
  399. let accountArr = retFastData.data.account.split(",");
  400. if (accountArr.length > 0) {
  401. let randAccount = accountArr[Math.floor(Math.random() * accountArr.length)];
  402. let retCompensate = await setCompensateFastTrackAccount(randAccount);
  403. if (retCompensate && retCompensate.code == 0) {
  404. msgTipsText += `账号[${randAccount}]设置为补偿的账号成功\n`;
  405. htmlContent += `<p style="margin-bottom: 10px;">账号[${randAccount}]设置为补偿的账号成功</p>`;
  406. } else {
  407. msgTipsText += `账号[${randAccount}]设置为补偿的账号失败,原因:${retCompensate?.message}\n`;
  408. htmlContent += `<p style="margin-bottom: 10px;">账号[${randAccount}]设置为补偿的账号失败,原因:${retCompensate?.message}</p>`;
  409. }
  410. }
  411. }
  412. }
  413. if (msgTipsText.length > 0) {
  414. magicJS.notification.appendNotifyInfo(msgTipsText);
  415. await doCustomWxpusherSend(`小芒嫩模助手监控任务开启`, htmlContent, 'https://mo.10coo.com/');
  416. }
  417. }
  418. async function doLogin(username, password) {
  419. const url = `https://mo.10coo.com/goLogin`;
  420. const reqData = {
  421. username: username,
  422. password: password,
  423. };
  424. let headers = { ...gCommonHeaders };
  425. headers['Referer'] = `https://mo.10coo.com/login`;
  426. let options = {
  427. url: url,
  428. headers: headers,
  429. body: magicJS.objToQueryStr(reqData),
  430. };
  431. let result = await magicJS.http.post(options).then(response => {
  432. try {
  433. let rspData = response.body;
  434. if (rspData.code == 0) {
  435. let cookieSetStr = headers['Set-Cookie'];
  436. magicJS.data.write(MoConstKey.Cookie, cookieSetStr);
  437. let username = getUserUserNameByCookie(cookieSetStr);
  438. magicJS.data.write(`${MoConstKey.Cookie}#${username}`, cookieSetStr);
  439. updateHeaders();
  440. }
  441. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  442. return rspData;
  443. } catch (e) {
  444. magicJS.logger.error(e);
  445. }
  446. }).catch(err => {
  447. const msg = `请求登录异常\n${JSON.stringify(err)}`;
  448. magicJS.logger.error(msg);
  449. });
  450. return result;
  451. }
  452. async function getConfig() {
  453. const url = `https://mo.10coo.com/getConfig`;
  454. let options = {
  455. url: url,
  456. headers: gCommonHeaders,
  457. body: ``,
  458. };
  459. let result = await magicJS.http.get(options).then(response => {
  460. try {
  461. let rspData = response.body;
  462. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  463. return rspData;
  464. } catch (e) {
  465. magicJS.logger.error(e);
  466. }
  467. }).catch(err => {
  468. const msg = `请求配置信息异常\n${JSON.stringify(err)}`;
  469. magicJS.logger.error(msg);
  470. });
  471. return result;
  472. }
  473. async function listValidXmAccount(size = 500) {
  474. const url = `https://mo.10coo.com/listValidXmAccount?size=${size}`;
  475. let options = {
  476. url: url,
  477. headers: gCommonHeaders,
  478. body: ``,
  479. };
  480. let result = await magicJS.http.get(options).then(response => {
  481. try {
  482. let rspData = response.body;
  483. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  484. return rspData;
  485. } catch (e) {
  486. magicJS.logger.error(e);
  487. }
  488. }).catch(err => {
  489. const msg = `请求账号列表异常\n${JSON.stringify(err)}`;
  490. magicJS.logger.error(msg);
  491. });
  492. return result;
  493. }
  494. async function preStartTask() {
  495. const url = `https://mo.10coo.com/preStartTask`;
  496. let options = {
  497. url: url,
  498. headers: gCommonHeaders,
  499. body: ``,
  500. };
  501. let result = await magicJS.http.get(options).then(response => {
  502. try {
  503. let rspData = response.body;
  504. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  505. return rspData;
  506. } catch (e) {
  507. magicJS.logger.error(e);
  508. }
  509. }).catch(err => {
  510. const msg = `请求启动前检查异常\n${JSON.stringify(err)}`;
  511. magicJS.logger.error(msg);
  512. });
  513. return result;
  514. }
  515. async function startTask(ids) {
  516. const url = `https://mo.10coo.com/start?ids=${ids.join(',')}`;
  517. let options = {
  518. url: url,
  519. headers: gCommonHeaders,
  520. body: ``,
  521. };
  522. let result = await magicJS.http.get(options).then(response => {
  523. try {
  524. let rspData = response.body;
  525. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  526. return rspData;
  527. } catch (e) {
  528. magicJS.logger.error(e);
  529. }
  530. }).catch(err => {
  531. const msg = `请求启动任务异常\n${JSON.stringify(err)}`;
  532. magicJS.logger.error(msg);
  533. });
  534. return result;
  535. }
  536. async function stopTask() {
  537. const url = `https://mo.10coo.com/stop`;
  538. let options = {
  539. url: url,
  540. headers: gCommonHeaders,
  541. body: ``,
  542. };
  543. let result = await magicJS.http.get(options).then(response => {
  544. try {
  545. let rspData = response.body;
  546. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  547. return rspData;
  548. } catch (e) {
  549. magicJS.logger.error(e);
  550. }
  551. }).catch(err => {
  552. const msg = `请求停止任务异常\n${JSON.stringify(err)}`;
  553. magicJS.logger.error(msg);
  554. });
  555. return result;
  556. }
  557. async function getCommonFastTrackAccount() {
  558. const url = `https://mo.10coo.com/getCommonFastTrackAccount`;
  559. let options = {
  560. url: url,
  561. headers: gCommonHeaders,
  562. body: ``,
  563. };
  564. let result = await magicJS.http.get(options).then(response => {
  565. try {
  566. let rspData = response.body;
  567. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  568. return rspData;
  569. } catch (e) {
  570. magicJS.logger.error(e);
  571. }
  572. }).catch(err => {
  573. const msg = `请求神秘空间加速账号列表异常\n${JSON.stringify(err)}`;
  574. magicJS.logger.error(msg);
  575. });
  576. return result;
  577. }
  578. async function setCommonFastTrackAccount(account) {
  579. const url = `https://mo.10coo.com/setCommonFastTrackAccount`;
  580. let reqData = {
  581. account: account,
  582. };
  583. let options = {
  584. url: url,
  585. headers: gCommonHeaders,
  586. body: magicJS.objToQueryStr(reqData),
  587. };
  588. let result = await magicJS.http.post(options).then(response => {
  589. try {
  590. let rspData = response.body;
  591. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  592. return rspData;
  593. } catch (e) {
  594. magicJS.logger.error(e);
  595. }
  596. }).catch(err => {
  597. const msg = `请求设置神秘空间加速账号异常\n${JSON.stringify(err)}`;
  598. magicJS.logger.error(msg);
  599. });
  600. return result;
  601. }
  602. async function setCompensateFastTrackAccount(account) {
  603. const url = `https://mo.10coo.com/setCompensateFastTrackAccount`;
  604. let reqData = {
  605. account: account,
  606. };
  607. let options = {
  608. url: url,
  609. headers: gCommonHeaders,
  610. body: magicJS.objToQueryStr(reqData),
  611. };
  612. let result = await magicJS.http.post(options).then(response => {
  613. try {
  614. let rspData = response.body;
  615. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  616. return rspData;
  617. } catch (e) {
  618. magicJS.logger.error(e);
  619. }
  620. }).catch(err => {
  621. const msg = `请求设置补偿账号异常\n${JSON.stringify(err)}`;
  622. magicJS.logger.error(msg);
  623. });
  624. return result;
  625. }
  626. async function checkAutoLoginTask() {
  627. let resData = await listXmAccount();
  628. if (!resData.data || !resData.code != 0) {
  629. return;
  630. }
  631. let list = resData.data.data;
  632. for (let i = 0; i < list.length; i++) {
  633. const item = list[i];
  634. if (item.loginStatus != 1 && item.loginStatus != 2) {// 1:已登录,2:登录中,其他:未登录
  635. let retAccInfo = await getXmAccount(item.id);
  636. if (retAccInfo.code == 0 && (retAccInfo.data.loginStatus != 1 && retAccInfo.data.loginStatus != 2)) {
  637. let retAdd = await addLoginTask(item.id);
  638. if (retAdd && retAdd.code == 0) {
  639. magicJS.logger.info(`账号[${item.xmAccount}]自动登录任务添加成功`);
  640. } else {
  641. magicJS.logger.info(`账号[${item.xmAccount}]自动登录任务添加失败`);
  642. }
  643. }
  644. }
  645. }
  646. }
  647. async function listXmAccount(page = 0, size = 500) {
  648. const url = `https://mo.10coo.com/listXmAccount`;
  649. let reqData = {
  650. page: page,
  651. size: size,
  652. };
  653. let options = {
  654. url: url,
  655. headers: gCommonHeaders,
  656. body: magicJS.objToQueryStr(reqData),
  657. };
  658. let result = await magicJS.http.post(options).then(response => {
  659. try {
  660. let rspData = response.body;
  661. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  662. return rspData;
  663. } catch (e) {
  664. magicJS.logger.error(e);
  665. }
  666. }).catch(err => {
  667. const msg = `获取账号列表异常\n${JSON.stringify(err)}`;
  668. magicJS.logger.error(msg);
  669. });
  670. return result;
  671. }
  672. async function getXmAccount(id) {
  673. const url = `https://mo.10coo.com/getXmAccount`;
  674. let reqData = {
  675. id: id,
  676. };
  677. let options = {
  678. url: url,
  679. headers: gCommonHeaders,
  680. body: magicJS.objToQueryStr(reqData),
  681. };
  682. let result = await magicJS.http.post(options).then(response => {
  683. try {
  684. let rspData = response.body;
  685. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  686. return rspData;
  687. } catch (e) {
  688. magicJS.logger.error(e);
  689. }
  690. }).catch(err => {
  691. const msg = `获取账号信息异常\n${JSON.stringify(err)}`;
  692. magicJS.logger.error(msg);
  693. });
  694. return result;
  695. }
  696. async function addLoginTask(id) {
  697. const url = `https://mo.10coo.com/addLoginTask`;
  698. let reqData = {
  699. id: id,
  700. };
  701. let options = {
  702. url: url,
  703. headers: gCommonHeaders,
  704. body: magicJS.objToQueryStr(reqData),
  705. };
  706. let result = await magicJS.http.post(options).then(response => {
  707. try {
  708. let rspData = response.body;
  709. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  710. return rspData;
  711. } catch (e) {
  712. magicJS.logger.error(e);
  713. }
  714. }).catch(err => {
  715. const msg = `请求启动任务异常\n${JSON.stringify(err)}`;
  716. magicJS.logger.error(msg);
  717. });
  718. return result;
  719. }
  720. async function checkAutoRefreshAddress() {
  721. let resData = await listXmAccount();
  722. if (!resData.data || !resData.code != 0) {
  723. return;
  724. }
  725. let list = resData.data.data;
  726. for (let i = 0; i < list.length; i++) {
  727. const item = list[i];
  728. let retAccInfo = await getXmAccount(item.id);
  729. if (retAccInfo.code == 0 && (retAccInfo.data.enable == 1)) {
  730. let retChange = await doChangeAddress(retAccInfo);
  731. if (retChange && retChange.code == 0) {
  732. magicJS.logger.info(`账号[${item.xmAccount}]自动刷新地址成功`);
  733. } else {
  734. magicJS.logger.info(`账号[${item.xmAccount}]自动刷新地址失败`);
  735. }
  736. }
  737. }
  738. }
  739. async function doChangeAddress(accountInfo) {
  740. let id = accountInfo.id;
  741. let retAddress = await getAddress(id);
  742. if (!retAddress || retAddress.code != 0) {
  743. return false;
  744. }
  745. let addressId = accountInfo.addressId;
  746. let addressObj = JSON.parse(retAddress.data);
  747. let itemList = [];
  748. if (addressObj.code == 200) {
  749. itemList = addressObj.data || [];
  750. }
  751. let ret = null;
  752. let isUseNewAddress = false;
  753. if (itemList.length < 10 && Math.random() > 0.9) {
  754. isUseNewAddress = true;
  755. }
  756. if (isUseNewAddress) {
  757. let retRandAddrData = await getRandXmAddress(accountInfo.id);
  758. let retCreate = await createAddress(accountInfo.id, retRandAddrData);
  759. if (retCreate && retCreate.code == 0) {
  760. let createObj = JSON.parse(retCreate.data);
  761. if (createObj.code == 200) {
  762. ret = await setXmAddress(createObj.data);;
  763. }
  764. }
  765. } else {
  766. for (let i = 0; i < itemList.length; i++) {
  767. const itemInfo = itemList[i];
  768. if (itemInfo.address_id != addressId) {
  769. ret = await setXmAddress(itemInfo);
  770. break;
  771. }
  772. }
  773. }
  774. return ret;
  775. }
  776. async function getAddress(id) {
  777. const url = `https://mo.10coo.com/getAddress`;
  778. let reqData = {
  779. id: id,
  780. };
  781. let options = {
  782. url: url,
  783. headers: gCommonHeaders,
  784. body: magicJS.objToQueryStr(reqData),
  785. };
  786. let result = await magicJS.http.post(options).then(response => {
  787. try {
  788. let rspData = response.body;
  789. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  790. return rspData;
  791. } catch (e) {
  792. magicJS.logger.error(e);
  793. }
  794. }).catch(err => {
  795. const msg = `获取地址异常\n${JSON.stringify(err)}`;
  796. magicJS.logger.error(msg);
  797. });
  798. return result;
  799. }
  800. async function setXmAddress(data) {
  801. const url = `https://mo.10coo.com/setXmAddress`;
  802. // 数据示例:
  803. // data = {
  804. // "address_id": 7496453,
  805. // "uuid": "0f2c12b8871c573ebf4a938fddc9a373",
  806. // "consignee": "孙芸思",
  807. // "province": "湖南省",
  808. // "city": "长沙市",
  809. // "district": "天心区",
  810. // "address": "芙蓉南路7号",
  811. // "mobile": "15575110716",
  812. // "is_default": 0,
  813. // "tag": ""
  814. // }
  815. let reqData = data;
  816. let options = {
  817. url: url,
  818. headers: gCommonHeaders,
  819. body: magicJS.objToQueryStr(reqData),
  820. };
  821. let result = await magicJS.http.post(options).then(response => {
  822. try {
  823. let rspData = response.body;
  824. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  825. return rspData;
  826. } catch (e) {
  827. magicJS.logger.error(e);
  828. }
  829. }).catch(err => {
  830. const msg = `设置地址异常\n${JSON.stringify(err)}`;
  831. magicJS.logger.error(msg);
  832. });
  833. return result;
  834. }
  835. async function getRandXmAddress(id) {
  836. const url = `https://mo.10coo.com/getRandXmAddress?xmAccountId=${id}`;
  837. let options = {
  838. url: url,
  839. headers: gCommonHeaders,
  840. body: ``,
  841. };
  842. let result = await magicJS.http.get(options).then(response => {
  843. try {
  844. let rspData = response.body;
  845. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  846. return rspData;
  847. } catch (e) {
  848. magicJS.logger.error(e);
  849. }
  850. }).catch(err => {
  851. const msg = `请求随机地址异常\n${JSON.stringify(err)}`;
  852. magicJS.logger.error(msg);
  853. });
  854. return result;
  855. }
  856. async function createAddress(xmAccountId, data) {
  857. const url = `https://mo.10coo.com/createAddress`;
  858. let reqData = {
  859. id: data.id,
  860. xmAccountId: xmAccountId,
  861. consignee: data.consignee,
  862. mobile: data.mobile,
  863. province: data.province,
  864. city: data.city,
  865. district: data.district,
  866. address: data.address,
  867. };
  868. let options = {
  869. url: url,
  870. headers: gCommonHeaders,
  871. body: magicJS.objToQueryStr(reqData),
  872. };
  873. let result = await magicJS.http.post(options).then(response => {
  874. try {
  875. let rspData = response.body;
  876. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  877. return rspData;
  878. } catch (e) {
  879. magicJS.logger.error(e);
  880. }
  881. }).catch(err => {
  882. const msg = `创建地址异常\n${JSON.stringify(err)}`;
  883. magicJS.logger.error(msg);
  884. });
  885. return result;
  886. }
  887. /**
  888. * 编辑账号
  889. * @param {*} xmAccountId
  890. * @param {*} xmAccount 账号手机号
  891. * @param {*} xmPassword 账号密码-可编辑
  892. * @param {*} alias 别名-可编辑
  893. * @returns
  894. */
  895. async function editAccount(xmAccountId, xmAccount, xmPassword, alias) {
  896. const url = `https://mo.10coo.com/editAccount`;
  897. let reqData = {
  898. xmId: xmAccountId,
  899. xmAccount: xmAccount,
  900. xmPassword: xmPassword,
  901. alias: alias,
  902. };
  903. let options = {
  904. url: url,
  905. headers: gCommonHeaders,
  906. body: magicJS.objToQueryStr(reqData),
  907. };
  908. let result = await magicJS.http.post(options).then(response => {
  909. try {
  910. let rspData = response.body;
  911. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  912. return rspData;
  913. } catch (e) {
  914. magicJS.logger.error(e);
  915. }
  916. }).catch(err => {
  917. const msg = `编辑账号异常\n${JSON.stringify(err)}`;
  918. magicJS.logger.error(msg);
  919. });
  920. return result;
  921. }
  922. function expireDateFormat(date) {
  923. if (!date) {
  924. return "未知";
  925. }
  926. let currentTime = new Date();
  927. // 获取订单时间
  928. let orderTime = new Date(date);
  929. // 将当前时间增加30分钟
  930. let expirationTime = new Date(orderTime.getTime() + 30 * 60 * 1000);
  931. // 计算剩余时间(以毫秒为单位)
  932. let remainingTime = expirationTime - currentTime;
  933. // 将剩余时间转换为分钟和秒数
  934. let remainingMinutes = Math.floor(remainingTime / 1000 / 60);
  935. let remainingSeconds = Math.floor((remainingTime / 1000) % 60);
  936. if (remainingSeconds < 0) {
  937. return "可能已过期";
  938. }
  939. // 输出剩余时间
  940. if (remainingMinutes === 0) {
  941. return remainingSeconds + "秒后过期";
  942. }
  943. return remainingMinutes + "分" + remainingSeconds + "秒后过期";
  944. }
  945. async function checkExistOrderNoPay(username) {
  946. let retOrderList = await queryToBePaidStatusOrderList();
  947. if (retOrderList && retOrderList.code == 0) {
  948. let msgTipsText = '';
  949. let nopayCount = 0;
  950. let orderList = retOrderList.data;
  951. let htmlContent = `<h1><strong><span style="text-align: center;">嫩模[${username}]订单信息</span></strong></h1>`;
  952. for (let i = 0; i < orderList.length; i++) {
  953. let d = orderList[i];
  954. let orderId = d.id;
  955. let xmAccount = d.xmAccount;
  956. let alias = d.alias ? "(" + d.alias + ")" : "";
  957. let createDate = (d.orderTime ? Date(d.orderTime) : Date(d.createTime));
  958. let expireStr = (d.orderTime ? expireDateFormat(d.orderTime) : expireDateFormat(d.createTime));
  959. let goodsStr = d.goodsName + d.skuStr;
  960. let priceStr = `¥${d.amount}`;
  961. let payStatus = await doPayResultCheck(orderId);
  962. if (payStatus) {
  963. msgTipsText += `嫩模[${username}]账号[${xmAccount}](${alias})下的订单:\n${goodsStr}(${priceStr})已付款,无需处理\n`;
  964. htmlContent += `<p style="margin-bottom: 10px; font-weight: bold;">账号[${xmAccount}](${alias})下的订单:</p>`;
  965. htmlContent += `<p style="margin-bottom: 10px;">订单产品:${goodsStr}(${priceStr})</p>`;
  966. htmlContent += `<p style="margin-bottom: 10px;">订单状态:已付款,无需处理</p>`;
  967. } else {
  968. nopayCount += 1;
  969. let retDetail = await queryOrderDetail(orderId);
  970. if (retDetail && retDetail.code == 0) {
  971. let data = retDetail.data;
  972. let statusMap = {
  973. [-4]: "待更新",
  974. [-3]: "未知或待支付",
  975. [-2]: "已关闭",
  976. [-1]: "已取消",
  977. [0]: "待支付",
  978. [1]: "已支付",
  979. [2]: "待签收",
  980. [3]: "已签收",
  981. [4]: "已签收",
  982. [8]: "已删除",
  983. };
  984. let officialStatus = data.officialStatus;
  985. magicJS.logger.info(`订单状态:${statusMap[officialStatus] || `未知状态[${officialStatus}]`},订单号:${orderId}`);
  986. msgTipsText += `嫩模[${username}]账号[${xmAccount}]${alias}下的订单:\n${goodsStr}(${priceStr})${statusMap[officialStatus]},请尽快去处理!过期时间:${expireStr}\n`;
  987. htmlContent += `<p style="margin-bottom: 10px;">账号[${xmAccount}](${alias})下的订单:</p>`;
  988. htmlContent += `<p style="margin-bottom: 10px;">订单产品:${goodsStr}(${priceStr})</p>`;
  989. htmlContent += `<p style="margin-bottom: 10px;">订单状态:${expireStr}</p>`;
  990. if (officialStatus === -3 || officialStatus === 0) {
  991. let wxKey = `${MoConstKey.OrderWxPayUrlData}#${orderId}`;
  992. let wxPayUrl = magicJS.data.read(wxKey, null);
  993. if (!wxPayUrl) {
  994. wxPayUrl = await getWxPayUrl(orderId);
  995. if (wxPayUrl) {
  996. magicJS.data.write(wxKey, wxPayUrl);
  997. }
  998. }
  999. if (wxPayUrl) {
  1000. msgTipsText += `微信支付链接,请在微信内打开进行支付:\n${wxPayUrl}\n`;
  1001. msgTipsText += `\n`;
  1002. htmlContent += `<p style="margin-bottom: 10px;">请在微信内打开进行支付:<a href="${wxPayUrl}" style="color: #007bff; text-decoration: none;">请点击链接</a></p>`;
  1003. }
  1004. let zfbKey = `${MoConstKey.OrderZfbPayUrlData}#${orderId}`;
  1005. let zfbPayUrl = magicJS.data.read(zfbKey, null);
  1006. if (!zfbPayUrl) {
  1007. zfbPayUrl = await getZFBPayUrl(orderId);
  1008. if (zfbPayUrl) {
  1009. magicJS.data.write(zfbKey, zfbPayUrl);
  1010. }
  1011. }
  1012. if (zfbPayUrl) {
  1013. msgTipsText += `支付宝支付链接,请浏览器打开跳转支付宝支付:\n${zfbPayUrl}\n`;
  1014. msgTipsText += `\n`;
  1015. htmlContent += `<p style="margin-bottom: 10px;">请浏览器打开跳转支付宝支付:<a href="${zfbPayUrl}" style="color: #007bff; text-decoration: none;">请点击链接</a></p>`;
  1016. }
  1017. htmlContent += `<p><br /></p>`;
  1018. msgTipsText += `\n`;
  1019. }
  1020. } else {
  1021. msgTipsText += `账号[${xmAccount}]${alias}下的订单:\n${goodsStr}(${priceStr})未付款,请尽快去处理!过期时间:${expireStr}\n`;
  1022. }
  1023. }
  1024. }
  1025. magicJS.logger.info(msgTipsText);
  1026. if (nopayCount > 0) {
  1027. await doCustomWxpusherSend(`存在待付订单(${nopayCount})`, htmlContent, 'https://mo.10coo.com/');
  1028. }
  1029. }
  1030. }
  1031. async function doCustomWxpusherSend(title, content, url = '') {
  1032. let data = {
  1033. appToken: "AT_7wDWqSoT8xpJCQqJtHpshKhw7kXc0XCW",
  1034. content: content,// 这是主体内容
  1035. summary: title,// 该参数可选,默认为 msg 的前10个字符
  1036. contentType: 2,// 1:纯文本,2:html,3:markdown
  1037. // topicIds: [32852],
  1038. topicIds: [],
  1039. uids: [
  1040. "UID_6P4B00X6Zv8U2oKC0I2R09emxtqq"
  1041. ],
  1042. url: url,
  1043. verifyPay: false,
  1044. verifyPayType: 0,
  1045. };
  1046. return await magicJS.doWxpusherSend(data);
  1047. }
  1048. async function queryToBePaidStatusOrderList() {
  1049. const url = `https://mo.10coo.com/order/queryToBePaidStatusOrderList`;
  1050. let options = {
  1051. url: url,
  1052. headers: gCommonHeaders,
  1053. body: ``,
  1054. };
  1055. let result = await magicJS.http.get(options).then(response => {
  1056. try {
  1057. let rspData = response.body;
  1058. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  1059. return rspData;
  1060. } catch (e) {
  1061. magicJS.logger.error(e);
  1062. }
  1063. }).catch(err => {
  1064. const msg = `请求等待付款订单列表异常\n${JSON.stringify(err)}`;
  1065. magicJS.logger.error(msg);
  1066. });
  1067. return result;
  1068. }
  1069. async function doPayResultCheck(id) {
  1070. let ret = await queryOfficialStatus(id);
  1071. if (ret && ret.code == 0) {
  1072. let isExist = await checkPayResult(id);
  1073. return isExist;
  1074. }
  1075. return false;
  1076. }
  1077. async function queryOfficialStatus(id) {
  1078. const url = `https://mo.10coo.com/order/queryOfficialStatus?orderId=${id}`;
  1079. let options = {
  1080. url: url,
  1081. headers: gCommonHeaders,
  1082. body: ``,
  1083. };
  1084. let result = await magicJS.http.get(options).then(response => {
  1085. try {
  1086. let rspData = response.body;
  1087. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  1088. return rspData;
  1089. } catch (e) {
  1090. magicJS.logger.error(e);
  1091. }
  1092. }).catch(err => {
  1093. const msg = `检测支付结果异常\n${JSON.stringify(err)}`;
  1094. magicJS.logger.error(msg);
  1095. });
  1096. return result;
  1097. }
  1098. async function checkPayResult(id) {
  1099. const url = `https://mo.10coo.com/order/batchQueryOrderStatus?orderIds=${id}`;
  1100. let options = {
  1101. url: url,
  1102. headers: gCommonHeaders,
  1103. body: ``,
  1104. };
  1105. let result = await magicJS.http.get(options).then(response => {
  1106. try {
  1107. let rspData = response.body;
  1108. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  1109. if (rspData.data[0][1] == 1 || rspData.data[0][1] == 2 || rspData.data[0][1] == 3 || rspData.data[0][1] == 4) {
  1110. return true;
  1111. } else if (rspData.data[0][1] == -3 || rspData.data[0][1] == 0) {
  1112. magicJS.logger.info(`未检测到支付`);
  1113. return false;
  1114. }
  1115. return false;
  1116. } catch (e) {
  1117. magicJS.logger.error(e);
  1118. }
  1119. }).catch(err => {
  1120. const msg = `检测支付结果异常\n${JSON.stringify(err)}`;
  1121. magicJS.logger.error(msg);
  1122. });
  1123. return result;
  1124. }
  1125. async function queryOrderDetail(id) {
  1126. const url = `https://mo.10coo.com/order/queryOrderDetail?orderId=${id}`;
  1127. let options = {
  1128. url: url,
  1129. headers: gCommonHeaders,
  1130. body: ``,
  1131. };
  1132. let result = await magicJS.http.get(options).then(response => {
  1133. try {
  1134. let rspData = response.body;
  1135. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  1136. return rspData;
  1137. } catch (e) {
  1138. magicJS.logger.error(e);
  1139. }
  1140. }).catch(err => {
  1141. const msg = `检测订单详情异常\n${JSON.stringify(err)}`;
  1142. magicJS.logger.error(msg);
  1143. });
  1144. return result;
  1145. }
  1146. async function getZFBPayUrl(id) {
  1147. const url = `https://mo.10coo.com/zPay?orderId=${id}`;
  1148. let options = {
  1149. url: url,
  1150. headers: gCommonHeaders,
  1151. body: ``,
  1152. };
  1153. let result = await magicJS.http.get(options).then(response => {
  1154. try {
  1155. let rspData = response.body;
  1156. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  1157. if (rspData.code === 0 && rspData.data.code === 200) {
  1158. return rspData.data.data.data.url;
  1159. }
  1160. return null;
  1161. } catch (e) {
  1162. magicJS.logger.error(e);
  1163. }
  1164. }).catch(err => {
  1165. const msg = `生成支付宝支付链接异常\n${JSON.stringify(err)}`;
  1166. magicJS.logger.error(msg);
  1167. });
  1168. return result;
  1169. }
  1170. async function getWxPayUrl(id) {
  1171. const url = `https://mo.10coo.com/wPay?orderId=${id}`;
  1172. let options = {
  1173. url: url,
  1174. headers: gCommonHeaders,
  1175. body: ``,
  1176. };
  1177. let result = await magicJS.http.get(options).then(response => {
  1178. try {
  1179. let rspData = response.body;
  1180. magicJS.logger.info(`rspData=${JSON.stringify(rspData)}`);
  1181. if (rspData.code === 0 && rspData.data.code === 200) {
  1182. return rspData.data.data.openlink;
  1183. }
  1184. return null;
  1185. } catch (e) {
  1186. magicJS.logger.error(e);
  1187. }
  1188. }).catch(err => {
  1189. const msg = `生成微信支付链接异常\n${JSON.stringify(err)}`;
  1190. magicJS.logger.error(msg);
  1191. });
  1192. return result;
  1193. }
  1194. Main().catch((e) => magicJS.logger.log(`-\n ${e}`)).finally(() => magicJS.done());
  1195. //---SyncByPyScript---MagicJS3-start
  1196. function MagicJS(e = "MagicJS", t = "INFO") { const n = () => { const e = "undefined" != typeof $loon, t = "undefined" != typeof $task, r = "undefined" != typeof module, n = "undefined" != typeof $httpClient && !e, o = "undefined" != typeof $storm, i = "undefined" != typeof $environment && void 0 !== $environment["stash-build"]; var s = n || e || o || i; const a = "undefined" != typeof importModule; return { isLoon: e, isQuanX: t, isNode: r, isSurge: n, isStorm: o, isStash: i, isSurgeLike: s, isScriptable: a, get name() { return e ? "Loon" : t ? "QuantumultX" : r ? "NodeJS" : n ? "Surge" : a ? "Scriptable" : "unknown" }, get build() { return n ? $environment["surge-build"] : i ? $environment["stash-build"] : o ? $storm.buildVersion : void 0 }, get language() { if (n || i) return $environment.language }, get version() { return n ? $environment["surge-version"] : i ? $environment["stash-version"] : o ? $storm.appVersion : r ? process.version : void 0 }, get system() { return n ? $environment.system : r ? process.platform : void 0 }, get systemVersion() { if (o) return $storm.systemVersion }, get deviceName() { if (o) return $storm.deviceName } } }, o = (r, e = "INFO") => { let n = e, t = "\n"; const o = { SNIFFER: 6, DEBUG: 5, INFO: 4, NOTIFY: 3, WARNING: 2, ERROR: 1, CRITICAL: 0, NONE: -1 }, i = { SNIFFER: "", DEBUG: "", INFO: "", NOTIFY: "", WARNING: "❗ ", ERROR: "❌ ", CRITICAL: "❌ ", NONE: "" }, s = (e, t = "INFO") => { o[n] < o[t.toUpperCase()] || console.log(`██[${r}][${t}]` + i[t.toUpperCase()] + e + "\n") }; return { getLevel: () => n, setLevel: e => { n = e }, sniffer: (...e) => { e = e.join(t); s(e, "SNIFFER") }, log: (...e) => { e = e.join(t); console.log(`██[${r}]` + e + "\n") }, debug: (...e) => { e = e.join(t); s(e, "DEBUG") }, info: (...e) => { e = e.join(t); s(e, "INFO") }, notify: (...e) => { e = e.join(t); s(e, "NOTIFY") }, warning: (...e) => { e = e.join(t); s(e, "WARNING") }, error: (...e) => { e = e.join(t); s(e, "ERROR") }, retry: (...e) => { e = e.join(t); s(e, "RETRY") } } }; return new class { constructor(e, t) { var r; this._startTime = Date.now(), this.version = "3.0.0", this.scriptName = e, this.env = n(), this.logger = o(e, t), this.http = "function" == typeof MagicHttp ? MagicHttp(this.env, this.logger) : void 0, this.data = "function" == typeof MagicData ? MagicData(this.env, this.logger) : void 0, this.notification = "function" == typeof MagicNotification ? MagicNotification(this.scriptName, this.env, this.logger, this.http) : void 0, this.utils = "function" == typeof MagicUtils ? MagicUtils(this.env, this.logger) : void 0, this.qinglong = "function" == typeof MagicQingLong ? MagicQingLong(this.env, this.data, this.logger) : void 0, void 0 !== this.data && (t = this.data.read("magic_loglevel"), r = this.data.read("magic_bark_url"), t && this.logger.setLevel(t.toUpperCase()), r) && this.notification.setBark(r), this.logger.info(e + ", 开始执行!") } get isRequest() { return "undefined" != typeof $request } get isStrictRequest() { return "undefined" != typeof $request && "undefined" == typeof $response } get isResponse() { return "undefined" != typeof $response } get isDebug() { return "DEBUG" === this.logger.level } get request() { return "undefined" != typeof $request ? $request : void 0 } get response() { if ("undefined" != typeof $response) return $response.hasOwnProperty("status") && ($response.statusCode = $response.status), $response.hasOwnProperty("statusCode") && ($response.status = $response.statusCode), $response } log(...e) { this.logger.log(e) } toStr(e, t = null) { try { return JSON.stringify(e) } catch { return t } } toObj(e, t = null) { try { return JSON.parse(e) } catch { return t } } checkRecordRequestBody() { if (this.isRequest) { var t = $request.body; if (t) { var r = this.env, n = $request.path; let e = this.scriptName + "#" + n.replace("/", "_"); e = e.replace("?", "#"), r.isQuanX && $prefs.setValueForKey(t, e), (r.isLoon || r.isSurge) && $persistentStore.write(t, e), r.isNode && require("fs").writeFileSync(e + ".json", t, { flag: "w" }, e => console.log(e)) } } } getRequestBody() { var e = this.env, t = $request.path; let r = this.scriptName + "#" + t.replace("/", "_"); if (r = r.replace("?", "#"), e.isSurge || e.isLoon) return $persistentStore.read(r); if (e.isQuanX) return $prefs.valueForKey(r); if (e.isNode) { t = r + ".json", e = require("fs"); if (!e.existsSync(t)) return JSON.parse(e.readFileSync(t)) } } getResponseBody() { if ($response) return $response.body } parseCookies(e, t = !1) { let r = {}; return t ? e && e.split(";").forEach(function (e) { e = e.split("="); r[e.shift().trim()] = decodeURIComponent(e.join("=")) }) : e && e.split(";").forEach(function (e) { e = e.split("="); r[e.shift().trim()] = e.join("=") }), r } serializeCookies(e, t = !1) { var r = []; if (t) for (var n in e) { var o = e[n], n = n + "=" + encodeURIComponent(o); r.push(n) } else for (var i in e) { var s = e[i], i = i + "=" + s; r.push(i) } return r.join("; ") } parseSetCookies(e) { var t = e.split(/,\s*/).map(e => { var e = e.trim().split(/;\s*(?=[^=]+=[^;]*)/), t = e[0].split("=").map(e => e.trim()); const r = { name: t[0], value: t[1] }; return e.slice(1).forEach(e => { var [e, t] = e.split("=").map(e => e.trim()); r[e] = t || !0 }), r }), r = []; let n = 0; for (; n < t.length;) { var o, i = t[n]; r.push(i), i.Expires ? ((o = t[n + 1]) && (i.Expires = i.Expires + "," + o.name), n += 2) : n += 1 } return r } objToQueryStr(t, r) { let n = ""; for (const o in t) { let e = t[o]; null != e && "" !== e && ("object" == typeof e ? e = JSON.stringify(e) : r && (e = encodeURIComponent(e)), n += `${o}=${e}&`) } return n = n.substring(0, n.length - 1) } parseQueryStr(e) { var t = {}, r = (e = -1 < e.indexOf("?") ? e.split("?")[1] : e).split("&"); for (let e = 0; e < r.length; e++) { var n = r[e].split("="); t[n[0]] = n[1] } return t } deepClone(e, t) { for (var r in t = t || {}, e) "object" == typeof e[r] ? (t[r] = e[r].constructor === Array ? [] : {}, this.deepClone(e[r], t[r])) : t[r] = e[r]; return t } formatDate(e, t) { var r, n = { "M+": e.getMonth() + 1, "d+": e.getDate(), "H+": e.getHours(), "m+": e.getMinutes(), "s+": e.getSeconds(), "q+": Math.floor((e.getMonth() + 3) / 3), S: e.getMilliseconds() }; for (r in /(y+)/.test(t) && (t = t.replace(RegExp.$1, (e.getFullYear() + "").substr(4 - RegExp.$1.length))), n) new RegExp("(" + r + ")").test(t) && (t = t.replace(RegExp.$1, 1 == RegExp.$1.length ? n[r] : ("00" + n[r]).substr(("" + n[r]).length))); return t } parseDate(a, e) { let l = { y: 0, M: 1, d: 0, H: 0, h: 0, m: 0, s: 0, S: 0 }; (e = e || "yyyy-MM-dd").replace(/([^yMdHmsS]*?)(([yMdHmsS])\3*)([^yMdHmsS]*?)/g, function (e, t, r, n, o, i, s) { return a = a.replace(new RegExp(t + "(\\d{" + r.length + "})" + o), function (e, t) { return l[n] = parseInt(t), "" }), "" }), l.M--; e = new Date(l.y, l.M, l.d, l.H, l.m, l.s); return 0 !== l.S && e.setMilliseconds(l.S), e } getBaseDoneHeaders(e = {}) { return Object.assign({ "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "POST,GET,OPTIONS,PUT,DELETE", "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept" }, e) } getHtmlDoneHeaders() { return this.getBaseDoneHeaders({ "Content-Type": "text/html;charset=UTF-8" }) } getJsonDoneHeaders() { return this.getBaseDoneHeaders({ "Content-Type": "text/json; charset=utf-8", Connection: "keep-alive" }) } doWxpusherSend(e) { var t = this.getJsonDoneHeaders(), t = (t.Host = "wxpusher.zjiecode.com", t["Content-Type"] = "application/json;charset=UTF-8", { url: "https://wxpusher.zjiecode.com/api/send/message", headers: t, body: JSON.stringify(e) }); return this.http.post(t) } fastWxpusherSend(e, t = "", r = "") { return this.doWxpusherSend({ appToken: "AT_7wDWqSoT8xpJCQqJtHpshKhw7kXc0XCW", content: e, summary: t, contentType: 1, topicIds: [], uids: ["UID_6P4B00X6Zv8U2oKC0I2R09emxtqq"], url: r, verifyPay: !1, verifyPayType: 0 }) } isEmpty(e) { return void 0 === e || null == e || "" == e || "null" == e || "undefined" == e || 0 === e.length } base64Encode(e) { var t, r, n, o = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; let i, s, a; for (a = e.length, s = 0, i = ""; s < a;) { if (t = 255 & e.charCodeAt(s++), s == a) { i = (i += o.charAt(t >> 2)) + o.charAt((3 & t) << 4) + "=="; break } if (r = e.charCodeAt(s++), s == a) { i = (i = (i += o.charAt(t >> 2)) + o.charAt((3 & t) << 4 | (240 & r) >> 4)) + o.charAt((15 & r) << 2) + "="; break } n = e.charCodeAt(s++), i = (i = (i = (i += o.charAt(t >> 2)) + o.charAt((3 & t) << 4 | (240 & r) >> 4)) + o.charAt((15 & r) << 2 | (192 & n) >> 6)) + o.charAt(63 & n) } return i } base64Decode(e) { var t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; if (/([^\s]+[^0-9a-zA-Z\+\/\=]|[^0-9a-zA-Z\+\/\=]\s+)/.test(e)) throw new Error("Invalid base64 input"); var r, n, o, i, s, a, l = e.replace(/\s/g, ""); let u = "", c = 0; for (; c < l.length;)o = t.indexOf(l.charAt(c++)), r = (15 & (i = t.indexOf(l.charAt(c++)))) << 4 | (s = t.indexOf(l.charAt(c++))) >> 2, n = (3 & s) << 6 | (a = t.indexOf(l.charAt(c++))), u += String.fromCharCode(o << 2 | i >> 4), 64 !== s && (u += String.fromCharCode(r)), 64 !== a && (u += String.fromCharCode(n)); return u = this.utf8Decode(u) } utf8Decode(t) { let e = [], r = 0, n = 0, o = 0; for (t = t.replace(/\r\n/g, "\n"); r < t.length;) { n = 255 & t.charCodeAt(r), o = 0, o = n <= 191 ? (n &= 127, 1) : n <= 223 ? (n &= 31, 2) : n <= 239 ? (n &= 15, 3) : (n &= 7, 4); for (let e = 1; e < o; ++e)n = n << 6 | 63 & t.charCodeAt(e + r); 4 === o ? (n -= 65536, e.push(String.fromCharCode(55296 | n >> 10 & 1023)), e.push(String.fromCharCode(56320 | 1023 & n))) : e.push(String.fromCharCode(n)), r += o } return e.join("") } parseJwt(e) { try { var t = e.split("."), r = t[0].replace(/-/g, "+").replace(/_/g, "/"), n = this.base64Decode(r).replace(/\0/g, ""), o = JSON.parse(n), i = t[1].replace(/-/g, "+").replace(/_/g, "/"), s = this.base64Decode(i).replace(/\0/g, ""); return { header: o, payload: JSON.parse(s), signature: t[2] } } catch (e) { return this.log(e), null } } costTime() { var e = this.scriptName + "执行完毕!", t = (this._endTime = (new Date).getTime(), this._endTime - this._startTime); this.logger.info(e + `耗时【${t / 1e3}】秒`) } done = (e = {}) => { this.costTime(), "undefined" != typeof $done && $done(e) } }(e, t) } function MagicHttp(d, p) { var e; let g; d.isNode && (e = require("axios"), g = e.create()); class t { constructor(e = !0) { this.handlers = [], this.isRequest = e } use(e, t, r) { return "function" == typeof e && p.debug("Register fulfilled " + e.name), "function" == typeof t && p.debug("Register rejected " + t.name), this.handlers.push({ fulfilled: e, rejected: t, synchronous: !(!r || "boolean" != typeof r.synchronous) && r.synchronous, runWhen: r ? r.runWhen : null }), this.handlers.length - 1 } eject(e) { this.handlers[e] && (this.handlers[e] = null) } forEach(t) { this.handlers.forEach(e => { null !== e && t(e) }) } } function f(e) { let r = { ...e }; return r.params && !d.isNode && (e = Object.keys(r.params).map(e => { var t = encodeURIComponent(e); return r.url = r.url.replace(new RegExp(e + "=[^&]*", "ig"), ""), r.url = r.url.replace(new RegExp(t + "=[^&]*", "ig"), ""), t + "=" + encodeURIComponent(r.params[e]) }).join("&"), r.url.indexOf("?") < 0 && (r.url += "?"), /(&|\?)$/g.test(r.url) || (r.url += "&"), r.url += e, delete r.params, p.debug("Params to QueryString: " + r.url)), r } const h = (e, t = null) => { if (e) { t = { ...e, config: e.config || t, status: e.statusCode || e.status, body: e.body || e.data, headers: e.headers || e.header }; if ("string" == typeof t.body) try { t.body = JSON.parse(t.body) } catch { } return delete t.data, t } return e }; const y = (e, t = null) => { if (e && 400 <= e.status) return p.debug("Raise exception when status code is " + e.status), { name: "RequestException", message: "Request failed with status code " + e.status, config: t || e.config, response: e } }, m = { request: new t, response: new t(!1) }; let v = [], b = [], S = !0; function N(e) { return e = f(e), p.debug(`HTTP ${e.method.toUpperCase()}:` + "\n" + JSON.stringify(e)), e } function E(t) { try { t = t && h(t), p.sniffer(`HTTP ${t.config.method.toUpperCase()}:` + "\n" + JSON.stringify(t.config) + "\nSTATUS CODE:\n" + t.status + "\nRESPONSE:\n" + ("object" == typeof t.body ? JSON.stringify(t.body) : t.body)); var e = y(t); return e ? Promise.reject(e) : t } catch (e) { return p.error(e), t } } const r = (e, r) => { let n; r = ((e, t) => { let r = "object" == typeof t ? { headers: {}, ...t } : { url: t, headers: {} }; return r.method || (r.method = e), !0 === (r = f(r)).rewrite && (d.isSurge ? (r.headers["X-Surge-Skip-Scripting"] = !1, delete r.rewrite) : d.isQuanX && (r.hints = !1, delete r.rewrite)), d.isSurgeLike ? (t = r.headers["content-type"] || r.headers["Content-Type"], "GET" !== r.method && t && 0 <= t.indexOf("application/json") && r.body instanceof Array && (r.body = JSON.stringify(r.body), p.debug("Convert Array object to String: " + r.body))) : d.isQuanX ? (r.hasOwnProperty("body") && "string" != typeof r.body && (r.body = JSON.stringify(r.body)), r.method = e) : d.isNode && ("POST" === e || "PUT" === e || "PATCH" === e || "DELETE" === e ? r.data = r.data || r.body : "GET" === e && (r.params = r.params || r.body), delete r.body), r })(e.toUpperCase(), r), n = d.isNode ? g : d.isSurgeLike ? i => new Promise((n, o) => { $httpClient[e.toLowerCase()](i, (e, t, r) => { e ? (e = { name: e.name || e, message: e.message || e, stack: e.stack || e, config: i, response: h(t) }, o(e)) : (t.config = i, t.body = r, n(t)) }) }) : n => new Promise((r, t) => { $task.fetch(n).then(e => { e = h(e, n); var t = y(e, n); if (t) return Promise.reject(t); r(e) }).catch(e => { e = { name: e.message || e.error, message: e.message || e.error, stack: e.error, config: n, response: e.response ? h(e.response) : null }; t(e) }) }); let o; var t = r; try { v = [], b = [], m.request.forEach(e => { "function" == typeof e.runWhen && !1 === e.runWhen(t) || (S = S && e.synchronous, v.unshift(e.fulfilled, e.rejected)) }), m.response.forEach(e => { b.push(e.fulfilled, e.rejected) }) } catch (e) { p.error(`Failed to register interceptors: ${e}.`) } var i = [N, void 0], s = [E, void 0]; if (S) { for (p.debug("Interceptors are executed in synchronous mode"), Array.prototype.unshift.apply(v, i), v = v.concat([N, void 0]); v.length;) { var a = v.shift(), l = v.shift(); try { "function" == typeof a && p.debug("Executing request fulfilled " + a.name), r = a(r) } catch (e) { "function" == typeof l && p.debug("Executing request rejected " + l.name), l(e); break } } try { o = (!d.isNode && r.timeout ? c : n)(r) } catch (e) { return Promise.reject(e) } for (Array.prototype.unshift.apply(b, s); b.length;)o = o.then(b.shift(), b.shift()); return o } { p.debug("Interceptors are executed in asynchronous mode"); let t = [n, void 0]; for (Array.prototype.unshift.apply(t, i), Array.prototype.unshift.apply(t, v), t = (t = t.concat(s)).concat(b), o = Promise.resolve(r); t.length;)try { let e = t.shift(); var u = t.shift(); "function" == typeof (e = !d.isNode && r.timeout && e === n ? c : e) && p.debug("Executing request fulfilled " + e.name), "function" == typeof u && p.debug("Executing request rejected " + u.name), o = o.then(e, u) } catch (e) { p.error("request exception: " + e) } return o } function c(r) { try { var e = new Promise((e, t) => { setTimeout(() => { var e = { message: `timeout of ${r.timeout}ms exceeded.`, config: r }; t(e) }, r.timeout) }); return Promise.race([n(r), e]) } catch (e) { p.error(`Request Timeout exception: ${e}.`) } } }; return { request: r, interceptors: m, convertHeadersToLowerCase: r => Object.keys(r).reduce((e, t) => (e[t.toLowerCase()] = r[t], e), {}), convertHeadersToCamelCase: r => Object.keys(r).reduce((e, t) => { return e[t.split("-").map(e => e[0].toUpperCase() + e.slice(1)).join("-")] = r[t], e }, {}), modifyResponse: h, get: e => r("GET", e), post: e => r("POST", e), put: e => r("PUT", e), patch: e => r("PATCH", e), delete: e => r("DELETE", e), head: e => r("HEAD", e), options: e => r("OPTIONS", e) } } function MagicData(d, p) { let g = { fs: void 0, data: {} }; if (d.isNode) { g.fs = require("fs"); try { g.fs.accessSync("./magic.json", g.fs.constants.R_OK | g.fs.constants.W_OK) } catch (e) { g.fs.writeFileSync("./magic.json", "{}", { encoding: "utf8" }) } g.data = require("./magic.json") } const s = (e, t) => "object" != typeof t && e === t, a = e => "true" === e || "false" !== e && (void 0 === e ? null : e), l = (e, t, r, n) => { if (r) try { e = !0 === (e = "string" == typeof e ? JSON.parse(e) : e).magic_session ? e[r] : null } catch { e = null } if ("string" == typeof e && "null" !== e) try { e = JSON.parse(e) } catch { } return null == (e = !1 === n && e && !0 === e.magic_session ? null : e) && null != t && (e = t), e = a(e) }, f = t => { if ("string" != typeof t) return t instanceof Array || null == t || t != t || "boolean" == typeof t ? {} : t; { let e = {}; try { var r = typeof (e = JSON.parse(t)); ("object" != r || e instanceof Array || "bool" == r || null === e) && (e = {}) } catch { } return e } }, u = (e, t = null, r = "", n = !1, o = null) => { let i = ""; return i = o || d.isNode ? ((e, t = null, r = "", n = !1, o = null) => { o = o || g.data; return val = o && void 0 !== o[e] && null !== o[e] ? o[e] : r ? {} : null, val = l(val, t, r, n) })(e, t, r, n, o) : (d.isSurgeLike ? i = $persistentStore.read(e) : d.isQuanX && (i = $prefs.valueForKey(e)), l(i, t, r, n)), p.debug(`READ DATA [${e}]${r ? `[${r}]` : ""} <${typeof i}>` + "\n" + JSON.stringify(i)), i }, c = (e, t, r = "", n = null) => { if (void 0 === t || t != t) return !1; d.isNode || "boolean" != typeof t && "number" != typeof t || (t = String(t)); let o = ""; var i, s, a, l, u, c; if (n || d.isNode ? o = ([i, s, a = "", l = null] = [e, t, r, n], c = l || g.data, c = f(c), a ? ((u = f(c[i])).magic_session = !0, u[a] = s, c[i] = u) : c[i] = s, null !== l && (l = c), c) : r ? (d.isSurgeLike ? o = $persistentStore.read(e) ? $persistentStore.read(e) : o : d.isQuanX && (o = $prefs.valueForKey(e) ? $prefs.valueForKey(e) : o), (o = f(o)).magic_session = !0, o[r] = t) : o = t, o && "object" == typeof o && (o = JSON.stringify(o, null, 4)), p.debug(`WRITE DATA [${e}]${r ? `[${r}]` : ""} <${typeof t}>` + "\n" + JSON.stringify(t)), !n) { if (d.isSurgeLike) return $persistentStore.write(o, e); if (d.isQuanX) return $prefs.setValueForKey(o, e); if (d.isNode) try { g.fs.writeFileSync("./magic.json", o) } catch (e) { return p.error(e), !1 } } return !0 }; return { read: u, write: c, del: (e, t = "", r = null) => { let n = {}; if (r || d.isNode) n = (o = e, i = t, s = r || g.data, s = f(s), i ? (delete (obj = f(s[o]))[i], s[o] = obj) : delete s[o], s), r ? r = n : g.fs.writeFileSync("./magic.json", JSON.stringify(n, null, 4)); else if (t) { d.isSurgeLike ? n = $persistentStore.read(e) : d.isQuanX && (n = $prefs.valueForKey(e)), delete (n = f(n))[t]; i = JSON.stringify(n, null, 4); c(e, i) } else { if (d.isStorm) return $persistentStore.remove(e); if (d.isSurgeLike) return $persistentStore.write(null, e); if (d.isQuanX) return $prefs.removeValueForKey(e) } var o, i, s; p.debug(`DELETE KEY [${e}]` + (t ? `[${t}]` : "")) }, update: (e, t, r, n = s, o = null) => { var i; return t = a(t), !0 !== n(u(e, null, r, !1, o), t) && (i = c(e, t, r, o), e = u(e, null, r, !1, o), n === s && "object" == typeof e ? i : n(t, e)) }, allSessions: (e, t = null) => { let r = {}; t = u(e, null, null, !0, t); return !0 === (t = f(t)).magic_session && delete (r = { ...t }).magic_session, p.debug(`READ ALL SESSIONS [${e}] <${typeof r}>` + "\n" + JSON.stringify(r, null, 4)), r }, allSessionNames: (e, t = null) => { let r = []; t = u(e, null, null, !0, t), t = f(t); return r = !0 !== t.magic_session ? [] : Object.keys(t).filter(e => "magic_session" !== e), p.debug(`READ ALL SESSIONS [${e}] <${typeof r}>` + "\n" + JSON.stringify(r, null, 4)), r }, defaultValueComparator: s, convertToObject: f } } function MagicNotification(i, o, s, a) { let l = null, u = null, c = []; function d(e = i, t = "", r = "", n = "") { n = (t => { try { let e = {}; var r; return "string" == typeof t ? 0 < t.length && (o.isLoon ? e = { openUrl: t } : o.isQuanX ? e = { "open-url": t } : o.isSurge && (e = { url: t })) : "object" == typeof t && (o.isLoon ? (e.openUrl = t["open-url"] || "", e.mediaUrl = t["media-url"] || "") : o.isQuanX ? e = t["open-url"] || t["media-url"] ? t : {} : o.isSurge && (r = t["open-url"] || t.openUrl, e = r ? { url: r } : {})), e } catch (e) { s.error("通知选项转换失败" + e) } return t })(n), 1 === arguments.length && (e = i, t = "", r = arguments[0]), s.notify("\ntitle:" + e + "\nsubTitle:" + t + "\nbody:" + r + "\noptions:" + ("object" == typeof n ? JSON.stringify(n) : n)), o.isSurge ? $notification.post(e, t, r, n) : o.isLoon ? n ? $notification.post(e, t, r, n) : $notification.post(e, t, r) : o.isQuanX && $notify(e, t, r, n), l && u && p(e, t, r) } function p(e = i, t = "", r = "", n) { if (void 0 === a || void 0 === a.post) throw "Bark notification needs to import MagicHttp module."; e = { url: l, headers: { "content-type": "application/json; charset=utf-8" }, body: { title: e, body: t ? t + "\n" + r : r, device_key: u } }; a.post(e).catch(e => { s.error("Bark notify error: " + e) }) } return { post: d, debug: function (e = i, t = "", r = "", n = "") { "DEBUG" === s.getLevel() && (1 === arguments.length && (e = i, t = "", r = arguments[0]), this.post(e, t, r, n)) }, bark: p, setBark: e => { try { var t = e.replace(/\/+$/g, ""); l = /^https?:\/\/([^/]*)/.exec(t)[0] + "/push", u = /\/([^\/]+)\/?$/.exec(t)[1] } catch (e) { s.error(`Bark url error: ${e}.`) } }, appendNotifyInfo: function (e, t) { 1 == t ? c = e : c.push(e) }, prependNotifyInfo: function (e) { c.splice(0, 0, e) }, msg: function (e, t, r, n) { var o = {}; r && (o["open-url"] = r), n && (o["media-url"] = n), (t = t && 0 != t.length ? t : Array.isArray(c) ? c.join("\n") : c) && 0 < t.length && d(i, "", t, o) } } } function MagicUtils(n, u) { const e = (e, t = "yyyy-MM-dd hh:mm:ss") => { var r, n = { "M+": e.getMonth() + 1, "d+": e.getDate(), "h+": e.getHours(), "m+": e.getMinutes(), "s+": e.getSeconds(), "q+": Math.floor((e.getMonth() + 3) / 3), S: e.getMilliseconds() }; for (r in /(y+)/.test(t) && (t = t.replace(RegExp.$1, (e.getFullYear() + "").substr(4 - RegExp.$1.length))), n) new RegExp("(" + r + ")").test(t) && (t = t.replace(RegExp.$1, 1 === RegExp.$1.length ? n[r] : ("00" + n[r]).substr(("" + n[r]).length))); return t }; return { retry: (i, s = 5, a = 0, l = null) => (...e) => new Promise((r, n) => { function o(...t) { Promise.resolve().then(() => i.apply(this, t)).then(e => { "function" == typeof l ? Promise.resolve().then(() => l(e)).then(() => { r(e) }).catch(e => { 1 <= s ? 0 < a ? setTimeout(() => o.apply(this, t), a) : o.apply(this, t) : n(e), s-- }) : r(e) }).catch(e => { u.error(e), 1 <= s && 0 < a ? setTimeout(() => o.apply(this, t), a) : 1 <= s ? o.apply(this, t) : n(e), s-- }) } o.apply(this, e) }), formatTime: e, now: () => e(new Date, "yyyy-MM-dd hh:mm:ss"), today: () => e(new Date, "yyyy-MM-dd"), sleep: t => new Promise(e => setTimeout(e, t)), assert: (e, t = null) => { var r; n.isNode ? (r = require("assert"), t ? r(e, t) : r(e)) : !0 !== e && u.error("AssertionError: " + (t || "The expression evaluated to a falsy value.")) } } } function MagicQingLong(e, a, o) { let i = "", s = "", l = "", u = "", c = "", t = ""; const d = "magic.json", p = MagicHttp(e, o); async function r() { return l = l || a.read("magic_qlclient"), u = u || a.read("magic_qlsecrt"), s = s || a.read("magic_qlname"), c = c || a.read("magic_qlpwd"), i && l && u ? (o.info("Get token from QingLong Panel"), await p.get({ url: "/open/auth/token", headers: { "content-type": "application/json" }, params: { client_id: l, client_secret: u } }).then(e => { if (!(0 < Object.keys(e.body).length && e.body.data && e.body.data.token)) throw new Error("Get QingLong Panel token failed."); o.info("Successfully logged in to QingLong Panel"), t = e.body.data.token, a.write("magic_qltoken", t) }).catch(e => { o.error("Error logging in to QingLong Panel.\n" + (e.message || e)) })) : i && s && c && await p.post({ url: "/api/user/login", headers: { "content-type": "application/json" }, body: { username: s, password: c } }).then(e => { o.info("Successfully logged in to QingLong Panel"), t = e.body.data.token, a.write("magic_qltoken", t) }).catch(e => { o.error("Error logging in to QingLong Panel.\n" + (e.message || e)) }), t } async function g(e) { let t = []; return await p.post({ url: "/api/envs", headers: { "content-type": "application/json" }, body: e }).then(e => { 200 === e.body.code ? e.body.data.forEach(e => { o.debug(`QINGLONG ADD ENV ${e.name} <${typeof e.value}> (${e.id})` + "\n" + JSON.stringify(e)), t.push(e.id) }) : o.error("Error adding environments variable from QingLong Panel.\n" + JSON.stringify(e)) }).catch(e => { o.error("Error adding environments variable from QingLong Panel.\n" + (e.message || e)) }), t } async function n(n = null, e = "", t) { let o = []; return await p.get({ url: "/api/envs", headers: { "content-type": "application/json" }, params: { searchValue: e } }).then(e => { if (200 !== e.body.code) throw new Error("Error reading environment variable from QingLong Panel.\n" + JSON.stringify(e)); e = e.body.data; if (n) { var t = []; for (const r of e) r.name === n && o.push(r); o = t } o = e }).catch(e => { throw new Error("Error reading environments variable from QingLong Panel.\n" + (e.message || e)) }), o } async function f(e, t = "") { let r = ""; return await p.get({ url: "/api/scripts/" + e, params: { path: t } }).then(e => { if (200 !== e.body.code) throw new Error("Error reading data from QingLong Panel.\n" + JSON.stringify(e)); r = e.body.data }).catch(e => { throw new Error("Error reading data from QingLong Panel.\n" + (e.message || e)) }), r } async function h(e, t = "", r = "") { let n = !1; return await p.put({ url: "/api/scripts", headers: { "content-type": "application/json" }, body: { filename: e, path: t, content: r } }).then(e => { 200 === e.body.code ? n = !0 : o.error("Error reading data from QingLong Panel.\n" + JSON.stringify(e)) }).catch(e => { o.error("Error reading data from QingLong Panel.\n" + (e.message || e)) }), n } return p.interceptors.request.use(function (e) { return i = i || a.read("magic_qlurl"), e.url.indexOf(i) < 0 && (e.url = "" + i + e.url), { ...e, timeout: 3e3 } }, void 0), p.interceptors.request.use(function (e) { return (l = l || a.read("magic_qlclient")) && (e.url = e.url.replace("/api/", "/open/")), e }, void 0, { runWhen: e => e.url.indexOf("api/user/login") < 0 && e.url.indexOf("open/auth/token") < 0 }), p.interceptors.request.use(async function (e) { return (t = t || a.read("magic_qltoken", "")) || await r(), e.headers.authorization = "Bearer " + t, e }, void 0, { runWhen: e => e.url.indexOf("api/user/login") < 0 && e.url.indexOf("open/auth/token") < 0 }), p.interceptors.request.use(function (e) { return e.params = { ...e.params, t: Date.now() }, e }, void 0, { runWhen: e => e.url.indexOf("open/auth/token") < 0 }), p.interceptors.request.use(function (e) { return i = i || a.read("magic_qlurl"), t = t || a.read("magic_qltoken"), o.debug("QingLong url: " + i + "\nQingLong token: " + t), e }, void 0), p.interceptors.response.use(void 0, async function (e) { try { var t = e.message || e.error || JSON.stringify(e); return (0 <= t.indexOf("NSURLErrorDomain") && 0 <= t.indexOf("-1012") || e.response && 401 === e.response.status) && e.config && !0 !== e.config.refreshToken ? (o.warning("QingLong Panel token has expired"), o.info("Refreshing the QingLong Panel token"), await r(), e.config.refreshToken = !0, o.info("Call the previous method again"), await p.request(e.config.method, e.config)) : Promise.reject(e) } catch (e) { return Promise.reject(e) } }), { url: i || a.read("magic_qlurl"), init: (e, t, r, n, o) => { i = e, l = t, u = r, s = n, c = o }, getToken: r, setEnv: async function (t, r, n = null) { if (i = i || a.read("magic_qlurl"), null === n) { var e = await g([{ name: t, value: r }]); if (e && 1 === e.length) return e[0] } else await p.put({ url: "/api/envs", headers: { "content-type": "application/json" }, body: { name: t, value: r, id: n } }).then(e => { if (200 === e.body.code) return o.debug(`QINGLONG UPDATE ENV ${t} <${typeof r}> (${n})` + "\n" + JSON.stringify(r)), !0; o.error("Error adding environment variable from QingLong Panel.\n" + JSON.stringify(e)) }).catch(e => (o.error("Error adding environment variable from QingLong Panel.\n" + (e.message || e)), !1)) }, setEnvs: g, getEnv: async function (e) { let t = null; for (const r of await n()) if (r.id === e) { t = r; break } return t }, getEnvs: n, delEnvs: async function (t) { return p.delete({ url: "/api/envs", headers: { accept: "application/json", "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6", connection: "keep-alive", "content-type": "application/json;charset=UTF-8", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.30" }, body: t }).then(e => 200 === e.body.code ? (o.debug("QINGLONG DELETE ENV IDS: " + t), !0) : (o.error("Error deleting environments variable from QingLong Panel.\n" + JSON.stringify(e)), !1)).catch(e => { o.error("Error deleting environments variable from QingLong Panel.\n" + (e.message || e)) }) }, disableEnvs: async function (t) { let r = !1; return await p.put({ url: "/api/envs/disable", headers: { accept: "application/json", "accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6", connection: "keep-alive", "content-type": "application/json;charset=UTF-8", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.30" }, body: t }).then(e => { 200 === e.body.code ? (o.debug("QINGLONG DISABLED ENV IDS: " + t), r = !0) : o.error("Error disabling environments variable from QingLong Panel.\n" + JSON.stringify(e)) }).catch(e => { o.error("Error disabling environments variable from QingLong Panel.\n" + (e.message || e)) }), r }, enableEnvs: async function (t) { let r = !1; return await p.put({ url: "/api/envs/enable", headers: { accept: "application/json", "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6", connection: "keep-alive", "content-type": "application/json;charset=UTF-8", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.30" }, body: t }).then(e => { 200 === e.body.code ? (o.debug("QINGLONG ENABLED ENV IDS: " + t), r = !0) : o.error("Error enabling environments variable from Qilong panel.\n" + JSON.stringify(e)) }).catch(e => { o.error("Error enabling environments variable from Qilong panel.\n" + (e.message || e)) }), r }, addScript: async function (e, t = "", r = "") { let n = !1; return await p.post({ url: "/api/scripts", headers: { "content-type": "application/json" }, body: { filename: e, path: t, content: r } }).then(e => { 200 === e.body.code ? n = !0 : o.error("Error reading data from QingLong Panel.\n" + JSON.stringify(e)) }).catch(e => { o.error("Error reading data from QingLong Panel.\n" + (e.message || e)) }), n }, getScript: f, editScript: h, delScript: async function (e, t = "") { let r = !1; return await p.delete({ url: "/api/scripts", headers: { "content-type": "application/json" }, body: { filename: e, path: t } }).then(e => { 200 === e.body.code ? r = !0 : o.error("Error reading data from QingLong Panel.\n" + JSON.stringify(e)) }).catch(e => { o.error("Error reading data from QingLong Panel.\n" + (e.message || e)) }), r }, write: async function (e, t, r = "") { var n = await f(d, ""), o = a.convertToObject(n), e = a.write(e, t, r, o), n = JSON.stringify(o, null, 4); return await h(d, "", n) && e }, read: async function (e, t, r = "", n = !1) { var o = await f(d, ""), o = a.convertToObject(o); return a.read(e, t, r, n, o) }, del: async function (e, t = "") { var r = await f(d, ""), n = a.convertToObject(r), e = a.del(e, t, n), r = JSON.stringify(n, null, 4), t = await h(d, "", r); return e && t }, update: async function (e, t, r, n = a.defaultValueComparator) { var o = await f(d, ""), i = a.convertToObject(o), e = a.update(e, t, r, n, i); let s = !1; return !0 === e && (o = JSON.stringify(i, null, 4), s = await h(d, "", o)), e && s }, batchWrite: async function (...e) { var t, r = await f(d, ""), n = a.convertToObject(r); for (t of e) a.write(t[0], t[1], void 0 !== t[2] ? t[2] : "", n); return r = JSON.stringify(n, null, 4), h(d, "", r) }, batchRead: async function (...e) { var t, r = await f(d, ""), n = a.convertToObject(r), o = []; for (t of e) { var i = a.read(t[0], t[1], void 0 !== t[2] ? t[2] : "", "boolean" == typeof t[3] && t[3], n); o.push(i) } return o }, batchUpdate: async function (...e) { var t, r = await f(d, ""), n = a.convertToObject(r); for (t of e) a.update(t[0], t[1], void 0 !== t[2] ? t[2] : "", void 0 !== t[3] ? t.comparator : a.defaultValueComparator, n); return r = JSON.stringify(n, null, 4), h(d, "", r) }, batchDel: async function (...e) { var t, r = await f(d, ""), n = a.convertToObject(r); for (t of e) a.del(t[0], void 0 !== t[1] ? t[1] : "", n); return r = JSON.stringify(n, null, 4), h(d, "", r) }, allSessions: async function (e) { var t = await f(d, ""), t = a.convertToObject(t); return a.allSessions(e, t) }, allSessionNames: async function (e) { var t = await f(d, ""), t = a.convertToObject(t); return a.allSessionNames(e, t) } } }
  1197. //---SyncByPyScript---MagicJS3-end