Authored by 陈峰

commit

  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 +};
  1 +const _ = require('lodash');
  2 +const aes = require('../../../utils/aes');
  3 +const mySqlCli = global.yoho.utils.mysqlCli;
  4 +const UfoApi = global.yoho.UfoAPI;
  5 +const TABLE_PRIZE = 'act_prize_user_log';
  6 +const TABLE_USER_DETAIL = 'act_user_detail_info';
  7 +const logger = global.yoho.logger;
  8 +const mysqlCli = global.yoho.utils.mysqlCli;
  9 +
  10 +const coupons = ['57008016-f16a-4814-854b-2ed8c8cab3b5', '418b5d9b-6574-4d62-8457-d74da6ec94e2'];
  11 +
  12 +class Coupon511 extends global.yoho.BaseModel {
  13 + constructor(ctx) {
  14 + super(ctx);
  15 + this.redis = global.yoho.redis;
  16 + this.client = this.redis.client;
  17 + }
  18 + _getCoupon(uid, inx) {
  19 + const token = coupons[inx - 1];
  20 +
  21 + return this.get({
  22 + url: 'coupon',
  23 + data: {
  24 + method: 'ufo.coupons.send',
  25 + uid: uid,
  26 + coupon_tokens: token
  27 + },
  28 + api: UfoApi
  29 + });
  30 + }
  31 + concatPhone(uid) {
  32 + let phone = '';
  33 + let appStrLen = 11 - uid.toString().length;
  34 +
  35 + for (let i = 0; i < appStrLen; i++) {
  36 + phone += '0';
  37 + }
  38 + phone += uid.toString();
  39 + return phone;
  40 + }
  41 + _saveUserInfo(uid) {
  42 + let actId = 9999, name = '000', birthday = '', gender = '', email = 'u@yoho.cn', province = '', city = '';
  43 + let phone = this.concatPhone(uid);
  44 +
  45 + let strFind = `SELECT COUNT(*) AS user_count
  46 + FROM ${TABLE_USER_DETAIL}
  47 + WHERE act_id=:actId
  48 + AND user_phone=:phone`;
  49 +
  50 + let strInsert = `INSERT INTO ${TABLE_USER_DETAIL}
  51 + (act_id, user_name, user_birthday, user_gender,
  52 + user_phone, user_email, user_province, user_city)
  53 + VALUES (:actId, :name, :birthday, :gender, :phone, :email, :province, :city)`;
  54 +
  55 + return mySqlCli.query(strFind, {
  56 + actId,
  57 + phone
  58 + }).then(ret => {
  59 + if (ret && ret[0].user_count <= 0) {
  60 + return mySqlCli.insert(strInsert, {
  61 + actId,
  62 + name,
  63 + birthday,
  64 + gender,
  65 + phone,
  66 + email,
  67 + province,
  68 + city
  69 + }).then(ret2 => {
  70 + return Promise.resolve({
  71 + code: 200,
  72 + message: '用户信息添加成功',
  73 + data: ret2
  74 + });
  75 + });
  76 + } else {
  77 + return Promise.resolve({
  78 + code: 203,
  79 + message: '已存在的用户信息'
  80 + });
  81 + }
  82 + });
  83 + }
  84 +
  85 + async getStatus(uid) {
  86 + const hasInx = await this.client.getAsync(`coupon511_${uid}`);
  87 +
  88 + if (hasInx) {
  89 + return {
  90 + code: 200,
  91 + data: hasInx
  92 + };
  93 + }
  94 + return {
  95 + code: 204,
  96 + };
  97 + }
  98 + async takeCoupon(obj, inx) {
  99 + const hasInx = await this.client.getAsync(`coupon511_${obj.uid}`);
  100 +
  101 + if (hasInx) {
  102 + return {
  103 + code: 200,
  104 + message: '已领取过 不可重复领取'
  105 + };
  106 + }
  107 + let uid = {
  108 + toString: () => {
  109 + return _.parseInt(obj.uid);
  110 + },
  111 + sessionKey: obj.sessionKey,
  112 + appVersion: obj.appVersion,
  113 + appSessionType: obj.sessionType
  114 + };
  115 +
  116 + let result;
  117 +
  118 + if (inx < 3) {
  119 + result = await this._getCoupon(uid, inx);
  120 + } else {
  121 + this._saveUserInfo(obj.uid);
  122 + result = {
  123 + code: 200,
  124 + message: '领取成功'
  125 + };
  126 + }
  127 + if (result.code === 200 || result.code === 401) {
  128 + this.client.set(`coupon511_${obj.uid}`, inx.toString());
  129 + }
  130 + return result;
  131 + }
  132 +}
  133 +
  134 +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 22
@@ -85,4 +86,8 @@ router.post('/activity/wheelSurf/user/prize', wheelSurf.getUserPrize); @@ -85,4 +86,8 @@ router.post('/activity/wheelSurf/user/prize', wheelSurf.getUserPrize);
85 // 公共服务 86 // 公共服务
86 router.get('/common/resource', common.getResource); 87 router.get('/common/resource', common.getResource);
87 88
  89 +// 公共服务
  90 +router.post('/coupon511/status', coupon511.status);
  91 +router.post('/coupon511/take', coupon511.take);
  92 +
88 module.exports = router; 93 module.exports = router;
@@ -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