ts-glitter 21.5.3 → 21.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lowcode/Entry.js +1 -1
- package/lowcode/Entry.ts +1 -1
- package/lowcode/cms-plugin/account-info.js +79 -67
- package/lowcode/cms-plugin/account-info.ts +327 -311
- package/lowcode/cms-plugin/information/information-module.ts +3 -1
- package/lowcode/cms-plugin/list-header-option.js +1 -0
- package/lowcode/cms-plugin/list-header-option.ts +1 -0
- package/lowcode/cms-plugin/shopping-information.js +468 -0
- package/lowcode/cms-plugin/shopping-information.ts +557 -0
- package/lowcode/cms-plugin/shopping-market-shopee.js +283 -231
- package/lowcode/cms-plugin/shopping-market-shopee.ts +401 -361
- package/lowcode/cms-plugin/shopping-order-manager.js +16 -4
- package/lowcode/cms-plugin/shopping-order-manager.ts +18 -3
- package/lowcode/glitter-base/global/language.js +4 -1
- package/lowcode/glitter-base/global/language.ts +4 -2
- package/lowcode/glitter-base/route/shopee.js +48 -11
- package/lowcode/glitter-base/route/shopee.ts +119 -80
- package/lowcode/glitterBundle/plugins/html-render.js +121 -90
- package/lowcode/glitterBundle/plugins/html-render.ts +367 -318
- package/lowcode/modules/image-library.js +2 -3
- package/lowcode/modules/image-library.ts +21 -7
- package/lowcode/public-components/checkout/index.js +90 -41
- package/lowcode/public-components/checkout/index.ts +101 -49
- package/lowcode/public-components/footer/footer-initial.js +11 -2
- package/lowcode/public-components/footer/footer-initial.ts +29 -18
- package/lowcode/public-components/headers/header-class.js +47 -35
- package/lowcode/public-components/headers/header-class.ts +54 -38
- package/lowcode/public-components/layout-plugin/social-links-01.js +122 -3
- package/lowcode/public-components/layout-plugin/social-links-01.ts +135 -10
- package/lowcode/public-components/product/pd-card-01.js +23 -14
- package/lowcode/public-components/product/pd-card-01.ts +25 -14
- package/lowcode/public-components/product/pd-card-02.js +23 -16
- package/lowcode/public-components/product/pd-card-02.ts +25 -16
- package/lowcode/public-components/product/pd-card-03.js +25 -16
- package/lowcode/public-components/product/pd-card-03.ts +27 -16
- package/lowcode/public-components/terms-related/index.js +13 -2
- package/lowcode/public-components/terms-related/index.ts +15 -2
- package/lowcode/public-components/user-manager/um-class.js +490 -501
- package/lowcode/public-components/user-manager/um-class.ts +872 -882
- package/lowcode/public-components/user-manager/um-info.js +41 -40
- package/lowcode/public-components/user-manager/um-info.ts +54 -56
- package/lowcode/public-components/user-manager/um-login.js +10 -13
- package/lowcode/public-components/user-manager/um-login.ts +15 -23
- package/lowcode/public-components/user-manager/um-orderlist.js +60 -51
- package/lowcode/public-components/user-manager/um-orderlist.ts +289 -275
- package/lowcode/public-components/user-manager/um-rebate.js +104 -82
- package/lowcode/public-components/user-manager/um-rebate.ts +294 -267
- package/lowcode/public-components/user-manager/um-receive.js +582 -0
- package/lowcode/public-components/user-manager/um-receive.ts +599 -0
- package/lowcode/public-components/user-manager/um-wishlist.js +72 -68
- package/lowcode/public-components/user-manager/um-wishlist.ts +240 -230
- package/package.json +1 -1
- package/src/api-public/controllers/shopee.js +17 -0
- package/src/api-public/controllers/shopee.js.map +1 -1
- package/src/api-public/controllers/shopee.ts +32 -0
- package/src/api-public/services/monitor.d.ts +1 -0
- package/src/api-public/services/post.js +17 -7
- package/src/api-public/services/post.js.map +1 -1
- package/src/api-public/services/rebate.js +2 -11
- package/src/api-public/services/rebate.js.map +1 -1
- package/src/api-public/services/rebate.ts +5 -12
- package/src/api-public/services/shopee.d.ts +23 -2
- package/src/api-public/services/shopee.js +230 -111
- package/src/api-public/services/shopee.js.map +1 -1
- package/src/api-public/services/shopee.ts +1012 -838
- package/src/api-public/services/user.js +2 -2
- package/src/api-public/services/user.js.map +1 -1
- package/src/api-public/services/user.ts +3 -3
- package/src/index.js +17 -7
- package/src/index.js.map +1 -1
- package/src/modules/tool.d.ts +4 -4
- package/src/modules/tool.js +2 -1
- package/src/modules/tool.js.map +1 -1
- package/src/services/backend-service.js +17 -7
- package/src/services/backend-service.js.map +1 -1
- package/src/services/template.d.ts +1 -1
- package/src/services/template.js +24 -18
- package/src/services/template.js.map +1 -1
- package/src/services/template.ts +34 -37
|
@@ -86,23 +86,26 @@ class Shopee {
|
|
|
86
86
|
return `${Shopee.path}${api_path}?partner_id=${partner_id}×tamp=${timestamp}&sign=${sign}`;
|
|
87
87
|
}
|
|
88
88
|
cryptoSign(partner_id, api_path, timestamp, access_token, shop_id) {
|
|
89
|
-
const baseString = `${partner_id}${api_path}${timestamp}${access_token !== null && access_token !== void 0 ? access_token :
|
|
89
|
+
const baseString = `${partner_id}${api_path}${timestamp}${access_token !== null && access_token !== void 0 ? access_token : ''}${shop_id !== null && shop_id !== void 0 ? shop_id : ''}`;
|
|
90
90
|
const partner_key = Shopee.partner_key;
|
|
91
|
-
return crypto_1.default
|
|
91
|
+
return crypto_1.default
|
|
92
|
+
.createHmac('sha256', partner_key !== null && partner_key !== void 0 ? partner_key : '')
|
|
93
|
+
.update(baseString)
|
|
94
|
+
.digest('hex');
|
|
92
95
|
}
|
|
93
96
|
generateAuth(redirectUrl) {
|
|
94
97
|
const partner_id = Shopee.partner_id;
|
|
95
|
-
const api_path =
|
|
98
|
+
const api_path = '/api/v2/shop/auth_partner';
|
|
96
99
|
const timestamp = Math.floor(Date.now() / 1000);
|
|
97
100
|
const baseString = `${partner_id}${api_path}${timestamp}`;
|
|
98
|
-
const signature = this.cryptoSign(partner_id !== null && partner_id !== void 0 ? partner_id :
|
|
101
|
+
const signature = this.cryptoSign(partner_id !== null && partner_id !== void 0 ? partner_id : '', api_path, timestamp);
|
|
99
102
|
return `${Shopee.path}${api_path}?partner_id=${partner_id}×tamp=${timestamp}&redirect=${redirectUrl}&sign=${signature}`;
|
|
100
103
|
}
|
|
101
104
|
async getToken(code, shop_id) {
|
|
102
105
|
var _a;
|
|
103
106
|
const timestamp = Math.floor(Date.now() / 1000);
|
|
104
|
-
const partner_id = (_a = Shopee.partner_id) !== null && _a !== void 0 ? _a :
|
|
105
|
-
const api_path =
|
|
107
|
+
const partner_id = (_a = Shopee.partner_id) !== null && _a !== void 0 ? _a : '';
|
|
108
|
+
const api_path = '/api/v2/auth/token/get';
|
|
106
109
|
const config = {
|
|
107
110
|
method: 'post',
|
|
108
111
|
url: this.generateUrl(partner_id, api_path, timestamp),
|
|
@@ -113,29 +116,30 @@ class Shopee {
|
|
|
113
116
|
code: code,
|
|
114
117
|
partner_id: parseInt(partner_id, 10),
|
|
115
118
|
shop_id: parseInt(shop_id),
|
|
116
|
-
})
|
|
119
|
+
}),
|
|
117
120
|
};
|
|
118
121
|
try {
|
|
119
122
|
const response = await (0, axios_1.default)(config);
|
|
120
|
-
const data =
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
123
|
+
const data = await database_js_1.default.execute(`select *
|
|
124
|
+
from \`${config_js_1.saasConfig.SAAS_NAME}\`.private_config
|
|
125
|
+
where \`app_name\` = '${this.app}'
|
|
126
|
+
and \`key\` = 'shopee_access_token'
|
|
127
|
+
`, []);
|
|
125
128
|
let passData = Object.assign(Object.assign({}, response.data), { expires_at: new Date(Date.now() + 14373 * 1000).toISOString(), created_at: new Date().toISOString(), shop_id: shop_id });
|
|
126
129
|
if (data.length == 0) {
|
|
127
130
|
await database_js_1.default.execute(`
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
+
INSERT INTO \`${config_js_1.saasConfig.SAAS_NAME}\`.private_config (\`app_name\`, \`key\`, \`value\`, \`updated_at\`)
|
|
132
|
+
VALUES (?, ?, ?, ?);
|
|
133
|
+
`, [this.app, 'shopee_access_token', passData, new Date()]);
|
|
131
134
|
}
|
|
132
135
|
else {
|
|
133
136
|
await database_js_1.default.execute(`
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
137
|
+
UPDATE \`${config_js_1.saasConfig.SAAS_NAME}\`.\`private_config\`
|
|
138
|
+
SET \`value\` = ?,
|
|
139
|
+
updated_at=?
|
|
140
|
+
where \`app_name\` = '${this.app}'
|
|
141
|
+
and \`key\` = 'shopee_access_token'
|
|
142
|
+
`, [passData, new Date()]);
|
|
139
143
|
}
|
|
140
144
|
}
|
|
141
145
|
catch (error) {
|
|
@@ -150,14 +154,14 @@ class Shopee {
|
|
|
150
154
|
async getItemList(start, end, index = 0) {
|
|
151
155
|
var _a;
|
|
152
156
|
const timestamp = Math.floor(Date.now() / 1000);
|
|
153
|
-
const partner_id = (_a = Shopee.partner_id) !== null && _a !== void 0 ? _a :
|
|
154
|
-
const api_path =
|
|
157
|
+
const partner_id = (_a = Shopee.partner_id) !== null && _a !== void 0 ? _a : '';
|
|
158
|
+
const api_path = '/api/v2/product/get_item_list';
|
|
155
159
|
await this.fetchShopeeAccessToken();
|
|
156
|
-
const data =
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
160
|
+
const data = await database_js_1.default.execute(`select *
|
|
161
|
+
from \`${config_js_1.saasConfig.SAAS_NAME}\`.private_config
|
|
162
|
+
where \`app_name\` = '${this.app}'
|
|
163
|
+
and \`key\` = 'shopee_access_token'
|
|
164
|
+
`, []);
|
|
161
165
|
const config = {
|
|
162
166
|
method: 'get',
|
|
163
167
|
url: this.generateShopUrl(partner_id, api_path, timestamp, data[0].value.access_token, parseInt(data[0].value.shop_id)),
|
|
@@ -179,17 +183,26 @@ class Shopee {
|
|
|
179
183
|
const response = await (0, axios_1.default)(config);
|
|
180
184
|
if (response.data.error.length > 0) {
|
|
181
185
|
return {
|
|
182
|
-
type:
|
|
183
|
-
message: response.data.error
|
|
186
|
+
type: 'error',
|
|
187
|
+
message: response.data.error,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
console.log("response -- ", response);
|
|
191
|
+
if (response.data.response.total_count == 0) {
|
|
192
|
+
return {
|
|
193
|
+
type: 'success',
|
|
194
|
+
data: response.data.response,
|
|
195
|
+
message: '該時間區間查無商品',
|
|
184
196
|
};
|
|
185
197
|
}
|
|
186
198
|
const itemList = response.data.response.item;
|
|
187
199
|
const productData = await Promise.all(itemList.map(async (item, index) => {
|
|
200
|
+
console.log('here -- OK');
|
|
188
201
|
try {
|
|
189
|
-
const pd_data =
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
202
|
+
const pd_data = await database_js_1.default.query(`SELECT count(1)
|
|
203
|
+
FROM ${this.app}.t_manager_post
|
|
204
|
+
WHERE (content ->>'$.type'='product')
|
|
205
|
+
AND (content ->>'$.shopee_id' = ${item.item_id});`, []);
|
|
193
206
|
if (pd_data[0]['count(1)'] > 0) {
|
|
194
207
|
return null;
|
|
195
208
|
}
|
|
@@ -202,7 +215,7 @@ class Shopee {
|
|
|
202
215
|
}
|
|
203
216
|
}));
|
|
204
217
|
const temp = {};
|
|
205
|
-
temp.data = productData.reverse().filter(
|
|
218
|
+
temp.data = productData.reverse().filter(dd => {
|
|
206
219
|
return dd;
|
|
207
220
|
});
|
|
208
221
|
temp.collection = [];
|
|
@@ -213,24 +226,24 @@ class Shopee {
|
|
|
213
226
|
}
|
|
214
227
|
return {
|
|
215
228
|
data: temp.data,
|
|
216
|
-
message: '匯入OK'
|
|
229
|
+
message: '匯入OK',
|
|
217
230
|
};
|
|
218
231
|
}
|
|
219
232
|
catch (error) {
|
|
220
233
|
console.error(error);
|
|
221
234
|
return {
|
|
222
|
-
type:
|
|
235
|
+
type: 'error',
|
|
223
236
|
data: temp.data,
|
|
224
|
-
message: '產品匯入資料庫失敗'
|
|
237
|
+
message: '產品匯入資料庫失敗',
|
|
225
238
|
};
|
|
226
239
|
}
|
|
227
240
|
}
|
|
228
241
|
catch (error) {
|
|
229
242
|
if (axios_1.default.isAxiosError(error) && error.response) {
|
|
230
|
-
console.log(
|
|
243
|
+
console.log('Try get_item_list error');
|
|
231
244
|
console.error('Error Response:', error.response.data);
|
|
232
245
|
return {
|
|
233
|
-
type:
|
|
246
|
+
type: 'error',
|
|
234
247
|
error: error.response.data.error,
|
|
235
248
|
message: error.response.data.message,
|
|
236
249
|
};
|
|
@@ -250,8 +263,8 @@ class Shopee {
|
|
|
250
263
|
async function getModel(postMD) {
|
|
251
264
|
var _a;
|
|
252
265
|
const timestamp = Math.floor(Date.now() / 1000);
|
|
253
|
-
const partner_id = (_a = Shopee.partner_id) !== null && _a !== void 0 ? _a :
|
|
254
|
-
const api_path =
|
|
266
|
+
const partner_id = (_a = Shopee.partner_id) !== null && _a !== void 0 ? _a : '';
|
|
267
|
+
const api_path = '/api/v2/product/get_model_list';
|
|
255
268
|
const config = {
|
|
256
269
|
method: 'get',
|
|
257
270
|
url: that.generateShopUrl(partner_id, api_path, timestamp, token.access_token, parseInt(token.shop_id)),
|
|
@@ -261,7 +274,7 @@ class Shopee {
|
|
|
261
274
|
params: {
|
|
262
275
|
shop_id: parseInt(token.shop_id),
|
|
263
276
|
access_token: token.access_token,
|
|
264
|
-
item_id: id
|
|
277
|
+
item_id: id,
|
|
265
278
|
},
|
|
266
279
|
};
|
|
267
280
|
try {
|
|
@@ -273,13 +286,13 @@ class Shopee {
|
|
|
273
286
|
let temp = {
|
|
274
287
|
title: dd.name,
|
|
275
288
|
option: [],
|
|
276
|
-
language_title: {}
|
|
289
|
+
language_title: {},
|
|
277
290
|
};
|
|
278
291
|
dd.option_list.map((option) => {
|
|
279
292
|
temp.option.push({
|
|
280
293
|
title: option.option,
|
|
281
294
|
expand: false,
|
|
282
|
-
language_title: {}
|
|
295
|
+
language_title: {},
|
|
283
296
|
});
|
|
284
297
|
});
|
|
285
298
|
return temp;
|
|
@@ -299,30 +312,30 @@ class Shopee {
|
|
|
299
312
|
weight: 0,
|
|
300
313
|
shipment_type: 'none',
|
|
301
314
|
sku: data.model_sku,
|
|
302
|
-
barcode:
|
|
315
|
+
barcode: '',
|
|
303
316
|
stock: data.stock_info_v2.summary_info.total_available_stock,
|
|
304
317
|
stockList: {},
|
|
305
|
-
preview_image:
|
|
306
|
-
show_understocking:
|
|
307
|
-
type:
|
|
318
|
+
preview_image: '',
|
|
319
|
+
show_understocking: 'true',
|
|
320
|
+
type: 'product',
|
|
308
321
|
};
|
|
309
|
-
if (!(option && option.skip_image_load) && ((
|
|
322
|
+
if (!(option && option.skip_image_load) && ((_a = data === null || data === void 0 ? void 0 : data.image) === null || _a === void 0 ? void 0 : _a.image_url_list.length) > 0) {
|
|
310
323
|
try {
|
|
311
324
|
const imageUrl = data.image.image_url_list[0];
|
|
312
325
|
if (imageUrl) {
|
|
313
326
|
const buffer = await that.downloadImage(imageUrl);
|
|
314
|
-
const fileExtension =
|
|
327
|
+
const fileExtension = 'jpg';
|
|
315
328
|
const fileName = `shopee/${postMD.title}/${new Date().getTime()}_0.${fileExtension}`;
|
|
316
329
|
newVariants.preview_image = await that.uploadFile(fileName, buffer);
|
|
317
330
|
}
|
|
318
331
|
else {
|
|
319
332
|
console.warn('圖片 URL 列表為空,無法處理');
|
|
320
|
-
newVariants.preview_image =
|
|
333
|
+
newVariants.preview_image = '';
|
|
321
334
|
}
|
|
322
335
|
}
|
|
323
336
|
catch (error) {
|
|
324
337
|
console.error('下載或上傳失敗:', error);
|
|
325
|
-
newVariants.preview_image =
|
|
338
|
+
newVariants.preview_image = '';
|
|
326
339
|
}
|
|
327
340
|
}
|
|
328
341
|
newVariants.shopee_model_id = data.model_id;
|
|
@@ -340,8 +353,8 @@ class Shopee {
|
|
|
340
353
|
}
|
|
341
354
|
}
|
|
342
355
|
const timestamp = Math.floor(Date.now() / 1000);
|
|
343
|
-
const partner_id = (_a = Shopee.partner_id) !== null && _a !== void 0 ? _a :
|
|
344
|
-
const api_path =
|
|
356
|
+
const partner_id = (_a = Shopee.partner_id) !== null && _a !== void 0 ? _a : '';
|
|
357
|
+
const api_path = '/api/v2/product/get_item_base_info';
|
|
345
358
|
const config = {
|
|
346
359
|
method: 'get',
|
|
347
360
|
url: this.generateShopUrl(partner_id, api_path, timestamp, token.access_token, parseInt(token.shop_id)),
|
|
@@ -351,7 +364,7 @@ class Shopee {
|
|
|
351
364
|
params: {
|
|
352
365
|
shop_id: parseInt(token.shop_id),
|
|
353
366
|
access_token: token.access_token,
|
|
354
|
-
item_id_list: id
|
|
367
|
+
item_id_list: id,
|
|
355
368
|
},
|
|
356
369
|
};
|
|
357
370
|
try {
|
|
@@ -360,12 +373,11 @@ class Shopee {
|
|
|
360
373
|
let origData = {};
|
|
361
374
|
try {
|
|
362
375
|
origData = await database_js_1.default.query(`SELECT *
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
}
|
|
367
|
-
catch (e) {
|
|
376
|
+
FROM \`${this.app}\`.t_manager_post
|
|
377
|
+
WHERE (content ->>'$.type'='product')
|
|
378
|
+
AND (content ->>'$.shopee_id' = ?);`, [id]);
|
|
368
379
|
}
|
|
380
|
+
catch (e) { }
|
|
369
381
|
let postMD;
|
|
370
382
|
postMD = this.getInitial({});
|
|
371
383
|
if (origData.length > 0) {
|
|
@@ -378,7 +390,7 @@ class Shopee {
|
|
|
378
390
|
if (item1.field_type == 'image' && !(option && option.skip_image_load)) {
|
|
379
391
|
try {
|
|
380
392
|
const buffer = await this.downloadImage(item1.image_info.image_url);
|
|
381
|
-
const fileExtension =
|
|
393
|
+
const fileExtension = 'jpg';
|
|
382
394
|
const fileName = `shopee/${postMD.title}/${new Date().getTime()}_${item1.image_info.image_id}.${fileExtension}`;
|
|
383
395
|
item1.image_info.s3 = await this.uploadFile(fileName, buffer);
|
|
384
396
|
}
|
|
@@ -392,13 +404,12 @@ class Shopee {
|
|
|
392
404
|
if (item.description_info && item.description_info.extended_description) {
|
|
393
405
|
item.description_info.extended_description.field_list.map((item) => {
|
|
394
406
|
if (item.field_type == 'image' && !(option && option.skip_image_load)) {
|
|
395
|
-
temp += html `
|
|
396
|
-
|
|
397
|
-
|
|
407
|
+
temp += html ` <div style="white-space: pre-wrap;">
|
|
408
|
+
<img src="${item.image_info.s3}" alt="${item.image_info.image_id}" />
|
|
409
|
+
</div>`;
|
|
398
410
|
}
|
|
399
411
|
else if (item.field_type == 'text') {
|
|
400
|
-
temp += html `
|
|
401
|
-
<div style="white-space: pre-wrap;">${item.text}</div>`;
|
|
412
|
+
temp += html ` <div style="white-space: pre-wrap;">${item.text}</div>`;
|
|
402
413
|
}
|
|
403
414
|
});
|
|
404
415
|
}
|
|
@@ -416,13 +427,13 @@ class Shopee {
|
|
|
416
427
|
v_height: item.dimension.package_height,
|
|
417
428
|
weight: item.weight,
|
|
418
429
|
shipment_type: 'none',
|
|
419
|
-
sku:
|
|
420
|
-
barcode:
|
|
430
|
+
sku: '',
|
|
431
|
+
barcode: '',
|
|
421
432
|
stock: item.stock_info_v2.summary_info.total_available_stock,
|
|
422
433
|
stockList: {},
|
|
423
|
-
preview_image:
|
|
424
|
-
show_understocking:
|
|
425
|
-
type:
|
|
434
|
+
preview_image: '',
|
|
435
|
+
show_understocking: 'true',
|
|
436
|
+
type: 'product',
|
|
426
437
|
};
|
|
427
438
|
postMD.variants = [];
|
|
428
439
|
postMD.variants.push(newVariants);
|
|
@@ -434,7 +445,7 @@ class Shopee {
|
|
|
434
445
|
postMD.preview_image = await Promise.all(item.image.image_url_list.map(async (imageUrl, index) => {
|
|
435
446
|
try {
|
|
436
447
|
const buffer = await this.downloadImage(imageUrl);
|
|
437
|
-
const fileExtension =
|
|
448
|
+
const fileExtension = 'jpg';
|
|
438
449
|
const fileName = `shopee/${postMD.title}/${new Date().getTime()}_${index}.${fileExtension}`;
|
|
439
450
|
const uploadedData = await this.uploadFile(fileName, buffer);
|
|
440
451
|
return uploadedData;
|
|
@@ -470,11 +481,11 @@ class Shopee {
|
|
|
470
481
|
return;
|
|
471
482
|
}
|
|
472
483
|
let basicData = {
|
|
473
|
-
|
|
474
|
-
|
|
484
|
+
item_id: obj.product.content.shopee_id,
|
|
485
|
+
stock_list: [],
|
|
475
486
|
};
|
|
476
|
-
const partner_id = (_a = Shopee.partner_id) !== null && _a !== void 0 ? _a :
|
|
477
|
-
const api_path =
|
|
487
|
+
const partner_id = (_a = Shopee.partner_id) !== null && _a !== void 0 ? _a : '';
|
|
488
|
+
const api_path = '/api/v2/product/get_model_list';
|
|
478
489
|
const timestamp = Math.floor(Date.now() / 1000);
|
|
479
490
|
const config = {
|
|
480
491
|
method: 'get',
|
|
@@ -485,7 +496,7 @@ class Shopee {
|
|
|
485
496
|
params: {
|
|
486
497
|
shop_id: parseInt(obj.shop_id),
|
|
487
498
|
access_token: obj.access_token,
|
|
488
|
-
item_id: obj.product.content.shopee_id
|
|
499
|
+
item_id: obj.product.content.shopee_id,
|
|
489
500
|
},
|
|
490
501
|
};
|
|
491
502
|
try {
|
|
@@ -495,14 +506,16 @@ class Shopee {
|
|
|
495
506
|
}
|
|
496
507
|
obj.product.content.variants.map((variant) => {
|
|
497
508
|
let basicStock = {
|
|
498
|
-
|
|
499
|
-
|
|
509
|
+
model_id: 0,
|
|
510
|
+
seller_stock: [
|
|
500
511
|
{
|
|
501
|
-
|
|
502
|
-
}
|
|
503
|
-
]
|
|
512
|
+
stock: 0,
|
|
513
|
+
},
|
|
514
|
+
],
|
|
504
515
|
};
|
|
505
|
-
let findModel = response.data.response.model.find((item) => {
|
|
516
|
+
let findModel = response.data.response.model.find((item) => {
|
|
517
|
+
return item.model_name == variant.spec.join(',');
|
|
518
|
+
});
|
|
506
519
|
console.log(`findModel===>`, findModel);
|
|
507
520
|
if (findModel || response.data.response.model.length == 0) {
|
|
508
521
|
basicStock.model_id = (findModel && findModel.model_id) || 0;
|
|
@@ -512,7 +525,7 @@ class Shopee {
|
|
|
512
525
|
});
|
|
513
526
|
const updateConfig = {
|
|
514
527
|
method: 'post',
|
|
515
|
-
url: this.generateShopUrl(partner_id,
|
|
528
|
+
url: this.generateShopUrl(partner_id, '/api/v2/product/update_stock', timestamp, obj.access_token, parseInt(obj.shop_id)),
|
|
516
529
|
params: {
|
|
517
530
|
shop_id: parseInt(obj.shop_id),
|
|
518
531
|
access_token: obj.access_token,
|
|
@@ -520,7 +533,7 @@ class Shopee {
|
|
|
520
533
|
headers: {
|
|
521
534
|
'Content-Type': 'application/json',
|
|
522
535
|
},
|
|
523
|
-
data: JSON.stringify(basicData)
|
|
536
|
+
data: JSON.stringify(basicData),
|
|
524
537
|
};
|
|
525
538
|
try {
|
|
526
539
|
const response = await (0, axios_1.default)(updateConfig);
|
|
@@ -550,9 +563,9 @@ class Shopee {
|
|
|
550
563
|
let origData = {};
|
|
551
564
|
try {
|
|
552
565
|
origData = await database_js_1.default.query(`SELECT *
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
566
|
+
FROM \`${this.app}\`.t_manager_post
|
|
567
|
+
WHERE (content ->>'$.type'='product')
|
|
568
|
+
AND (content ->>'$.shopee_id' IS NOT NULL AND content ->>'$.shopee_id' <> '')`, []);
|
|
556
569
|
let temp = await this.fetchShopeeAccessToken();
|
|
557
570
|
return Promise.all(origData.map((product) => new Promise((resolve, reject) => {
|
|
558
571
|
try {
|
|
@@ -562,36 +575,37 @@ class Shopee {
|
|
|
562
575
|
resolve();
|
|
563
576
|
},
|
|
564
577
|
access_token: temp.access_token,
|
|
565
|
-
shop_id: temp.shop_id
|
|
578
|
+
shop_id: temp.shop_id,
|
|
566
579
|
});
|
|
567
580
|
}
|
|
568
581
|
catch (e) {
|
|
569
582
|
reject(e);
|
|
570
583
|
}
|
|
571
|
-
})))
|
|
572
|
-
|
|
584
|
+
})))
|
|
585
|
+
.then(() => {
|
|
586
|
+
console.log('所有產品的庫存同步完成!');
|
|
573
587
|
return {
|
|
574
|
-
result:
|
|
588
|
+
result: 'OK',
|
|
575
589
|
};
|
|
576
|
-
})
|
|
577
|
-
|
|
590
|
+
})
|
|
591
|
+
.catch(error => {
|
|
592
|
+
console.error('同步庫存時發生錯誤:', error);
|
|
578
593
|
});
|
|
579
594
|
}
|
|
580
|
-
catch (e) {
|
|
581
|
-
}
|
|
595
|
+
catch (e) { }
|
|
582
596
|
}
|
|
583
597
|
async fetchShopeeAccessToken() {
|
|
584
598
|
try {
|
|
585
|
-
const sqlData = await database_js_1.default.execute(`SELECT *
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
599
|
+
const sqlData = await database_js_1.default.execute(`SELECT *
|
|
600
|
+
FROM \`${config_js_1.saasConfig.SAAS_NAME}\`.private_config
|
|
601
|
+
WHERE \`app_name\` = '${this.app}'
|
|
602
|
+
AND \`key\` = 'shopee_access_token'`, []);
|
|
589
603
|
const obj = {};
|
|
590
604
|
obj.accessToken = sqlData;
|
|
591
|
-
if (new Date().getTime() >=
|
|
605
|
+
if (new Date().getTime() >= new Date(sqlData[0].updated_at).getTime() + 3.9 * 3600 * 1000) {
|
|
592
606
|
console.log(`確認要刷新token`);
|
|
593
607
|
const partner_id = Shopee.partner_id;
|
|
594
|
-
const api_path =
|
|
608
|
+
const api_path = '/api/v2/auth/access_token/get';
|
|
595
609
|
const timestamp = Math.floor(Date.now() / 1000);
|
|
596
610
|
const config = {
|
|
597
611
|
method: 'post',
|
|
@@ -602,26 +616,27 @@ class Shopee {
|
|
|
602
616
|
data: JSON.stringify({
|
|
603
617
|
shop_id: parseInt(obj.accessToken[0].value.shop_id),
|
|
604
618
|
refresh_token: obj.accessToken[0].value.refresh_token,
|
|
605
|
-
partner_id: parseInt(partner_id)
|
|
619
|
+
partner_id: parseInt(partner_id),
|
|
606
620
|
}),
|
|
607
621
|
};
|
|
608
622
|
try {
|
|
609
623
|
const response = await (0, axios_1.default)(config);
|
|
610
624
|
try {
|
|
611
625
|
await database_js_1.default.execute(`
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
626
|
+
UPDATE \`${config_js_1.saasConfig.SAAS_NAME}\`.\`private_config\`
|
|
627
|
+
SET \`value\` = ?,
|
|
628
|
+
updated_at = ?
|
|
629
|
+
where \`app_name\` = '${this.app}'
|
|
630
|
+
and \`key\` = 'shopee_access_token'
|
|
631
|
+
`, [response.data, new Date()]);
|
|
617
632
|
return response.data;
|
|
618
633
|
}
|
|
619
634
|
catch (e) {
|
|
620
|
-
console.error(
|
|
635
|
+
console.error('refresh private_config shopee_access_token error : ', e.data);
|
|
621
636
|
}
|
|
622
637
|
}
|
|
623
638
|
catch (e) {
|
|
624
|
-
console.error(
|
|
639
|
+
console.error('Shopee access token API request failed:', e);
|
|
625
640
|
}
|
|
626
641
|
}
|
|
627
642
|
else {
|
|
@@ -629,7 +644,111 @@ class Shopee {
|
|
|
629
644
|
}
|
|
630
645
|
}
|
|
631
646
|
catch (e) {
|
|
632
|
-
console.error(
|
|
647
|
+
console.error('Database query for Shopee access token failed:', e);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
async getOrderList(start, end, index = 0) {
|
|
651
|
+
var _a;
|
|
652
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
653
|
+
const partner_id = (_a = Shopee.partner_id) !== null && _a !== void 0 ? _a : '';
|
|
654
|
+
const api_path = '/api/v2/order/get_order_list';
|
|
655
|
+
await this.fetchShopeeAccessToken();
|
|
656
|
+
console.log();
|
|
657
|
+
const data = await database_js_1.default.execute(`select *
|
|
658
|
+
from \`${config_js_1.saasConfig.SAAS_NAME}\`.private_config
|
|
659
|
+
where \`app_name\` = '${this.app}'
|
|
660
|
+
and \`key\` = 'shopee_access_token'
|
|
661
|
+
`, []);
|
|
662
|
+
const config = {
|
|
663
|
+
method: 'get',
|
|
664
|
+
url: this.generateShopUrl(partner_id, api_path, timestamp, data[0].value.access_token, parseInt(data[0].value.shop_id)),
|
|
665
|
+
headers: {
|
|
666
|
+
'Content-Type': 'application/json',
|
|
667
|
+
},
|
|
668
|
+
params: {
|
|
669
|
+
shop_id: parseInt(data[0].value.shop_id),
|
|
670
|
+
access_token: data[0].value.access_token,
|
|
671
|
+
offset: index || 0,
|
|
672
|
+
page_size: 10,
|
|
673
|
+
update_time_from: start,
|
|
674
|
+
update_time_to: Math.floor(Date.now() / 1000),
|
|
675
|
+
item_status: ['NORMAL', 'BANNED', 'UNLIST'],
|
|
676
|
+
},
|
|
677
|
+
paramsSerializer: (params) => qs_1.default.stringify(params, { arrayFormat: 'repeat' }),
|
|
678
|
+
};
|
|
679
|
+
try {
|
|
680
|
+
const response = await (0, axios_1.default)(config);
|
|
681
|
+
if (response.data.error.length > 0) {
|
|
682
|
+
return {
|
|
683
|
+
type: 'error',
|
|
684
|
+
message: response.data.error,
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
console.log("order response -- ", response.data);
|
|
688
|
+
if (response.data.response.total_count == 0) {
|
|
689
|
+
return {
|
|
690
|
+
type: 'success',
|
|
691
|
+
data: response.data.response,
|
|
692
|
+
message: '該時間區間查無商品',
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
return;
|
|
696
|
+
const itemList = response.data.response.item;
|
|
697
|
+
const productData = await Promise.all(itemList.map(async (item, index) => {
|
|
698
|
+
console.log('here -- OK');
|
|
699
|
+
try {
|
|
700
|
+
const pd_data = await database_js_1.default.query(`SELECT count(1)
|
|
701
|
+
FROM ${this.app}.t_manager_post
|
|
702
|
+
WHERE (content ->>'$.type'='product')
|
|
703
|
+
AND (content ->>'$.shopee_id' = ${item.item_id});`, []);
|
|
704
|
+
if (pd_data[0]['count(1)'] > 0) {
|
|
705
|
+
return null;
|
|
706
|
+
}
|
|
707
|
+
else {
|
|
708
|
+
return await this.getProductDetail(item.item_id);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
catch (error) {
|
|
712
|
+
return null;
|
|
713
|
+
}
|
|
714
|
+
}));
|
|
715
|
+
const temp = {};
|
|
716
|
+
temp.data = productData.reverse().filter(dd => {
|
|
717
|
+
return dd;
|
|
718
|
+
});
|
|
719
|
+
temp.collection = [];
|
|
720
|
+
try {
|
|
721
|
+
await new shopping_js_1.Shopping(this.app, this.token).postMulProduct(temp);
|
|
722
|
+
if (response.data.response.has_next_page) {
|
|
723
|
+
await this.getItemList(start, end, response.data.response.next_offset);
|
|
724
|
+
}
|
|
725
|
+
return {
|
|
726
|
+
data: temp.data,
|
|
727
|
+
message: '匯入OK',
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
catch (error) {
|
|
731
|
+
console.error(error);
|
|
732
|
+
return {
|
|
733
|
+
type: 'error',
|
|
734
|
+
data: temp.data,
|
|
735
|
+
message: '產品匯入資料庫失敗',
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
catch (error) {
|
|
740
|
+
if (axios_1.default.isAxiosError(error) && error.response) {
|
|
741
|
+
console.log('Try get_item_list error');
|
|
742
|
+
console.error('Error Response:', error.response.data);
|
|
743
|
+
return {
|
|
744
|
+
type: 'error',
|
|
745
|
+
error: error.response.data.error,
|
|
746
|
+
message: error.response.data.message,
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
else {
|
|
750
|
+
console.error('Unexpected Error:', error.message);
|
|
751
|
+
}
|
|
633
752
|
}
|
|
634
753
|
}
|
|
635
754
|
getInitial(obj) {
|