Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
vv314 committed Jan 14, 2024
1 parent 5754186 commit 7546c08
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/coupons/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const gundamActConfs = [

const wxfwhActConfs = [
// { gid: '1C0wLz', name: '天天神券服务号专属福利' },
{ gid: '1HgnjG', name: '神奇福利社' },
{ gid: '1I9uL6', name: '社群专属福利' }
{ gid: '1I9uL6', name: '社群专属福利' },
{ gid: '1HgnjG', name: '神奇福利社' }
]

export { ECODE, gundamActConfs, mainActConf, wxfwhActConfs, lotteryActConfs }
7 changes: 5 additions & 2 deletions src/coupons/gundam.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ function resolveRedMod(text, renderList) {

async function getPayload(
cookie,
{ gundamId, gdId, appJs, renderList },
{ gundamId, gdId, pageId, appJs, renderList },
guard
) {
const jsText = await fetch(appJs).then((res) => res.text())
const data =
resolveRedMod(jsText, renderList) ??
resolveRedMod(jsText, await getRenderList(cookie, gdId, guard))
resolveRedMod(
jsText,
await getRenderList(cookie, { gundamId, gdId, pageId }, guard)
)

if (!data) {
throw new Error(`[${gundamId}] Gundam Payload 生成失败`)
Expand Down
6 changes: 3 additions & 3 deletions src/coupons/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async function runTask(cookie, guard) {
* @param {Number} maxRetry 最大重试次数
* @return {Promise(<Object>)} 结果
*/
async function getCoupons(token, { maxRetry = 0, httpProxy }) {
async function getCoupons(token, { maxRetry = 0, proxy }) {
if (!token) {
return {
code: ECODE.RUNTIME,
Expand All @@ -77,8 +77,8 @@ async function getCoupons(token, { maxRetry = 0, httpProxy }) {
}

// 优先设置代理
if (httpProxy) {
fetch.setProxyAgent(httpProxy)
if (proxy) {
fetch.setProxyAgent(proxy)
}

const cookieJar = createMTCookie(token)
Expand Down
5 changes: 3 additions & 2 deletions src/coupons/wxfwh.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fetch from '../fetch.js'
import { dateFormat, groupBy } from '../util/index.js'
import { getTemplateData, matchMoudleData } from '../template.js'
import { getTemplateData, getRenderList, matchMoudleData } from '../template.js'
import { ECODE } from './const.js'

/* 服务号专属活动 */
Expand Down Expand Up @@ -69,9 +69,10 @@ async function getPayload(
guard
) {
const jsText = await fetch(appJs).then((res) => res.text())
const list = await getRenderList(cookie, { gundamId, gdId, pageId }, guard)
let data = null

console.log('+++++++++ getPayload', gundamId, renderList)
console.log('+++++++++ getPayload', gundamId, list)

try {
for (const instanceId of renderList) {
Expand Down
2 changes: 2 additions & 0 deletions src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ async function fetch(url, opts = {}) {

if (fetch._proxyAgent) {
opts.agent = fetch._proxyAgent
} else if (opts.proxy) {
opts.agent = new HttpsProxyAgent(opts.proxy)
}

opts.headers = Object.assign({}, defHeader, opts.headers)
Expand Down
2 changes: 1 addition & 1 deletion src/shadow/const.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const yodaReady = 'h5'
export const csecPlatform = 4
export const guardVersion = '2.3.1'
export const guardVersion = '2.4.0'
21 changes: 10 additions & 11 deletions src/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ function extractAppJsUrl(text) {

async function getTemplateData(cookie, gundamId) {
const text = await fetch(
`https://market.waimai.meituan.com/api/template/get?env=current&el_biz=waimai&el_page=gundam.loader&gundam_id=${gundamId}`,
{ cookie }
`https://market.waimai.meituan.com/api/template/get?env=current&el_biz=waimai&el_page=gundam.loader&gundam_id=${gundamId}`
).then((rep) => rep.text())
const matchGlobal = text.match(/globalData: ({.+})/)
const appJs = extractAppJsUrl(text)

try {
const globalData = JSON.parse(matchGlobal[1])

console.log('globalData', globalData.renderInfo)

return {
gundamId,
gdId: globalData.gdId,
Expand All @@ -34,21 +31,21 @@ async function getTemplateData(cookie, gundamId) {
}

function getRenderListFromGlobalData(globalData) {
const renderInfo = globalData.renderInfo?.componentRenderInfos
const renderInfo = globalData.renderInfo

if (!renderInfo) return []
if (renderInfo.status != 0 || !renderInfo?.componentRenderInfos) return []

return formatRendeInfo(renderInfo)
return filterRenderableKeys(renderInfo.componentRenderInfos)
}

function formatRendeInfo(renderInfo) {
function filterRenderableKeys(renderInfo) {
return Object.entries(renderInfo)
.filter(([_, v]) => v.render)
.map(([k]) => k)
}

// 通过接口获取真实的渲染列表
async function getRenderList(cookie, gdNumId, guard) {
async function getRenderList(cookie, { gundamId, gdId, pageId }, guard) {
let data

try {
Expand All @@ -58,7 +55,9 @@ async function getRenderList(cookie, gdNumId, guard) {
params: {
el_biz: 'waimai',
el_page: 'gundam.loader',
gdId: gdNumId,
gundam_id: gundamId,
gdId: gdId,
pageId: pageId,
tenant: 'gundam'
},
cookie,
Expand All @@ -71,7 +70,7 @@ async function getRenderList(cookie, gdNumId, guard) {
throw new Error('renderinfo 接口调用失败:' + e.message)
}

return formatRendeInfo(data)
return filterRenderableKeys(data)
}

function matchMoudleData(text, start, end) {
Expand Down
2 changes: 1 addition & 1 deletion test/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test('Test Main Payload', async () => {
})

test('Test Wxfwh Payload', async () => {
const tmplData = await getTemplateData(null, wxfwhActConfs[1].gid)
const tmplData = await getTemplateData(null, wxfwhActConfs[0].gid)
const payload = await wxfwhAct.getPayload(cookie, tmplData, guard)

return expect(payload).toMatchObject({
Expand Down
24 changes: 14 additions & 10 deletions test/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ const cookie = createMTCookie(tokens[0].token)

beforeAll(() => guard.init(mainGrab.getActUrl(wxfwhActConfs[0].gid)))

test('Test getTemplateData', async () => {
const res = await getTemplateData(null, wxfwhActConfs[0].gid)
// test('Test getTemplateData', async () => {
// const res = await getTemplateData(null, wxfwhActConfs[0].gid)

return expect(res).toMatchObject({
pageId: expect.any(Number),
gdId: expect.any(Number),
actName: expect.any(String),
appJs: expect.stringMatching(/app[^"]*\.js/)
})
})
// return expect(res).toMatchObject({
// pageId: expect.any(Number),
// gdId: expect.any(Number),
// actName: expect.any(String),
// appJs: expect.stringMatching(/app[^"]*\.js/)
// })
// })

test('Test getRenderList', async () => {
const renderList = await getRenderList(cookie, 466632, guard)
const renderList = await getRenderList(
cookie,
{ gundamId: '1I9uL6', pageId: 544003, gdId: 466632 },
guard
)

console.log(renderList)

Expand Down

0 comments on commit 7546c08

Please sign in to comment.