Code 1
Viqazo
javascript
a year ago
5.4 kB
40
No Index
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();
return data?.data?.order_data?.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',
8: 'Đang giao',
9: 'Chờ thanh toán',
12: 'Trả hàng'
};
function mapListType(listType) {
return listTypeMapping[listType] || 'Không rõ';
}
async function getAllOrders() {
const limit = 20;
let offset = 0;
let allOrders = [];
let sum = 0;
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;
const 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(group =>
group.items.map(item =>
`${item.name} -- SL: ${item.amount} -- Giá: ${new Intl.NumberFormat('vi-VN', { style: 'currency', currency: 'VND' }).format(item.item_price / 100000)}`
).join(', ')
).join('; ');
const name = products[0].items[0].name;
const subTotalNative = new Intl.NumberFormat('vi-VN', { style: 'currency', currency: 'VND' }).format(subTotal);
if (listType !== 4 && listType !== 12) {
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(`Đã thống kê được: ${offset} đơn hàng. Đang lấy thêm dữ liệu ...`);
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>
<tr>
<th></th>
<th>Tổng cộng:</th>
<th>${count}</th>
<th>${new Intl.NumberFormat('vi-VN', { style: 'currency', currency: 'VND' }).format(sum)}</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</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() {
$('#myTable').DataTable({
info: false,
fixedHeader: {
header: true,
footer: true
},
paging: false,
scrollCollapse: true,
scrollX: true,
scrollY: 700
});
});
</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 = 'Xem Báo Cáo Shopee Chi Tiết!!';
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 = `
<p><a href="https://viqazo.com/">viqazo.com</a></p>
<h3>Số tiền bạn ĐÃ ĐỐT vào Shopee là: ${new Intl.NumberFormat('vi-VN', { style: 'currency', currency: 'VND' }).format(sum)}.</h3>
<textarea id="textarea">${allOrders.join('\r\n')}</textarea><br>
`;
document.body.innerHTML = output;
document.body.appendChild(link);
}
getAllOrders();Editor is loading...
Leave a Comment