message.js 7.49 KB
// 消息 by acgpiano
'use strict';

const moment = require('moment');
const api = global.yoho.API;
const helpers = global.yoho.helpers;

// 格式化时间
const _formatTime = (time) => {
    return moment(time).format('HH:mm:ss');
};

// 格式年月日
const _formatDay = (day) => {
    return moment(day).format('YYYY.MM.DD');
};

// 获取列表页
const _list = (result) => {
    let final = [];

    if (result.data && result.data.list) {
        // PHP排序了,应该不用
        for (let item of result.data.list) {

            let obj = {
                id: item.id,
                isNotReaded: item.is_read === 'Y' ? false : true,
                title: item.title,
                time: `${item.create_date} ${_formatTime(item.create_time * 1000)}`,
            };

            if (item.body && item.type === 'pullCoupon') {
                Object.assign(obj, {
                    birthType: true,

                    // name: item.body.name,
                    // collarTime: item.body.collar_time,
                    // useTime: item.body.use_time,
                    isCollar: item.body.is_collar === 'Y' ? true : false,
                    isOverTime: item.body.is_over_time === 'Y' ? true : false
                });
            }

            final.push(obj);
        }
    }
    return final;
};

// 获取生日信息
const _getBirthCouponById = (uid) => {
    return api.get('', {
        method: 'app.promotion.queryBirthCoupon',
        uid: uid,
        couponType: 4,
    }).then(result => {
        
        if (result && result.data) {
            return result.data;
        } else {
            if (result.code === 410) {
                return [{err: result.message}];
            } else if (result.code === 411) {
                return [{err: result.message}];
            } else {
                return [];
            }
        }
    });
};

// 获取详情页
const _detail = (result, id, couponData) => {
    let final = {};

    if (result.data && result.data.list) {
        for (let item of result.data.list) {
            if (item.id === Number(id) && item.type !== 'showGetCoin' && item.type !== 'notice') {
                final.sender = item.from;
                final.title = item.title;
                final.time = `${item.create_date} ${_formatTime(item.create_time * 1000)}`; // 时间

                // 判断消息类型
                switch (item.type) {
                    case 'pullCoupon':
                        final = {
                            ticket: []
                        };
                        final.birthType = true;

                        if (result.data.list[0].body.is_collar === 'Y') {

                            final = {
                                isCollar: true
                            };

                        } else {
                            if (couponData && !couponData[0].err) {
                                for (let coupon of couponData) {
                                    final.ticket.push({
                                        id: coupon.id ? coupon.id : '',
                                        couponAmount: coupon.couponAmount,
                                        couponName: coupon.couponName.split('-')[0],
                                        startTime: _formatDay(coupon.startTime),
                                        endTime: _formatDay(coupon.endTime),
                                        useLimit: coupon.useLimit
                                    });
                                }
                            } else {
                                final = {
                                    err: couponData[0].err
                                };
                            }
                        }

                        // 领取生日券消息
                        // final.coupons = [];
                        // for (let coupon of couponData) {
                        //     final.coupons.push({
                        //         id: coupon.id ? coupon.id : '',
                        //         remark: coupon.couponName ? coupon.couponName : '',
                        //         useTime: item.body.use_time ? item.body.use_time : '',
                        //         pickTime: item.body.collar_time ? item.body.collar_time : '',
                        //         canPick: true
                        //     });
                        // }
                        break;
                    case 'button':
                        // 促销活动
                        if (item.body) {
                            final.sale = {
                                image: item.body.image ? item.body.image : '',
                                content: item.body.text ? item.body.text : '',
                                btnLink: item.body.pc_link ? item.body.pc_link : '',
                                btnName: item.body.button_text ? item.body.button_text : '',
                            };
                        }
                        break;
                    case 'pushCoupon':
                        // 查看优惠券
                        if (item.body) {
                            final.coupons = [];
                            final.coupons.push({
                                remark: item.body.coupon_name ? item.body.coupon_name : '',
                                useTime: item.body.time ? item.body.time : '',
                                id: item.body.inboxId ? item.body.inboxId : '',
                                price: item.body.price ? item.body.price : '',
                                url: helpers.urlFormat('/home/coupons', {
                                    t: (new Date().getTime()) / 1000,
                                }),
                            });
                        }
                        break;
                    default:
                        // 普通文本
                        final.text = {
                            content: item.body.content ? item.body.content : '',
                        };
                        break;
                }
            }
        }
    }
    return final;
};

// 获取列表或详情
const getList = (params) => {
    return api.get('', {
        method: 'app.inbox.getlist',
        page: params.page || 1,
        size: params.size || 10,
        uid: params.uid,
    }).then(result => {
        if (params.msgid) {
            // 绕的一比
            return _getBirthCouponById(params.uid).then(couponData => {
                return _detail(result, params.msgid, couponData);
            });
        } else {
            return _list(result);
        }
    });
};

// 删除消息
const delMsg = (params) => {
    return api.get('', {
        method: 'app.inbox.delmessage',
        uid: params.uid,
        id: params.msgid,
    }).then(result => {
        if (result && result.code === 200) {
            return {
                code: 200
            };
        } else {
            return {
                code: 400,
                message: '出错啦~',
            };
        }
    });
};

// 领取优惠券
const pickCoupon = (params) => {
    return api.get('', {
        method: 'app.promotion.getCoupon',
        uid: params.uid,
        couponId: params.couponId,
    }).then(result => {
        if (result && result.code === 200) {
            return {
                code: 200
            };
        } else {
            return {
                code: result.code,
                message: result.message,
            };
        }
    });
};

module.exports = {
    getList,
    delMsg,
    pickCoupon,
};