Authored by yyq

send message

... ... @@ -4,6 +4,7 @@
* @date: 19/07/2018
*/
const _ = require('lodash');
const moment = require('moment');
const mysqlCli = global.yoho.utils.mysqlCli;
const MemoryCache = require('../../../utils/memory-cache');
... ... @@ -20,6 +21,14 @@ const PAGE_SIZE = 10;
const userTimesCache = new MemoryCache();
const timeFormat = (time) => {
if (_.isNumber(time)) {
time = moment.unix(time);
}
return moment(time).format('YYYY-MM-DD HH:mm:ss');
};
function handelResult(result) {
return {
code: 200,
... ... @@ -278,7 +287,9 @@ module.exports = class extends global.yoho.BaseModel {
message: '活动已结束或已达人数上限'
};
let status = getActivityStatus(_.get(info, '[0][0]'), _.get(info, '[1].join_num', 0));
let productInfo = _.get(info, '[0][0]');
let status = getActivityStatus(productInfo, _.get(info, '[1].join_num', 0));
if (!status || status >= 3) {
return errorData;
... ... @@ -292,7 +303,12 @@ module.exports = class extends global.yoho.BaseModel {
this.sendPrizeCode(extra.shareUid, actPrizeId, {isShareTake: true});
}
return this.sendPrizeCode(uid, actPrizeId, Object.assign(extra, {isShareTake: false}));
return this.sendPrizeCode(uid, actPrizeId, Object.assign(extra, {
isShareTake: false,
actName: productInfo.name,
actStartTime: productInfo.start_time,
actEndTime: productInfo.end_time
}));
}
/**
... ... @@ -409,6 +425,10 @@ module.exports = class extends global.yoho.BaseModel {
}
if (_.isNumber(insert)) {
if (userJoinTimes < 1 && extra.actName) {
this.sendWechatMessage(uid, actPrizeId, extra);
}
return {
code: 200,
data: {prizeCode},
... ... @@ -418,4 +438,26 @@ module.exports = class extends global.yoho.BaseModel {
return errorData;
}
}
/**
* 向用户发送获得抽奖码消息
* @param uid
* @param actPrizeId
* @param extra
* @returns {*}
*/
sendWechatMessage(uid, actPrizeId, extra = {}) {
return this.post({
url: '/yoho-message-controller/MessageRest/sendMessage',
data: {
sendScene: 'MINI_ACTIVITY_JOIN',
params: JSON.stringify({
activityTitle: extra.actName,
activityTime: `${timeFormat(extra.actStartTime)} - ${timeFormat(extra.actEndTime)}`,
pageUrl: '/page/subPackage/pages/zeroSell/detail?actPrizeId=' + actPrizeId
}),
uidList: [uid]
}
});
}
};
... ...
... ... @@ -37,8 +37,16 @@ const zeroBuy = {
.then(res.json).catch(next);
},
zeroBuyPublish(req, res, next) {
req.ctx(ActivityModel).editZerobuyStatus(req.body.id, 2)
.then(res.json).catch(next);
let ctx = req.ctx(ActivityModel);
let id = req.body.id;
ctx.editZerobuyStatus(id, 2).then(result => {
if (result.code === 200) {
ctx.sendWechatMessage(id);
}
res.json(result);
}).catch(next);
},
zeroBuyExport(req, res, next) {
let id = req.query.id;
... ...
... ... @@ -716,6 +716,47 @@ class AdminModel extends global.yoho.BaseModel {
};
});
}
/**
* 向参加活动用户发送开奖消息
* @param uid
* @param actPrizeId
* @param extra
* @returns {*}
*/
async sendWechatMessage(id) {
let info = await Promise.all([
mysqlCli.query(`select name from ${TABLE_ACT_PRIZE_PRODUCT}
where id = :id limit 1`, {id}),
mysqlCli.query(`select union_id from ${TABLE_ACT_PRIZE_PRODUCT_USER}
where act_prize_id = :id`, {id})
]);
let productInfo = _.get(info, '[0][0]');
let userList = [];
_.forEach(_.get(info, '[1]', []), value => {
userList.push(value.union_id);
});
if (!userList.length) {
return Promise.resolve();
}
return this.post({
url: '/yoho-message-controller/MessageRest/sendMessage',
data: {
sendScene: 'MINI_WINNING_NOTICE',
params: JSON.stringify({
activityTitle: productInfo.name,
activityTime: moment().format('YYYY-MM-DD HH:mm') + ':00',
pageUrl: '/page/subPackage/pages/zeroSell/detail?actPrizeId=' + id
}),
uidList: _.uniq(userList)
}
});
}
}
module.exports = AdminModel;
... ...