Authored by hongweigao

分页、验证码

... ... @@ -5,67 +5,14 @@ const Promise = require('bluebird');
const co = Promise.coroutine;
let couponsModel = require('../models/coupons-model');
const helpers = global.yoho.helpers;
const index = (req, res)=>{
const index = (req, res, next)=>{
let uid = '8041246';
let type = req.query.type || couponsModel.UNUSED;
let page = '';
let limit = '';
co(function*() {
let coupons = yield couponsModel.getCouponsList(uid, type, page, limit);
let data = {};
data.pager = {
hasCheckAll: false,
count: coupons.pager.total || 0,
curPage: coupons.pager.page || 0,
totalPages: coupons.pager.pageTotal || 0
};
if (type === couponsModel.UNUSED) {
if (!coupons.list.length) {
data.unUseCoupons = {empty: '您没有优惠券'};
} else {
data.unUseCoupons = coupons.list;
}
data.unUse = true;
} else if (type === couponsModel.USED) {
if (!coupons.list.length) {
data.usedCoupons = {empty: '您没有优惠券'};
} else {
data.usedCoupons = coupons.list;
}
data.used = true;
} else if (type === couponsModel.INVALID) {
if (!coupons.list.length) {
data.noValidCoupons = {empty: '您没有优惠券'};
} else {
data.noValidCoupons = coupons.list;
}
data.noValid = true;
}
data.tabs = [
{
active: type === couponsModel.UNUSED ? true : false,
url: helpers.urlFormat('/home/coupons', {type: couponsModel.UNUSED}),
name: '未使用优惠券'
},
{
active: type === couponsModel.USED ? true : false,
url: helpers.urlFormat('/home/coupons', {type: couponsModel.USED}),
name: '已使用优惠券'
},
{
active: type === couponsModel.INVALID ? true : false,
url: helpers.urlFormat('/home/coupons', {type: couponsModel.INVALID}),
name: '已失效优惠券'
}
];
res.render('coupons', data);
})();
couponsModel.couponsData(uid, req.query).then(result => {
res.render('coupons', result);
}).catch(next);
};
module.exports = {
... ...
... ... @@ -16,20 +16,14 @@ const convertUnitTime = (src) => {
};
const index = (req, res, next)=>{
let $uid = '8041246';
let uid = '8041246';
let $condition = {
page: req.query.page || 1,
queryType: req.query.type || 0,
beginTime: req.query.beginTime || convertUnitTime(new Date() / 1000 - 3600 * 24 * 90)
};
co(function*() {
let data = yield currencyModel.currencyData($uid, $condition);
currencyModel.currencyData(uid, req.query).then(result => {
res.render('currency', {
content: data
content: result
});
})().catch(next);
}).catch(next);
};
module.exports = {
... ...
... ... @@ -6,7 +6,10 @@ const co = Promise.coroutine;
const UserData = require('./user-data');
const helpers = global.yoho.helpers;
const path = require('path');
// 使用 product中的分页逻辑
const pagerPath = path.join(global.appRoot, '/apps/product/models/public-handler.js');
const pager = require(pagerPath).handlePagerData;
const UNUSED = 'notuse';
const USED = 'use';
... ... @@ -16,6 +19,7 @@ const getCouponsList = (uid, type, page, limit)=>{
return co(function*() {
let couponsInfo = yield UserData.getCouponsList(uid, type, page, limit);
let result = [];
if (!couponsInfo.data.couponList) {
return result;
}
... ... @@ -71,17 +75,79 @@ const getCouponsList = (uid, type, page, limit)=>{
}
});
}
return {list: result, pager: {
total: couponsInfo.data.total,
pageTotal: couponsInfo.data.totalPageNum,
page: page
}};
let pageNum = pager(couponsInfo.data.total, {
page: page,
limit: limit,
type: type
})
return {
list: result,
pager: Object.assign({
count: couponsInfo.data.total || 0,
curPage: page,
totalPages: couponsInfo.data.totalPageNum
}, pageNum)
};
})();
};
const couponsData = (uid, params)=>{
let type = params.type || UNUSED;
let page = params.page || 1;
let limit = params.limit || 10;
return co(function*() {
let coupons = yield getCouponsList(uid, type, page, limit);
let data = {};
data.pager = coupons.pager;
if (type === UNUSED) {
if (!coupons.list.length) {
data.unUseCoupons = {empty: '您没有优惠券'};
} else {
data.unUseCoupons = coupons.list;
}
data.unUse = true;
} else if (type === USED) {
if (!coupons.list.length) {
data.usedCoupons = {empty: '您没有优惠券'};
} else {
data.usedCoupons = coupons.list;
}
data.used = true;
} else if (type === INVALID) {
if (!coupons.list.length) {
data.noValidCoupons = {empty: '您没有优惠券'};
} else {
data.noValidCoupons = coupons.list;
}
data.noValid = true;
}
data.tabs = [
{
active: type === UNUSED ? true : false,
url: helpers.urlFormat('/home/coupons', {type: UNUSED}),
name: '未使用优惠券'
},
{
active: type === USED ? true : false,
url: helpers.urlFormat('/home/coupons', {type: USED}),
name: '已使用优惠券'
},
{
active: type === INVALID ? true : false,
url: helpers.urlFormat('/home/coupons', {type: INVALID}),
name: '已失效优惠券'
}
];
return data;
})();
};
module.exports = {
getCouponsList,
UNUSED,
USED,
INVALID
couponsData
};
... ...
... ... @@ -24,13 +24,20 @@ const convertUnitTime = (src) => {
return moment.unix(src).format('YYYY-MM-DD');
};
const currencyData = (uid, condition)=>{
const currencyData = (uid, prama)=>{
let condition = {
page: prama.page || 1,
queryType: prama.type || 0,
beginTime: prama.beginTime || convertUnitTime(new Date() / 1000 - 3600 * 24 * 90)
};
return co(function*() {
let result = {};
let yohoCoinInfo = yield currencyApi.yohoCoinTotal(uid);
if (yohoCoinInfo.code && yohoCoinInfo.code == 200) {
let yohoCoinInfoData = yohoCoinInfo.data;
result.myCurrency = yohoCoinInfoData.yohocoin_num ? yohoCoinInfoData.yohocoin_num : 0;
if (yohoCoinInfoData.nearExpCoinNum && yohoCoinInfoData.nearExpCoinNum > 0) {
result.tip.count = yohoCoinInfoData.nearExpCoinNum;
... ... @@ -38,6 +45,7 @@ const currencyData = (uid, condition)=>{
}
}
let currency = yield currencyList(uid, condition);
result.currency = currency.list;
result.pager = currency.pager;
result.coinHelperUrl = '//www.yohobuy.com/help/detail?id=105';// yoho币帮助
... ... @@ -51,6 +59,7 @@ const currencyData = (uid, condition)=>{
const currencyList = (uid, condition)=>{
return co(function*() {
let result = {'list': [], 'pager': []};
condition.limit = condition.limit || 15;
let data = yield currencyApi.yohoCoinList(uid, condition);
... ... @@ -86,18 +95,17 @@ const currencyList = (uid, condition)=>{
}
// 分页
let total = data.data.total;
let pagerObj = pager(total, {
page: data['data']['page'],
limit: condition['limit']
});
// result['pager']={};
// result['pager']['hasCheckAll'] = false;
// result['pager']['count'] = data['data']['total'];
// result['pager']['curPage'] = data['data']['page'];
// result['pager']['totalPages'] = Math.ceil(data['data']['total'] / condition['limit']);
// result['pager']['pagerHtml'] = HelperSearch::pager(data['data']['total'], condition['limit']);
result.pager = Object.assign({
count: data.data.total,
curPage: data.data.page,
totalPages: Math.ceil(data.data.total / condition.limit)
}, pager(data.data.total, {
page: condition.page,
limit: condition.limit,
type: condition.queryType,
beginTime: condition.beginTime
}));
}
return result;
})();
... ...
... ... @@ -47,11 +47,9 @@
</li>
{{/ currency}}
</ul>
{{#with ../pager}}
{{> common/foot-pager footPager=this}}
{{/with}}
{{> pager}}
</div>
{{/ content}}
</div>
</div>
... ...
... ... @@ -31,12 +31,9 @@
<span class="gift-error"></span>
</li>
<li class="captchaCode">
请输入验证码:
<input type="text" value="" name="verifyCode" id="captchaCode" class="gift-input" />
<img src="" id="imgcode"/>
看不清楚?
<a href="#" class="check-img">换一张</a>
<span class="gift-error"></span>
<!--<input type="text" value="" name="verifyCode" id="captchaCode" class="gift-input" />-->
<div class="captcha-img" ></div>
<!--<span class="gift-error"></span>-->
</li>
<li>
<input type="button" class="btn-b1" id="sub-gift" value="确认兑换"/>
... ...
... ... @@ -3,14 +3,14 @@
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/02/22
*/
var $ = require('yoho-jquery');
var $ = require('yoho-jquery'),
Captcha = require('../plugins/captcha');
var dialog = require('../common/dialog');
var Alert = dialog.Alert;
var $giftError = $('.giftCardCode').find('.gift-error'),
$codeError = $('.captchaCode').find('.gift-error'),
reg = /^[0-9a-zA-Z]{4,4}$/,
code = '',
i = 1;
... ... @@ -57,35 +57,23 @@ var Gift = {
$('#giftCardCode3').bind('blur keyup', function() {
Gift.checkCard(3);
});
$('#captchaCode').bind('blur keyup', function() {
code = $('#captchaCode').val();
if (code.length <= 0) {
$codeError.html('请输入验证码!');
Gift.suc[3] = false;
} else {
$codeError.html('');
Gift.suc[3] = true;
}
});
},
checkForm: function() {
if (!reg.test($('#giftCardCode1').val()) || !reg.test($('#giftCardCode2').val()) ||
!reg.test($('#giftCardCode3').val())) {
$giftError.html('您输入的兑换码有误,兑换码必须为数字或字母,每个文本框里只能输入四个兑换码!');
return false;
return $.Deferred().reject().promise();
}
if ($.trim($('#captchaCode').val()) === '') {
$codeError.html('请输入验证码!');
return false;
}
return true;
return captcha.check();
}
};
var captcha = new Captcha('.captcha-img').init();
captcha.refresh();
require('../common');
// 更换验证码
... ... @@ -97,7 +85,8 @@ function refreshCaptcha() {
}
$('#sub-gift').on('click', function() {
if (Gift.checkForm()) {
Gift.checkForm().then(function () {
$.post('/home/gift/exchange', $('#giftCardForm').serialize(), function(data) {
if (data.code === 200) {
window.location.href = '/home/gift?type=1';
... ... @@ -109,9 +98,7 @@ $('#sub-gift').on('click', function() {
window.location.href = '/home/gift?type=2';
}
}, 'json');
} else {
return false;
}
})
});
$(document).on('click', '#imgcode,.check-img', function() {
... ...
/**
* Created by DELL on 2017.2.10.
*/
require('../common');
... ...
... ... @@ -566,6 +566,15 @@
cursor: pointer;
}
.captcha-img {
display: inline-block;
position: relative;
.img-check-tip {
top: 120px;
}
}
.check-img {
color: #468fa2;
text-decoration: underline;
... ...