const scriptName = '饿了么'; const getCookieRegex = /^https?:\/\/h5\.ele\.me\/restapi\/biz\.svip_scene\/svip\/engine\/queryTrafficSupply\?.*longitude=([^&]*).*latitude=([^&]*)/; const elemeCookieKey = 'eleme_app_cookie'; const elemeLongitudeKey = 'eleme_app_longitude'; const elemeLatitudeKey = 'eleme_app_latitude'; let magicJS = MagicJS(scriptName, "INFO"); magicJS.unifiedPushUrl = magicJS.read('eleme_app_unified_push_url') || magicJS.read('magicjs_unified_push_url'); function GetCookie(){ try{ let cookie = magicJS.request.headers.Cookie; let arr = magicJS.request.url.match(getCookieRegex); let longitude = arr[1]; let latitude = arr[2]; 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){ magicJS.write(elemeCookieKey, cookie); magicJS.logInfo(`旧的Cookie:${hisCookie}\n新的Cookie:${cookie}\nCookie不同,写入新的Cookie成功!`); magicJS.notify('Cookie写入成功!!'); } else{ magicJS.logInfo('Cookie没有变化,无需更新'); } } catch(err){ magicJS.notify('获取Cookie出现异常,请查阅日志。'); magicJS.logError(`获取Cookie出现执行异常,异常信息:${err}`); } } // 获取待领取的吃货豆列表 function GetPeaList(cookie, longitude, latitude){ return new Promise((resolve, reject)=>{ let options = { url: `https://h5.ele.me/restapi/biz.svip_core/v1/foodie/homepage?longitude=${longitude}&latitude=${latitude}`, headers: { "Accept": "application/json, text/plain, */*", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-cn", "Connection": "keep-alive", "Cookie": cookie, "Host": "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-74", "f-refer": "wv_h5", "x-shard": `loc=${longitude},${latitude}` } } magicJS.get(options, (err, resp, data)=>{ if (err){ magicJS.logError(`获取待领取的吃货豆失败,请求异常:${err}`); reject('获取待领取的吃货豆失败,请求异常,请查阅日志!'); } else{ try{ magicJS.logDebug(`获取待领取吃货豆列表响应结果:${data}`); let obj = typeof data === 'string'? JSON.parse(data) : data; if (obj.success === true){ let peaList = []; obj.foodiePeaBlock.peaList.forEach(element => { peaList.push({'id': element.id, 'count': element.count, 'description': element.description}); }); magicJS.logInfo(`获取待领取的吃货豆成功:${JSON.stringify(peaList)}`); resolve(peaList); } else{ magicJS.logError(`获取待领取的吃货豆失败,响应异常:${data}`); reject('获取待领取的吃货豆失败,响应异常,请查阅日志!'); } } catch(err){ magicJS.logError(`获取待领取的吃货豆失败,执行异常:${err},接口响应:${data}`); reject('获取待领取的吃货豆失败,执行异常,请查阅日志!'); } } }) }) } // 领取吃货豆 function DrawPea(cookie, peaId, longitude, latitude){ return new Promise((resolve, reject)=>{ let options = { url: `https://h5.ele.me/restapi/biz.svip_bonus/v1/users/supervip/pea/draw?peaId=${peaId}`, headers: { "Accept": "application/json, text/plain, */*", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-cn", "Connection": "keep-alive", "Content-Type": "application/json;charset=utf-8", "Cookie": cookie, "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-73", "f-refer": "wv_h5", "x-shard": `loc=${longitude},${latitude}` }, body: JSON.stringify({ "longitude": longitude, "latitude": latitude }) } magicJS.post(options, (err, resp, data)=>{ if (err){ magicJS.logError(`领取吃货豆失败,请求异常:${err}`); reject('领取吃货豆失败,请求异常,请查阅日志!'); } else{ try{ magicJS.logDebug(`领取吃货豆响应结果:${data}`); let obj = typeof data === 'string'? JSON.parse(data) : data; if (obj.success === true){ magicJS.logInfo(`领取吃货豆成功:${data}`); resolve(true); } else{ magicJS.logError(`领取吃货豆失败,响应异常:${data}`); reject('领取吃货豆失败,响应异常,请查阅日志!'); } } catch(err){ magicJS.logError(`领取吃货豆失败,执行异常:${err},接口响应:${data}`); reject('领取吃货豆失败,执行异常,请查阅日志!'); } } }) }) } ;(async()=>{ if (magicJS.isRequest){ if (getCookieRegex.test(magicJS.request.url) && magicJS.request.method == "GET"){ GetCookie(); } } 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{ // 获取待领取的吃货豆 let [getPeaListErr, peaList] = await magicJS.attempt(GetPeaList(cookie, longitude, latitude), []); content = '吃货豆领取结果:'; if (getPeaListErr){ content += '\n获取待领取的吃货豆异常,请查阅日志'; } else if (peaList.length == 0){ content += '\n没有发现待领取的吃货豆'; } else{ let peaCount = 0; let drawPeaContent = ''; for (let j=0;j{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)}