shoes.js 7.23 KB
/**
 * Created by qiujun on 2018/4/13.
 */
const ShoesModel = require('../models/shoes');
const _ = require('lodash');
const INVALID_PARAMS = '参数错误';
const http = require('http');

const shoes = {
    /**
     *检查并添加好友
     * @param req
     * @param res
     * @param next
     */
    async checkAddFriends(req, res) {
        let {unionId, friendUnionId, actId = 27} = req.query;

        if (!unionId || !friendUnionId) {
            return res.json({
                code: 400,
                message: INVALID_PARAMS
            });
        }
        if (unionId === friendUnionId) {
            return res.json({
                code: 401,
                message: '自己不能挑战自己'
            });
        }

        try {
            let checkFriend = await req.ctx(ShoesModel).checkFriends(unionId, friendUnionId);

            if (checkFriend && checkFriend.length > 0) {
                return res.json({
                    code: 200,
                    data: checkFriend[0],
                    message: '查询好友成功'
                });
            } else {
                let addFriend = await req.ctx(ShoesModel).addFriends(actId, unionId, friendUnionId);

                // console.log(addFriend);
                return res.json({
                    code: 200,
                    data: addFriend,
                    message: '添加好友成功'
                });
            }
        } catch (err) {
            return res.json(err);
        }
    },

    /**
     * 查询用户游戏记录
     * @param req
     * @param res
     * @returns {Promise.<void>}
     */
    async checkUserPlayed(req, res) {
        let {actId = 27, unionId} = req.query;

        try {
            let userInfo = await req.ctx(ShoesModel).checkUserPlayed(actId, unionId);
            let result = [];

            _.each(userInfo, (obj) => {
                // console.log(obj);
                result.push(obj);
            });

            return res.json({
                code: 200,
                count: result.length || 0,
                data: result,
                message: '查询用户游戏信息成功'
            });
        } catch (err) {
            return res.json(err);
        }
    },

    /**
     * 保存用户游戏记录
     * @param req
     * @param res
     * @returns {Promise.<void>}
     */
    async saveUserResult(req, res) {
        let {actId = 29, unionId, list = [], question} = req.body;

        if (list === []) {
            return res.json({
                code: 204,
                actId: actId,
                unionId: unionId,
                message: INVALID_PARAMS
            });
        } else if (parseInt(question, 10) === 0) {
            try {
                list = JSON.parse(list);
                let userInfo = await req.ctx(ShoesModel).getUserId(unionId);
                let userId = 0;
                let result = [];

                if (userInfo.length > 0) {
                    userId = userInfo[0].id;
                }

                if (userId) {
                    for (let i = 0; i < list.length; i++) {
                        let qsObj = list[i];
                        let qs = await req.ctx(ShoesModel).
                        saveResult(
                            actId,
                            userId,
                            qsObj.questionId,
                            qsObj.userSelectOption || 'F',
                            qsObj.isCorrect,
                            qsObj.time || 1000
                        );

                        result.push(qs);
                    }

                    return res.json({
                        code: 200,
                        userId,
                        actId,
                        unionId,
                        data: result,
                        message: '插入用户结果成功'
                    });
                } else {
                    return res.json({
                        code: 204,
                        message: '插入用户结果失败'
                    });
                }

            } catch (err) {
                return res.send(err);
            }

        } else {
            res.json({
                code: 201,
                message: '用户已经进行过游戏'
            });
        }
    },

    /**
     * 获取用户得分以及好友得分列表
     * @param req
     * @param res
     * @returns {Promise.<void>}
     */
    async getUserShareInfo(req, res) {
        let {unionId, actId = 29, per = 10} = req.query;

        if (!unionId) {
            return res.json({
                code: 400,
                message: INVALID_PARAMS
            });
        }
        try {
            let scoreInfo = await req.ctx(ShoesModel).getUserScore(unionId, actId, per);

            let friendsInfo = await req.ctx(ShoesModel).getFriendsScoreList(scoreInfo.userid, actId, per, scoreInfo.score);

            return res.json(
                {
                    code: 200,
                    message: '好友得分列表',
                    data: {
                        currentUser: scoreInfo,
                        record: friendsInfo.record,
                        friendList: friendsInfo.friendList
                    }
                }
            );
        } catch (err) {
            return res.json(err);
        }
    },

    /**
     * 获取全球排名信息
     * @param req
     * @param res
     * @returns {Promise.<void>}
     */
    async getUserPlayInfo(req, res) {
        let {unionId, actId = 29, per = 10} = req.query;

        if (!unionId) {
            return res.json({
                code: 400,
                message: INVALID_PARAMS
            });
        }
        try {
            let scoreInfo = await req.ctx(ShoesModel).getUserRankScore(unionId, actId, per);

            let friendsInfo = await req.ctx(ShoesModel).getAllUserScoreList(actId, per);

            return res.json(
                {
                    code: 200,
                    message: '好友得分列表',
                    data: {
                        currentUser: scoreInfo,
                        friendList: friendsInfo
                    }
                }
            );
        } catch (err) {
            return res.json(err);
        }
    },
    async getImageData(req, res) {

        let pic_url = req.query.image;

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

        return new Promise((resolve, reject) => {
            http.get(pic_url, (reqs) => {
                let chunks = [];
                let size = 0;

                reqs.on('data', (data) => {
                    chunks.push(data);
                    size += data.length;
                });
                reqs.on('end', () => {
                    let data = Buffer.concat(chunks, size);
                    let base64Image = 'data:image/jpg;base64,' + data.toString('base64');

                    return resolve(base64Image);
                });
                reqs.on('error', () => {
                    return reject(pic_url);
                });
            });
        }).then(result => {
            res.json({
                data: result
            });
        });
    },
};

module.exports = shoes;