Authored by yyq

send message

@@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
4 * @date: 19/07/2018 4 * @date: 19/07/2018
5 */ 5 */
6 const _ = require('lodash'); 6 const _ = require('lodash');
  7 +const moment = require('moment');
7 const mysqlCli = global.yoho.utils.mysqlCli; 8 const mysqlCli = global.yoho.utils.mysqlCli;
8 const MemoryCache = require('../../../utils/memory-cache'); 9 const MemoryCache = require('../../../utils/memory-cache');
9 10
@@ -20,6 +21,14 @@ const PAGE_SIZE = 10; @@ -20,6 +21,14 @@ const PAGE_SIZE = 10;
20 21
21 const userTimesCache = new MemoryCache(); 22 const userTimesCache = new MemoryCache();
22 23
  24 +const timeFormat = (time) => {
  25 + if (_.isNumber(time)) {
  26 + time = moment.unix(time);
  27 + }
  28 +
  29 + return moment(time).format('YYYY-MM-DD HH:mm:ss');
  30 +};
  31 +
23 function handelResult(result) { 32 function handelResult(result) {
24 return { 33 return {
25 code: 200, 34 code: 200,
@@ -278,7 +287,9 @@ module.exports = class extends global.yoho.BaseModel { @@ -278,7 +287,9 @@ module.exports = class extends global.yoho.BaseModel {
278 message: '活动已结束或已达人数上限' 287 message: '活动已结束或已达人数上限'
279 }; 288 };
280 289
281 - let status = getActivityStatus(_.get(info, '[0][0]'), _.get(info, '[1].join_num', 0)); 290 + let productInfo = _.get(info, '[0][0]');
  291 +
  292 + let status = getActivityStatus(productInfo, _.get(info, '[1].join_num', 0));
282 293
283 if (!status || status >= 3) { 294 if (!status || status >= 3) {
284 return errorData; 295 return errorData;
@@ -292,7 +303,12 @@ module.exports = class extends global.yoho.BaseModel { @@ -292,7 +303,12 @@ module.exports = class extends global.yoho.BaseModel {
292 this.sendPrizeCode(extra.shareUid, actPrizeId, {isShareTake: true}); 303 this.sendPrizeCode(extra.shareUid, actPrizeId, {isShareTake: true});
293 } 304 }
294 305
295 - return this.sendPrizeCode(uid, actPrizeId, Object.assign(extra, {isShareTake: false})); 306 + return this.sendPrizeCode(uid, actPrizeId, Object.assign(extra, {
  307 + isShareTake: false,
  308 + actName: productInfo.name,
  309 + actStartTime: productInfo.start_time,
  310 + actEndTime: productInfo.end_time
  311 + }));
296 } 312 }
297 313
298 /** 314 /**
@@ -409,6 +425,10 @@ module.exports = class extends global.yoho.BaseModel { @@ -409,6 +425,10 @@ module.exports = class extends global.yoho.BaseModel {
409 } 425 }
410 426
411 if (_.isNumber(insert)) { 427 if (_.isNumber(insert)) {
  428 + if (userJoinTimes < 1 && extra.actName) {
  429 + this.sendWechatMessage(uid, actPrizeId, extra);
  430 + }
  431 +
412 return { 432 return {
413 code: 200, 433 code: 200,
414 data: {prizeCode}, 434 data: {prizeCode},
@@ -418,4 +438,26 @@ module.exports = class extends global.yoho.BaseModel { @@ -418,4 +438,26 @@ module.exports = class extends global.yoho.BaseModel {
418 return errorData; 438 return errorData;
419 } 439 }
420 } 440 }
  441 +
  442 + /**
  443 + * 向用户发送获得抽奖码消息
  444 + * @param uid
  445 + * @param actPrizeId
  446 + * @param extra
  447 + * @returns {*}
  448 + */
  449 + sendWechatMessage(uid, actPrizeId, extra = {}) {
  450 + return this.post({
  451 + url: '/yoho-message-controller/MessageRest/sendMessage',
  452 + data: {
  453 + sendScene: 'MINI_ACTIVITY_JOIN',
  454 + params: JSON.stringify({
  455 + activityTitle: extra.actName,
  456 + activityTime: `${timeFormat(extra.actStartTime)} - ${timeFormat(extra.actEndTime)}`,
  457 + pageUrl: '/page/subPackage/pages/zeroSell/detail?actPrizeId=' + actPrizeId
  458 + }),
  459 + uidList: [uid]
  460 + }
  461 + });
  462 + }
421 }; 463 };
@@ -37,8 +37,16 @@ const zeroBuy = { @@ -37,8 +37,16 @@ const zeroBuy = {
37 .then(res.json).catch(next); 37 .then(res.json).catch(next);
38 }, 38 },
39 zeroBuyPublish(req, res, next) { 39 zeroBuyPublish(req, res, next) {
40 - req.ctx(ActivityModel).editZerobuyStatus(req.body.id, 2)  
41 - .then(res.json).catch(next); 40 + let ctx = req.ctx(ActivityModel);
  41 + let id = req.body.id;
  42 +
  43 + ctx.editZerobuyStatus(id, 2).then(result => {
  44 + if (result.code === 200) {
  45 + ctx.sendWechatMessage(id);
  46 + }
  47 +
  48 + res.json(result);
  49 + }).catch(next);
42 }, 50 },
43 zeroBuyExport(req, res, next) { 51 zeroBuyExport(req, res, next) {
44 let id = req.query.id; 52 let id = req.query.id;
@@ -716,6 +716,47 @@ class AdminModel extends global.yoho.BaseModel { @@ -716,6 +716,47 @@ class AdminModel extends global.yoho.BaseModel {
716 }; 716 };
717 }); 717 });
718 } 718 }
  719 +
  720 +
  721 + /**
  722 + * 向参加活动用户发送开奖消息
  723 + * @param uid
  724 + * @param actPrizeId
  725 + * @param extra
  726 + * @returns {*}
  727 + */
  728 + async sendWechatMessage(id) {
  729 + let info = await Promise.all([
  730 + mysqlCli.query(`select name from ${TABLE_ACT_PRIZE_PRODUCT}
  731 + where id = :id limit 1`, {id}),
  732 + mysqlCli.query(`select union_id from ${TABLE_ACT_PRIZE_PRODUCT_USER}
  733 + where act_prize_id = :id`, {id})
  734 + ]);
  735 +
  736 + let productInfo = _.get(info, '[0][0]');
  737 + let userList = [];
  738 +
  739 + _.forEach(_.get(info, '[1]', []), value => {
  740 + userList.push(value.union_id);
  741 + });
  742 +
  743 + if (!userList.length) {
  744 + return Promise.resolve();
  745 + }
  746 +
  747 + return this.post({
  748 + url: '/yoho-message-controller/MessageRest/sendMessage',
  749 + data: {
  750 + sendScene: 'MINI_WINNING_NOTICE',
  751 + params: JSON.stringify({
  752 + activityTitle: productInfo.name,
  753 + activityTime: moment().format('YYYY-MM-DD HH:mm') + ':00',
  754 + pageUrl: '/page/subPackage/pages/zeroSell/detail?actPrizeId=' + id
  755 + }),
  756 + uidList: _.uniq(userList)
  757 + }
  758 + });
  759 + }
719 } 760 }
720 761
721 module.exports = AdminModel; 762 module.exports = AdminModel;