erp2goods-service.js
1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Created by TaoHuang on 2016/11/14.
*/
'use strict';
const api = require('./erp2goods-api');
const _ = require('lodash');
const url = require('url');
const findBySkn = (skn) => {
return api.getProductBySknAsync(skn).then((result) => {
if (result.code === 200 && result.data.product_url) {
return {
url: url.parse(_.get(result, 'data.product_url', '')).path
};
} else {
return {};
}
});
};
const findBySku = (sku) => {
return api.getProductBySkuAsync(`[${sku}]`).then((result) => {
if (result.code === 200 && !_.isEmpty(result.data)) {
let product = _.head(result.data) || {};
let skn = product.erpProductId || '';
if (skn) {
return findBySkn(skn);
}
return {};
} else {
return {};
}
});
};
const find = (q) => {
if (q.sku) {
return findBySku(q.sku);
} else if (q.skn) {
return findBySkn(q.skn);
} else {
return Promise.reject();
}
};
module.exports = {
find
};