Get total spend shopee
unknown
javascript
2 years ago
5.1 kB
10
Indexable
async function getOrders(offset, limit) {
try {
const url = `https://shopee.vn/api/v4/order/get_all_order_and_checkout_list?limit=${limit}&offset=${offset}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}
const data = await response.json();
const orderData = data?.data?.order_data || [];
return orderData.details_list || [];
} catch (error) {
console.error('Error in getOrders:', error);
return [];
}
}
const listTypeMapping = {
3: 'Hoàn thành',
4: 'Đã hủy',
7: 'Vận chuyển',
// Add other mappings here
};
function mapListType(listType) {
return listTypeMapping[listType] || 'Không rõ';
}
async function getAllOrders() {
const limit = 20;
let offset = 0;
let allOrders = [];
let sum = 0; // Initialize the sum variable
let count = 0;
let order = 0;
let tbody = '';
while (true) {
const data = await getOrders(offset, limit);
if (data.length === 0) break;
for (const item of data) {
order++;
const cTime = item?.shipping?.tracking_info?.ctime;
const deliveredTime = cTime ? new Date(cTime * 1000).toLocaleDateString('vi-VN') : '';
const infoCard = item.info_card;
const listType = item.list_type;
const strListType = mapListType(listType);
const productCount = infoCard.product_count;
let subTotal = listType !== 4 && listType !== 12 ? infoCard.subtotal / 1e5 : 0;
count += productCount;
const orderCard = infoCard.order_list_cards[0];
const shopName = `${orderCard.shop_info.username} - ${orderCard.shop_info.shop_name}`;
const products = orderCard.product_info.item_groups;
const productSummary = products
.map((product) =>
product.items
.map(
(item) =>
`${item.name} -- amount: ${item.amount} -- price: ${new Intl.NumberFormat().format(item.item_price)}`,
)
.join(', ')
)
.join('; ');
const name = products[0].items[0].name;
subTotalNative = new Intl.NumberFormat().format(subTotal); // Removed `let` declaration
// Add the subtotal to the sum
sum += subTotal;
const row = [
order,
name,
productCount,
subTotalNative,
strListType,
shopName,
productSummary,
deliveredTime,
];
tbody += `<tr>${row.map((item) => `<td>${item}</td>`).join('')}</tr>`;
allOrders.push(row.join('\t'));
}
console.log(`Collected: ${offset}`);
offset += limit;
}
const thead = `
<thead>
<tr>
<th>STT</th>
<th>Tên chung</th>
<th>Số lượng</th>
<th>Tổng tiền (VNĐ)</th>
<th>Trạng thái</th>
<th>Tên shop</th>
<th>Chi tiết</th>
<th>Ngày giao</th>
</tr>
</thead>
`;
tbody = `<tbody>${tbody}</tbody>`;
const totalRow = `
<tfoot>
<th></th>
<th>Tổng cộng:</th>
<th>${count}</th>
<th>${new Intl.NumberFormat().format(sum)}</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tfoot>
`;
const table = `
<table id="myTable" class="display" style="width: 100%">
${thead}
${tbody}
${totalRow}
</table>
`;
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css" />
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
</head>
<body>
${table}
<script>
$(document).ready( function () {
var table = $('#myTable').DataTable({
searching: false,
paging: false,
info: false,
bScrollInfinite: true,
bScrollCollapse: true,
sScrollY: "700px"
});
});
</script>
</body>
</html>
`;
const blob = new Blob([htmlContent], { type: 'text/html' });
const tempURL = URL.createObjectURL(blob);
const link = document.createElement('a');
link.id = 'link';
link.href = tempURL;
link.textContent = 'View Detail Shopee Report';
link.addEventListener('click', async function (event) {
event.preventDefault();
const iframe = document.createElement('iframe');
iframe.src = tempURL;
iframe.style.width = '100%';
iframe.style.height = '900px';
document.body.appendChild(iframe);
link.style.pointerEvents = 'none';
link.hidden = true;
});
const output = `
<h3>Click copy and paste to a blank excel file! </h3>
<textarea id="textarea">${allOrders.join('\r\n')}</textarea><br>
<button onclick="copy()">Copy</button>
`;
document.body.innerHTML = output;
document.body.appendChild(link);
}
function copy() {
const textarea = document.getElementById('textarea');
textarea.select();
document.execCommand('copy');
}
getAllOrders();
Editor is loading...