shop-service.js
2.56 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
/**
* 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 _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: result[0].title || '',
list: result
};
};
/**
* 基础模板
*/
exports.basisTemplateAsync = shopId => {
return co(function * () {
let data = {signboard: {}, shopTopBanner: {}};
const ResourceHandler = {
shopTopBanner: _shopTopBanner,
signboard: _signboard
};
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':
case 'signboard':
{
data[resourceHandlerName] = resourceData;
break;
}
}
});
return data;
})();
};