y100.js 3.65 KB
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 = 20, tag = '', top = 1} = req.query;
        let top_int = parseInt(top, 10);

        if (!actId) {
            return res.json({
                code: 400,
                message: INVALID_PARAMS
            });
        }

        try {
            const result = await req.ctx(Y100Model).articleY100List({
                actId,
                pageNo,
                pageSize,
                tag,
                top: top_int
            });

            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;

        console.log(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;