Commit e04e6148 by yexing

优化代码

parent 107a897b
3.5.0 3.5.3
\ No newline at end of file \ No newline at end of file
No preview for this file type
...@@ -383,15 +383,15 @@ var G = (function (exports) { ...@@ -383,15 +383,15 @@ var G = (function (exports) {
if (!(this.uri && this.partnerId)) { if (!(this.uri && this.partnerId)) {
throw new Error(`uri: ${this.uri}, partnerId: ${this.partnerId}`); throw new Error(`uri: ${this.uri}, partnerId: ${this.partnerId}`);
} }
this._ext = this.options.ext || "csv"; // 扩展名
this._retryLimit = 6; this._retryLimit = 6;
this._retryCount = 0; this._retryCount = 0;
} }
getFileName(sf) { getFileName(sf) {
const ext = this.options.ext || 'csv';
let name = this.names.showName; let name = this.names.showName;
name = this.options.noLowerCase ? name : name.toLowerCase(); name = this.options.noLowerCase ? name : name.toLowerCase();
sf = sf === undefined || sf === null ? '' : ' ' + sf; sf = sf === undefined || sf === null ? '' : ' ' + sf;
return `${this.partnerId} #${name}# ${this.desc}${sf}.${ext}`; return `${this.partnerId} #${name}# ${this.desc}${sf}.${this._ext}`;
} }
async _retry(args) { async _retry(args) {
if (++this._retryCount < this._retryLimit) { if (++this._retryCount < this._retryLimit) {
...@@ -409,7 +409,7 @@ var G = (function (exports) { ...@@ -409,7 +409,7 @@ var G = (function (exports) {
const url = new URL(`${this.uri}${this.names.apiName || ''}`); const url = new URL(`${this.uri}${this.names.apiName || ''}`);
const method = this.options.method || 'GET'; const method = this.options.method || 'GET';
const callback = this.options.callback; const callback = this.options.callback;
const ext = this.options.ext || 'csv'; const sign = this.options.sign;
next |= this.options.next; next |= this.options.next;
let body = this.options.body; let body = this.options.body;
...@@ -438,23 +438,21 @@ var G = (function (exports) { ...@@ -438,23 +438,21 @@ var G = (function (exports) {
console.error(`重试回调, ${e.message}`); console.error(`重试回调, ${e.message}`);
return this._retry(arguments); return this._retry(arguments);
} }
} else if (ext == 'json') { } else if (sign == 'json') {
const json = await response.json(); const json = await response.json();
if (Object.keys(json).length === 0) { if (Object.keys(json).length === 0) {
return true; return true;
} }
const csv = JSON2CSV(json); const csv = JSON2CSV(json);
blob = new Blob([csv], { type: "text/csv" }); blob = new Blob([csv], { type: "text/csv" });
this.options.ext = "csv";
} else { } else {
blob = await response.blob(); blob = await response.blob();
} }
if (blob === undefined || blob === null) return false; if (blob === undefined || blob === null) return false;
if (next) return blob; if (next) return blob;
const fn = this.getFileName(); const fn = this.getFileName();
if (ext === 'zip') { if (sign === 'zip') {
const newFn = fn.replace(/\.\w+$/g, ".csv"); return renameZipFile(blob, fn);
return renameZipFile(blob, newFn);
} }
return createZip([blob], [fn]); return createZip([blob], [fn]);
} catch (e) { } catch (e) {
...@@ -914,7 +912,7 @@ var G = (function (exports) { ...@@ -914,7 +912,7 @@ var G = (function (exports) {
reportDate, reportDate,
}, },
{ {
ext: "zip", sign: "zip",
} }
), ),
new Task( new Task(
...@@ -926,7 +924,7 @@ var G = (function (exports) { ...@@ -926,7 +924,7 @@ var G = (function (exports) {
reportDate, reportDate,
}, },
{ {
ext: "zip", sign: "zip",
} }
) )
); );
...@@ -1122,7 +1120,7 @@ var G = (function (exports) { ...@@ -1122,7 +1120,7 @@ var G = (function (exports) {
fromDate, fromDate,
toDate, toDate,
}, },
{ ext: "json", isMmonthly: true } { sign: "json", isMmonthly: true }
), ),
new Task( new Task(
{ {
...@@ -1134,7 +1132,7 @@ var G = (function (exports) { ...@@ -1134,7 +1132,7 @@ var G = (function (exports) {
toDate, toDate,
type: "MCS", type: "MCS",
}, },
{ ext: "json", isMmonthly: true } { sign: "json", isMmonthly: true }
), ),
new TaskGroup( new TaskGroup(
...@@ -1148,7 +1146,7 @@ var G = (function (exports) { ...@@ -1148,7 +1146,7 @@ var G = (function (exports) {
fromDate, fromDate,
toDate: cut ? cutOffDate : toDate, toDate: cut ? cutOffDate : toDate,
}, },
{ ext: "json" } { sign: "json" }
), ),
new Task( new Task(
{ {
...@@ -1159,7 +1157,7 @@ var G = (function (exports) { ...@@ -1159,7 +1157,7 @@ var G = (function (exports) {
fromDate: cut ? cutOffDate : fromDate, fromDate: cut ? cutOffDate : fromDate,
toDate, toDate,
}, },
{ ext: "json" } { sign: "json" }
), ),
].slice(...se) ].slice(...se)
), ),
...@@ -1322,7 +1320,7 @@ var G = (function (exports) { ...@@ -1322,7 +1320,7 @@ var G = (function (exports) {
let fn = task.getFileName(); let fn = task.getFileName();
const isJson = fn.endsWith('.json'); const isJson = fn.endsWith('.json');
if (!isJson) fn = fn.replace(/\.\w+$/g, ".zip"); if (!isJson) fn = fn.replace(/\.\w+$/g, ".zip");
if ((isSingle || isJson) && !await checkLog(fn)) { if (isSingle || isJson ? !await checkLog(fn): false) {
console.log(`${pf} skip2`); console.log(`${pf} skip2`);
continue; continue;
} }
......
{"name":"WalmartExports","description":"导出沃尔玛报表","version":"3.5.0","manifest_version":3,"background":{"service_worker":"./js/background.js"},"permissions":["tabs","activeTab","scripting","notifications","storage","alarms"],"action":{"default_title":"WalmartExports","default_popup":"popup.html","default_icon":{"16":"icon.png","32":"icon.png","48":"icon.png","128":"icon.png"}},"icons":{"16":"icon.png","32":"icon.png","48":"icon.png","128":"icon.png"},"web_accessible_resources":[{"resources":["src/main.js"],"matches":["<all_urls>"]}],"host_permissions":["https://seller.walmart.com/*","https://advertising.walmart.com/*","https://login.account.wal-mart.com/*","https://marketplace.walmartapis.com/*","https://walmart.meinuosha.com/*"],"content_scripts":[{"matches":["https://seller.walmart.com/*","https://advertising.walmart.com/*","https://login.account.wal-mart.com/*","https://marketplace.walmartapis.com/*","https://walmart.meinuosha.com/*"],"js":["./js/main.js"]}]} {"name":"WalmartExports","description":"导出沃尔玛报表","version":"3.5.3","manifest_version":3,"background":{"service_worker":"./js/background.js"},"permissions":["tabs","activeTab","scripting","notifications","storage","alarms"],"action":{"default_title":"WalmartExports","default_popup":"popup.html","default_icon":{"16":"icon.png","32":"icon.png","48":"icon.png","128":"icon.png"}},"icons":{"16":"icon.png","32":"icon.png","48":"icon.png","128":"icon.png"},"web_accessible_resources":[{"resources":["src/main.js"],"matches":["<all_urls>"]}],"host_permissions":["https://seller.walmart.com/*","https://advertising.walmart.com/*","https://login.account.wal-mart.com/*","https://marketplace.walmartapis.com/*","https://walmart.meinuosha.com/*"],"content_scripts":[{"matches":["https://seller.walmart.com/*","https://advertising.walmart.com/*","https://login.account.wal-mart.com/*","https://marketplace.walmartapis.com/*","https://walmart.meinuosha.com/*"],"js":["./js/main.js"]}]}
\ No newline at end of file \ No newline at end of file
...@@ -413,7 +413,7 @@ export async function createTasks(uri, fromDate, toDate) { ...@@ -413,7 +413,7 @@ export async function createTasks(uri, fromDate, toDate) {
reportDate, reportDate,
}, },
{ {
ext: "zip", sign: "zip",
} }
), ),
new Task( new Task(
...@@ -425,7 +425,7 @@ export async function createTasks(uri, fromDate, toDate) { ...@@ -425,7 +425,7 @@ export async function createTasks(uri, fromDate, toDate) {
reportDate, reportDate,
}, },
{ {
ext: "zip", sign: "zip",
} }
) )
); );
...@@ -621,7 +621,7 @@ export async function createTasks(uri, fromDate, toDate) { ...@@ -621,7 +621,7 @@ export async function createTasks(uri, fromDate, toDate) {
fromDate, fromDate,
toDate, toDate,
}, },
{ ext: "json", isMmonthly: true } { sign: "json", isMmonthly: true }
), ),
new Task( new Task(
{ {
...@@ -633,7 +633,7 @@ export async function createTasks(uri, fromDate, toDate) { ...@@ -633,7 +633,7 @@ export async function createTasks(uri, fromDate, toDate) {
toDate, toDate,
type: "MCS", type: "MCS",
}, },
{ ext: "json", isMmonthly: true } { sign: "json", isMmonthly: true }
), ),
new TaskGroup( new TaskGroup(
...@@ -647,7 +647,7 @@ export async function createTasks(uri, fromDate, toDate) { ...@@ -647,7 +647,7 @@ export async function createTasks(uri, fromDate, toDate) {
fromDate, fromDate,
toDate: cut ? cutOffDate : toDate, toDate: cut ? cutOffDate : toDate,
}, },
{ ext: "json" } { sign: "json" }
), ),
new Task( new Task(
{ {
...@@ -658,7 +658,7 @@ export async function createTasks(uri, fromDate, toDate) { ...@@ -658,7 +658,7 @@ export async function createTasks(uri, fromDate, toDate) {
fromDate: cut ? cutOffDate : fromDate, fromDate: cut ? cutOffDate : fromDate,
toDate, toDate,
}, },
{ ext: "json" } { sign: "json" }
), ),
].slice(...se) ].slice(...se)
), ),
......
...@@ -38,7 +38,7 @@ export async function run(options = {}) { ...@@ -38,7 +38,7 @@ export async function run(options = {}) {
let fn = task.getFileName(); let fn = task.getFileName();
const isJson = fn.endsWith('.json'); const isJson = fn.endsWith('.json');
if (!isJson) fn = fn.replace(/\.\w+$/g, ".zip"); if (!isJson) fn = fn.replace(/\.\w+$/g, ".zip");
if ((isSingle || isJson) && !await checkLog(fn)) { if (isSingle || isJson ? !await checkLog(fn): false) {
console.log(`${pf} skip2`); console.log(`${pf} skip2`);
continue; continue;
} }
......
...@@ -59,15 +59,15 @@ export class Task { ...@@ -59,15 +59,15 @@ export class Task {
if (!(this.uri && this.partnerId)) { if (!(this.uri && this.partnerId)) {
throw new Error(`uri: ${this.uri}, partnerId: ${this.partnerId}`); throw new Error(`uri: ${this.uri}, partnerId: ${this.partnerId}`);
} }
this._ext = this.options.ext || "csv" // 扩展名
this._retryLimit = 6 this._retryLimit = 6
this._retryCount = 0 this._retryCount = 0
} }
getFileName(sf) { getFileName(sf) {
const ext = this.options.ext || 'csv';
let name = this.names.showName; let name = this.names.showName;
name = this.options.noLowerCase ? name : name.toLowerCase(); name = this.options.noLowerCase ? name : name.toLowerCase();
sf = sf === undefined || sf === null ? '' : ' ' + sf; sf = sf === undefined || sf === null ? '' : ' ' + sf;
return `${this.partnerId} #${name}# ${this.desc}${sf}.${ext}`; return `${this.partnerId} #${name}# ${this.desc}${sf}.${this._ext}`;
} }
async _retry(args) { async _retry(args) {
if (++this._retryCount < this._retryLimit) { if (++this._retryCount < this._retryLimit) {
...@@ -85,7 +85,7 @@ export class Task { ...@@ -85,7 +85,7 @@ export class Task {
const url = new URL(`${this.uri}${this.names.apiName || ''}`); const url = new URL(`${this.uri}${this.names.apiName || ''}`);
const method = this.options.method || 'GET'; const method = this.options.method || 'GET';
const callback = this.options.callback; const callback = this.options.callback;
const ext = this.options.ext || 'csv'; const sign = this.options.sign;
next |= this.options.next; next |= this.options.next;
let body = this.options.body; let body = this.options.body;
...@@ -114,23 +114,21 @@ export class Task { ...@@ -114,23 +114,21 @@ export class Task {
console.error(`重试回调, ${e.message}`); console.error(`重试回调, ${e.message}`);
return this._retry(arguments); return this._retry(arguments);
} }
} else if (ext == 'json') { } else if (sign == 'json') {
const json = await response.json(); const json = await response.json();
if (Object.keys(json).length === 0) { if (Object.keys(json).length === 0) {
return true; return true;
} }
const csv = JSON2CSV(json); const csv = JSON2CSV(json);
blob = new Blob([csv], { type: "text/csv" }); blob = new Blob([csv], { type: "text/csv" });
this.options.ext = "csv";
} else { } else {
blob = await response.blob(); blob = await response.blob();
} }
if (blob === undefined || blob === null) return false; if (blob === undefined || blob === null) return false;
if (next) return blob; if (next) return blob;
const fn = this.getFileName(); const fn = this.getFileName();
if (ext === 'zip') { if (sign === 'zip') {
const newFn = fn.replace(/\.\w+$/g, ".csv"); return renameZipFile(blob, fn);
return renameZipFile(blob, newFn);
} }
return createZip([blob], [fn]); return createZip([blob], [fn]);
} catch (e) { } catch (e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment