Authored by 郭成尧

'format-to-model'

@@ -12,233 +12,6 @@ const _ = require('lodash'); @@ -12,233 +12,6 @@ const _ = require('lodash');
12 const helpers = global.yoho.helpers; 12 const helpers = global.yoho.helpers;
13 13
14 /** 14 /**
15 - * 组织店铺页面数据  
16 - * @param {array} data 接口返回的店铺页所需数据  
17 - * @param {int} shopId 店铺id  
18 - * @param {int} isApp app版本  
19 - * @return array  
20 - */  
21 -const _formShopData = (data, shopId, isApp) => {  
22 - let formatData = {  
23 - shopId: shopId,  
24 - appVersion: isApp  
25 - },  
26 - single = false;  
27 - let brandId;  
28 -  
29 - // 组织店铺装修  
30 - if (data.decorator) {  
31 - _.forEach(data.decorator.list, (floor) => {  
32 - let resData = JSON.parse(floor.resourceData);  
33 -  
34 - floor[_.camelCase(floor.resourceName)] = true;  
35 -  
36 - // 店铺banner  
37 - if (floor.shopTopBannerApp) {  
38 - formatData.branerImg = resData[0].shopSrc;  
39 - }  
40 -  
41 - // 资源位小图 接口返回两组,取每组第一张  
42 - if (floor.oneRowTwoColImagesApp) {  
43 - let spring = [];  
44 -  
45 - _.forEach(resData, (item) => {  
46 - if (item.data) {  
47 - spring.push({  
48 - url: isApp ? listModel.modifyAppUrl(item.data[0], shopId) :  
49 - listModel.modifyWapUrl(item.data[0], shopId),  
50 - springType: helpers.image(item.data[0].src)  
51 - });  
52 - }  
53 - });  
54 -  
55 - formatData.spring = spring;  
56 - }  
57 -  
58 -  
59 - // 店铺品牌一览  
60 - if (floor.brandBrowse) {  
61 - let brandNumber = resData.length;  
62 - let brand = {  
63 - list: []  
64 - };  
65 -  
66 - brandId = '';  
67 -  
68 - // 少于2个不展示 单品店:单品店根据品牌id查询  
69 - if (brandNumber < 2) {  
70 - brandId = formatData.brand = resData[0].id;  
71 - single = true;  
72 - } else {  
73 - _.forEach(resData, (item) => {  
74 - if (item.brandDomain) {  
75 - brand.list.push({  
76 - url: helpers.urlFormat('', '', item.brandDomain) + (isApp ? `?openby:yohobuy={"action":"go.brand","params":{"shop_id":${shopId},"brand_id":${item.id}}}` : ''), //eslint-disable-line  
77 - img: helpers.image(item.brandIco, 640, 400),  
78 - brandName: item.brandName  
79 - });  
80 - brandId += item.id + ',';  
81 - }  
82 - });  
83 -  
84 - formatData.brandList = brand;  
85 -  
86 - // 大于5个返回更多品牌链接  
87 - if (brandNumber > 5) {  
88 - formatData.brandList.url = helpers.urlFormat('/product/index/allBrand', {  
89 - shop_id: shopId  
90 - });  
91 - }  
92 - }  
93 - }  
94 -  
95 - // 资源位大图幻灯  
96 - if (floor.largeSlideImgApp) {  
97 - let bannerTop = {  
98 - data: []  
99 - };  
100 -  
101 - _.forEach(resData, (item) => {  
102 - if (item.data[0]) {  
103 - bannerTop.data.push({  
104 - url: isApp ? listModel.modifyAppUrl(item.data[0], shopId) :  
105 - listModel.modifyWapUrl(item.data[0], shopId),  
106 - src: item.data[0].src  
107 - });  
108 - }  
109 - });  
110 -  
111 - formatData.bannerTop = bannerTop;  
112 - }  
113 -  
114 - // 热门品类  
115 - if (floor.recommendApp) {  
116 - let hotCategory = {  
117 - data: {  
118 - title: {  
119 - title: '热门品类'  
120 - },  
121 - list: []  
122 - }  
123 - };  
124 -  
125 - _.forEach(resData, (cate) => {  
126 - hotCategory.data.list.push({  
127 - url: cate.url,  
128 - src: helpers.image(cate.src)  
129 - });  
130 - });  
131 -  
132 - formatData.hotCategory = hotCategory;  
133 - }  
134 -  
135 - // 人气单品  
136 - if (floor.hotProductsApp) {  
137 - let goods = [];  
138 -  
139 - _.forEach(resData, (item) => {  
140 - let url = '//item.yohobuy.com/product/pro_';  
141 -  
142 - if (!item.cnAlphabet) {  
143 - item.cnAlphabet = 'goods.html';  
144 - }  
145 - url += item.productId + '_' + item.goodsId + '/' + item.cnAlphabet + '.html';  
146 - goods.push({  
147 - url: url + (isApp ? `?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":${item.productSkn}}}` : ''),//eslint-disable-line  
148 - img: helpers.image(item.src, 235, 314),  
149 - productName: item.productName,  
150 - salesPrice: item.salesPrice,  
151 - presentPrice: item.salesPrice  
152 - });  
153 - });  
154 -  
155 - formatData.hotList = goods;  
156 - }  
157 - });  
158 - }  
159 -  
160 - // 店铺基本信息  
161 - if (data.shopInfo) {  
162 - let allGoodsParam = {  
163 - title: '全部商品'  
164 - };  
165 -  
166 - formatData = _.assign({  
167 - logoImg: data.shopInfo.shopLogo,  
168 - storeName: (data.shopInfo.isShowShopName === 'Y') ? data.shopInfo.shopName : '',  
169 - collect: data.shopInfo.isFavorite === 'Y',  
170 - url: helpers.urlFormat('', {  
171 - shop_id: shopId  
172 - }, 'search'), // 搜索链接  
173 - more_url: helpers.urlFormat('', {  
174 - shop_id: shopId,  
175 - order: 's_n_d',  
176 - title: '人气单品'  
177 - }, 'search') // 人气单品的链接  
178 - }, formatData);  
179 -  
180 - if (single) { // 单品点  
181 - allGoodsParam.brand = brandId;  
182 - } else { // 店铺id  
183 - formatData.shopId = shopId;  
184 - allGoodsParam.shopId = shopId;  
185 - }  
186 -  
187 - if (!isApp) {  
188 - formatData = _.assign({  
189 - allGoods: helpers.urlFormat('', allGoodsParam, 'search'),  
190 - shopIntroHref: helpers.urlFormat('/product/index/intro', {  
191 - shop_id: shopId  
192 - }) // 店铺简介页地址  
193 - }, formatData);  
194 - } else {  
195 - formatData = _.assign({  
196 - allGoods: `${helpers.urlFormat('', allGoodsParam, 'search')}&openby:yohobuy={"action":"go.list","params":{"title":"全部商品", "actiontype":"0","shop_id":${shopId},"page":"1"}}`,//eslint-disable-line  
197 - shopIntroHref: helpers.urlFormat('/product/index/intro', {  
198 - shop_id: shopId,  
199 - app_version: isApp  
200 - }),  
201 - more_url: formatData.more_url + `?openby:yohobuy{"action":"go.list","params":{"shop_id":${shopId},"title":"人气单品"}}`//eslint-disable-line  
202 - }, formatData);  
203 - }  
204 - }  
205 -  
206 - // 店铺下面的所有分类  
207 - if (data.shopCategory) {  
208 - let total = data.shopCategory.length;  
209 - let shopCategory = {  
210 - list: []  
211 - };  
212 -  
213 - if (total > 6) {  
214 - shopCategory.url = helpers.urlFormat('/product/index/category', {  
215 - shop_id: shopId  
216 - });  
217 - } else {  
218 - _.forEach(data.shopCategory, (item) => {  
219 - shopCategory.list.push({  
220 - url: helpers.urlFormat('', {  
221 - shop_id: shopId,  
222 - sort: item.relationParameter.sort  
223 - }, 'search'),  
224 - categoryId: item.categoryId,  
225 - name: item.categoryName  
226 - });  
227 - });  
228 - }  
229 -  
230 - formatData.shopCategory = shopCategory;  
231 - }  
232 -  
233 - if (single) {  
234 - formatData.favId = shopId;  
235 - formatData.shopId = '';  
236 - }  
237 -  
238 - return formatData;  
239 -};  
240 -  
241 -/**  
242 * 新店铺首页 15 * 新店铺首页
243 * @param {object} req 16 * @param {object} req
244 * @param {int} shopId 店铺id 17 * @param {int} shopId 店铺id
@@ -246,29 +19,30 @@ const _formShopData = (data, shopId, isApp) => { @@ -246,29 +19,30 @@ const _formShopData = (data, shopId, isApp) => {
246 * @param {string} isApp app版本 19 * @param {string} isApp app版本
247 */ 20 */
248 const _getShopData = (req, shopId, uid, isApp) => { 21 const _getShopData = (req, shopId, uid, isApp) => {
249 - let data = {}; 22 + let shopData = {};
250 let channel = req.yoho.channel; 23 let channel = req.yoho.channel;
251 24
252 return Promise.all([listModel.getShopDecorator(shopId), listModel.getShopInfo(shopId, uid)]).then((result) => { 25 return Promise.all([listModel.getShopDecorator(shopId), listModel.getShopInfo(shopId, uid)]).then((result) => {
253 - data = { 26 + shopData = {
254 decorator: result[0], // 店铺装修资源数据 27 decorator: result[0], // 店铺装修资源数据
255 shopInfo: result[1] // 店铺信息 28 shopInfo: result[1] // 店铺信息
256 }; 29 };
257 30
258 // 店铺使用基础模板,返回品牌页面 31 // 店铺使用基础模板,返回品牌页面
259 - if (data.shopInfo.shopTemplateType && data.shopInfo.shopTemplateType === '1') { 32 + if (shopData.shopInfo.shopTemplateType && shopData.shopInfo.shopTemplateType === '1') {
260 return { 33 return {
261 - goBrand: data.shopInfo 34 + goBrand: shopData.shopInfo
262 }; 35 };
263 } 36 }
264 37
265 // 店铺分类 38 // 店铺分类
266 return listModel.getShopCategory(shopId, channel).then((shopCategory) => { 39 return listModel.getShopCategory(shopId, channel).then((shopCategory) => {
267 - data = _.assign({ 40 + shopData = _.assign({
268 shopCategory: shopCategory 41 shopCategory: shopCategory
269 - }, data); 42 + }, shopData);
270 43
271 - return _formShopData(data, shopId, isApp); // 组织楼层数据 44 + //noinspection JSCheckFunctionSignatures
  45 + return listModel.formShopData(shopData, shopId, isApp); // 组织楼层数据
272 }); 46 });
273 }); 47 });
274 }; 48 };
@@ -47,6 +47,236 @@ const modifyAppUrl = (origin, shopId) => { @@ -47,6 +47,236 @@ const modifyAppUrl = (origin, shopId) => {
47 } 47 }
48 }; 48 };
49 49
  50 +/**
  51 + * 组织店铺页面数据
  52 + * @param {array} data 接口返回的店铺页所需数据
  53 + * @param {int} shopId 店铺id
  54 + * @param {int} isApp app版本
  55 + * @return array
  56 + */
  57 +const formShopData = (data, shopId, isApp) => {
  58 + let formatData = {
  59 + shopId: shopId,
  60 + appVersion: isApp
  61 + },
  62 + single = false;
  63 + let brandId;
  64 +
  65 + // 组织店铺装修
  66 + if (data.decorator) {
  67 +
  68 + _.forEach(data.decorator.list, floor => {
  69 + let resData = JSON.parse(floor.resourceData);
  70 +
  71 + if (floor.resourceName) {
  72 + floor[_.camelCase(floor.resourceName)] = true;
  73 + }
  74 +
  75 + // 店铺banner
  76 + if (floor.shopTopBannerApp) {
  77 + formatData.branerImg = resData[0].shopSrc;
  78 + }
  79 +
  80 + // 资源位小图 接口返回两组,取每组第一张
  81 + if (floor.oneRowTwoColImagesApp) {
  82 +
  83 + let spring = [];
  84 +
  85 + _.forEach(resData, (item) => {
  86 + if (item.data) {
  87 + spring.push({
  88 + url: isApp ? modifyAppUrl(item.data[0], shopId) :
  89 + modifyWapUrl(item.data[0], shopId),
  90 + springType: helpers.image(item.data[0].src)
  91 + });
  92 + }
  93 + });
  94 +
  95 + formatData.spring = spring;
  96 + }
  97 +
  98 + // 店铺品牌一览
  99 + if (floor.brandBrowse) {
  100 + let brandNumber = resData.length;
  101 + let brand = {
  102 + list: []
  103 + };
  104 +
  105 + brandId = '';
  106 +
  107 + // 少于2个不展示 单品店:单品店根据品牌id查询
  108 + if (brandNumber < 2) {
  109 + brandId = formatData.brand = resData[0].id;
  110 + single = true;
  111 + } else {
  112 + _.forEach(resData, (item) => {
  113 + if (item.brandDomain) {
  114 + brand.list.push({
  115 + url: helpers.urlFormat('', '', item.brandDomain) + (isApp ? `?openby:yohobuy={"action":"go.brand","params":{"shop_id":${shopId},"brand_id":${item.id}}}` : ''), //eslint-disable-line
  116 + img: helpers.image(item.brandIco, 640, 400),
  117 + brandName: item.brandName
  118 + });
  119 + brandId += item.id + ',';
  120 + }
  121 + });
  122 +
  123 + formatData.brandList = brand;
  124 +
  125 + // 大于5个返回更多品牌链接
  126 + if (brandNumber > 5) {
  127 + formatData.brandList.url = helpers.urlFormat('/product/index/allBrand', {
  128 + shop_id: shopId
  129 + });
  130 + }
  131 + }
  132 + }
  133 +
  134 + // 资源位大图幻灯
  135 + if (floor.largeSlideImgApp) {
  136 + let bannerTop = {
  137 + data: []
  138 + };
  139 +
  140 + _.forEach(resData, (item) => {
  141 + if (item.data[0]) {
  142 + bannerTop.data.push({
  143 + url: isApp ? modifyAppUrl(item.data[0], shopId) :
  144 + modifyWapUrl(item.data[0], shopId),
  145 + src: item.data[0].src
  146 + });
  147 + }
  148 + });
  149 +
  150 + formatData.bannerTop = bannerTop;
  151 + }
  152 +
  153 + // 热门品类
  154 + if (floor.recommendApp) {
  155 + let hotCategory = {
  156 + data: {
  157 + title: {
  158 + title: '热门品类'
  159 + },
  160 + list: []
  161 + }
  162 + };
  163 +
  164 + _.forEach(resData, (cate) => {
  165 + hotCategory.data.list.push({
  166 + url: cate.url,
  167 + src: helpers.image(cate.src)
  168 + });
  169 + });
  170 +
  171 + formatData.hotCategory = hotCategory;
  172 + }
  173 +
  174 + // 人气单品
  175 + if (floor.hotProductsApp) {
  176 + let goods = [];
  177 +
  178 + _.forEach(resData, (item) => {
  179 + let url = '//item.yohobuy.com/product/pro_';
  180 +
  181 + if (!item.cnAlphabet) {
  182 + item.cnAlphabet = 'goods.html';
  183 + }
  184 + url += item.productId + '_' + item.goodsId + '/' + item.cnAlphabet + '.html';
  185 + goods.push({
  186 + url: url + (isApp ? `?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":${item.productSkn}}}` : ''),//eslint-disable-line
  187 + img: helpers.image(item.src, 235, 314),
  188 + productName: item.productName,
  189 + salesPrice: item.salesPrice,
  190 + presentPrice: item.salesPrice
  191 + });
  192 + });
  193 +
  194 + formatData.hotList = goods;
  195 + }
  196 + });
  197 + }
  198 +
  199 + // 店铺基本信息
  200 + if (data.shopInfo) {
  201 + let allGoodsParam = {
  202 + title: '全部商品'
  203 + };
  204 +
  205 + formatData = _.assign({
  206 + logoImg: data.shopInfo.shopLogo,
  207 + storeName: (data.shopInfo.isShowShopName === 'Y') ? data.shopInfo.shopName : '',
  208 + collect: data.shopInfo.isFavorite === 'Y',
  209 + url: helpers.urlFormat('', {
  210 + shop_id: shopId
  211 + }, 'search'), // 搜索链接
  212 + more_url: helpers.urlFormat('', {
  213 + shop_id: shopId,
  214 + order: 's_n_d',
  215 + title: '人气单品'
  216 + }, 'search') // 人气单品的链接
  217 + }, formatData);
  218 +
  219 + if (single) { // 单品点
  220 + allGoodsParam.brand = brandId;
  221 + } else { // 店铺id
  222 + formatData.shopId = shopId;
  223 + allGoodsParam.shopId = shopId;
  224 + }
  225 +
  226 + if (!isApp) {
  227 + formatData = _.assign({
  228 + allGoods: helpers.urlFormat('', allGoodsParam, 'search'),
  229 + shopIntroHref: helpers.urlFormat('/product/index/intro', {
  230 + shop_id: shopId
  231 + }) // 店铺简介页地址
  232 + }, formatData);
  233 + } else {
  234 + formatData = _.assign({
  235 + allGoods: `${helpers.urlFormat('', allGoodsParam, 'search')}&openby:yohobuy={"action":"go.list","params":{"title":"全部商品", "actiontype":"0","shop_id":${shopId},"page":"1"}}`,//eslint-disable-line
  236 + shopIntroHref: helpers.urlFormat('/product/index/intro', {
  237 + shop_id: shopId,
  238 + app_version: isApp
  239 + }),
  240 + more_url: formatData.more_url + `?openby:yohobuy{"action":"go.list","params":{"shop_id":${shopId},"title":"人气单品"}}`//eslint-disable-line
  241 + }, formatData);
  242 + }
  243 + }
  244 +
  245 + // 店铺下面的所有分类
  246 + if (data.shopCategory) {
  247 + let total = data.shopCategory.length;
  248 + let shopCategory = {
  249 + list: []
  250 + };
  251 +
  252 + if (total > 6) {
  253 + shopCategory.url = helpers.urlFormat('/product/index/category', {
  254 + shop_id: shopId
  255 + });
  256 + } else {
  257 + _.forEach(data.shopCategory, (item) => {
  258 + shopCategory.list.push({
  259 + url: helpers.urlFormat('', {
  260 + shop_id: shopId,
  261 + sort: item.relationParameter.sort
  262 + }, 'search'),
  263 + categoryId: item.categoryId,
  264 + name: item.categoryName
  265 + });
  266 + });
  267 + }
  268 +
  269 + formatData.shopCategory = shopCategory;
  270 + }
  271 +
  272 + if (single) {
  273 + formatData.favId = shopId;
  274 + formatData.shopId = '';
  275 + }
  276 +
  277 + return formatData;
  278 +};
  279 +
50 /* 多品牌店铺列表数据信息处理*/ 280 /* 多品牌店铺列表数据信息处理*/
51 const _processBrandShops = (list) => { 281 const _processBrandShops = (list) => {
52 const formatDat = []; 282 const formatDat = [];
@@ -325,6 +555,7 @@ const getShopBrands = (shopId) => { @@ -325,6 +555,7 @@ const getShopBrands = (shopId) => {
325 module.exports = { 555 module.exports = {
326 modifyWapUrl, 556 modifyWapUrl,
327 modifyAppUrl, 557 modifyAppUrl,
  558 + formShopData,
328 getBrandLogoByDomain, 559 getBrandLogoByDomain,
329 getBrandIntro, 560 getBrandIntro,
330 getBrandShops, 561 getBrandShops,