#最新
Javascript技术:在线统计owhat集资金额

2015-12-28 841

javascript技术:在线统计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 !== "www.dhaomu.com" && host !== "www.dhaomu.com") {
        console.warn("请打开相关ow商品页面再执行统计,ow主页: https://www.dhaomu.com");
        if(confirm("是否跳转ow主页?")) {
          location.href = "https://www.dhaomu.com";
        }
        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(); //停止统计

声明: 本站仅提供资源学习下载,资源费用仅为赞助站长的整理费,不代表资源自身价值也不包含任何服务。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
本站提供的资源来自网络,版权争议与本站无关,所有内容及软件的文章仅限用于学习和研究目的。
如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。侵删请致信E-mail:duhaomu@163.com

浩沐资源网 开发技巧 Javascript技术:在线统计owhat集资金额 https://www.dhaomu.com/85661.html

相关文章

发表评论
暂无评论