@@ -12,78 +12,82 @@ export default {
1212 } ,
1313
1414 onClickExtension : async ( ) => {
15- // Order List : https://shopee.vn/api/v4/order/get_all_order_and_checkout_list?limit=5&offset=0
15+ // Order&Checkout List: https://shopee.vn/api/v4/order/get_all_order_and_checkout_list?limit=5&offset=0
1616 // Order Detail: https://shopee.vn/api/v4/order/get_order_detail?order_id=124388457229298
17+ // Order List: https://shopee.vn/api/v4/order/get_order_list?limit=5&list_type=4&offset=0
18+ // get refund (POST): https://shopee.vn/api/v4/return/return_data/get_return_refund_list
1719 // Image: https://cf.shopee.vn/file/ecd20c9d39e0c865d53e3f47e6e2e3a7
18- // POST: https://www.facebook.com/groups/j2team.community/permalink/1169967376668714/
19-
20- let Statistics = {
21- bougth : {
22- totalOrders : 0 ,
23- totalItems : 0 ,
24- totalSpent : 0 ,
25- totalDiscount : 0 ,
26- totalShip : 0 ,
27- } ,
28- canceled : {
29- totalOrders : 0 ,
30- totalItems : 0 ,
31- totalSpent : 0 ,
32- totalDiscount : 0 ,
33- totalShip : 0 ,
34- } ,
20+ // FB POST: https://www.facebook.com/groups/j2team.community/permalink/1169967376668714/
21+
22+ let OrderType = {
23+ completed : 3 ,
24+ canceled : 4 ,
25+ ship : 7 ,
26+ shipping : 8 ,
27+ // waitPay: 9,
28+ // refunded: 12,
3529 } ;
36- let Type = {
37- bougth : "bougth" ,
38- canceled : "canceled" ,
30+
31+ let OrderTypeName = {
32+ [ OrderType . completed ] : "Hoàn thành" ,
33+ [ OrderType . canceled ] : "Đã hủy" ,
34+ [ OrderType . ship ] : "Vận chuyển" ,
35+ [ OrderType . shipping ] : "Đang giao" ,
36+ // [OrderType.waitPay]: "Chờ thanh toán",
37+ // [OrderType.refunded]: "Trả hàng/Hoàn tiền",
3938 } ;
4039
41- let pulling = true ;
42- let offset = 0 ;
43- let limit = 20 ;
40+ async function getStatistics ( orderType ) {
41+ let pulling = true ;
42+ let offset = 0 ;
43+ let limit = 10 ;
44+
45+ let totalDiscount = 0 ;
46+ let totalShip = 0 ;
47+ let totalSpent = 0 ;
48+ let totalItems = 0 ;
49+ let totalOrders = 0 ;
4450
45- async function getStatistics ( ) {
4651 while ( pulling ) {
4752 setLoadingText ( "Đang tải đơn hàng thứ " + ( offset + 1 ) + "..." ) ;
4853 let res = await fetch (
49- "https://shopee.vn/api/v4/order/get_all_order_and_checkout_list?limit=" +
50- limit +
51- "&offset=" +
52- offset
54+ `https://shopee.vn/api/v4/order/get_order_list?limit=${ limit } &list_type=${ orderType } &offset=${ offset } `
5355 ) ;
5456 let json = await res . json ( ) ;
5557 if ( json ?. error ) throw Error ( "Error: " + json . error ) ;
5658
57- let orders = json . data . order_data . details_list ;
59+ let orders = json . data . details_list ;
5860 for ( let i = 0 ; i < orders . length ; i ++ ) {
5961 setLoadingText ( "Đang tải đơn hàng thứ " + ( offset + i + 1 ) + "..." ) ;
6062
6163 let order = orders [ i ] ;
62- let wasCanceled = order . list_type == 4 ;
63- let type = wasCanceled ? Type . canceled : Type . bougth ;
6464
6565 order . info_card . order_list_cards . forEach ( ( card ) => {
6666 card . items . forEach ( ( item ) => {
67- Statistics [ type ] . totalDiscount +=
67+ totalDiscount +=
6868 ( item . price_before_discount - item . item_price ) / 100000 ;
69-
70- Statistics [ type ] . totalSpent += item . item_price / 100000 ;
71- Statistics [ type ] . totalItems += item . amount ;
69+ totalSpent += item . item_price / 100000 ;
70+ totalItems += item . amount ;
7271 } ) ;
7372 } ) ;
7473
75- if ( ! wasCanceled ) {
76- let orderId = order . info_card . order_id ;
77- let tpsa = await getShippingSpent ( orderId ) ;
78- Statistics [ type ] . totalShip += tpsa / 100000 ;
79- }
80-
81- Statistics [ type ] . totalOrders ++ ;
74+ let orderId = order . info_card . order_id ;
75+ let tpsa = await getShippingSpent ( orderId ) ;
76+ totalShip += tpsa / 100000 ;
77+ totalOrders ++ ;
8278 }
8379
8480 pulling = orders . length >= limit ;
8581 offset += limit ;
8682 }
83+
84+ return {
85+ totalDiscount,
86+ totalShip,
87+ totalSpent,
88+ totalItems,
89+ totalOrders,
90+ } ;
8791 }
8892
8993 async function getShippingSpent ( orderId ) {
@@ -117,26 +121,35 @@ export default {
117121
118122 let { closeLoading, setLoadingText } = showLoading ( "Đang chuẩn bị..." ) ;
119123 try {
120- await getStatistics ( ) ;
124+ let options = Object . entries ( OrderTypeName ) ;
125+ let optionsTxt = options
126+ . map ( ( [ key , value ] , index ) => ` ${ index } : ${ value } ` )
127+ . join ( "\n" ) ;
128+
129+ let choice = prompt (
130+ "Chọn loại đơn hàng muốn thống kê:\n" + optionsTxt ,
131+ 0
132+ ) ;
133+
134+ if ( choice == null ) return ;
135+ let orderType = options [ Number ( choice ) ] [ 0 ] ;
136+ let orderTypeName = OrderTypeName [ orderType ] ;
137+
138+ let { totalDiscount, totalShip, totalSpent, totalItems, totalOrders } =
139+ await getStatistics ( orderType ) ;
140+
121141 let stats = {
122- "ĐÃ MUA:" : "------" ,
123- "+ Tổng đơn hàng đã giao:" : Statistics . bougth . totalOrders ,
124- "+ Tổng sản phẩm đã đặt" : Statistics . bougth . totalItems ,
125- "+ Tổng chi tiêu" : moneyFormat ( Statistics . bougth . totalSpent ) ,
126- "+ Tổng giảm giá" : moneyFormat ( Statistics . bougth . totalDiscount ) ,
127- "+ Tổng tiền ship" : moneyFormat ( Statistics . bougth . totalShip ) ,
128- "ĐÃ HỦY:" : "------" ,
129- "+ Tổng đơn hàng đã hủy:" : Statistics . bougth . totalOrders ,
130- "+ Tổng sản phẩm đã hủy" : Statistics . bougth . totalItems ,
131- "+ Tổng chi tiêu đã hủy" : moneyFormat ( Statistics . bougth . totalSpent ) ,
132- "+ Tổng giảm giá đã hủy" : moneyFormat ( Statistics . bougth . totalDiscount ) ,
133- "+ Tổng tiền ship đã hủy" : moneyFormat ( Statistics . bougth . totalShip ) ,
142+ "THỐNG KÊ Shopee: " : orderTypeName ,
143+ "+ Tổng đơn hàng: " : totalOrders ,
144+ "+ Tổng sản phẩm: " : totalItems ,
145+ "+ Tổng chi tiêu: " : moneyFormat ( totalSpent ) ,
146+ "+ Tổng giảm giá: " : moneyFormat ( totalDiscount ) ,
147+ "+ Tổng tiền ship: " : moneyFormat ( totalShip ) ,
134148 } ;
135149 alert (
136- `THỐNG KÊ:\n` +
137- Object . entries ( stats )
138- . map ( ( [ key , value ] ) => `${ key } ${ value } ` )
139- . join ( "\n" )
150+ Object . entries ( stats )
151+ . map ( ( [ key , value ] ) => `${ key } ${ value } ` )
152+ . join ( "\n" )
140153 ) ;
141154 console . table ( stats ) ;
142155 } catch ( e ) {
0 commit comments