eprolo-cli 1.0.108 → 1.0.110

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.
Files changed (2) hide show
  1. package/bin/eprolo-cli.js +102 -87
  2. package/package.json +1 -1
package/bin/eprolo-cli.js CHANGED
@@ -47,23 +47,23 @@ function readFlag(argv, flag) {
47
47
  // HTTPS POST 请求
48
48
  // ---------------------------------------------------------------------------
49
49
 
50
- function postJson(url, body, headers = {},method = "POST") {
50
+ function postJson(url, body, headers = {}, method = "POST") {
51
51
  const config = readConfig();
52
- let signValue={}
53
- if(config.timestamp){
52
+ let signValue = {}
53
+ if (config.timestamp) {
54
54
  signValue.timestamp = config.timestamp;
55
55
  signValue.sign = config.accessToken;
56
56
  }
57
- if(method == "GET") {
58
- signValue = {...signValue, ...body};
57
+ if (method == "GET") {
58
+ signValue = { ...signValue, ...body };
59
59
  }
60
60
  url += "?" + new URLSearchParams(signValue).toString();
61
61
  return new Promise((resolve, reject) => {
62
62
  const target = new URL(url);
63
63
  const payload = JSON.stringify(body);
64
- let reqMethod=https;
65
- if(String(target.protocol).startsWith("http:")){
66
- reqMethod=http;
64
+ let reqMethod = https;
65
+ if (String(target.protocol).startsWith("http:")) {
66
+ reqMethod = http;
67
67
  }
68
68
  const req = reqMethod.request({
69
69
  hostname: target.hostname,
@@ -108,7 +108,7 @@ async function postJsonWithRetry(url, body, config, method = "POST") {
108
108
  let result = await postJson(url, body, headers, method);
109
109
  if (result.data.code == '-1') {
110
110
  // 刷新 token
111
- const refreshRes = await postJson(LOGIN_API, {openApiKey: config.refreshToken});
111
+ const refreshRes = await postJson(LOGIN_API, { openApiKey: config.refreshToken });
112
112
 
113
113
  if (refreshRes.data.code == 0) {
114
114
  // 更新凭据文件
@@ -116,14 +116,14 @@ async function postJsonWithRetry(url, body, config, method = "POST") {
116
116
 
117
117
  // 用新 token 重试
118
118
  headers["apiKey"] = `${refreshRes.data.data.apiKey}`;
119
- result = await postJson(url, body, headers,method);
119
+ result = await postJson(url, body, headers, method);
120
120
  }
121
121
  else {
122
122
  const err = new Error("Token 刷新失败,请重新登录授权");
123
123
  err.response = refreshRes.data;
124
124
  throw err;
125
125
  }
126
- }else if (result.data.code != 0) {
126
+ } else if (result.data.code != 0) {
127
127
  const err = new Error(`HTTP ${result.statusCode}`);
128
128
  err.response = result.data.data.msg;
129
129
  throw err;
@@ -208,7 +208,7 @@ async function queryProductId(productId) {
208
208
  console.error(JSON.stringify({ success: false, errorMessage: "缺少产品 ID" }));
209
209
  process.exit(1);
210
210
  }
211
- const data = await postJsonWithRetry(PRODUCT_TYPE_API, {id:productId}, config, "GET");
211
+ const data = await postJsonWithRetry(PRODUCT_TYPE_API, { id: productId }, config, "GET");
212
212
  console.log(JSON.stringify(data, null, 2));
213
213
  process.exit(0);
214
214
  }
@@ -230,99 +230,114 @@ function readPayload(arg) {
230
230
  }
231
231
  return arg;
232
232
  }
233
- function getId(id){
234
- let ids=String(id).split('/');
235
- return ids[ids.length-1];
233
+ function getId(id) {
234
+ let ids = String(id).split('/');
235
+ return ids[ids.length - 1];
236
236
  }
237
237
  async function insertProduct(arg) {
238
238
  const config = readConfig();
239
239
  const product = JSON.parse(arg);
240
- const info={};
241
- const optionList=[];
242
- const variantsList=[];
243
- const imageList=[];
240
+ const info = {};
241
+ const optionList = [];
242
+ const variantsList = [];
243
+ const imageList = [];
244
244
  info.title = product.title;
245
245
  info.product_id = getId(product.id);
246
- info.body_html=product.description;
247
- info.optionList=optionList;
248
- info.variantsList=variantsList;
249
- info.imageList=imageList;
250
- product.variants.edges.forEach((variant,index)=>{
251
- let newInfo={
252
- image_id:getId(variant.id),
253
- title:variant.title,
254
- sku:variant.sku
255
- };
256
- variant.selectedOptions.forEach((option,optionIndex)=>{
257
- if(index==0){
258
- optionList.push({name:option.name});
246
+ info.body_html = product.description;
247
+ info.optionList = optionList;
248
+ info.variantsList = variantsList;
249
+ info.imageList = imageList;
250
+ if (product.variants && product.variants.edges) {
251
+ product.variants.edges.forEach((variant, index) => {
252
+ let newInfo = {
253
+ image_id: getId(variant.id),
254
+ title: variant.title,
255
+ sku: variant.sku
256
+ };
257
+ if (variant.selectedOptions && variant.selectedOptions.length > 0) {
258
+ variant.selectedOptions.forEach((option, optionIndex) => {
259
+ if (index == 0) {
260
+ optionList.push({ name: option.name });
261
+ }
262
+ newInfo['option' + optionIndex] = option.name;
263
+ })
259
264
  }
260
- newInfo['option'+optionIndex]=option.name;
265
+ variantsList.push(newInfo);
261
266
  })
262
- variantsList.push(newInfo);
263
- })
264
- product.media.edges.forEach((edge)=>{
265
- imageList.push({src:edge.url,images_id:getId(edge.id)});
266
- })
267
- const data = await postJsonWithRetry(BASE_URL+'/insert_product.html', info, config, "POST");
268
- if(data.data.code=='-1'){
269
- if(data.data.msg=="product_id exist"){
270
- const result = await postJsonWithRetry(BASE_URL+'/getproduct.html', {product_id:info.product_id}, config);
271
- if(result.data.code==0){
272
- console.log(JSON.stringify({variantlist:result.data.data.variantsList.map((item)=>{
273
- return {
274
- variantsid:item.id,
275
- sku:item.sku
276
- }
277
- })}, null, 2));
278
- process.exit(0);
267
+ }
268
+ if (product.media && product.media.edges) {
269
+ product.media.edges.forEach((edge) => {
270
+ imageList.push({ src: edge.url, images_id: getId(edge.id) });
271
+ })
272
+ }
273
+ const data = await postJsonWithRetry(BASE_URL + '/insert_product.html', info, config, "POST");
274
+ if (data.data.code == '-1') {
275
+ if (data.data.msg == "product_id exist") {
276
+ const result = await postJsonWithRetry(BASE_URL + '/getproduct.html', { product_id: info.product_id }, config, "GET");
277
+ if (result.data.code == 0) {
278
+ console.log({
279
+ variantlist: result.data.data.variantlist.map((item) => {
280
+ return {
281
+ variantsid: item.id,
282
+ sku: item.sku
283
+ }
284
+ })
285
+ });
286
+ process.exit(0);
279
287
  }
280
- }else{
281
- console.log(JSON.stringify(data, null, 2));
282
- process.exit(0);
288
+ } else {
289
+ console.log(JSON.stringify(data, null, 2));
290
+ process.exit(0);
283
291
  }
284
292
  }
285
- console.log(JSON.stringify({variantlist:data.data.data.variantsList.map((item)=>{
286
- return {
287
- variantsid:item.id,
288
- sku:item.sku
289
- }
290
- })}, null, 2));
293
+ console.log({
294
+ variantlist: data.data.data.variantsList.map((item) => {
295
+ return {
296
+ variantsid: item.id,
297
+ sku: item.sku
298
+ }
299
+ })
300
+ });
291
301
  process.exit(0);
292
302
  }
293
303
  async function addOrder(arg) {
294
304
  const config = readConfig();
295
305
  const order = JSON.parse(arg);
296
- console.log("-----------------",order);
297
- const newOrder={};
298
- const orderItemlist=[];
299
- newOrder.note=order.name;
300
- newOrder.shipping_country_code=order.currencyCode;
301
- newOrder.order_id=getId(order.id);
302
- newOrder.order_number=order.name;
303
- if(order.shippingAddress){
304
- newOrder.shipping_name=order.shippingAddress.firstName + ' ' + order.shippingAddress.lastName;
305
- newOrder.shipping_phone=order.shippingAddress.phone
306
- // newOrder.shipping_company=order.
307
- newOrder.shipping_country=order.shippingAddress.country;
308
- newOrder.shipping_address=order.shippingAddress.address1;
309
- newOrder.shipping_province=order.shippingAddress.province;
310
- // newOrder.shipping_province_code=order.shippingAddress.;
311
- newOrder.shipping_address2=order.shippingAddress.address2
312
- newOrder.shipping_city=order.shippingAddress.city;
313
- // newOrder.shipping_taxNumber=order.shippingAddress.taxNumber;
306
+ console.log("-----------------", order);
307
+ const newOrder = {};
308
+ const orderItemlist = [];
309
+ newOrder.note = order.name;
310
+ newOrder.shipping_country_code = order.currencyCode;
311
+ newOrder.order_id = getId(order.id);
312
+ newOrder.order_number = order.name;
313
+ if (order.shippingAddress) {
314
+ newOrder.shipping_name = order.shippingAddress.firstName + ' ' + order.shippingAddress.lastName;
315
+ newOrder.shipping_phone = order.shippingAddress.phone
316
+ // newOrder.shipping_company=order.
317
+ newOrder.shipping_country = order.shippingAddress.country;
318
+ newOrder.shipping_address = order.shippingAddress.address1;
319
+ newOrder.shipping_province = order.shippingAddress.province;
320
+ // newOrder.shipping_province_code=order.shippingAddress.;
321
+ newOrder.shipping_address2 = order.shippingAddress.address2
322
+ newOrder.shipping_city = order.shippingAddress.city;
323
+ // newOrder.shipping_taxNumber=order.shippingAddress.taxNumber;
314
324
  }
315
- if(order.lineItems && order.lineItems.edges){
316
- order.lineItems.edges.forEach((edge)=>{
317
- orderItemlist.push({
318
- variantsid:getId(edge.node.variant.id),
319
- quantity:edge.node.quantity,
325
+ if (order.lineItems && order.lineItems.edges) {
326
+ order.lineItems.edges.forEach((edge) => {
327
+ console.log("-------666----------", order.lineItems.eproloSyncResult[edge.node.product.id].message);
328
+ let newOrderItemlist = { quantity: edge.node.quantity }
329
+ let messageResult = order.lineItems.eproloSyncResult[edge.node.product.id].message;
330
+ if (messageResult) {
331
+ let newMessage = JSON.parse(messageResult);
332
+ newOrderItemlist.variantsid = newMessage.variantlist.find((item) => item.sku == edge.node.sku).variantsid;
333
+ }
334
+ orderItemlist.push(newOrderItemlist)
320
335
  })
321
- })
322
- newOrder.orderItemlist=orderItemlist;
336
+ newOrder.orderItemlist = orderItemlist;
323
337
  }
324
- console.log("-------222----------",newOrder);
325
- const data = await postJsonWithRetry(`http://43.153.35.208:3000/addInfo`, newOrder, config, "POST");
338
+ console.log("-------222----------", newOrder);
339
+ // const data = await postJsonWithRetry(`http://43.153.35.208:3000/addInfo`, newOrder, config, "POST");
340
+ const data = await postJsonWithRetry(BASE_URL + '/add_order.html', newOrder, config, "POST");
326
341
  console.log(JSON.stringify(data, null, 2));
327
342
  process.exit(0);
328
343
  }
@@ -369,7 +384,7 @@ Credential file (~/.eprolo-toolkit/config.json):
369
384
 
370
385
  async function main() {
371
386
  const argv = process.argv.slice(2);
372
- console.log("-----------------",argv);
387
+ console.log("-----------------", argv);
373
388
  if (argv[0] === "auth" && argv[1] === "login") {
374
389
  await login(argv.slice(2));
375
390
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eprolo-cli",
3
- "version": "1.0.108",
3
+ "version": "1.0.110",
4
4
  "description": "CLI for Dianxiaobao ICBU AI publishing, authorization profile management, task submission, and task status checks.",
5
5
  "bin": {
6
6
  "eprolo": "bin/eprolo-cli.js"