eprolo-cli 1.0.107 → 1.0.109

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 +96 -76
  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,88 +230,108 @@ 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);
266
+ })
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) });
261
271
  })
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({product_id:result.data.data.id}, null, 2));
273
- process.exit(0);
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(JSON.stringify({
279
+ variantlist: result.data.data.variantlist.map((item) => {
280
+ return {
281
+ variantsid: item.id,
282
+ sku: item.sku
283
+ }
284
+ })
285
+ }, null, 2));
286
+ process.exit(0);
274
287
  }
275
- }else{
276
- console.log(JSON.stringify(data, null, 2));
277
- process.exit(0);
288
+ } else {
289
+ console.log(JSON.stringify(data, null, 2));
290
+ process.exit(0);
278
291
  }
279
292
  }
280
- console.log(JSON.stringify({product_id:data.data.data.id}, null, 2));
293
+ console.log(JSON.stringify({
294
+ variantlist: data.data.data.variantsList.map((item) => {
295
+ return {
296
+ variantsid: item.id,
297
+ sku: item.sku
298
+ }
299
+ })
300
+ }, null, 2));
281
301
  process.exit(0);
282
302
  }
283
303
  async function addOrder(arg) {
284
304
  const config = readConfig();
285
305
  const order = JSON.parse(arg);
286
- console.log("-----------------",order);
287
- const newOrder={};
288
- const orderItemlist=[];
289
- newOrder.note=order.name;
290
- newOrder.shipping_country_code=order.currencyCode;
291
- newOrder.order_id=getId(order.id);
292
- newOrder.order_number=order.name;
293
- if(order.shippingAddress){
294
- newOrder.shipping_name=order.shippingAddress.firstName + ' ' + order.shippingAddress.lastName;
295
- newOrder.shipping_phone=order.shippingAddress.phone
296
- // newOrder.shipping_company=order.
297
- newOrder.shipping_country=order.shippingAddress.country;
298
- newOrder.shipping_address=order.shippingAddress.address1;
299
- newOrder.shipping_province=order.shippingAddress.province;
300
- // newOrder.shipping_province_code=order.shippingAddress.;
301
- newOrder.shipping_address2=order.shippingAddress.address2
302
- newOrder.shipping_city=order.shippingAddress.city;
303
- // 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;
304
324
  }
305
- if(order.lineItems && order.lineItems.edges){
306
- order.lineItems.edges.forEach((edge)=>{
307
- orderItemlist.push({
308
- variantsid:getId(edge.node.variant.id),
309
- quantity:edge.node.quantity,
325
+ if (order.lineItems && order.lineItems.edges) {
326
+ order.lineItems.edges.forEach((edge) => {
327
+ orderItemlist.push({
328
+ variantsid: getId(edge.node.variant.id),
329
+ quantity: edge.node.quantity,
330
+ })
310
331
  })
311
- })
312
- newOrder.orderItemlist=orderItemlist;
332
+ newOrder.orderItemlist = orderItemlist;
313
333
  }
314
- console.log("-------222----------",newOrder);
334
+ console.log("-------222----------", newOrder);
315
335
  const data = await postJsonWithRetry(`http://43.153.35.208:3000/addInfo`, newOrder, config, "POST");
316
336
  console.log(JSON.stringify(data, null, 2));
317
337
  process.exit(0);
@@ -359,7 +379,7 @@ Credential file (~/.eprolo-toolkit/config.json):
359
379
 
360
380
  async function main() {
361
381
  const argv = process.argv.slice(2);
362
- console.log("-----------------",argv);
382
+ console.log("-----------------", argv);
363
383
  if (argv[0] === "auth" && argv[1] === "login") {
364
384
  await login(argv.slice(2));
365
385
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eprolo-cli",
3
- "version": "1.0.107",
3
+ "version": "1.0.109",
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"