AbstractService.class.php 13 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
<?php
/**
 * AbstractService.class.php
 * Service 层基类
 * @author: houyingcai
 * @copyright: vchangyi.com
 */
namespace Common\Service;

use Common\Common\Msg;
use Common\Common\Tag;
use Common\Common\User;
use VcySDK\Member;
use VcySDK\Service;


abstract class AbstractService extends \Com\Service
{
    // 默认页码
    const DEFAULT_PAGE = 1;
    // 默认每页条数
    const DEFAULT_LIMIT = 15;

    //【活动中心】成功发布活动
    const MSG_ACTIVITY_PUBLISH = 1;

    //【活动中心】活动被编辑
    const MSG_ACTIVITY_UPDATE = 2;

    //【活动中心】评论被回复
    const MSG_COMMENT_REPLY = 3;

    //【活动中心】未参与提醒消息
    const MSG_REMAIND_NO_JOIN_USERS = 4;

    // 消息ID类型为活动
    const ACTIVITY = 1;

    // 消息ID类型为回复
    const REPLY = 2;

    // 积分规则:默认策略
    const INTEGRAL_ACTION_TYPE_DEFAULT = 1;
    // 积分规则:不使用策略
    const INTEGRAL_ACTION_TYPE_NO = 2;
    // 积分规则:使用自定义策略
    const INTEGRAL_ACTION_TYPE_MY = 3;

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

        parent::__construct();
    }

    /**
     * 即时消息通知方法 【发消息统一写这里,方便维护】
     * @param $params -通知参数
     *          + uids      -用户uid数组
     *              + memID     -用户uid
     *          + dp_ids    -部门id数组
     *              + dpID      -部门id
     *          + tag_ids   -标签id数组
     *              + tagID     -标签id
     *          + cid   -活动ID或者回复ID
     *          + description -摘要
     * @param $type -通知文案类型
     * @return bool
     */
    public function send_msg($params, $type)
    {
        // 获取应用名称
        $application_name = cfg('APPLICATION_NAME');

        $search = ["&nbsp;","&nbsp"];
        $replace = [" ", " "];
        $content = str_replace($search, $replace, strip_tags($params['content']));

        // 本方法根据类型区分拼接不通的文案
        switch ($type) {
            case self::MSG_ACTIVITY_PUBLISH:
                $data = array(
                    'title' => '【' . $application_name. '】有新的活动,快来参与吧',
                    'description' => '活动标题:' . $this->cutstr($params['subject'], 0, 20) . "\r\n活动时间:" .
                    rgmdate((string) $params['begin_time'], 'Y-m-d H:i') . '~' .
                    ($params['end_time'] ? rgmdate((string) $params['end_time'], 'Y-m-d H:i') : '无') . "\r\n活动描述:" . $this->cutstr($content, 0, 20),
                    'picUrl' => '',
                    'type' => self::ACTIVITY,
                );
                break;
            case self::MSG_ACTIVITY_UPDATE:
                $data = array(
                    'title' => '【' . $application_name. '】您被邀请参加的活动,信息有更新,请尽快查看!',
                    'description' => '活动标题:' . $this->cutstr($params['subject'], 0, 20) . "\r\n活动时间:" .
                    rgmdate((string) $params['begin_time'], 'Y-m-d H:i') . '~' .
                    ($params['end_time'] ? rgmdate((string) $params['end_time'], 'Y-m-d H:i') : '无') . "\r\n活动描述:" . $this->cutstr($content, 0, 20),
                    'picUrl' => '',
                    'type' => self::ACTIVITY,
                );
                break;
            case self::MSG_COMMENT_REPLY:
                $data = array(
                    'title' => '【' . $application_name. '】您的帖子有了新的评论,快来瞧瞧吧',
                    'description' => '活动标题:' . $this->cutstr($params['subject'], 0, 20) . "\r\n评论人:" . $params['username'] . "\r\n评论内容:" . $this->cutstr($content, 0, 10),
                    'picUrl' => '',
                    'type' => self::REPLY,
                );
                break;
            case self::MSG_REMAIND_NO_JOIN_USERS:
                $data = [
                    'title' => '【' . $application_name . '】您有一个活动尚未参与',
                    'description' => '标题:' . $params['subject'] . "\r\n活动时间:" . rgmdate((string)$params['begin_time'], 'Y-m-d H:i') . '~' . ($params['end_time'] ? rgmdate((string)$params['end_time'], 'Y-m-d H:i') : '无') . "\r\n活动描述:" . $this->cutstr($content, 0, 20),
                    'picUrl' => '',
                    'type' => self::ACTIVITY,
                ];
                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
     *          + cid   -ID
     * @param array $condition 消息参数
     *          + title  -标题
     *          + description -内容
     *          + picUrl -图片URL
     * @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'] : [];

        // 角色、岗位、标签发送消息
        if (!empty($params['job_ids']) || !empty($params['role_ids']) || !empty($params['tag_ids'])) {

            // 获取角色、岗位、标签下的人员
            $u_dp_ids = $this->get_job_role_uids($params);

            // 如果权限里有人员并且选择的不是全公司
            if (!empty($u_dp_ids)  && empty($params['is_all'])) {

                if (!empty($u_dp_ids['u_ids'])) {
                    $toUser = array_merge($toUser, $u_dp_ids['u_ids']);

                    // 去重复人员
                    $toUser = array_unique($toUser);
                }

                if (!empty($u_dp_ids['dp_ids'])) {
                    $toParty = array_merge($toParty, $u_dp_ids['dp_ids']);
                }
            }
        }

        // 如果用户和部门为空则不发消息
        if (empty($toUser) && empty($toParty)) {

            return true;
        }

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

        $msgServ = &Msg::instance();
        $msgServ->sendNews($toUser, $toParty, '', $articles);
        return true;

    }

    /**
     * 根据用户岗位角色获取用户uids
     * @param array $params
     *               + job_ids 岗位数据
     *               + role_ids 角色数据
     * @return array
     */
    public function get_job_role_uids($params = [])
    {
        $uids = [];

        // 处理岗位
        if (!empty($params['job_ids'])) {

            $params['job_ids'] = array_unique($params['job_ids']);
            sort($params['job_ids']);
            // 初始化SDK
            $member = new Member(Service::instance());

            // 获取岗位列表
            $job_list = $member->listAll(array('jobIdList' => $params['job_ids']));
            $job_list = $member->listAll(array('jobIdList' => $params['job_ids']), 1, $job_list['total']);

            // 如果岗位下存在人员
            if (!empty($job_list['list'])) {
                $uids = array_column($job_list['list'], 'memUid');
            }

        }

        // 处理角色
        if (!empty($params['role_ids'])) {

            $params['role_ids'] = array_unique($params['role_ids']);

            sort($params['role_ids']);

            // 初始化SDK
            $member = new Member(Service::instance());

            // 获取角色列表
            $role_List = $member->listAll(array('roleIdList' => $params['role_ids']));
            $role_List = $member->listAll(array('roleIdList' => $params['role_ids']), 1, $role_List['total']);

            // 如果岗位下存在人员
            if (!empty($role_List['list'])) {

                $uids = array_merge($uids, array_column($role_List['list'], 'memUid'));
            }

        }

        // 处理标签, 获取标签内人员与部门
        $dp_ids = [];
        if (!empty($params['tag_ids'])) {

            $tag_ids = array_unique($params['tag_ids']);

            $tag_s = new Tag();
            $tag_list = $tag_s->listUserByTagId($tag_ids, 1, 1000);
            $dp_ids = array_filter(array_column($tag_list['list'], 'dpId'));
            $tag_uid = array_filter(array_column($tag_list['list'], 'memUid'));
            $uids = array_merge($uids, (array)$tag_uid);
        }

        return ['u_ids' => $uids, 'dp_ids' => $dp_ids];
    }

    /**
     * 转换时间格式
     * 时间显示规则:1小时内显示XX分钟前,1天以内的显示XX个小时前,1~7天显示XX天前,超过7天显示具体年-月-日 时-分
     * @param string $time 当前时间戳
     * @return bool
     */
    public function get_time($time = '')
    {
        // 获取剩余秒数
        $way = NOW_TIME - ($time / 1000);

        // 一分钟之内
        if ($way < 60) {

            return '刚刚';
        }

        // 如果是一小时之内
        if ($way < 3600) {

            return intval($way / 60) . '分钟前';
        }

        // 如果是一小时之内
        if ($way < 3600) {

            return intval($way / 60) . '分钟前';
        }

        // 如果是一天之内
        if ($way < 86400) {

            return intval($way / 3600) . '小时前';
        }

        // 如果是一周之内
        if ($way < 604800) {

            return intval($way / 86400) . '天前';
        }

        return rgmdate(strval($time));
    }

    /**
     * 获取已删除用户信息
     * @param array &$user_list 用户列表
     * @param array $uids 全部用户UID
     * @return bool
     */
    public function user_list(&$user_list = [], $uids = [])
    {
        // 获取未删除的用户UID
        $_uids = array_column($user_list, 'memUid');

        // 获取被删除的用户UID
        $un_uid = array_diff($uids, $_uids);

        // 实例化
        $user = User::instance();

        // 遍历数据
        foreach ($un_uid as $k => $v) {

            // 如果UID不为空
            if (!empty($v)) {

                $user_list[] = $user->getByUid($v);
            }
        }

        return true;
    }

    /**
     * 字符串截取,支持中文和其他编码
     * static
     * access public
     * @param string $str 需要转换的字符串
     * @param int $start 开始位置
     * @param int $length 截取长度
     * @param string $charset 编码格式
     * @param bool $suffix 截断显示字符
     * @return string
     */
    public function cutstr($str, $start = 0, $length = 0, $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;
    }


    /**
     * 活动中心修改UC返回积分格式
     * @param array $msg_list 积分格式数据
     * @return array
     */
    public function msg_list($msg_list = [])
    {
        // 初始化
        $list = [];

        // 遍历数据重新组合
        foreach ($msg_list as $v) {

            if ($v['result'] == 'SUCCESS') {

                $list[] = array(
                    'number' => intval($v['score']),
                    'content' => '积分',
                );

            }

        }

        return $list;

    }

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

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

        $replace = array("\r\n", "\n", "\r", "&crarr;", "&#8629;");

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

    }
}