diff --git a/script/eleme/eleme_mission.js b/script/eleme/eleme_mission.js index 4e44ae600..dde15af0f 100644 --- a/script/eleme/eleme_mission.js +++ b/script/eleme/eleme_mission.js @@ -1,18 +1,17 @@ -const scriptName = '饿了么'; +const scriptName = "饿了么"; const getCookieRegex = /^https?:\/\/h5\.ele\.me\/restapi\/svip_biz\/v1\/supervip\/queryMissionCollect\?longitude=([^&]*).*latitude=([^&]*)/; -const elemeCookieKey = 'eleme_app_cookie'; -const elemeLongitudeKey = 'eleme_app_longitude'; -const elemeLatitudeKey = 'eleme_app_latitude'; +const elemeCookieKey = "eleme_app_cookie"; +const elemeLongitudeKey = "eleme_app_longitude"; +const elemeLatitudeKey = "eleme_app_latitude"; // 以下条件是AND的关系 -const taskKeywords = ['美食外卖']; // 任务JSON中含有此关键字的任务才会领取 -const requiredOrderAmount = 2; // 需要完成订单数量小于等于此数的任务才会领取 +const taskKeywords = ["美食外卖"]; // 任务JSON中含有此关键字的任务才会领取 +const requiredOrderAmount = 4; // 需要完成订单数量小于等于此数的任务才会领取 let magicJS = MagicJS(scriptName, "INFO"); -magicJS.unifiedPushUrl = magicJS.read('eleme_app_unified_push_url') || magicJS.read('magicjs_unified_push_url'); +magicJS.unifiedPushUrl = magicJS.read("eleme_app_unified_push_url") || magicJS.read("magicjs_unified_push_url"); - -function GetCookie(){ - try{ +function GetCookie() { + try { let cookie = magicJS.request.headers.Cookie; let arr = magicJS.request.url.match(getCookieRegex); let longitude = arr[1]; @@ -20,26 +19,24 @@ function GetCookie(){ let hisCookie = magicJS.read(elemeCookieKey); magicJS.write(elemeLongitudeKey, longitude); magicJS.write(elemeLatitudeKey, latitude); - let compareCookie2 = !!cookie? /cookie2=([a-zA-Z0-9]*)/.exec(cookie)[1]: null; - let compareHisCookie2 = !!hisCookie? /cookie2=([a-zA-Z0-9]*)/.exec(hisCookie)[1]: null; - if (!!!hisCookie || compareCookie2 !== compareHisCookie2){ + let compareCookie2 = !!cookie ? /cookie2=([a-zA-Z0-9]*)/.exec(cookie)[1] : null; + let compareHisCookie2 = !!hisCookie ? /cookie2=([a-zA-Z0-9]*)/.exec(hisCookie)[1] : null; + if (!!!hisCookie || compareCookie2 !== compareHisCookie2) { magicJS.write(elemeCookieKey, cookie); magicJS.logInfo(`旧的Cookie:${hisCookie}\n新的Cookie:${cookie}\nCookie不同,写入新的Cookie成功!`); - magicJS.notify('Cookie写入成功!!'); + magicJS.notify("Cookie写入成功!!"); + } else { + magicJS.logInfo("Cookie没有变化,无需更新"); } - else{ - magicJS.logInfo('Cookie没有变化,无需更新'); - } - } - catch(err){ - magicJS.notify('获取Cookie出现异常,请查阅日志。'); + } catch (err) { + magicJS.notify("获取Cookie出现异常,请查阅日志。"); magicJS.logError(`获取Cookie出现执行异常,异常信息:${err}`); } } // 获取超级会员任务列表 -function GetSuperVipMissions(cookie, longitude, latitude){ - return new Promise((resolve, reject)=>{ +function GetSuperVipMissions(cookie, longitude, latitude) { + return new Promise((resolve, reject) => { let options = { url: `https://h5.ele.me/restapi/svip_biz/v1/supervip/query_mission_list?longitude=${longitude}&latitude=${latitude}`, headers: { @@ -53,54 +50,52 @@ function GetSuperVipMissions(cookie, longitude, latitude){ "User-Agent": "Rajax/1 Apple/iPhone10,3 iOS/14.2 Eleme/9.3.8", "f-pTraceId": "WVNet_WV_2-3-30", "f-refer": "wv_h5", - "x-shard": `loc=${longitude},${latitude}` - } - } - magicJS.get(options, (err, resp, data)=>{ - if (err){ + "x-shard": `loc=${longitude},${latitude}`, + }, + }; + magicJS.get(options, (err, resp, data) => { + if (err) { magicJS.logError(`获取会员任务失败,请求异常:${err}`); - reject('❌获取会员任务失败,请求异常,请查阅日志!'); - } - else{ - try{ + reject("❌获取会员任务失败,请求异常,请查阅日志!"); + } else { + try { magicJS.logInfo(`获取会员任务,接口响应:${data}`); - let obj = typeof data === 'string'? JSON.parse(data) : data; - if (obj){ + let obj = typeof data === "string" ? JSON.parse(data) : data; + if (obj) { let result = []; - obj.missions.forEach(element => { + obj.missions.forEach((element) => { let missionInfo = JSON.stringify(element); let flag = false; - for (keyword of taskKeywords){ - if (missionInfo.indexOf(keyword) >= 0 && element.required_order_amount <= requiredOrderAmount){ + for (keyword of taskKeywords) { + if (missionInfo.indexOf(keyword) >= 0 && element.required_order_amount <= requiredOrderAmount) { flag = true; break; } } - if (flag === true){ + if (flag === true) { result.push(element.mission_id); } }); resolve(result); - } - else{ + } else { magicJS.logWarning(`没有可领取的会员任务,接口响应:${data}`); - reject('❌没有可领取的会员任务,请查阅日志!'); + reject("❌没有可领取的会员任务,请查阅日志!"); } - } - catch(err){ + } catch (err) { magicJS.logError(`获取会员任务失败,执行异常:${err},接口响应:${data}`); - reject('❌获取会员任务失败,执行异常,请查阅日志!'); + reject("❌获取会员任务失败,执行异常,请查阅日志!"); } } - }) - }) + }); + }); } // 接受超级会员任务列表中的任务 -function AcceptMission(cookie, longitude, latitude, mission_id){ - return new Promise((resolve, reject)=>{ +function AcceptMission(cookie, longitude, latitude, mission_id) { + let _mission_id = encodeURIComponent(mission_id); + return new Promise((resolve, reject) => { let options = { - url: `https://h5.ele.me/restapi/svip_biz/v1/supervip/accept_mission?type=0&mission_id=${mission_id}`, + url: `https://h5.ele.me/restapi/svip_biz/v1/supervip/accept_mission?type=0&receiveType=1&mission_id=${_mission_id}`, headers: { "Accept": "application/json, text/plain, */*", "Accept-Encoding": "gzip, deflate, br", @@ -111,82 +106,75 @@ function AcceptMission(cookie, longitude, latitude, mission_id){ "Host": "h5.ele.me", "Origin": "https://h5.ele.me", "Referer": "https://h5.ele.me/svip/home?entryStat=profile", - "User-Agent": "Rajax/1 Apple/iPhone10,3 iOS/14.2 Eleme/9.3.8", - "f-pTraceId": "WVNet_WV_2-3-79", + "User-Agent": "Rajax/1 Apple/iPhone10,3 iOS/14.5.1 Eleme/9.8.5", + "f-pTraceId": "WVNet_WV_1-1-40", "f-refer": "wv_h5", - "x-shard": `loc=${longitude},${latitude}` + "x-shard": "loc=121.52699279785156,31.2293643951416" }, - body: JSON.stringify({ - "longitude": longitude, - "latitude": latitude - }) - } - magicJS.post(options, (err, resp, data)=>{ - if (err){ + body: { + longitude: longitude, + latitude: latitude, + }, + }; + magicJS.post(options, (err, resp, data) => { + if (err) { magicJS.logError(`领取会员任务失败,任务Id:${mission_id},请求异常:${err}`); reject(`领取会员任务失败,任务Id:${mission_id}`); - } - else{ - try{ - let obj = typeof data === 'string'? JSON.parse(data) : data; - if (obj.success === true){ + } else { + try { + let obj = typeof data === "string" ? JSON.parse(data) : data; + if (obj.success === true) { magicJS.logInfo(`领取会员任务成功,任务Id:${mission_id},任务描述:${obj.mission.checkout_description}`); resolve(obj.mission.checkout_description); - } - else{ + } else { magicJS.logError(`领取会员任务失败,任务Id:${mission_id},响应异常:${data}`); reject(`领取会员任务失败,任务Id:${mission_id}`); } - } - catch(err){ + } catch (err) { magicJS.logError(`领取会员任务失败,任务Id:${mission_id},执行异常:${err},接口响应:${data}`); reject(`领取会员任务失败,任务Id:${mission_id}`); } } - }) - }) + }); + }); } -;(async()=>{ - if (magicJS.isRequest){ - if (getCookieRegex.test(magicJS.request.url) && magicJS.request.method == "GET"){ +(async () => { + if (magicJS.isRequest) { + if (getCookieRegex.test(magicJS.request.url) && magicJS.request.method == "GET") { GetCookie(); } - } - else{ + } else { let subTitle = ""; let content = ""; let cookie = magicJS.read(elemeCookieKey); let longitude = magicJS.read(elemeLongitudeKey); let latitude = magicJS.read(elemeLatitudeKey); - if (!!!cookie){ - magicJS.logWarning('没有读取到Cookie,请先从App中获取一次Cookie!'); - content = '❓没有读取到有效Cookie,请先从App中获取Cookie!!'; - } - else{ + if (!!!cookie) { + magicJS.logWarning("没有读取到Cookie,请先从App中获取一次Cookie!"); + content = "❓没有读取到有效Cookie,请先从App中获取Cookie!!"; + } else { // 领取会员任务 let [getMissionErr, missions] = await magicJS.attempt(magicJS.retry(GetSuperVipMissions, 3, 2000)(cookie, longitude, latitude), []); - if (getMissionErr){ + if (getMissionErr) { subTitle = getMissionErr; - } - else if (missions.length == 0){ - magicJS.log('领取任务失败,没有发现符合要求的任务。请查阅任务返回JSON,确认是否因为任务描述改变而无法领取。'); - subTitle = '❌没有符合要求的任务可以领取'; - } - else{ + } else if (missions.length == 0) { + magicJS.log("领取任务失败,没有发现符合要求的任务。请查阅任务返回JSON,确认是否因为任务描述改变而无法领取。"); + subTitle = "❌没有符合要求的任务可以领取"; + } else { magicJS.logDebug(`获取待领取的任务Id:${JSON.stringify(missions)}`); let acceptMissionList = []; - content = '会员任务领取结果:'; - for (let i=0;i{let t={};if(this.isSurge||this.isQuanX||this.isLoon){if(typeof e==="string"){if(this.isLoon)t={openUrl:e};else if(this.isQuanX)t={"open-url":e};else if(this.isSurge)t={url:e}}else if(typeof e==="object"){let s={Surge:{openUrl:"url","open-url":"url"},Loon:{url:"openUrl","open-url":"openUrl","media-url":"mediaUrl"},QuantumultX:{url:"open-url",openUrl:"open-url",mediaUrl:"media-url"}};let i=Object.keys(e);for(let o=0;o{})}if(this.isSurge||this.isLoon){$notification.post(e,t,s,i)}else if(this.isQuanX){$notify(e,t,s,i)}else if(this.isJSBox){let i={title:e,body:!!t?`${t}\n${s}`:s};$push.schedule(i)}}log(e,t="INFO"){if(!(this.logLevels[this._logLevel]{if(typeof i.body==="undefined")return"";return`${encodeURIComponent(e)}=${encodeURIComponent(i.body[e])}`}).join("&");if(i.url.indexOf("?")<0)i.url+="?";if(i.url.lastIndexOf("&")+1!=i.url.length&&i.url.lastIndexOf("?")+1!=i.url.length)i.url+="&";i.url+=e;delete i.body}if(this.isQuanX){if(i.hasOwnProperty("body")&&typeof i["body"]!=="string")i["body"]=JSON.stringify(i["body"]);i["method"]=t}else if(this.isNode){delete i.headers["Accept-Encoding"];if(typeof i.body==="object"){if(t==="GET"){i.qs=i.body;delete i.body}else if(t==="POST"){i["json"]=true;i.body=i.body}}}else if(this.isJSBox){i["header"]=i["headers"];delete i["headers"]}return i}get(e,t){let s=this.adapterHttpOptions(e,"GET");this.logDebug(`HTTP GET: ${JSON.stringify(s)}`);if(this.isSurge||this.isLoon){$httpClient.get(s,t)}else if(this.isQuanX){$task.fetch(s).then(e=>{e["status"]=e.statusCode;t(null,e,e.body)},e=>t(e.error,null,null))}else if(this.isNode){return this.node.request.get(s,t)}else if(this.isJSBox){s["handler"]=(e=>{let s=e.error?JSON.stringify(e.error):undefined;let i=typeof e.data==="object"?JSON.stringify(e.data):e.data;t(s,e.response,i)});$http.get(s)}}post(e,t){let s=this.adapterHttpOptions(e,"POST");this.logDebug(`HTTP POST: ${JSON.stringify(s)}`);if(this.isSurge||this.isLoon){$httpClient.post(s,t)}else if(this.isQuanX){$task.fetch(s).then(e=>{e["status"]=e.statusCode;t(null,e,e.body)},e=>{t(e.error,null,null)})}else if(this.isNode){return this.node.request.post(s,t)}else if(this.isJSBox){s["handler"]=(e=>{let s=e.error?JSON.stringify(e.error):undefined;let i=typeof e.data==="object"?JSON.stringify(e.data):e.data;t(s,e.response,i)});$http.post(s)}}done(e={}){if(typeof $done!=="undefined"){$done(e)}}isToday(e){if(e==null){return false}else{let t=new Date;if(typeof e=="string"){e=new Date(e)}if(t.getFullYear()==e.getFullYear()&&t.getMonth()==e.getMonth()&&t.getDay()==e.getDay()){return true}else{return false}}}isNumber(e){return parseFloat(e).toString()==="NaN"?false:true}attempt(e,t=null){return e.then(e=>{return[null,e]}).catch(e=>{this.logError(e);return[e,t]})}retry(e,t=5,s=0,i=null){return(...o)=>{return new Promise((r,n)=>{function a(...o){Promise.resolve().then(()=>e.apply(this,o)).then(e=>{if(typeof i==="function"){Promise.resolve().then(()=>i(e)).then(()=>{r(e)}).catch(e=>{this.logError(e);if(t>=1&&s>0){setTimeout(()=>a.apply(this,o),s)}else if(t>=1){a.apply(this,o)}else{n(e)}t--})}else{r(e)}}).catch(e=>{this.logError(e);if(t>=1&&s>0){setTimeout(()=>a.apply(this,o),s)}else if(t>=1){a.apply(this,o)}else{n(e)}t--})}a.apply(this,o)})}}formatTime(e,t="yyyy-MM-dd hh:mm:ss"){var s={"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()};if(/(y+)/.test(t))t=t.replace(RegExp.$1,(e.getFullYear()+"").substr(4-RegExp.$1.length));for(let e in s)if(new RegExp("("+e+")").test(t))t=t.replace(RegExp.$1,RegExp.$1.length==1?s[e]:("00"+s[e]).substr((""+s[e]).length));return t}now(){return this.formatTime(new Date,"yyyy-MM-dd hh:mm:ss")}today(){return this.formatTime(new Date,"yyyy-MM-dd")}sleep(e){return new Promise(t=>setTimeout(t,e))}}(e)}