Showing
5 changed files
with
205 additions
and
6 deletions
apps/api/controllers/coupon511.js
0 → 100644
1 | + | ||
2 | +/** | ||
3 | + * 0元抽奖活动 | ||
4 | + * @author: yyq <yanqing.yang@yoho.cn> | ||
5 | + * @date: 19/07/2018 | ||
6 | + */ | ||
7 | +const coupon511Model = require('../../api/models/coupon511'); | ||
8 | +const valid = require('../../../utils/validator'); | ||
9 | +const logger = global.yoho.logger; | ||
10 | + | ||
11 | +module.exports = { | ||
12 | + status(req, res, next) { | ||
13 | + let data = valid(req.body, { | ||
14 | + uid: {type: 'number', empty: false}, | ||
15 | + }); | ||
16 | + | ||
17 | + if (!data.uid) { | ||
18 | + return res.json({ | ||
19 | + code: 400, | ||
20 | + message: '参数错误' | ||
21 | + }); | ||
22 | + } | ||
23 | + return req.ctx(coupon511Model).getStatus(data.uid).then(result => { | ||
24 | + res.json(result); | ||
25 | + }).catch(next); | ||
26 | + }, | ||
27 | + take(req, res, next) { | ||
28 | + const inx = +req.query.couponInx; | ||
29 | + let data = valid(req.body, { | ||
30 | + uid: {type: 'number', empty: false}, | ||
31 | + sessionKey: {type: 'string', empty: false}, | ||
32 | + appVersion: {type: 'string', empty: false}, | ||
33 | + appSessionType: {type: 'string', empty: true} | ||
34 | + }); | ||
35 | + | ||
36 | + logger.warn(`inx: ${inx}, data: ${JSON.stringify(data)}`); | ||
37 | + | ||
38 | + if (!inx || !data.uid) { | ||
39 | + return res.json({ | ||
40 | + code: 400, | ||
41 | + message: '参数错误' | ||
42 | + }); | ||
43 | + } | ||
44 | + | ||
45 | + return req.ctx(coupon511Model).takeCoupon(data, inx).then(result => { | ||
46 | + res.json(result); | ||
47 | + }).catch(next); | ||
48 | + } | ||
49 | +}; |
apps/api/models/coupon511.js
0 → 100644
1 | +const _ = require('lodash'); | ||
2 | +const aes = require('../../../utils/aes'); | ||
3 | +const moment = require('moment'); | ||
4 | +const mySqlCli = global.yoho.utils.mysqlCli; | ||
5 | +const UfoApi = global.yoho.UfoAPI; | ||
6 | +const TABLE_PRIZE = 'act_prize_user_log'; | ||
7 | +const TABLE_USER_DETAIL = 'act_user_detail_info'; | ||
8 | +const logger = global.yoho.logger; | ||
9 | +const mysqlCli = global.yoho.utils.mysqlCli; | ||
10 | + | ||
11 | +const coupons = ['57008016-f16a-4814-854b-2ed8c8cab3b5', '418b5d9b-6574-4d62-8457-d74da6ec94e2']; | ||
12 | + | ||
13 | +class Coupon511 extends global.yoho.BaseModel { | ||
14 | + constructor(ctx) { | ||
15 | + super(ctx); | ||
16 | + this.redis = global.yoho.redis; | ||
17 | + this.client = this.redis.client; | ||
18 | + } | ||
19 | + _getCoupon(uid, inx) { | ||
20 | + const token = coupons[inx - 1]; | ||
21 | + | ||
22 | + return this.get({ | ||
23 | + url: 'coupon', | ||
24 | + data: { | ||
25 | + method: 'ufo.coupons.send', | ||
26 | + uid: uid, | ||
27 | + coupon_tokens: token | ||
28 | + }, | ||
29 | + api: UfoApi | ||
30 | + }); | ||
31 | + } | ||
32 | + concatPhone(uid) { | ||
33 | + let phone = ''; | ||
34 | + let appStrLen = 11 - uid.toString().length; | ||
35 | + | ||
36 | + for (let i = 0; i < appStrLen; i++) { | ||
37 | + phone += '0'; | ||
38 | + } | ||
39 | + phone += uid.toString(); | ||
40 | + return phone; | ||
41 | + } | ||
42 | + _saveUserInfo(uid) { | ||
43 | + let actId = 9999, name = '000', birthday = moment().format('YYYY-MM-DD'), gender = 0, email = 'u@yoho.cn', province = '', city = ''; | ||
44 | + let phone = this.concatPhone(uid); | ||
45 | + | ||
46 | + let strFind = `SELECT COUNT(*) AS user_count | ||
47 | + FROM ${TABLE_USER_DETAIL} | ||
48 | + WHERE act_id=:actId | ||
49 | + AND user_phone=:phone`; | ||
50 | + | ||
51 | + let strInsert = `INSERT INTO ${TABLE_USER_DETAIL} | ||
52 | + (act_id, user_name, user_birthday, user_gender, | ||
53 | + user_phone, user_email, user_province, user_city) | ||
54 | + VALUES (:actId, :name, :birthday, :gender, :phone, :email, :province, :city)`; | ||
55 | + | ||
56 | + return mySqlCli.query(strFind, { | ||
57 | + actId, | ||
58 | + phone | ||
59 | + }).then(ret => { | ||
60 | + if (ret && ret[0].user_count <= 0) { | ||
61 | + return mySqlCli.insert(strInsert, { | ||
62 | + actId, | ||
63 | + name, | ||
64 | + birthday, | ||
65 | + gender, | ||
66 | + phone, | ||
67 | + email, | ||
68 | + province, | ||
69 | + city | ||
70 | + }).then(ret2 => { | ||
71 | + return Promise.resolve({ | ||
72 | + code: 200, | ||
73 | + message: '用户信息添加成功', | ||
74 | + data: ret2 | ||
75 | + }); | ||
76 | + }); | ||
77 | + } else { | ||
78 | + return Promise.resolve({ | ||
79 | + code: 203, | ||
80 | + message: '已存在的用户信息' | ||
81 | + }); | ||
82 | + } | ||
83 | + }); | ||
84 | + } | ||
85 | + | ||
86 | + async getStatus(uid) { | ||
87 | + const hasInx = await this.client.getAsync(`coupon511_${uid}`); | ||
88 | + | ||
89 | + if (hasInx) { | ||
90 | + return { | ||
91 | + code: 200, | ||
92 | + data: hasInx | ||
93 | + }; | ||
94 | + } | ||
95 | + return { | ||
96 | + code: 204, | ||
97 | + }; | ||
98 | + } | ||
99 | + async takeCoupon(obj, inx) { | ||
100 | + const hasInx = await this.client.getAsync(`coupon511_${obj.uid}`); | ||
101 | + | ||
102 | + if (hasInx) { | ||
103 | + return { | ||
104 | + code: 200, | ||
105 | + message: '已领取过 不可重复领取' | ||
106 | + }; | ||
107 | + } | ||
108 | + let uid = { | ||
109 | + toString: () => { | ||
110 | + return _.parseInt(obj.uid); | ||
111 | + }, | ||
112 | + sessionKey: obj.sessionKey, | ||
113 | + appVersion: obj.appVersion, | ||
114 | + appSessionType: obj.sessionType | ||
115 | + }; | ||
116 | + | ||
117 | + let result; | ||
118 | + | ||
119 | + if (inx < 3) { | ||
120 | + result = await this._getCoupon(uid, inx); | ||
121 | + } else { | ||
122 | + this._saveUserInfo(obj.uid); | ||
123 | + result = { | ||
124 | + code: 200, | ||
125 | + message: '领取成功' | ||
126 | + }; | ||
127 | + } | ||
128 | + if (result.code === 200 || result.code === 401) { | ||
129 | + this.client.set(`coupon511_${obj.uid}`, inx.toString()); | ||
130 | + } | ||
131 | + return result; | ||
132 | + } | ||
133 | +} | ||
134 | + | ||
135 | +module.exports = Coupon511; |
@@ -16,6 +16,7 @@ const redpac = require('./controllers/redpac'); | @@ -16,6 +16,7 @@ const redpac = require('./controllers/redpac'); | ||
16 | const excel = require('./controllers/excel'); | 16 | const excel = require('./controllers/excel'); |
17 | const multipart = require('connect-multiparty'); | 17 | const multipart = require('connect-multiparty'); |
18 | const wheelSurf = require('./controllers/wheel-surf'); | 18 | const wheelSurf = require('./controllers/wheel-surf'); |
19 | +const coupon511 = require('./controllers/coupon511'); | ||
19 | const common = require('./controllers/common'); | 20 | const common = require('./controllers/common'); |
20 | const mutilpartMiddleware = multipart(); | 21 | const mutilpartMiddleware = multipart(); |
21 | const yohoActivitys = require('./controllers/yoho-activitys'); | 22 | const yohoActivitys = require('./controllers/yoho-activitys'); |
@@ -85,6 +86,8 @@ router.post('/activity/wheelSurf/user/prize', wheelSurf.getUserPrize); | @@ -85,6 +86,8 @@ router.post('/activity/wheelSurf/user/prize', wheelSurf.getUserPrize); | ||
85 | 86 | ||
86 | // 公共服务 | 87 | // 公共服务 |
87 | router.get('/common/resource', common.getResource); | 88 | router.get('/common/resource', common.getResource); |
89 | +router.post('/coupon511/status', coupon511.status); | ||
90 | +router.post('/coupon511/take', coupon511.take); | ||
88 | 91 | ||
89 | router.get('/activity/yohoActivitys/create', yohoActivitys.actCreate); // 创建一个活动 | 92 | router.get('/activity/yohoActivitys/create', yohoActivitys.actCreate); // 创建一个活动 |
90 | router.get('/activity/yohoActivitys/actInfo', yohoActivitys.getActInfo); // 获取活动信息 | 93 | router.get('/activity/yohoActivitys/actInfo', yohoActivitys.getActInfo); // 获取活动信息 |
@@ -20,13 +20,14 @@ module.exports = { | @@ -20,13 +20,14 @@ module.exports = { | ||
20 | 20 | ||
21 | // yohoVerifyUdid: '0f626ede-0e17-460b-a8ea-069ee506e8e9', | 21 | // yohoVerifyUdid: '0f626ede-0e17-460b-a8ea-069ee506e8e9', |
22 | domains: { | 22 | domains: { |
23 | - api: 'http://api-test3.dev.yohocorp.com/', | ||
24 | - service: 'http://api-test3.dev.yohocorp.com/', | ||
25 | - singleApi: 'http://api-test3.yohops.com:9999/', | 23 | + // api: 'http://api-test3.dev.yohocorp.com/', |
24 | + // service: 'http://api-test3.dev.yohocorp.com/', | ||
25 | + // singleApi: 'http://api-test3.yohops.com:9999/', | ||
26 | 26 | ||
27 | - // singleApi: 'http://api.yoho.cn/', | ||
28 | - // api: 'http://api.yoho.cn/', | ||
29 | - // service: 'http://service.yoho.cn/', | 27 | + singleApi: 'http://api.yoho.cn/', |
28 | + api: 'http://api.yoho.cn/', | ||
29 | + service: 'http://service.yoho.cn/', | ||
30 | + ufo: 'http://2.yohobuy.com', | ||
30 | store: 'http://192.168.102.47:8080/portal-gateway/wechat/', | 31 | store: 'http://192.168.102.47:8080/portal-gateway/wechat/', |
31 | serviceNotify: 'http://service.yoho.cn/', | 32 | serviceNotify: 'http://service.yoho.cn/', |
32 | platformApi: 'http://172.16.6.210:8088/', | 33 | platformApi: 'http://172.16.6.210:8088/', |
@@ -148,6 +149,7 @@ if (isProduction) { | @@ -148,6 +149,7 @@ if (isProduction) { | ||
148 | assetUrl: `/yoho-activity-platform/${pkg.version}/`, | 149 | assetUrl: `/yoho-activity-platform/${pkg.version}/`, |
149 | domains: { | 150 | domains: { |
150 | api: 'http://api.yoho.yohoops.org/', | 151 | api: 'http://api.yoho.yohoops.org/', |
152 | + ufo: 'http://ufoapi.yohoops.org/', | ||
151 | store: 'https://openstore.yohobuy.com', | 153 | store: 'https://openstore.yohobuy.com', |
152 | service: 'http://api.yoho.yohoops.org/', | 154 | service: 'http://api.yoho.yohoops.org/', |
153 | global: 'http://api-global.yohobuy.com', | 155 | global: 'http://api-global.yohobuy.com', |
@@ -220,6 +222,7 @@ if (isProduction) { | @@ -220,6 +222,7 @@ if (isProduction) { | ||
220 | assetUrl: `/yoho-activity-platform/${pkg.version}/`, | 222 | assetUrl: `/yoho-activity-platform/${pkg.version}/`, |
221 | domains: { | 223 | domains: { |
222 | api: 'http://api-test3.dev.yohocorp.com/', | 224 | api: 'http://api-test3.dev.yohocorp.com/', |
225 | + ufo: process.env.UFO_API || 'http://java-yohoufo-fore.test3.ingress.dev.yohocorp.com/ufo-gateway/', | ||
223 | service: 'http://api-test3.dev.yohocorp.com/', | 226 | service: 'http://api-test3.dev.yohocorp.com/', |
224 | global: 'http://global-test-soa.yohops.com:9999/', | 227 | global: 'http://global-test-soa.yohops.com:9999/', |
225 | store: 'http://192.168.102.210:8080/portal-gateway/wechat/', | 228 | store: 'http://192.168.102.210:8080/portal-gateway/wechat/', |
@@ -40,6 +40,15 @@ const serverError = (err, req, res, next) => { // eslint-disable-line | @@ -40,6 +40,15 @@ const serverError = (err, req, res, next) => { // eslint-disable-line | ||
40 | } catch(e) {} // eslint-disable-line | 40 | } catch(e) {} // eslint-disable-line |
41 | } | 41 | } |
42 | 42 | ||
43 | + if (err && err.code === 401) { | ||
44 | + if (req.xhr) { | ||
45 | + return res.status(401).json(err); | ||
46 | + } else if (req.jsonp) { | ||
47 | + err.auth = true; | ||
48 | + return res.jsonp(err); | ||
49 | + } | ||
50 | + } | ||
51 | + | ||
43 | return res.status(code).json({ | 52 | return res.status(code).json({ |
44 | code: code, | 53 | code: code, |
45 | message: msg | 54 | message: msg |
-
Please register or login to post a comment