Authored by 郭成尧

goods拆分

... ... @@ -35,16 +35,16 @@ exports.index = (req, res) => {
responseData.devEnv = true;
// 假数据输出
res.render('sale/index', Object.assign(responseData, simulation.saleIndex()));
// res.render('sale/index', Object.assign(responseData, simulation.saleIndex()));
// 真实数据输出
/* return sale.getSaleIndexDate().then(result => {
sale.getSaleIndexData().then(result => {
responseData.result = result;
responseData.resultShow = JSON.stringify(result, null, 4);
res.render('sale/index', responseData);
});*/
});
})
.catch(() => {
res.render('error', { devEnv: true, pageErr: true });
... ... @@ -175,3 +175,21 @@ exports.newSale = (req, res) => {
res.render('error', { devEnv: true, pageErr: true });
});
};
/**
* 获取商品列表
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
exports.getGoodsList = (req, res) => {
return sale.getSaleGoodsData().then(result => {
let responseData = {};
responseData.devEnv = true;
responseData.result = result;
responseData.footerTop = false;
res.render('goods', responseData);
});
};
... ...
... ... @@ -19,7 +19,7 @@ app.engine('.hbs', hbs({
defaultLayout: 'layout',
layoutsDir: doraemon,
partialsDir: [path.join(__dirname, 'views/partial'), `${doraemon}/partial`],
helpers: 'helpers'
helpers: require('../../library/helpers')
}));
// router
... ...
... ... @@ -2,29 +2,19 @@
* @Author: Targaryen
* @Date: 2016-05-19 10:20:08
* @Last Modified by: Targaryen
* @Last Modified time: 2016-05-21 14:39:36
* @Last Modified time: 2016-05-23 16:39:03
*/
'use strict';
const library = '../../../library';
const API = require(`${library}/api`).API;
const TARAPI = require(`${library}/tar-api`).TARAPI;
const sign = require(`${library}/sign`);
const api = new API();
const tarApi = new TARAPI();
const _ = require('lodash');
/**
* 处理 sale 首页原始数据
* @param {Object} origin [原始数据]
* @return {Object} [结果]
*/
const handleSaleIndexData = (origin) => {
var dest = {};
dest = origin;
return dest;
};
/**
* 处理商品列表数据
* @param {[type]} origin [description]
* @return {[type]} [description]
... ... @@ -131,7 +121,7 @@ const handleSaleFilterData = (origin) => {
};
/**
* 处理分类筛选数据
* 处理页面左侧分类筛选数据
* @return {[type]} [description]
*/
const handleSaleSortData = (origin) => {
... ... @@ -152,7 +142,19 @@ const handleSaleSortData = (origin) => {
};
/**
* 返回商品列表 promise 对象
* 处理折扣专区活动数据
* @param {[type]} origin [description]
* @return {[type]} [description]
*/
const handleSaleActivityData = (origin) => {
var dest = {};
dest = origin;
return dest;
};
/**
* 获取商品列表 promise 对象
* @return {[type]} [description]
*/
const getSaleGoodsList = () => {
... ... @@ -167,7 +169,7 @@ const getSaleGoodsList = () => {
};
/**
* 获取分类信息 promise 对象
* 获取左侧分类信息数据 promise 对象
* @return {[type]} [description]
*/
const getSortList = () => {
... ... @@ -178,22 +180,60 @@ const getSortList = () => {
};
/**
* 获取 Sale 首页数据
* 获取折扣专区活动列表 promise 对象
* @return {[type]} [description]
*/
exports.getSaleIndexDate = () => {
return api.get('', sign.apiSign({
const getSaleActivityList = () => {
return tarApi.getActivity('', sign.apiSign({
method: 'app.activity.get',
sort: 2,
plateform: 2
})).then(result => {
}));
};
/**
* 获取 Sale 首页数据
* @return {[type]} [description]
*/
exports.getSaleIndexData = () => {
return api.all([getSaleActivityList(), getSaleGoodsList()]).then(result => {
var finalResult = {};
if (result[0].code === 200) {
finalResult.activity = handleSaleActivityData(result[0].data);
}
if (result[1].code === 200) {
if (!_.isEmpty(result[1].data.product_list)) { // 处理商品列表数据
finalResult.saleCategory = {};
finalResult.saleCategory.saleImage = [];
finalResult.saleCategory.saleImage.push(handleSaleGoodsListData(result[1].data.product_list));
}
}
return finalResult;
});
};
/**
* 获取商品列表数据
* @return {[type]} [description]
*/
exports.getSaleGoodsData = () => {
return getSaleGoodsList().then(result => {
let finalResult = {};
return handleSaleIndexData(result);
if (result.code === 200) {
finalResult.goods = handleSaleGoodsListData(result.data.product_list);
}
return finalResult;
});
};
/**
* 获取页面全部信息
* 获取其他三个页面全部数据
* @return {[type]} [description]
*/
exports.getSaleData = () => {
... ...
... ... @@ -24,6 +24,7 @@ router.get('/sale/discount', sale.discount);
router.get('/sale/vip', sale.vip);
router.get('/sale/breakingYards', sale.breakingYards);
router.get('/sale/newSale', sale.newSale);
router.get('/sale/goods', sale.getGoodsList);
// 奥特莱斯routers
router.get('/outlets/index', outlets.index);
... ...
{{# result}}
{{#each goods}}
<div class="good-info" data-skn="{{skn}}" data-from="{{from}}">
<div class="tag-container clearfix">
{{# tags}}
{{# isNew}}
<span class="good-tag new-tag">NEW</span>
{{/ isNew}}
{{# isReNew}}
<span class="good-tag renew-tag">再到着</span>
{{/ isReNew}}
{{# isSale}}
<span class="good-tag sale-tag">SALE</span>
{{/ isSale}}
{{# isNewFestival}}
<span class="good-tag new-festival-tag">新品节</span>
{{/ isNewFestival}}
{{# isLimit}}
<span class="good-tag limit-tag">限量商品</span>
{{/ isLimit}}
{{# isYearEndPromotion}}
<span class="good-tag yep-tag">年终大促</span>
{{/ isYearEndPromotion}}
{{# isYearMidPromotion}}
<span class="good-tag ymp-tag">年中热促</span>
{{/ isYearMidPromotion}}
{{/ tags}}
</div>
<div class="good-detail-img">
<a class="good-thumb" href="{{url}}" target="_blank">
<img class="lazy" data-original="{{image thumb 235 315}}">
</a>
{{# isFew}}
<p class="few-tag">即将售罄</p>
{{/ isFew}}
{{#if showColBtn}}
<span class="col-btn iconfont{{#if coled}} coled{{/if}}">&#xe616;</span>
{{/if}}
</div>
<div class="good-detail-text">
<a href="{{url}}" target="_blank">{{{name}}}</a>
{{#brand}}
<p class="brand">
<a href="{{url}}">{{name}}</a>
</p>
{{/brand}}
<p class="price">
{{# marketPrice}}
<span class="market-price">¥{{.}}</span>
{{/ marketPrice}}
<span class="sale-price{{#unless marketPrice}}prime-cost{{/unless}}">
¥{{salePrice}}
</span>
</p>
</div>
</div>
{{/each}}
{{/ result}}
\ No newline at end of file
... ...
... ... @@ -12,7 +12,7 @@ const isTest = process.env.NODE_ENV === 'test';
module.exports = {
port: 6002,
domains: {
api: 'http://192.168.102.205:8080/gateway',
api: 'http://192.168.102.205:8080/gateway', // 192.168.102.205:8080 testapi.yoho.cn:28078
service: 'http://testservice.yoho.cn:28077',
search: 'http://192.168.10.64:8080/yohosearch/'
},
... ...
/*
* @Author: Targaryen
* @Date: 2016-05-23 15:27:46
* @Last Modified by: Targaryen
* @Last Modified time: 2016-05-23 16:40:48
*/
'use strict';
const rp = require('request-promise');
const qs = require('querystring');
const md5 = require('md5');
const _ = require('lodash');
const log = require('./logger');
const cache = require('./cache');
const Timer = require('./timer');
const config = require('../config/common');
const api = config.domains.api;
const serviceApi = config.domains.service;
const searchApi = config.domains.search;
let ApiUrl;
class TARAPI {
constructor() {
ApiUrl = api;
}
/**
* 获取请求 ID
*/
_getReqId(options) {
return md5(`${options.url}?${qs.stringify(options.qs || options.form)}`);
}
/**
* 调用接口
*/
_requestFromAPI(options, cacheOption, reqId) {
let timer = new Timer();
timer.put('getApi');// 统计时间开始
log.info(`get api: ${options.url}?${qs.stringify(options.qs)}`);
return rp(options).then((result) => {
let duration = timer.put('getApi');// 统计时间结束
log.info(`get api success: use: ${duration}ms`);
if (config.useCache && cacheOption) {
reqId = reqId || this._getReqId(options);
// 数据校验无误,写缓存, 否则返回 Slave 缓存服务器的数据
if (result && result.code) {
let cacheTime = _.isNumber(cacheOption) ? cacheOption : 60;
cache.set(`apiCache:${reqId}`, result, cacheTime);
cache.setSlave(`apiCache:${reqId}`, result, cacheTime);
} else {
return this._requestFromCache(options, true);
}
}
return result;
}).catch((error)=>{
let duration = timer.put('getApi');// 统计时间结束
log.error(`get api fail: use: ${duration}ms, statusCode: ${error.statusCode}, error: ${error.message}`);
// 使用缓存的时候,读取二级缓存
if (config.useCache) {
return this._requestFromCache(options, true);
}
return Promise.reject({
error: '接口调用失败'
});
});
}
/**
* 读取缓存
* @param {[object]} options
* @param {[boolean]} slave true: 读取二级缓存
* @return {[type]}
*/
_requestFromCache(options, slave) {
let reqId = this._getReqId(options);
let getCache = slave ? cache.getFromSlave : cache.get;
log.info(`get cache: ${reqId}, url: ${options.url}?${qs.stringify(options.qs)}`);
return getCache(`apiCache:${reqId}`).then((result) => {
if (!_.isNil(result)) {
try {
result = JSON.parse(result);
} finally {
log.info(slave ? 'get slave cache success' : 'get master cache success');
return result;
}
}
if (!slave) {
return this._requestFromAPI(options, true, reqId);
}
}).catch(() => {
log.error(slave ? 'get slave cache fail' : 'get master cache fail');
if (!slave) {
return this._requestFromAPI(options, true, reqId);
}
});
}
/**
* 使用 get 请求获取接口
* @param {[string]} url
* @param {[object]} data
* @param {[bool or number]} cacheOption 使用数字时,数字表示缓存时间
* @return {[type]}
*/
get(url, data, cacheOption) {
let options = {
url: `${ApiUrl}${url}`,
qs: data,
json: true,
timeout: 3000
};
// 从缓存获取数据
if (config.useCache && cacheOption) {
return this._requestFromCache(options);
}
return this._requestFromAPI(options, cacheOption);
}
/**
* 使用 get 请求获取接口
* @param {[string]} url
* @param {[object]} data
* @param {[bool or number]} cacheOption 使用数字时,数字表示缓存时间
* @return {[type]}
*/
getActivity(url, data, cacheOption) {
let options = {
url: `http://testapi.yoho.cn:28078/gateway${url}`,
qs: data,
json: true,
timeout: 3000
};
// 从缓存获取数据
if (config.useCache && cacheOption) {
return this._requestFromCache(options);
}
return this._requestFromAPI(options, cacheOption);
}
/**
* post
* @param url String
* @param data Obejct
*/
post(url, data) {
let options = {
url: `${ApiUrl}${url}`,
form: data,
method: 'post',
json: true,
timeout: 3000
};
return this._requestFromAPI(options);
}
all(list) {
if (_.isArray(list)) {
return Promise.all(list);
}
throw Error('the parameters of api all method should be Array!');
}
}
class ServiceAPI extends TARAPI {
constructor() {
super();
ApiUrl = serviceApi;
}
}
class SearchAPI extends TARAPI {
constructor() {
super();
ApiUrl = searchApi;
}
}
exports.TARAPI = TARAPI;
exports.ServiceAPI = ServiceAPI;
exports.SearchAPI = SearchAPI;
... ...