Authored by 陈峰

y100

... ... @@ -654,102 +654,6 @@ const article = {
return res.json(result);
}).catch(next);
},
/**
* Y100文章列表
* @param req
* @param res
* @param next
*/
async y100List(req, res, next) {
const {actId, pageNo = 1, pageSize = 10, tag = ''} = req.query;
if (!actId) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
try {
const result = await req.ctx(ArticleModel).articleY100List({
actId,
pageNo,
pageSize,
tag
});
return res.json({
code: 200,
data: result,
pageNo: +pageNo,
pageSize: +pageSize,
message: GET_SUCCESS
});
} catch (err) {
return next(err);
}
},
/**
* Y100文章列表-随机
* @param req
* @param res
* @param next
*/
async y100RandomList(req, res, next) {
const {actId, num} = req.query;
if (!actId || num > 100) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
try {
const result = await req.ctx(ArticleModel).articleY100RandomList({
actId,
num
});
return res.json({
code: 200,
data: result,
message: GET_SUCCESS
});
} catch (err) {
return next(err);
}
},
/**
* Y100详情
* @param req
* @param res
* @param next
*/
async y100Detail(req, res, next) {
const {id} = req.query;
if (!id) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
try {
const result = await req.ctx(ArticleModel).articleY100Detail(id);
return res.json({
code: 200,
data: result,
});
} catch (err) {
return next(err);
}
},
};
... ...
const Y100Model = require('../models/y100');
const GET_SUCCESS = '获取成功';
const INVALID_PARAMS = '参数错误';
const y100 = {
/**
* Y100文章列表
* @param req
* @param res
* @param next
*/
async y100List(req, res, next) {
const {actId, pageNo = 1, pageSize = 10, tag = ''} = req.query;
if (!actId) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
try {
const result = await req.ctx(Y100Model).articleY100List({
actId,
pageNo,
pageSize,
tag
});
return res.json({
code: 200,
data: result,
pageNo: +pageNo,
pageSize: +pageSize,
message: GET_SUCCESS
});
} catch (err) {
return next(err);
}
},
/**
* Y100文章列表-随机
* @param req
* @param res
* @param next
*/
async y100RandomList(req, res, next) {
const {actId, num} = req.query;
if (!actId || num > 100) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
try {
const result = await req.ctx(Y100Model).articleY100RandomList({
actId,
num
});
return res.json({
code: 200,
data: result,
message: GET_SUCCESS
});
} catch (err) {
return next(err);
}
},
/**
* Y100详情
* @param req
* @param res
* @param next
*/
async y100Detail(req, res, next) {
const {id} = req.query;
if (!id) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
try {
const result = await req.ctx(Y100Model).articleY100Detail(id);
return res.json({
code: 200,
data: result,
});
} catch (err) {
return next(err);
}
},
async y100DeviceScan(req, res, next) {
const {openId, avatar, deviceNo, actId, userName} = req.body;
if (!deviceNo || !openId) {
return res.json({
code: 400
});
}
try {
const result = await req.ctx(Y100Model).deviceUse({openId, avatar, deviceNo, actId, userName});
return res.json(result);
} catch (err) {
return next(err);
}
},
async y100DevicePost(req, res, next) {
const {openId, deviceNo, actId, userImage, userLabel} = req.body;
if (!deviceNo || !openId) {
return res.json({
code: 400
});
}
try {
const result = await req.ctx(Y100Model).devicePost({openId, deviceNo, actId, userImage, userLabel});
return res.json(result);
} catch (err) {
return next(err);
}
},
async checkDeviceStatus(req, res, next) {
const {deviceNo, actId} = req.query;
if (!deviceNo || !actId) {
return res.json({
code: 400
});
}
try {
const result = await req.ctx(Y100Model).checkDeviceStatus({deviceNo, actId});
return res.json(result);
} catch (err) {
return next(err);
}
}
};
module.exports = y100;
... ...
... ... @@ -9,7 +9,6 @@ const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_USER = 'user';
const TABLE_ACT_ARTICLE = 'act_article';
const TABLE_ACT_ARTICLE_Y100 = 'act_article_y100';
const TABLE_ACT_ARTICLE_IMG = 'act_article_img';
const TABLE_ACT_ARTICLE_GOOD = 'act_article_good';
... ... @@ -475,141 +474,6 @@ class ArticleModel extends global.yoho.BaseModel {
);
}
/**
* 获取Y100列表
* @returns {*}
*/
articleY100List({actId, pageNo, pageSize, tag}) {
const params = {
actId,
start: (pageNo - 1) * pageSize,
page: _.parseInt(pageSize)
};
let sql = `
SELECT
AA.id,
AA.good_count,
AA.create_time,
AAY.name,
AAY.img_url
FROM ${TABLE_ACT_ARTICLE} AA
INNER JOIN ${TABLE_ACT_ARTICLE_Y100} AAY ON AA.id = AAY.article_id
WHERE AA.act_id = :actId`;
if (tag) {
sql += ' and AAY.tag like :tag';
params.tag = `%${tag}%`;
}
sql += ' ORDER BY AA.good_count DESC';
sql += ' LIMIT :start, :page';
return mysqlCli.query(sql, params);
}
/**
* 获取Y100列表-随机
* @returns {*}
*/
async articleY100RandomList({actId, num}) {
const params = {
actId,
};
let maxSql = `
SELECT
max(\`index\`) as maxIndex
FROM ${TABLE_ACT_ARTICLE_Y100} AAY
WHERE AAy.act_id = :actId`;
const maxResult = await mysqlCli.query(maxSql, params);
let maxIndex = maxResult[0].maxIndex;
if (num > maxIndex) {
throw '随机数量超过数据库大小';
}
const ids = [],
selectIds = [];
for (let i = 0; i <= maxIndex; i++) {
ids.push(i);
}
for (let i = 0; i < num; i++) {
const index = parseInt(Math.random() * ids.length, 10);
selectIds.push(ids[index]);
ids.splice(index, 1);
}
let sql = `
SELECT
AA.id,
AA.good_count,
AA.create_time,
AAY.name,
AAY.img_url
FROM ${TABLE_ACT_ARTICLE} AA
INNER JOIN ${TABLE_ACT_ARTICLE_Y100} AAY ON AA.id = AAY.article_id
WHERE AA.act_id = :actId`;
sql += ` and AAY.index in (${selectIds.join(',')})`;
sql += ` order by field(AAY.index,${selectIds.join(',')});`;
return mysqlCli.query(sql, params);
}
/**
* 获取Y100详情
* @returns {*}
*/
async articleY100Detail(id) {
let sql = `
SELECT
AAY.id,
AAY.act_id,
AAY.article_id,
AAY.img_url,
AAY.tag,
AAY.name,
AAY.career,
AAY.style,
AAY.interest,
AAY.skns,
AAY.create_time,
AA.good_count
FROM ${TABLE_ACT_ARTICLE_Y100} AAY
INNER JOIN ${TABLE_ACT_ARTICLE} AA ON AAY.article_id = AA.id
where AAY.id = :id`;
const query = await mysqlCli.query(sql, {
id
});
if (query.length === 1) {
const result = query[0];
const {skns} = query[0];
if (skns) {
const productsResult = await this.get({
data: {
method: 'h5.product.batch',
limit: skns.split(',').length,
productSkn: skns,
contain_all: 'Y'
},
param: {
cache: true
}
});
if (productsResult.code === 200 && productsResult.data) {
result.products = productsResult.data.product_list;
}
}
return result;
}
}
}
module.exports = ArticleModel;
... ...
const _ = require('lodash');
const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_ACT_ARTICLE = 'act_article';
const TABLE_ACT_ARTICLE_Y100 = 'act_article_y100';
const playExpireTime = 45 * 1000;
const showExpireTime = 30 * 1000;
class Y100Model extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 获取Y100列表
* @returns {*}
*/
articleY100List({actId, pageNo, pageSize, tag}) {
const params = {
actId,
start: (pageNo - 1) * pageSize,
page: _.parseInt(pageSize)
};
let sql = `
SELECT
AA.id,
AA.good_count,
AA.create_time,
AAY.name,
AAY.img_url
FROM ${TABLE_ACT_ARTICLE} AA
INNER JOIN ${TABLE_ACT_ARTICLE_Y100} AAY ON AA.id = AAY.article_id
WHERE AA.act_id = :actId`;
if (tag) {
sql += ' and AAY.tag like :tag';
params.tag = `%${tag}%`;
}
sql += ' ORDER BY AA.good_count DESC';
sql += ' LIMIT :start, :page';
return mysqlCli.query(sql, params);
}
/**
* 获取Y100列表-随机
* @returns {*}
*/
async articleY100RandomList({actId, num}) {
const params = {
actId,
};
let maxSql = `
SELECT
max(\`index\`) as maxIndex
FROM ${TABLE_ACT_ARTICLE_Y100} AAY
WHERE AAy.act_id = :actId`;
const maxResult = await mysqlCli.query(maxSql, params);
let maxIndex = maxResult[0].maxIndex;
if (num > maxIndex) {
throw '随机数量超过数据库大小';
}
const ids = [],
selectIds = [];
for (let i = 0; i <= maxIndex; i++) {
ids.push(i);
}
for (let i = 0; i < num; i++) {
const index = parseInt(Math.random() * ids.length, 10);
selectIds.push(ids[index]);
ids.splice(index, 1);
}
let sql = `
SELECT
AA.id,
AA.good_count,
AA.create_time,
AAY.name,
AAY.img_url
FROM ${TABLE_ACT_ARTICLE} AA
INNER JOIN ${TABLE_ACT_ARTICLE_Y100} AAY ON AA.id = AAY.article_id
WHERE AA.act_id = :actId`;
sql += ` and AAY.index in (${selectIds.join(',')})`;
sql += ` order by field(AAY.index,${selectIds.join(',')});`;
return mysqlCli.query(sql, params);
}
/**
* 获取Y100详情
* @returns {*}
*/
async articleY100Detail(id) {
let sql = `
SELECT
AAY.id,
AAY.act_id,
AAY.article_id,
AAY.img_url,
AAY.tag,
AAY.name,
AAY.career,
AAY.style,
AAY.interest,
AAY.skns,
AAY.create_time,
AA.good_count
FROM ${TABLE_ACT_ARTICLE_Y100} AAY
INNER JOIN ${TABLE_ACT_ARTICLE} AA ON AAY.article_id = AA.id
where AAY.id = :id`;
const query = await mysqlCli.query(sql, {
id
});
if (query.length === 1) {
const result = query[0];
const {skns} = query[0];
if (skns) {
const productsResult = await this.get({
data: {
method: 'h5.product.batch',
limit: skns.split(',').length,
productSkn: skns,
contain_all: 'Y'
},
param: {
cache: true
}
});
if (productsResult.code === 200 && productsResult.data) {
result.products = productsResult.data.product_list;
}
}
return result;
}
}
/**
* 使用设备更新用户信息
* @returns {*}
*/
async deviceUse({openId, userName, avatar, deviceNo, actId}) {
const deviceQuery = await mysqlCli.query(
`SELECT
user_id,
user_name,
user_avatar,
user_image,
user_label,
expire_time,
status
FROM
act_devices_y100
WHERE
act_id = :actId
AND
device_no = :deviceNo`,
{
deviceNo,
actId,
});
if (deviceQuery.length !== 1) {
return {
code: 400,
message: '设备信息错误'
};
}
const device = _.head(deviceQuery);
if (device.expire_time && device.expire_time > Date.now()) {
return {
code: 401,
message: `用户${device.user_name}正在使用大屏,请稍等一会`
};
}
const expireTime = Date.now() + playExpireTime;
await mysqlCli.update(`
UPDATE
act_devices_y100
SET
user_id = :openId,
user_name = :userName,
user_avatar = :avatar,
user_image = '',
user_label = '',
expire_time = :expireTime,
status = 0
WHERE
act_id = :actId
AND
device_no = :deviceNo`, {
openId,
userName,
avatar,
expireTime,
actId,
deviceNo
});
return {
code: 200,
message: '扫描成功',
data: {
expireTime
}
};
}
/**
* 使用设备更新用户信息
* @returns {*}
*/
async devicePost({openId, deviceNo, actId, userImage, userLabel}) {
const deviceQuery = await mysqlCli.query(
`SELECT
user_id,
user_name,
user_avatar,
user_image,
user_label,
expire_time,
status
FROM
act_devices_y100
WHERE
act_id = :actId
AND
device_no = :deviceNo`,
{
deviceNo,
actId,
});
if (deviceQuery.length !== 1) {
return {
code: 400,
message: '设备信息错误'
};
}
const device = _.head(deviceQuery);
if (device.expire_time <= Date.now()) {
return {
code: 400,
message: '提交超时请再玩一次吧'
};
}
if (device.user_id !== openId) {
return {
code: 401,
message: `用户${device.user_name}正在使用大屏,请稍等一会`
};
}
const expireTime = Date.now() + showExpireTime;
await mysqlCli.update(`
UPDATE
act_devices_y100
SET
user_image = :userImage,
user_label = :userLabel,
expire_time = :expireTime,
status = 1
WHERE
act_id = :actId
AND
device_no = :deviceNo`, {
openId,
expireTime,
userImage,
userLabel,
actId,
deviceNo
});
return {
code: 200,
message: '提交成功',
};
}
/**
* 检查设备状态
* @returns {*}
*/
async checkDeviceStatus({deviceNo, actId}) {
const deviceQuery = await mysqlCli.query(
`SELECT
user_id,
user_name,
user_avatar,
user_image,
user_label,
expire_time,
status
FROM
act_devices_y100
WHERE
act_id = :actId
AND
device_no = :deviceNo`,
{
deviceNo,
actId,
});
if (deviceQuery.length !== 1) {
return {
code: 400,
message: '设备信息错误'
};
}
const device = _.head(deviceQuery);
if (device.expire_time > Date.now()) {
if (device.status === 0) {
return {
code: 1002,
message: 'playing',
data: {
openId: device.user_id,
userName: device.user_name,
avatar: device.user_avatar,
expireTime: device.expire_time
}
};
}
if (device.status === 1) {
return {
code: 10003,
message: 'show image',
data: {
openId: device.user_id,
userName: device.user_name,
avatar: device.user_avatar,
userImage: device.user_image,
userLabel: device.user_label,
expireTime: device.expire_time
}
};
}
}
return {
code: 1001,
message: 'show qrcode'
};
}
}
module.exports = Y100Model;
... ...
... ... @@ -7,12 +7,13 @@ const express = require('express');
const router = express.Router(); // eslint-disable-line
const auth = require('../../doraemon/middleware/auth');
const article = require('./controllers/article');
const y100 = require('./controllers/y100');
router.post('/like', article.like);
router.get('/list', article.list);
router.get('/y100list', article.y100List);
router.get('/y100randomlist', article.y100RandomList);
router.get('/y100detail', article.y100Detail);
router.get('/y100list', y100.y100List);
router.get('/y100randomlist', y100.y100RandomList);
router.get('/y100detail', y100.y100Detail);
router.get('/listNoImg', article.listNoImg);
router.get('/listNoUser', article.listNoUser);
router.post('/publish', auth, article.publish);
... ...
... ... @@ -28,10 +28,11 @@ CREATE TABLE IF NOT EXISTS act_devices_y100 (
`act_id` int(8) NOT NULL DEFAULT 0,
`device_no` varchar(200) DEFAULT '',
`user_id` varchar(200) DEFAULT '',
`user_name` varchar(200) DEFAULT '',
`user_avatar` varchar(200) DEFAULT '',
`user_image` varchar(200) DEFAULT '',
`user_label` varchar(200) DEFAULT '',
`expire_time` timestamp NOT NULL,
`expire_time` bigint NOT NULL,
`status` TINYINT DEFAULT 0,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) DEFAULT CHARSET=utf8;
... ...