UNPKG

create-chuntianxiaozhu

Version:

春天小猪模板工具

137 lines (134 loc) 4.07 kB
import { load } from "cheerio"; import { sendRequestToBackground } from "../../utils/api"; import { generateWangLink } from "../../utils/utils"; import { notifications } from "../../utils/chrome"; const ShopLevelMap: { [key: number]: string } = { 0: "0分", 1: "4分-10分", 2: "11分-40分", 3: "41分-90分", 4: "91分-150分", 5: "151分-250分", 6: "251分-500分", 7: "501分-1000分", 8: "1001分-2000分", 9: "2001分-5000分", 10: "5001分-10000分", 11: "10001分-20000分", 12: "20001分-50000分", 13: "50001分-100000分", 14: "100001分-200000分", 15: "200001分-500000分", 16: "500001分-1000000分", 17: "1000001分-2000000分", 18: "2000001分-5000000分", 19: "5000001分-10000000分", 20: "10000001分以上", }; export function parseShops(done: () => void) { const elements = document.getElementsByClassName("list-item"); let promiseResolve: any; const parseDone = new Promise<any[]>((resolve) => { promiseResolve = resolve; }); const result = Array.from(elements).map((item, index) => { if (!item) { return; } const shopName = item .getElementsByClassName("shop-name J_shop_name")?.[0] ?.textContent?.trim(); const rankEle = item.getElementsByClassName("rank")[0]; const shopLevel = parseInt( rankEle?.getAttribute("class")?.replace("rank seller-rank-", "") || "0" ); const levelName = ShopLevelMap[shopLevel]; const shopUrl = item .getElementsByClassName("shop-name J_shop_name")[0] .getAttribute("href") || ""; const shopInfo: any = { index, shopName, shopUrl, shopLevel, levelName, }; sendRequestToBackground({ url: `https:${shopUrl}`, method: "GET", success: (res) => { const $ = load(res.data); $(".rateinfo>em").each(function (index) { if (index == 0) { // 描述评分 shopInfo.descScore = $(this).text(); } else if (index == 1) { shopInfo.serviceScore = $(this).text(); } else if (index == 2) { shopInfo.deliveryScore = $(this).text(); } }); shopInfo.wangLink = generateWangLink($); const bail = $(".tb-seller-bail").first().text(); shopInfo.bail = bail; const licenceHref = $(".licence").first().attr("href"); if (licenceHref) { sendRequestToBackground({ url: licenceHref, method: "GET", success: (licenceRes) => { const $2 = load(licenceRes.data); $2(".fn-left").each(function (index) { switch (index) { case 0: shopInfo.companyName = $(this).text(); break; case 1: shopInfo.registerCode = $(this).text(); break; case 2: shopInfo.address = $(this).text(); break; case 3: shopInfo.legalName = $(this).text(); break; case 4: shopInfo.type = $(this).text(); break; case 5: shopInfo.startTime = $(this).text(); break; case 6: shopInfo.endTime = $(this).text(); break; case 7: done(); promiseResolve(result); break; } }); }, fail: (msg) => { done(); notifications('', msg); promiseResolve(msg); }, }); } else { done(); promiseResolve(result); } }, fail: (msg) => { done(); notifications('', msg); promiseResolve(msg); }, }); // 千牛联系方式 // 开店时间 return shopInfo; }); return parseDone; }