guochao.js 1.95 KB
/* eslint-disable array-callback-return */
const {actGuochaoShop} = require('../../../db');

class Guochao extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    async checkFavs(uid, ids, type) {
        try {
            let result = await this.get({data: {
                method: 'app.favorite.batchCheckIsFavorite',
                favIds: ids,
                uid: uid,
                type: type }
            });

            if (result.code === 200) {
                let obj = {};

                result.data.map((value)=>{
                    obj[value.id] = value.favorite;
                });
                return Promise.resolve({code: 200, result: true, data: obj});
            } else {
                return Promise.resolve(result);
            }
        } catch (e) {
            return Promise.resolve({code: 202, result: false, errorMsg: e});
        }

    }

    async addFavAsync(uid, id, type) {
        try {

            let checkFlag = await this.get({ data: {
                method: 'app.favorite.isFavorite',
                id: id,
                uid: uid,
                type: type
            }});

            if (checkFlag.data) {
                return Promise.resolve({code: 203, result: true, data: '已经收藏过'});
            }

            await this.get({
                data: {
                    method: 'app.favorite.add',
                    id: id,
                    uid: uid,
                    type: type
                }
            });

            let item = await actGuochaoShop.findOne({where: {shop_id: id}});
            let result = await item.increment('collect_count');


            return Promise.resolve({code: 200, result: true, data: result});
        } catch (e) {
            return Promise.resolve({code: 202, result: false, errorMsg: e});
        }
    }

    async list() {
        return actGuochaoShop.findAll({order: [['sort', 'desc']]});
    }
}

module.exports = Guochao;