total spend on shopee
unknown
javascript
a year ago
5.7 kB
7
Indexable
async function getOrders(offset, limit) { const url = `https://shopee.vn/api/v4/order/get_all_order_and_checkout_list?limit=${limit}&offset=${offset}` const response = await fetch(url) const data = await response.json() const orderData = data?.data?.order_data || [] return orderData.details_list || [] } function formatVietNamCurrency(number) { return new Intl.NumberFormat().format(number) } async function getAllOrders() { const limit = 20 let offset = 0 let allOrders = [] const header = [ 'STT', 'Tên chung', 'Số lượng', 'Tổng tiền (VNĐ)', 'Trạng thái', 'Tên shop', 'Chi tiết', 'Ngày giao', ] const thead = `<thead><tr>${header.map((item) => `<th>${item}</th>`).join('')}</tr></thead>` allOrders.push(header.join('\t')) 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 let strListType = 'Không rõ' switch (listType) { case 3: strListType = 'Hoàn thành' break case 4: strListType = 'Đã hủy' break case 7: strListType = 'Vận chuyển' break case 8: strListType = 'Đang giao' break case 9: strListType = 'Chờ thanh toán' break case 12: strListType = 'Trả hàng' break } const productCount = infoCard.product_count let subTotal = infoCard.subtotal / 1e5 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: ${formatVietNamCurrency( item.item_price, )}`, ) .join(', '), ) .join('; ') const name = products[0].items[0].name if (listType !== 4 && listType !== 12) sum += subTotal else subTotal = 0 const subTotalNative = formatVietNamCurrency(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 } tbody = `<tbody>${tbody}</tbody>` allOrders.push(['Tổng cộng:', count, formatVietNamCurrency(sum)].join('\t')) const tfoot = ` <tfoot> <th></th> <th>Tổng cộng:</th> <th>${count}</th> <th>${formatVietNamCurrency(sum)}</th> <th></th> <th></th> <th></th> <th></th> </tfoot> ` const table = `<table id="myTable" class="display" style="width: 100%">${thead}${tbody}${tfoot}</table>` // Your HTML content 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> ` // Create a Blob with the HTML content const blob = new Blob([htmlContent], { type: 'text/html' }) // Create a temporary URL for the Blob const tempURL = URL.createObjectURL(blob) // Create a link element to load the HTML content when clicked const link = document.createElement('a') link.id = 'link' link.href = tempURL link.textContent = 'View Detail Shopee Report' // Add a click event handler to load the content when the link is clicked link.addEventListener('click', function (event) { event.preventDefault() const iframe = document.createElement('iframe') iframe.src = tempURL iframe.style.width = '100%' iframe.style.height = '900px' // Set the height as per your requirements document.body.appendChild(iframe) link.style.pointerEvents = 'none' link.hidden = true }) // Remove all content in shopee document.body.innerHTML = ` <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> ` // Append the link to the document document.body.appendChild(link) } function copy() { const textarea = document.getElementById('textarea') textarea.select() document.execCommand('copy') } getAllOrders()
Editor is loading...