jiangzm 2020-07-29 22:32:49

在线统计owhat集资金额

/**
 * 在线统计owhat集资金额
 * 步骤如下:
 * 1、先在PC浏览器打开ow主页或物品页 (可登录自己的ow账号)
 * 2、打开浏览器开发者工具,打开控制台(Console)页面
 * 3、执行以下代码即可实时统计(统计频率可自己调节,建议不小于30s)
 * 4、执行timer.stop()即可停止统计(刷新或关闭网页也行)
 * 
 * 更新:1、不在ow页面询问跳转ow主页
 *      2、物品页面支持获取当前物品Id
 */
const timer = (function(job) {
  let timeId = 0;
  const { host, pathname, search } = location;
  const query = new URLSearchParams(search);
  const goodsId = pathname === "/shop/shopdetail.html" && query.get("id") || 111400;
  return {
    start(intervals) {
      if (host !== "m.owhat.cn" && host !== "www.owhat.cn") {
        console.warn("请打开相关ow商品页面再执行统计,ow主页: https://www.owhat.cn");
        if(confirm("是否跳转ow主页?")) {
          location.href = "https://www.owhat.cn";
        }
        return;
      }
      console.log(`已开始统计,物品Id:${goodsId}.`);
      job(goodsId);
      intervals = intervals || 30;
      timeId && clearInterval(timeId);
      timeId = setInterval(() => job(goodsId), intervals*1000);
      return timeId;
    },
    stop(){
      timeId && clearInterval(timeId);
      console.log("已停止统计.");
    }
  }
})(async function (id) {
  const data = $.apiInit($.apimap, "goodselect", { fk_goods_id: id });
  let res = await fetch(`${location.origin}/api?requesttimestap=${new Date().getTime()}`, {
    method: "POST",
    body: $.param(data),
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
    credentials: "same-origin",
  });

  if (res.status === 200) {
    let result = await res.json();
    if (
      result && result.data && 
      result.data.prices && 
      result.data.prices.length
    ) {
      let table = [];
      result.data.prices.forEach(item => {
        table.push({
          "名称": item.name,
          "单价": item.price,
          "数量": item.salestock,
          "金额": parseInt(100 * item.price * item.salestock)/100
        });
      });
      table.length && table.push({
        "名称": "总额",
        "单价": "-",
        "数量": "-",
        "金额": parseInt(100*table.reduce((r,c)=>r+c["金额"],0))/100
      });
      console.log(
        `%c截止[${new Date().toLocaleString()}]统计金额为: ¥${table[table.length-1]["金额"]}`,
        'font-size:20px;color:red;'
      );
      console.table(table);
      return;
    } else {
      console.warn("request failure.", result);
      return;
    }
  }

  console.error("request error.", res.statusText);
});

timer.start(30);//每隔30s统计一次
//timer.stop(); //停止统计

评论

首页 - Wiki
Copyright © 2011-2024 iteam. Current version is 2.124.0. UTC+08:00, 2024-04-26 04:31
浙ICP备14020137号-1 $访客地图$