Skip to content

Instantly share code, notes, and snippets.

@rabbithunter0502
Forked from Vinh2uang/Shopee.js
Last active October 30, 2022 17:33
Show Gist options
  • Save rabbithunter0502/e224dff482f7c451c66e02fabcdbf848 to your computer and use it in GitHub Desktop.
Save rabbithunter0502/e224dff482f7c451c66e02fabcdbf848 to your computer and use it in GitHub Desktop.
Cách tính tổng số tiền đã mua hàng trên Shopee
var totalOrders = 0;
var totalSpent = 0;
var totalShippingSpent = 0;
var totalItems = 0;
var pulling = true;
var offset = 0;
function getStatistics() {
var orders = [];
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
orders = JSON.parse(this.responseText)['data']['order_data']['details_list'];
totalOrders += orders.length;
pulling = orders.length >= 10;
orders.forEach(order => {
let tpa = order["info_card"]["subtotal"] / 100000;
totalSpent += tpa;
let tpti = order["info_card"]["product_count"];
totalItems += tpti;
});
offset += 10;
console.log('Đã lấy được: ' + totalOrders + ' đơn hàng');
if(pulling) {
console.log('Đang kéo thêm...');
getStatistics();
}
else {
console.log("%cTổng đơn hàng đã giao: "+"%c"+moneyFormat(totalOrders), "font-size: 30px;","font-size: 30px; color:red");
console.log("%cTổng sản phẩm đã đặt: " + "%c" + moneyFormat(totalItems), "font-size: 30px;","font-size: 30px; color:red");
console.log("%cTổng chi tiêu: "+"%c"+moneyFormat(totalSpent)+"đ", "font-size: 30px;","font-size: 30px; color:red");
}
}
};
xhttp.open("GET", `https://shopee.vn/api/v4/order/get_all_order_and_checkout_list?limit=10&offset=${offset}`, true);
xhttp.send();
}
function moneyFormat(number, fixed=0) {
if(isNaN(number)) return 0;
number = number.toFixed(fixed);
let delimeter = ',';
number += '';
let rgx = /(\d+)(\d{3})/;
while (rgx.test(number)) {
number = number.replace(rgx, '$1' + delimeter + '$2');
}
return number;
}
getStatistics()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment