|
| 1 | +import { showLoading } from "./helpers/utils.js"; |
| 2 | + |
| 3 | +export default { |
| 4 | + icon: "https://tiki.vn/favicon.ico", |
| 5 | + name: { |
| 6 | + en: "Tiki - Total spend money?", |
| 7 | + vi: "Tiki - Đã mua bao nhiêu tiền?", |
| 8 | + }, |
| 9 | + description: { |
| 10 | + en: "See how much money you have spend on Tiki", |
| 11 | + vi: "Xem bạn đã mua hết bao nhiêu tiền trên Tiki", |
| 12 | + }, |
| 13 | + |
| 14 | + onClick: async () => { |
| 15 | + // https://tiki.vn/api/v2/orders?page=0&limit=10 |
| 16 | + // https://www.facebook.com/groups/j2team.community/permalink/1169967376668714/ |
| 17 | + |
| 18 | + var totalOrders = 0; |
| 19 | + var totalSpent = 0; |
| 20 | + var pulling = true; |
| 21 | + var page = 1; |
| 22 | + |
| 23 | + function getStatistics() { |
| 24 | + var orders = []; |
| 25 | + var xhttp = new XMLHttpRequest(); |
| 26 | + xhttp.onreadystatechange = function () { |
| 27 | + if (this.readyState == 4 && this.status == 200) { |
| 28 | + orders = JSON.parse(this.responseText)["data"]; |
| 29 | + pulling = orders.length >= 10; |
| 30 | + orders = orders.filter((order) => order["status"] == "hoan_thanh"); |
| 31 | + totalOrders += orders.length; |
| 32 | + orders.forEach((order) => { |
| 33 | + let tpa = order["grand_total"]; |
| 34 | + totalSpent += tpa; |
| 35 | + }); |
| 36 | + page += 1; |
| 37 | + console.log("Đã lấy được: " + totalOrders + " đơn hàng"); |
| 38 | + if (pulling) { |
| 39 | + console.log("Đang kéo thêm..."); |
| 40 | + getStatistics(); |
| 41 | + } else { |
| 42 | + console.log( |
| 43 | + "%cTổng đơn hàng đã giao: " + "%c" + moneyFormat(totalOrders), |
| 44 | + "font-size: 30px;", |
| 45 | + "font-size: 30px; color:red" |
| 46 | + ); |
| 47 | + console.log( |
| 48 | + "%cTổng chi tiêu: " + "%c" + moneyFormat(totalSpent) + "đ", |
| 49 | + "font-size: 30px;", |
| 50 | + "font-size: 30px; color:red" |
| 51 | + ); |
| 52 | + } |
| 53 | + } |
| 54 | + }; |
| 55 | + xhttp.open( |
| 56 | + "GET", |
| 57 | + "https://tiki.vn/api/v2/me/orders?page=" + page + "&limit=10", |
| 58 | + true |
| 59 | + ); |
| 60 | + xhttp.send(); |
| 61 | + } |
| 62 | + |
| 63 | + function moneyFormat(number, fixed = 0) { |
| 64 | + if (isNaN(number)) return 0; |
| 65 | + number = number.toFixed(fixed); |
| 66 | + let delimeter = ","; |
| 67 | + number += ""; |
| 68 | + let rgx = /(\d+)(\d{3})/; |
| 69 | + while (rgx.test(number)) { |
| 70 | + number = number.replace(rgx, "$1" + delimeter + "$2"); |
| 71 | + } |
| 72 | + return number; |
| 73 | + } |
| 74 | + |
| 75 | + getStatistics(); |
| 76 | + }, |
| 77 | +}; |
0 commit comments