shop-service.js
4.23 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* 店铺相关数据处理
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 2016/07/14
**/
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const camelCase = global.yoho.camelCase;
const BrandService = require('./brand-service');
const ShopApi = require('./shop-api');
const _ = require('lodash');
/**
* 处理店铺默认菜单
* @param domain
* @returns {*[]}
*/
function shopMenu(domain) {
let menus = [
{id: 'index', name: '店铺首页', href: `/product/shop/${domain}`},
{id: 'all', name: '全部商品', href: `/product/shop/${domain}/list`, icon: ''},
{id: 'hot', name: '人气单品', href: `/product/shop/${domain}/list?order=s_n_desc`},
{id: 'new', name: '新品上架', href: `/product/shop/${domain}/list?order=s_t_desc`}
];
return menus;
}
/**
* 处理店铺首页资源位数据
* @param data
* @returns {{}}
*/
function resourceDataHandle(data) {
let resource = {};
if (data && _.isArray(data)) {
data.forEach(d => {
resource[d.resourceName] = JSON.parse(d.resourceData);
});
}
return resource;
}
const ShopService = {
/**
* 获取店铺信息
* @param shopId
* @param uid
*/
getShopIntro(shopId, uid) {
return co(function*() {
let shopIntro = yield ShopApi.getShopIntro(shopId, uid);
if (shopIntro && shopIntro.code === 200) {
return camelCase(shopIntro.data);
} else {
return {};
}
})();
},
/**
* 获取店铺装修
* @param shopId
*/
getShopDecorator(shopId) {
return co(function*() {
let data = yield ShopApi.getShopDecorator(shopId);
if (data && data.code === 200) {
return camelCase(data.data);
} else {
return {};
}
})();
},
/**
* 获取店铺二级分类
* @param shopId
*/
getShopSecondSorts(shopId) {
return co(function*() {
let data = yield ShopApi.getShopSorts(shopId);
if (data && data.code === 200) {
let sorts = camelCase(data.data);
return sorts.sort((a, b) => {
return a.sub.length >= b.sub.length;
});
} else {
return [];
}
})();
},
/**
* 获取店铺首页数据(头部、装修)
* @param domain
* @param uid
*/
getShopHeadData(domain, uid) {
return co(function*() {
let domainInfo = yield BrandService.getDomainInfo(domain);
let info = {
brandId: domainInfo.id,
shopId: ''
};
if (domainInfo.shopId) {
let shopId = domainInfo.shopId;
let shopIntro = yield ShopService.getShopIntro(shopId, uid);
info.shopId = shopId;
info.name = shopIntro.shopName;
info.info = shopIntro.shopIntro;
info.btnName = '店铺介绍';
info.isFavorite = shopIntro.isFavorite === 'Y';
let shopData = yield Promise.all([ShopService.getShopDecorator(shopId),
ShopService.getShopSecondSorts(shopId)]);
let shopList = shopData[0];
let sorts = shopData[1];
let resources = resourceDataHandle(shopList.list);
info.sorts = sorts;
if (resources.shopTopBanner) { // eslint-disable-line
info.banner = resources.shopTopBanner.detailSrc; // eslint-disable-line
}
info.resources = resources;
info.menus = shopMenu(domain);
} else {
let brandId = domainInfo.id;
let brandInfo = yield BrandService.getBrandInfo(brandId, uid);
info.name = brandInfo.brandName;
info.info = brandInfo.brandIntro;
info.btnName = '品牌介绍';
info.isFavorite = brandInfo.isFavorite === 'Y';
info.banner = domainInfo.brandBanner;
}
return info;
})();
}
};
module.exports = ShopService;