Authored by 陈峰
Committed by 陈轩

秒杀列表页接口已经集成

/**
* 一键下单, 直接结算
* @author xuan.chen@yoho.cn
*/
'use strict';
class OneClick {
// TODO
}
module.exports = {
OneClick
};
... ...
/**
* sub app cart
* @author: xuan.chen@yoho.cn<xuan.chen@yoho.cn>
* @date: 2016/09/26
*/
var express = require('express'),
path = require('path'),
hbs = require('express-handlebars');
var app = express();
// set view engin
var doraemon = path.join(__dirname, '../../doraemon/views'); // parent view root
app.on('mount', function(parent) {
delete parent.locals.settings; // 不继承父 App 的设置
Object.assign(app.locals, parent.locals);
});
app.set('views', path.join(__dirname, 'views/action'));
app.engine('.hbs', hbs({
extname: '.hbs',
defaultLayout: 'layout',
layoutsDir: doraemon,
partialsDir: [path.join(__dirname, 'views/partial'), `${doraemon}/partial`],
helpers: global.yoho.helpers
}));
// router
app.use(require('./router'));
module.exports = app;
... ...
/**
* router of sub app cart
* @author: xuan.chen@yoho.cn<xuan.chen@yoho.cn>
* @date: 2016/09/26
*/
'use strict';
const router = require('express').Router(); //eslint-disable-line
const cRoot = './controllers';
// Your controller here
module.exports = router;
... ...
... ... @@ -5,104 +5,114 @@
*/
'use strict';
const mRoot = '../models';
const headerModel = require('../../../doraemon/models/header');
const seckillModel = require(`${mRoot}/seckill`);
/**
* [时间缺0补0]
*/
const _timeFormat = (tick) => {
return tick < 10 ? `0${tick}` : tick;
}
/**
* [秒杀列表页面]
*/
const _index = (req, res, next) => {
const index = (req, res, next) => {
let headerData = headerModel.setNav({
navTitle: '秒杀活动',
navBtn: true,
navTitle: '秒杀活动',
navBtn: true,
}),
result = {},
hbsHelper = {
helpers: {
statusClass: (nav) => {
if (nav.over) {
return 'over';
} else if (nav.now) {
return 'now';
} else if (nav.wait) {
return 'wait';
} else {
return '';
}
}
}
};
return seckillModel.queryActivity().then((resultActivity) => {
if (resultActivity.code !== 200) {
return next();
}
result.activitys = resultActivity.data.secKillProductVoList;
let nowTime = Date.parse(new Date());
}),
result = {
products: [{
id: 1,
now: true
}, {
id: 2,
wait: true
}, {
id: 1,
over: true
}, {
id: 1,
chance: true
}],
navs: [{
day: '2016/09/1',
time: '12:00',
over: true
}, {
day: '2016/09/1',
time: '12:00',
over: true
}, {
day: '2016/09/2',
time: '12:00',
over: true
}, {
day: '2016/09/2',
time: '12:00',
over: true
}, {
day: '2016/09/09',
time: '12:00',
over: true
}, {
day: '2016/09/12',
time: '10:40',
now: true
}, {
day: '2016/09/12',
time: '10:50',
wait: true
}, {
day: '2016/09/12',
time: '11:00',
wait: true
}, {
day: '2016/09/12',
time: '11:10',
last: true,
wait: true
}]
},
hbsHelper = {
helpers: {
statusClass: (nav) => {
if (nav.over) {
return 'over';
} else if (nav.now) {
return 'now';
} else if (nav.wait) {
return 'wait';
result.activitys.forEach((activity, i) => {
let date,
hour = 0,
minute = 0;
activity.startTime *= 1000;
date = new Date(activity.startTime);
hour = date.getHours();
minute = date.getMinutes();
activity.time = `${_timeFormat(hour)}:${_timeFormat(minute)}`;
if (nowTime > activity.startTime) { // 当前时间大于这个时间段,已经开始和即将开始两种情况
if (i < result.activitys.length - 1) {
let nextTime = result.activitys[i + 1].startTime * 1000;
if (nowTime < nextTime) { // 下一个时间段与当前时间来区别是否正在抢购
activity.now = true;
activity.focus = true;
} else {
return '';
activity.over = true;
}
} else { // 大于这个时间段但是后面没有秒抢时间端了,则依然显示抢购中
activity.now = true;
}
} else {
activity.wait = true;
}
});
if (result.activitys.length && result.activitys.findIndex(activity => activity.focus) < 0) {
result.activitys[0].focus = true;
}
let focusActivity = result.activitys.find(activity => activity.focus);
return seckillModel.queryProductList(focusActivity.activityId).then((resultProducts) => {
result.products = resultProducts.data;
res.render('seckill', Object.assign({
pageHeader: headerData,
pageFooter: true,
width750: true,
times: 12
}, result, hbsHelper));
})
})
};
};
try {
let a = 1;
console.log(a);
} catch (ex) {
/**
* [xhr根据活动id获取商品列表]
*/
const getProductList = (req, res, next) => {
if (!req.xhr) {
return next();
}
res.render('seckill', Object.assign({
pageHeader: headerData,
pageFooter: true,
width750: true,
times: 12
}, result, hbsHelper));
};
let activityId = req.query.activityId;
if (!activityId) {
return next();
}
return seckillModel.queryProductList(activityId).then((resultProducts) => {
let result = {
products: resultProducts.data
}
res.render('seckill/product-list', Object.assign(result, {
layout: false
}));
})
}
module.exports = {
index: _index
index,
getProductList
};
... ...
/**
* 秒杀models
* @author: 陈峰<feng.chen@yoho.cn>
* @date: 2016/9/18
*/
'use strict';
const api = global.yoho.API;
/**
* [获取秒杀时间栏接口]
* @return {[object]}
*/
const queryActivity = () => {
return api.get('', {
method: 'app.seckill.queryActivity'
}, {
cache: true
});
}
/**
* [获取指定秒杀活动商品列表接口]
* @param {[int]} activityId [秒杀活动id]
* @return {[object]}
*/
const queryProductList = (activityId) => {
return api.get('', {
method: 'app.seckill.queryProductList',
activityId: activityId
}, {
cache: true
});
}
module.exports = {
queryActivity,
queryProductList
}
\ No newline at end of file
... ...
... ... @@ -61,5 +61,6 @@ router.get('/recommend-for-you/userCenter', recommendForYou.userCenter);
router.get('/recommend-for-you/cart', recommendForYou.cart);
router.get('/seckill', seckill.index); // 秒杀列表页
router.get('/seckill/get-product-list', seckill.getProductList); // 秒杀列表根据活动id获取商品列表
module.exports = router;
... ...
{{# products}}
{{> seckill/product}}
{{/products}}
\ No newline at end of file
... ...
<nav class="nav-list">
<ul class="nav-ul hide">
{{# navs}}
<li class="time-item {{# if now}}focus{{/if}} {{statusClass this}} {{# if @last}}last{{/if}}">
<input type="hidden" name="" class="date" value="{{day}} {{time}}">
{{# activitys}}
<li class="time-item {{# if @last}}last{{/if}} {{# if focus}}focus{{/if}} {{statusClass this}}">
<input type="hidden" name="" class="date" value="{{startTime}}">
<input type="hidden" name="" class="activityId" value="{{activityId}}">
<div class="normal">
<p class="time">{{time}}</p>
<p class="status tip-over">已开抢</p>
... ... @@ -34,6 +35,6 @@
</div>
</div>
</li>
{{/navs}}
{{/activitys}}
</ul>
</nav>
\ No newline at end of file
... ...
<a href="#" class="item">
<div class="item-img">
<img src="//img10.static.yhbimg.com/goodsimg/2016/09/06/16/010f4203952bfb27f121a8778421c14f97.jpg?imageView/2/w/235/h/314" alt="">
<img src="{{image coverUrl 235 314}}" alt="">
</div>
<div class="item-info">
<div class="item-title">
影时光Mtime 联盟之剑手机壳iPhone6/6s
{{productName}}
</div>
{{# if wait}}
<div class="item-price">
<ins>¥99</ins><del>¥234</del>
<ins>¥{{secKillPrice}}</ins><del>¥{{marketPrice}}</del>
</div>
{{/if}}
... ... @@ -17,7 +17,7 @@
<div class="item-time"><span><i class="iconfont">&#xe603;</i><time>8月31日 19:00</time>开始</span></div>
{{else}}
<div class="item-price">
<ins>¥99</ins><del>¥234</del>
<ins>¥{{secKillPrice}}</ins><del>¥{{marketPrice}}</del>
</div>
{{/if}}
... ...
... ... @@ -16,7 +16,7 @@ module.exports = {
domains: {
// api: 'http://devapi.yoho.cn:58078/',
// service: 'http://devservice.yoho.cn:58077/'
api: 'http://api.yoho.cn/',
api: 'http://dev-api.yohops.com:9999/',
service: 'http://service.yoho.cn/',
liveApi: 'http://testapi.live.yohops.com:9999/'
},
... ...
... ... @@ -5,6 +5,7 @@
*/
var $ = require('yoho-jquery'),
IScroll = require('yoho-iscroll'),
tip = require('../plugin/tip'),
seckillObj = {};
seckillObj = {
... ... @@ -40,7 +41,7 @@ seckillObj = {
that.initNav();
if (focus.length && (focus.hasClass('now') || focus.hasClass('wait'))) {
that.initTick($('.nav-ul>li.focus'));
that.initTick($el.$navUl.find('>li.focus'));
}
},
... ... @@ -53,7 +54,7 @@ seckillObj = {
$el.timeWidth = ($el.$navUl.find('>li:not(.focus)').width() + 1);
$el.focusTimeWidth = ($el.$navUl.find('>li.focus').width() + 1);
$el.$navUl.width(($el.times - 1) * $el.timeWidth + $el.focusTimeWidth).removeClass('hide');
$el.$navUl.width($el.times * $el.timeWidth + $el.focusTimeWidth).removeClass('hide');
// 只有时间段大于3个才需要定位
if ($el.$navUl.find('>li').length > 3 && $el.$navUl.find('>li.focus').length) {
... ... @@ -129,17 +130,24 @@ seckillObj = {
}
}
if ($el.currentTick) {
clearTimeout($el.currentTick);
$el.currentTick = undefined;
}
if ($(elem).hasClass('now') || $(elem).hasClass('wait')) {
// 初始化倒计时并开始计时
that.initTick(elem);
}
// 刷新商品列表
that.refreshProductList($(elem).find('input.activityId').val());
},
/**
* [刷新状态]
*/
refreshList: function(elem) {
var $el = this.el, time, nowTime, nextTime;
var $el = this.el, that = this, time, nowTime, nextTime;
// 刷新时间段状态
$el.$navUl.find('>li').each(function() {
... ... @@ -159,14 +167,32 @@ seckillObj = {
} else { // 大于这个时间段但是后面没有秒抢时间端了,则依然显示抢购中
$(this).addClass('now');
}
} else if (!$(this).hasClass('nothing')) {
} else {
$(this).addClass('wait');
}
});
// 刷新列表状态
console.log('刷新啊');
console.log(elem);
// 刷新商品列表
var focusElem = $el.$navUl.find('>li.focus');
if (focusElem.length) {
that.refreshProductList(focusElem.find('input.activityId').val());
}
},
/**
* [异步加载商品列表]
*/
refreshProductList: function(activityId) {
$.ajax({
url: '/product/seckill/get-product-list',
data: {
activityId: activityId
},
success: function(data) {
$('.product-list').html(data);
},
error: function(data) {
tip.show('网络断开连接了~');
}
})
},
/**
... ... @@ -178,14 +204,10 @@ seckillObj = {
nowTime = Date.parse(new Date()) / 1000,
offsetTime;
if ($el.currentTick) {
clearTimeout($el.currentTick);
}
if ($(elem).hasClass('now')) {
time = Date.parse(new Date($(elem).next().find('input.date').val())) / 1000;
time = $(elem).next().find('input.date').val() / 1000;
} else {
time = Date.parse(new Date($(elem).find('input.date').val())) / 1000;
time = $(elem).find('input.date').val() / 1000;
}
offsetTime = time - nowTime;
that.startTick(elem, offsetTime);
... ... @@ -196,20 +218,24 @@ seckillObj = {
*/
startTick: function(elem, offsetTime) {
var that = this,
$el = this.el,
hour = parseInt(offsetTime / (60 * 60), 10),
minute = parseInt(offsetTime % (60 * 60) / 60, 10),
second = offsetTime % 60;
$(elem).find('.tick.hour').text(hour < 0 ? '00' : (hour < 10 ? ('0' + hour) : hour));
$(elem).find('.tick.minute').text(minute < 0 ? '00' : (minute < 10 ? ('0' + minute) : minute));
$(elem).find('.tick.second').text(second < 0 ? '00' : (second < 10 ? ('0' + second) : second));
if (offsetTime <= 0) { // 结束倒计时刷新状态
that.refreshList(elem);
} else {
setTimeout(function() {
that.startTick(elem, --offsetTime);
}, 1000);
if (offsetTime) {
$(elem).find('.tick.hour').text(hour < 0 ? '00' : (hour < 10 ? ('0' + hour) : hour));
$(elem).find('.tick.minute').text(minute < 0 ? '00' : (minute < 10 ? ('0' + minute) : minute));
$(elem).find('.tick.second').text(second < 0 ? '00' : (second < 10 ? ('0' + second) : second));
if (offsetTime <= 0) { // 结束倒计时刷新状态
that.refreshList(elem);
} else {
$el.currentTick = setTimeout(function() {
that.startTick(elem, --offsetTime);
}, 1000);
}
}
}
};
... ...