Authored by yyq

get api

/**
* 0元抽奖活动
* @author: yyq <yanqing.yang@yoho.cn>
* @date: 19/07/2018
*/
const zerobuyModel = require('../models/zero-buy');
module.exports = {
list(req, res, next) {
req.ctx(zerobuyModel).getList(req.query.status)
.then(res.json).catch(next);
},
content(req, res, next) {
req.ctx(zerobuyModel).getContent(req.query.prizeId)
.then(res.json).catch(next);
},
listMine(req, res, next) {
req.ctx(zerobuyModel).getListMine(req.query.uid)
.then(res.json).catch(next);
},
codeRecent(req, res, next) {
req.ctx(zerobuyModel).getCodeRecent()
.then(res.json).catch(next);
},
codeMine(req, res, next) {
let {uid, prizeId} = req.query;
req.ctx(zerobuyModel).getCodeMine(uid, prizeId)
.then(res.json).catch(next);
},
};
... ...
/**
* 0元抽奖活动
* @author: yyq <yanqing.yang@yoho.cn>
* @date: 19/07/2018
*/
const _ = require('lodash');
const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_ACT_PRIZE_PRODUCT = 'act_prize_product';
const TABLE_ACT_PRIZE_PRODUCT_CONTENT = 'act_prize_product_content';
const TABLE_ACT_PRIZE_PRODUCT_USER = 'act_prize_product_user';
function handelResult(result) {
return {
code: 200,
data: result
};
}
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 0元购活动列表
* @param status
* @param actId
* @returns {*}
*/
getList(status, actId) {
status = parseInt(status, 10);
actId = parseInt(actId, 10) || 0;
status = _.isNaN(status) ? '> 0' : `= ${status}`;
return mysqlCli.query(`select * from ${TABLE_ACT_PRIZE_PRODUCT}
where act_id = :actId and status ${status}`, {
actId
}).then(handelResult);
}
/**
* 0元购用户参与列表
* @param uid
* @param actId
* @returns {*}
*/
getListMine(uid, actId) {
uid = parseInt(uid, 10) || 0;
actId = parseInt(actId, 10) || 0;
return mysqlCli.query(`select u.*, p.name, p.price, p.status, p.cover_img from
${TABLE_ACT_PRIZE_PRODUCT_USER} u left join
${TABLE_ACT_PRIZE_PRODUCT} p on u.act_prize_id = p.id
where u.act_id = :actId and u.uid = :uid
order by u.create_time desc`, {
uid,
actId
}).then(handelResult);
}
/**
* 0元购详情
* @param actPrizeId
* @returns {*}
*/
getContent(actPrizeId) {
let resData = {};
if (!actPrizeId) {
return Promise.resolve(handelResult(resData));
}
return Promise.all([
mysqlCli.query(`select * from ${TABLE_ACT_PRIZE_PRODUCT}
where id = :actPrizeId limit 1`, {actPrizeId}),
mysqlCli.query(`select * from ${TABLE_ACT_PRIZE_PRODUCT_CONTENT}
where act_prize_id = :actPrizeId`, {actPrizeId})
]).then(result => {
let [product, content] = result;
if (product && product.length) {
resData = product[0];
resData.content = _.sortBy(content || [], o => {
return o.sort;
});
}
return resData;
}).then(handelResult);
}
/**
* 0元购抽奖码最近获取记录
* @returns {*}
*/
getCodeRecent() {
return mysqlCli.query(`select user_name, user_thumb, create_time
from ${TABLE_ACT_PRIZE_PRODUCT_USER}
order by u.create_time desc limit 10`).then(handelResult);
}
/**
* 0元购用户单个活动抽奖码
* @param uid
* @param actPrizeId
* @returns {*}
*/
getCodeMine(uid, actPrizeId) {
uid = parseInt(uid, 10) || 0;
actPrizeId = parseInt(actPrizeId, 10) || 0;
return mysqlCli.query(`select * from ${TABLE_ACT_PRIZE_PRODUCT_USER}
where act_prize_id = :actPrizeId and uid = :uid`, {
actPrizeId,
uid
}).then(handelResult);
}
};
... ...
... ... @@ -17,7 +17,10 @@ router.post('/couponExp/checkLogin', couponExp.checkLogin);
router.post('/couponExp/friendHelp', couponExp.friendHelp);
router.get('/zerobuy/list', zeroBuy.list);
router.get('/zerobuy/content', zeroBuy.content);
router.get('/zerobuy/list', zeroBuy.list); // 0元购活动列表
router.get('/zerobuy/list/mine', zeroBuy.listMine); // 0元购用户参与列表
router.get('/zerobuy/content', zeroBuy.content); // 0元购详情
router.get('/zerobuy/code/recent', zeroBuy.codeRecent); // 0元购抽奖码最近获取记录
router.get('/zerobuy/code/mine', zeroBuy.codeMine); // 0元购用户单个活动抽奖码
module.exports = router;
... ...
... ... @@ -9,7 +9,7 @@
CREATE TABLE IF NOT EXISTS act_prize_product (
`id` int(8) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`act_id` int(8) NOT NULL DEFAULT 0,
`name` varchar(200) NOT NULL DEFAULT '' comment '商品名称',
`name` varchar(200) NOT NULL DEFAULT '' comment '商品名称',
`price` decimal DEFAULT 0 comment '商品价格',
`status` TINYINT DEFAULT 0 comment '商品状态 0:关闭 1:活动开始 2:已开奖',
`start_time` int(10) NOT NULL DEFAULT 0 comment '活动开始时间',
... ... @@ -39,6 +39,8 @@ CREATE TABLE IF NOT EXISTS act_prize_product_user (
`act_id` int(8) NOT NULL DEFAULT 0,
`act_prize_id` int(8) NOT NULL DEFAULT 0,
`uid` int(8) NOT NULL DEFAULT 0,
`user_name` VARCHAR(50) DEFAULT '' comment '用户昵称',
`user_thumb` VARCHAR(200) DEFAULT '' comment '用户头像',
`union_id` VARCHAR(50) DEFAULT '' comment '微信union_id',
`prize_code` varchar(20) DEFAULT '' comment '抽奖码8位随机字母加数字(大写),且同一个大活动不重复',
`is_share_take` TINYINT DEFAULT 0 comment '是分享出去而得到的抽奖码',
... ... @@ -47,4 +49,4 @@ CREATE TABLE IF NOT EXISTS act_prize_product_user (
) DEFAULT CHARSET=utf8;
GO;
ALTER TABLE act_prize_product_user ADD INDEX INDEX_ACT_PRIZE_ID_UID (`act_prize_id`, `uid`);
GO;
\ No newline at end of file
GO;
... ...