shop-service.js
3.16 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Created by TaoHuang on 2016/6/28.
*/
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const api = require('./shop-api');
const DEFAULT_IMG = '01091c21f2317a64f123f1649fbbccf7ba';
/**
* 生成缩略图
*/
const _imageView2 = (url, width, height) => {
url += url.includes('?') ? '&' : '?';
return `${url}imageView2/1/w${width}/h/${height}`;
};
/**
* 解析 resource_data 参数
*/
const _getResourceData = list => {
list.resource_data = JSON.parse(list.resource_data || '[]');
return list;
};
/**
* 店铺Banner 资源位
*/
const _shopTopBanner = data => {
let result = {
banner: '',
isShowShopName: false,
bannerHeight: 150
};
let resource = _.head(data.resource_data);
if (resource.shopSrc) {
result.banner = _imageView2(resource.shopSrc, 1150, 150);
}
if (resource.detailSrc) {
result.detailSrc = _imageView2(resource.detailSrc, 1150, 150);
}
if (resource.isShowShopName) {
result.isShowShopName = resource.isShowShopName === 'Y';
}
return result;
};
/**
* 水牌
*/
const _signboard = data => {
let result = [];
let resources = data.resource_data;
resources.forEach(resource => {
if (_.isEmpty(resource)) {
return;
}
resource.data.forEach(val => {
result.push({
img: _imageView2(val.src, 160, 240),
url: val.url
});
});
});
return {
title: '',
list: result
};
};
const _shopTopBanner_base = _shopTopBanner;
/**
* 基础模板
*/
exports.basisTemplateAsync = shopId => {
return co(function * () {
let data = { shopTopBanner_base: {}};
const ResourceHandler = {
shopTopBanner_base: _shopTopBanner_base
};
let shops = yield api.shopsDecoratorListAsync(shopId);
if (!shops.code || shops.code !== 200 || _.isEmpty(shops.data.list)) {
return data;
}
shops.data.list.forEach(shop => {
let resourceHandlerName = shop.resource_name;
if (!ResourceHandler[resourceHandlerName]) {
return;
}
let resourceData = ResourceHandler[resourceHandlerName](_getResourceData(shop));
switch (resourceHandlerName) {
case 'shopTopBanner_base':
{
data[resourceHandlerName] = resourceData;
break;
}
}
});
return data;
})();
};
exports.getShopBannerAsync = (shopId) => {
return api.shopBannerAsync(shopId).then((result) => {
if (result.code === 200) {
return _.includes(result.data.banner, DEFAULT_IMG) ? '' : result.data.banner;
} else {
return null;
}
});
};
exports.queryShopByBrandIdAsync = (bid) => {
return co(function * () {
let result = yield api.queryShopsByBrandId(bid);
if (_.get(result, 'code') !== 200) {
return false;
}
return _.some(_.get(result, 'data', []), i => _.has(i, 'shop_id'));
})();
};