AbstractService.class.php 9.09 KB
<?php
/**
 * AbstractService.class.php
 *
 * Service 层基类
 * @author: daijun
 */

namespace Common\Service;

use Common\Common\Constant;
use Common\Common\Msg;
use Common\Common\User;

abstract class AbstractService extends \Com\Service
{

    // 构造方法
    public function __construct()
    {

        parent::__construct();
    }

    /**
     * 即时消息通知方法 【发消息统一写这里,方便维护】
     *
     * @param $params -通知参数
     *          + uids      -用户uid数组
     *              + memID     -用户uid
     *          + dp_ids    -部门id数组
     *              + dpID      -部门id
     *          + tag_ids   -标签id数组
     *              + tagID     -标签id
     *          + ac_id  -活动id
     *          + title  -活动标题
     *          + start_time  -活动开始时间
     *          + end_time  -活动结束时间
     *          + integral  -活动消耗积分
     * @param $msg_type -通知文案类型
     *
     * @return bool
     */
    public function send_msg($params, $msg_type)
    {
        // 本方法根据类型区分拼接不同的文案
        switch ($msg_type) {
            case Constant::MSG_ACTIVITY_PUBLISH:
                // 活动发布时
                $data = [
                    'title' => '【积分中心】有新的积分抽奖活动发布,快来参与吧',
                    'description' => '标题:' . $params['title'] . "\r\n活动时间:" .
                        rgmdate((string)$params['start_time'], 'Y-m-d H:i') . '~' .
                        ($params['end_time'] ? rgmdate((string)$params['end_time'], 'Y-m-d H:i') : '无') . "\r\n参与积分:" . $params['integral'],
                    'picUrl' => '',
                    'type' => ''
                ];
                break;
            case Constant::MSG_ACTIVITY_REMIND:
                // 手动提醒
                $data = [
                    'title' => '【积分中心】您有一个抽奖活动尚未参与,快来瞧瞧吧',
                    'description' => '标题:' . $params['title'] . "\r\n活动时间:" .
                        rgmdate((string)$params['start_time'], 'Y-m-d H:i') . '~' .
                        ($params['end_time'] ? rgmdate((string)$params['end_time'], 'Y-m-d H:i') : '无') . "\r\n参与积分:" . $params['integral'],
                    'picUrl' => '',
                    'type' => ''
                ];
                break;
            case Constant::MSG_PRIZE:
                // 中奖时
                $data = [
                    'title' => '【积分中心】恭喜您,获得' . $params['name'],
                    'description' => '标题:' . $params['title'] . "\r\n活动时间:" .
                        rgmdate((string)$params['start_time'], 'Y-m-d H:i') . '~' .
                        ($params['end_time'] ? rgmdate((string)$params['end_time'], 'Y-m-d H:i') : '无') . "\r\n参与积分:" . $params['integral'],
                    'picUrl' => '',
                    'type' => Constant::MSG_PRIZE
                ];
                break;

            default:

                return true;
        }

        $this->send($params, $data);

        return true;
    }

    /**
     * 发送消息
     *
     * @param $params -通知参数
     *          + is_all    -全公司
     *          + uids      -用户uid数组
     *              + memID     -用户uid
     *          + dp_ids    -部门id数组
     *              + dpID      -部门id
     *          + tag_ids   -标签id数组
     *              + tagID     -标签id
     *          + obj_id   -数据id
     * @param array $condition 消息参数
     *          + title  -标题
     *          + description -内容
     *          + picUrl -图片URL
     *          + type   -数据类型(1:活动  2:奖品)
     *
     * @return bool
     */
    private function send($params = [], $condition = [])
    {
        // 发送消息接收人
        $msgUser = !empty($params['uids']) ? $params['uids'] : [];
        $toUser = isset($params['is_all']) && !empty($params['is_all']) ? '@all' : $msgUser;

        // 发送消息部门
        $toParty = !empty($params['dp_ids']) ? $params['dp_ids'] : [];

        $articles = [
            [
                'title' => $condition['title'],
                'description' => $condition['description'],
                'url' => oaUrl('Frontend/Index/Msg/Index',
                    [
                        'ac_id' => intval($params['ac_id']),
                        'type' => $condition['type']
                    ]
                ),
                'picUrl' => '',
            ],
        ];

        $msgServ = &Msg::instance();

        // 根据部门ID查询其下的所有用户UID(兼容发送对象超过1000人的情况)
        if (!empty($toParty) && $toUser != '@all') {

            $params = [
                'dpIdList' => $toParty, // 跟UC确认可以传部门的数组
                'departmentChildrenFlag' => 1, // 按部门条件查询时,表示部门递归查询人员
            ];
            // 部门下的用户UID获取
            $toPartUser = User::instance()->listAllBasic($params);
            $toUser = array_merge($toUser, (array)array_column($toPartUser, 'memUid'));
        }

        // 发消息给指定人员
        if ($toUser != '@all') {
            // 去除用户UID数组 空值和重复值
            $toUser = is_array($toUser) ? array_unique(array_filter($toUser)) : [$toUser];
            $uid_groups = array_chunk($toUser, Msg::USER_MAX_COUNT);
            foreach ($uid_groups as $uid_group) {
                $msgServ->sendNews($uid_group, null, null, $articles);
            }
        } else {
            // 发送给全公司
            $msgServ->sendNews($toUser, null, null, $articles);
        }

        return true;

    }


    /**
     * 字符串截取,支持中文和其他编码
     * static
     * access public
     *
     * @param string $str 需要转换的字符串
     * @param int $start 开始位置
     * @param string $length 截取长度
     * @param string $charset 编码格式
     * @param bool|string $suffix 截断显示字符
     *
     * @return string
     */
    public function cutstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true)
    {
        if (function_exists("mb_substr")) {
            $slice = mb_substr($str, $start, $length, $charset);
        } elseif (function_exists('iconv_substr')) {
            $slice = iconv_substr($str, $start, $length, $charset);
            if (false === $slice) {
                $slice = '';
            }
        } else {
            $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
            $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
            $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
            $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
            preg_match_all($re[$charset], $str, $match);
            $slice = join("", array_slice($match[0], $start, $length));
        }

        if (get_str_len($str) > $length && $slice) {

            $slice = $slice . '...';
        }

        return $suffix ? $slice : $slice;
    }


    /**
     * 替换字符串中的/r/n为回车
     *
     * @param string $str 字符串
     *
     * @return string
     */
    public function Enter($str = '')
    {

        $str = trim($str); //清除字符串两边的空格

        $replace = ["\r\n", "\n", "\r", "&crarr;", "&#8629;"];

        return trim(str_replace($replace, "<br/>", $str)); //返回字符串

    }


    /**
     * 数组分页
     *
     * @param $data array 需分页的数组
     * @param $page int 页码
     * @param $limit int 每页数量
     *
     * @return array
     */
    public function get_page_data($data, $page, $limit)
    {
        //计算每次分页的开始位置
        $start_limt = ($page - 1) * $limit;
        $list = array_slice($data, $start_limt, $limit);

        return $list;
    }


    /**
     * 活动状态转化函数
     *
     * @param string $activity_status 活动状态
     * @param string $begin_time 开始时间
     * @param string $end_time 结束时间
     *
     * @return int 活动状态1:草稿,2:未开始,3:进行中,4:已结束,5:已终止
     */
    public function activity_status($activity_status = '0', $begin_time = '0', $end_time = '0')
    {
        if ($activity_status == Constant::ACTIVITY_DRAFT) {
            // 草稿
            $status = Constant::STATUS_DRAFT;
        } elseif ($activity_status == Constant::ACTIVITY_STOP) {
            // 已终止
            $status = Constant::STATUS_STOP;
        } else {
            // 已发布
            if ($begin_time > MILLI_TIME) {
                // 未开始
                $status = Constant::STATUS_NOT_START;
            } elseif ($begin_time <= MILLI_TIME && ($end_time > MILLI_TIME || $end_time == 0)) {
                // 进行中
                $status = Constant::STATUS_ING;
            } else {
                // 已结束
                $status = Constant::STATUS_END;
            }
        }

        return $status;
    }


}