Skip to content

Instantly share code, notes, and snippets.

@nhatkha1407
Created January 20, 2020 17:47
Show Gist options
  • Save nhatkha1407/327be8027ec063ee89912721ad720974 to your computer and use it in GitHub Desktop.
Save nhatkha1407/327be8027ec063ee89912721ad720974 to your computer and use it in GitHub Desktop.
Snippet code tính tổng chi tiêu Shopee & Tiki
var totalOrders = 0;
var totalSpent = 0;
var totalShippingSpent = 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)['orders'];
totalOrders += orders.length;
pulling = orders.length >= 10;
orders.forEach(order => {
let tpa = order["paid_amount"] / 100000;
totalSpent += tpa;
let tpsa = order["shipping_fee"] / 100000;
totalShippingSpent += tpsa;
});
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 chi tiêu: " + "%c" + moneyFormat(totalSpent) + "đ", "font-size: 30px;", "font-size: 30px; color:red");
console.log("%cTổng tiền ship: " + "%c" + moneyFormat(totalShippingSpent) + "đ", "font-size: 30px;", "font-size: 30px; color:red");
}
}
};
xhttp.open("GET", "https://shopee.vn/api/v1/orders/?order_type=3&offset=" + offset + "&limit=10", 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();
var totalOrders = 0;
var totalSpent = 0;
var pulling = true;
var page = 1;
function getStatistics() {
var orders = [];
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
orders = JSON.parse(this.responseText)['data'];
pulling = orders.length >= 10;
orders = orders.filter(order => order['status'] == 'hoan_thanh');
totalOrders += orders.length;
orders.forEach(order => {
let tpa = order["grand_total"];
totalSpent += tpa;
});
page += 1;
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 chi tiêu: " + "%c" + moneyFormat(totalSpent) + "đ", "font-size: 30px;", "font-size: 30px; color:red");
}
}
};
xhttp.open("GET", "https://tiki.vn/api/v2/me/orders?page=" + page + "&limit=10", 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