Authored by hongweigao

礼品卡部分代码

/**
* 个人中心---兑换礼品卡
* @author gaohongwei <hongwei.gao@yoho.cn>
* @date: 2016/9/7
*/
'use strict';
const mRoot = '../models';
const giftService = require(`${mRoot}/gift-service`); // user model
const helpers = global.yoho.helpers;
/**
* 礼品卡页面
*/
exports.index = (req, res, next) => {
if (!req.user.uid) {
res.redirect(helpers.urlFormat('/signin.html'));
}
let uid = req.user.uid;
let responseData = {
module: 'home',
page: 'gift'
};
// 真实数据输出
giftService.index(req.query, uid).then(result => {
responseData.meGiftPage = true;
Object.assign(responseData, result);
res.render('gift', responseData);
}).catch(next);
};
/**
* 个人中心-兑换礼品卡提交返回信息
*/
exports.exchange = (req, res, next) => {
if (!req.user.uid) {
res.redirect(helpers.urlFormat('/signin.html'));
}
let uid = req.user.uid;
// 真实数据输出
giftService.exchange(req, req.body, uid).then(result => {
res.json(result);
}).catch(next);
};
... ...
/**
* 个人中心---兑换礼品卡
* @author gaohongwei <hongwei.gao@yoho.cn>
* @date: 2016/9/7
*/
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const userApi = require('./user-api');
const currencyApi = require('./currency-data');
/**
* 礼品卡页面
*/
exports.index = (params, uid) => {
return co(function*() {
let respData = {},
type = params.type || '',
yohoCoinResult;
if (type !== '') {
yohoCoinResult = yield currencyApi.yohoCoinTotal(uid);
let yohoCoinInfo = yohoCoinResult.data;
if (yohoCoinInfo) {
respData.gift = {
resultInfo: {
success: (type === '1') ? true : false,
yohoCoin: yohoCoinInfo.yohocoin_num ? yohoCoinInfo.yohocoin_num : 0
}
};
}
} else {
respData.gift = {
resultInfo: false
};
}
return respData;
})();
};
/**
* 个人中心-兑换礼品卡提交返回信息
*/
exports.exchange = (req, params, uid) => {
return co(function*() {
let data = {};
data.giftCardCode1 = _.trim(params.giftCardCode1 || '');
data.giftCardCode2 = _.trim(params.giftCardCode2 || '');
data.giftCardCode3 = _.trim(params.giftCardCode3 || '');
data.captchaCode = _.trim(params.verifyCode || '').toLowerCase();
if (data.captchaCode && data.captchaCode !== req.session.captcha) {
return {
code: 400,
message: '图形验证码不正确'
};
}
let respData = yield userApi.exchangeGift(data, uid);
return respData;
})();
};
... ...
... ... @@ -37,7 +37,7 @@ const AccountController = require(`${cRoot}/account`);
const AddressController = require(`${cRoot}/address`);
// const GiftController = require(`${cRoot}/gift`);
const giftController = require(`${cRoot}/gift`);
const homeNav = (req) => {
return [
... ... @@ -319,9 +319,10 @@ router.get('/address/delAddress', AddressController.delAddress);
router.get('/address/defaultAddress', AddressController.defaultAddress);
//
// router.get('/gift', [getCommonHeader, getHomeNav], GiftController.index);
//
// router.post('/gift/exchange', GiftController.exchange);
// 兑换礼品卡
router.get('/gift', [getCommonHeader, getHomeNav], giftController.index);
router.post('/gift/exchange', giftController.exchange);
module.exports = router;
... ...
{{> layout/header}}
<div class="user-me-page me-page yoho-page clearfix">
{{> path}}
{{> navigation}}
{{# gift}}
{{> path}}
{{> navigation}}
<div class="me-main">
<div class="gift block">
<div class="title">
... ... @@ -48,7 +47,4 @@
</div>
</div>
{{/ gift}}
{{> help-us}}
</div>
{{> layout/footer}}
... ...
/**
* 个人中心页-兑换礼品卡
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/02/22
*/
var $ = require('yoho-jquery');
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;
var active;
var Gift = {
suc: [
false,
false,
false,
false
],
checkCard: function(num) {
if (!reg.test($('#giftCardCode' + num).val())) {
$giftError.html('您输入的兑换码有误,兑换码必须为数字或字母,每个文本框里只能输入四个兑换码!');
Gift.suc[num - 1] = false;
} else {
for (i = 1; i <= 3; i++) {
if (!reg.test($('#giftCardCode' + i).val())) {
$giftError.html('您输入的兑换码有误,兑换码必须为数字或字母,每个文本框里只能输入四个兑换码!');
Gift.suc[i] = false;
} else {
$giftError.html('');
Gift.suc[i] = true;
}
}
if (num !== 3) {
$('#giftCardCode' + (num + 1)).focus();
}
}
},
bindGiftCardForm: function() {
$('#giftCardCode1').bind('blur keyup', function() {
Gift.checkCard(1);
});
$('#giftCardCode2').bind('blur keyup', function() {
Gift.checkCard(2);
});
$('#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;
}
if ($.trim($('#captchaCode').val()) === '') {
$codeError.html('请输入验证码!');
return false;
}
return true;
}
};
require('../common');
// 更换验证码
function refreshCaptcha() {
var dt = new Date();
$('#imgcode').attr('src', '/passport/imagesNode?t=' + dt.getTime());
return false;
}
$('#sub-gift').on('click', function() {
if (Gift.checkForm()) {
$.post('/home/gift/exchange', $('#giftCardForm').serialize(), function(data) {
if (data.code === 200) {
window.location.href = '/home/gift?type=1';
} else if (data.code === 400) {
active = new Alert(data.message);
active.show();
refreshCaptcha();
} else {
window.location.href = '/home/gift?type=2';
}
}, 'json');
} else {
return false;
}
});
$(document).on('click', '#imgcode,.check-img', function() {
refreshCaptcha();
});
$(function() {
refreshCaptcha();
Gift.bindGiftCardForm();
});
... ...
/**
* Created by DELL on 2017.2.10.
*/
require('../common');
... ...