Showing
6 changed files
with
247 additions
and
11 deletions
apps/home/controllers/gift.js
0 → 100644
1 | +/** | ||
2 | + * 个人中心---兑换礼品卡 | ||
3 | + * @author gaohongwei <hongwei.gao@yoho.cn> | ||
4 | + * @date: 2016/9/7 | ||
5 | + */ | ||
6 | +'use strict'; | ||
7 | + | ||
8 | +const mRoot = '../models'; | ||
9 | +const giftService = require(`${mRoot}/gift-service`); // user 页 model | ||
10 | +const helpers = global.yoho.helpers; | ||
11 | + | ||
12 | +/** | ||
13 | + * 礼品卡页面 | ||
14 | + */ | ||
15 | +exports.index = (req, res, next) => { | ||
16 | + if (!req.user.uid) { | ||
17 | + res.redirect(helpers.urlFormat('/signin.html')); | ||
18 | + } | ||
19 | + let uid = req.user.uid; | ||
20 | + | ||
21 | + let responseData = { | ||
22 | + module: 'home', | ||
23 | + page: 'gift' | ||
24 | + }; | ||
25 | + | ||
26 | + // 真实数据输出 | ||
27 | + giftService.index(req.query, uid).then(result => { | ||
28 | + responseData.meGiftPage = true; | ||
29 | + Object.assign(responseData, result); | ||
30 | + res.render('gift', responseData); | ||
31 | + }).catch(next); | ||
32 | +}; | ||
33 | + | ||
34 | +/** | ||
35 | + * 个人中心-兑换礼品卡提交返回信息 | ||
36 | + */ | ||
37 | +exports.exchange = (req, res, next) => { | ||
38 | + if (!req.user.uid) { | ||
39 | + res.redirect(helpers.urlFormat('/signin.html')); | ||
40 | + } | ||
41 | + let uid = req.user.uid; | ||
42 | + | ||
43 | + // 真实数据输出 | ||
44 | + giftService.exchange(req, req.body, uid).then(result => { | ||
45 | + res.json(result); | ||
46 | + }).catch(next); | ||
47 | +}; |
apps/home/models/gift-service.js
0 → 100644
1 | +/** | ||
2 | + * 个人中心---兑换礼品卡 | ||
3 | + * @author gaohongwei <hongwei.gao@yoho.cn> | ||
4 | + * @date: 2016/9/7 | ||
5 | + */ | ||
6 | +'use strict'; | ||
7 | +const Promise = require('bluebird'); | ||
8 | +const co = Promise.coroutine; | ||
9 | +const _ = require('lodash'); | ||
10 | +const userApi = require('./user-api'); | ||
11 | +const currencyApi = require('./currency-data'); | ||
12 | + | ||
13 | +/** | ||
14 | + * 礼品卡页面 | ||
15 | + */ | ||
16 | +exports.index = (params, uid) => { | ||
17 | + return co(function*() { | ||
18 | + let respData = {}, | ||
19 | + type = params.type || '', | ||
20 | + yohoCoinResult; | ||
21 | + | ||
22 | + if (type !== '') { | ||
23 | + yohoCoinResult = yield currencyApi.yohoCoinTotal(uid); | ||
24 | + let yohoCoinInfo = yohoCoinResult.data; | ||
25 | + | ||
26 | + if (yohoCoinInfo) { | ||
27 | + respData.gift = { | ||
28 | + resultInfo: { | ||
29 | + success: (type === '1') ? true : false, | ||
30 | + yohoCoin: yohoCoinInfo.yohocoin_num ? yohoCoinInfo.yohocoin_num : 0 | ||
31 | + } | ||
32 | + }; | ||
33 | + } | ||
34 | + } else { | ||
35 | + respData.gift = { | ||
36 | + resultInfo: false | ||
37 | + }; | ||
38 | + } | ||
39 | + | ||
40 | + return respData; | ||
41 | + })(); | ||
42 | +}; | ||
43 | + | ||
44 | +/** | ||
45 | + * 个人中心-兑换礼品卡提交返回信息 | ||
46 | + */ | ||
47 | +exports.exchange = (req, params, uid) => { | ||
48 | + return co(function*() { | ||
49 | + let data = {}; | ||
50 | + | ||
51 | + data.giftCardCode1 = _.trim(params.giftCardCode1 || ''); | ||
52 | + data.giftCardCode2 = _.trim(params.giftCardCode2 || ''); | ||
53 | + data.giftCardCode3 = _.trim(params.giftCardCode3 || ''); | ||
54 | + data.captchaCode = _.trim(params.verifyCode || '').toLowerCase(); | ||
55 | + | ||
56 | + if (data.captchaCode && data.captchaCode !== req.session.captcha) { | ||
57 | + return { | ||
58 | + code: 400, | ||
59 | + message: '图形验证码不正确' | ||
60 | + }; | ||
61 | + | ||
62 | + } | ||
63 | + let respData = yield userApi.exchangeGift(data, uid); | ||
64 | + | ||
65 | + return respData; | ||
66 | + })(); | ||
67 | +}; |
@@ -37,7 +37,7 @@ const AccountController = require(`${cRoot}/account`); | @@ -37,7 +37,7 @@ const AccountController = require(`${cRoot}/account`); | ||
37 | 37 | ||
38 | const AddressController = require(`${cRoot}/address`); | 38 | const AddressController = require(`${cRoot}/address`); |
39 | 39 | ||
40 | -// const GiftController = require(`${cRoot}/gift`); | 40 | +const giftController = require(`${cRoot}/gift`); |
41 | 41 | ||
42 | const homeNav = (req) => { | 42 | const homeNav = (req) => { |
43 | return [ | 43 | return [ |
@@ -319,9 +319,10 @@ router.get('/address/delAddress', AddressController.delAddress); | @@ -319,9 +319,10 @@ router.get('/address/delAddress', AddressController.delAddress); | ||
319 | 319 | ||
320 | router.get('/address/defaultAddress', AddressController.defaultAddress); | 320 | router.get('/address/defaultAddress', AddressController.defaultAddress); |
321 | 321 | ||
322 | -// | ||
323 | -// router.get('/gift', [getCommonHeader, getHomeNav], GiftController.index); | ||
324 | -// | ||
325 | -// router.post('/gift/exchange', GiftController.exchange); | 322 | +// 兑换礼品卡 |
323 | + | ||
324 | +router.get('/gift', [getCommonHeader, getHomeNav], giftController.index); | ||
325 | + | ||
326 | +router.post('/gift/exchange', giftController.exchange); | ||
326 | 327 | ||
327 | module.exports = router; | 328 | module.exports = router; |
1 | -{{> layout/header}} | ||
2 | <div class="user-me-page me-page yoho-page clearfix"> | 1 | <div class="user-me-page me-page yoho-page clearfix"> |
2 | + {{> path}} | ||
3 | + {{> navigation}} | ||
3 | {{# gift}} | 4 | {{# gift}} |
4 | - {{> path}} | ||
5 | - {{> navigation}} | ||
6 | <div class="me-main"> | 5 | <div class="me-main"> |
7 | <div class="gift block"> | 6 | <div class="gift block"> |
8 | <div class="title"> | 7 | <div class="title"> |
@@ -48,7 +47,4 @@ | @@ -48,7 +47,4 @@ | ||
48 | </div> | 47 | </div> |
49 | </div> | 48 | </div> |
50 | {{/ gift}} | 49 | {{/ gift}} |
51 | - | ||
52 | - {{> help-us}} | ||
53 | </div> | 50 | </div> |
54 | -{{> layout/footer}} |
public/js/home/gift.page.js
0 → 100644
1 | +/** | ||
2 | + * 个人中心页-兑换礼品卡 | ||
3 | + * @author: wsl<shuiling.wang@yoho.cn> | ||
4 | + * @date: 2016/02/22 | ||
5 | + */ | ||
6 | +var $ = require('yoho-jquery'); | ||
7 | + | ||
8 | +var dialog = require('../common/dialog'); | ||
9 | + | ||
10 | +var Alert = dialog.Alert; | ||
11 | + | ||
12 | +var $giftError = $('.giftCardCode').find('.gift-error'), | ||
13 | + $codeError = $('.captchaCode').find('.gift-error'), | ||
14 | + reg = /^[0-9a-zA-Z]{4,4}$/, | ||
15 | + code = '', | ||
16 | + i = 1; | ||
17 | + | ||
18 | +var active; | ||
19 | + | ||
20 | +var Gift = { | ||
21 | + suc: [ | ||
22 | + false, | ||
23 | + false, | ||
24 | + false, | ||
25 | + false | ||
26 | + ], | ||
27 | + checkCard: function(num) { | ||
28 | + | ||
29 | + if (!reg.test($('#giftCardCode' + num).val())) { | ||
30 | + $giftError.html('您输入的兑换码有误,兑换码必须为数字或字母,每个文本框里只能输入四个兑换码!'); | ||
31 | + Gift.suc[num - 1] = false; | ||
32 | + } else { | ||
33 | + for (i = 1; i <= 3; i++) { | ||
34 | + if (!reg.test($('#giftCardCode' + i).val())) { | ||
35 | + $giftError.html('您输入的兑换码有误,兑换码必须为数字或字母,每个文本框里只能输入四个兑换码!'); | ||
36 | + Gift.suc[i] = false; | ||
37 | + } else { | ||
38 | + $giftError.html(''); | ||
39 | + Gift.suc[i] = true; | ||
40 | + } | ||
41 | + } | ||
42 | + | ||
43 | + if (num !== 3) { | ||
44 | + $('#giftCardCode' + (num + 1)).focus(); | ||
45 | + } | ||
46 | + } | ||
47 | + }, | ||
48 | + bindGiftCardForm: function() { | ||
49 | + $('#giftCardCode1').bind('blur keyup', function() { | ||
50 | + Gift.checkCard(1); | ||
51 | + }); | ||
52 | + | ||
53 | + $('#giftCardCode2').bind('blur keyup', function() { | ||
54 | + Gift.checkCard(2); | ||
55 | + }); | ||
56 | + | ||
57 | + $('#giftCardCode3').bind('blur keyup', function() { | ||
58 | + Gift.checkCard(3); | ||
59 | + }); | ||
60 | + | ||
61 | + $('#captchaCode').bind('blur keyup', function() { | ||
62 | + code = $('#captchaCode').val(); | ||
63 | + | ||
64 | + if (code.length <= 0) { | ||
65 | + $codeError.html('请输入验证码!'); | ||
66 | + Gift.suc[3] = false; | ||
67 | + } else { | ||
68 | + $codeError.html(''); | ||
69 | + Gift.suc[3] = true; | ||
70 | + } | ||
71 | + }); | ||
72 | + }, | ||
73 | + | ||
74 | + checkForm: function() { | ||
75 | + if (!reg.test($('#giftCardCode1').val()) || !reg.test($('#giftCardCode2').val()) || | ||
76 | + !reg.test($('#giftCardCode3').val())) { | ||
77 | + $giftError.html('您输入的兑换码有误,兑换码必须为数字或字母,每个文本框里只能输入四个兑换码!'); | ||
78 | + return false; | ||
79 | + } | ||
80 | + | ||
81 | + if ($.trim($('#captchaCode').val()) === '') { | ||
82 | + $codeError.html('请输入验证码!'); | ||
83 | + return false; | ||
84 | + } | ||
85 | + return true; | ||
86 | + } | ||
87 | +}; | ||
88 | + | ||
89 | +require('../common'); | ||
90 | + | ||
91 | +// 更换验证码 | ||
92 | +function refreshCaptcha() { | ||
93 | + var dt = new Date(); | ||
94 | + | ||
95 | + $('#imgcode').attr('src', '/passport/imagesNode?t=' + dt.getTime()); | ||
96 | + return false; | ||
97 | +} | ||
98 | + | ||
99 | +$('#sub-gift').on('click', function() { | ||
100 | + if (Gift.checkForm()) { | ||
101 | + $.post('/home/gift/exchange', $('#giftCardForm').serialize(), function(data) { | ||
102 | + if (data.code === 200) { | ||
103 | + window.location.href = '/home/gift?type=1'; | ||
104 | + } else if (data.code === 400) { | ||
105 | + active = new Alert(data.message); | ||
106 | + active.show(); | ||
107 | + refreshCaptcha(); | ||
108 | + } else { | ||
109 | + window.location.href = '/home/gift?type=2'; | ||
110 | + } | ||
111 | + }, 'json'); | ||
112 | + } else { | ||
113 | + return false; | ||
114 | + } | ||
115 | +}); | ||
116 | + | ||
117 | +$(document).on('click', '#imgcode,.check-img', function() { | ||
118 | + refreshCaptcha(); | ||
119 | +}); | ||
120 | + | ||
121 | +$(function() { | ||
122 | + refreshCaptcha(); | ||
123 | + Gift.bindGiftCardForm(); | ||
124 | +}); |
-
Please register or login to post a comment