Message.php 8.51 KB
<?php

namespace Home;

use WebPlugin\Helpers;
use LibModels\Web\Home\MessageData;
use WebPlugin\Paging;

/**
 * 个人中心——我的消息逻辑处理
 */
class MessageModel
{

    /**
     * 我的消息列表
     * @param type $uid
     * @param type $page
     * @param type $size
     * @return array
     */
    public static function getInboxList($uid, $page, $size)
    {
        $result = array('messages' => array(), 'pager' => array());
        $inboxlist = MessageData::getInboxList($uid, $page, $size);
        if (!empty($inboxlist['data']['list']) && !empty($inboxlist['data'])) {
            foreach ($inboxlist['data']['list'] as $inboxkey => $inboxval) {
                //信息过滤
                if ($inboxval['type'] == '$inboxval' || $inboxval['type'] == 'notice') {
                    continue;
                }
                $messages[$inboxkey]['href'] = Helpers::url('/home/message/content', array('page' => $page, 'id' => $inboxval['id'])); //消息内容链接
                $messages[$inboxkey]['sender'] = isset($inboxval['from']) ? $inboxval['from'] : 0; //消息发言人
                $messages[$inboxkey]['isNew'] = $inboxval['is_read'] == 'Y' ? false : true; //消息是否已读
                $messages[$inboxkey]['title'] = isset($inboxval['title']) ? $inboxval['title'] : 0;
                ; //消息标题
                $messages[$inboxkey]['time'] = date('Y-m-d H:i:s', $inboxval['create_time']); //消息创建时间
                $messages[$inboxkey]['id'] = isset($inboxval['id']) ? $inboxval['id'] : 0;
                ; //消息唯一标识id
                $result['messages'] = $messages;
            }
            $total = isset($inboxlist['data']['total']) ? $inboxlist['data']['total'] : 0; //消息总条数
            $pageTotal = isset($inboxlist['data']['page_total']) ? $inboxlist['data']['page_total'] : 0; //总页数
            $page = isset($inboxlist['data']['page']) ? $inboxlist['data']['page'] : 0; //当前页
            $result['pager'] = self::getPager($page, $total, $pageTotal, $size);
        }
        else {
            $result['messages']['empty'] = '您尚未收到任何短消息';
        }
        return $result;
    }

    /**
     * 我的信息内容详情
     * @param type $uid
     * @param type $page
     * @param type $size
     * @param type $id
     * @return type array
     */
    public static function getContentData($uid, $page, $size, $id, $name)
    {
        $result = array();
        $inboxlist = MessageData::getInboxList($uid, $page, $size);
        if (!empty($inboxlist['data']['list'])) {
            foreach ($inboxlist['data']['list'] as $contentkey => $contentval) {
                if ($contentval['id'] != $id) {
                    continue;
                }
                //信息过滤
                if ($contentval['type'] == 'showGetCoin' || $contentval['type'] == 'notice') {
                    continue;
                }
                $result['sender'] = $contentval['from']; //消息发言人
                $result['title'] = $contentval['title']; //消息标题
                $result['time'] = date('Y-m-d H:i:s', $contentval['create_time']); //消息创建时间
                //判断消息类型
                switch ($contentval['type']) {
                    case 'pullCoupon':
                        //领取生日券消息
                        $result['birthCoupon'] = array(
                            'text' =>  $name . ' 有货祝您生日快乐!感谢您对有货的支持,特赠您VIP生日专属礼,即刻享受生日福利吧!'
                        );

                        if ($contentval['body']['is_collar'] === 'Y') {
                            $result['birthCoupon']['over'] = true;
                            $result['birthCoupon']['overText'] = '已领取';
                            break;
                        }

                        if ($contentval['body']['is_over_time'] === 'Y') {
                            $result['birthCoupon']['over'] = true;
                            $result['birthCoupon']['overText'] = '生日券已过期';
                            break;
                        }

                        $result['coupons'] = array();

                        $coupondata = MessageData::getCouponId($uid); //获取优惠券信息

                        if ($coupondata['code'] !== 200) {
                            $result['birthCoupon']['errMsg'] = $coupondata['message'];
                            break;
                        }

                        if (!empty($coupondata['data'])) {
                            foreach ($coupondata['data'] as $couponval) {
                                $result['coupons'][] = array(
                                    'id' => isset($couponval['id']) ? $couponval['id'] : '',
                                    'remark' => isset($couponval['couponName']) ? $couponval['couponName'] : '',
                                    'useTime' => isset($contentval['body']['use_time']) ? $contentval['body']['use_time'] : '',
                                    'pickTime' => isset($contentval['body']['collar_time']) ? $contentval['body']['collar_time'] : '',
                                    'canPick' => true);
                            }
                        }
                        else {
                            $result['coupons'][] = array(
                                //已过期生日券信息
                                'remark' => isset($contentval['body']['name']) ? $contentval['body']['name'] : '',
                                'useTime' => isset($contentval['body']['use_time']) ? $contentval['body']['use_time'] : '',
                                'pickTime' => isset($contentval['body']['collar_time']) ? $contentval['body']['collar_time'] : '',
                                'canPick' => false);
                        }
                        break;
                    case 'button':
                        //促销活动
                        $result['sale'] = array(
                            'image' => isset($contentval['body']['image']) ? $contentval['body']['image'] : '',
                            'content' => isset($contentval['body']['text']) ? $contentval['body']['text'] : '',
                            'btnLink' => isset($contentval['body']['pc_link']) ? $contentval['body']['pc_link'] : '',
                            'btnName' => isset($contentval['body']['button_text']) ? $contentval['body']['button_text'] : '',
                        );
                        break;
                    case 'pushCoupon':
                        //查看优惠券
                        $result['coupons'] = array();
                        $result['coupons'][] = array(
                            'remark' => isset($contentval['body']['coupon_name']) ? $contentval['body']['coupon_name'] : '',
                            'useTime' => isset($contentval['body']['time']) ? $contentval['body']['time'] : '',
                            'id' => isset($contentval['body']['inboxId']) ? $contentval['body']['inboxId'] : '',
                            'price' => isset($contentval['body']['price']) ? $contentval['body']['price'] : '',
                            'url' => Helpers::url('/home/coupons', array('t' => microtime(true)))
                        );
                        break;
                    default:
                        //普通文本
                        $result['text'] = array(
                            'content' => isset($contentval['body']['content']) ? $contentval['body']['content'] : ''
                        );
                        break;
                }
            }
        }
        return $result;
    }

    /**
     * 设置分页数据
     * @param type $page
     * @param type $total
     * @param type $totalPage
     * @param type $size
     * @return type
     */
    public static function getPager($page, $total, $totalPage, $size)
    {
        $result = array();
        if (isset($page) && isset($total) && isset($totalPage)) {
            $result['count'] = $total;
            $result['curPage'] = $page;
            $result['totalPages'] = $totalPage;
            $result['hasCheckAll'] = false;
            $paging = new Paging('yoho');
            $paging->setTotal($total)->setSize($size)->setQuery(array('page' => $page));
            $result['pagerHtml'] = $paging->view(false);
        }
        return $result;
    }

}