JpushService.php 5.5 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 逻辑层:消息推送逻辑处理
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Jpush
 * @package   App\Jpush
 * @author    Richer <yangzi1028@163.com>
 * @date      2020年3月10日,16:52:41
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Jpush;

use App\Models\System\Notification;

/**
 * Class JpushService
 *
 * @category  App\Jpush
 * @package   App\Jpush
 * @author    Richer <yangzi1028@163.com>
 * @date      2020年3月10日,16:52:41
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
class JpushService
{
    /**
     * 发送推送消息
     *
     * @param Model  $model 模型
     * @param string $class 类
     *
     * @return \Illuminate\Http\Response
     */
    public static function pushNotification($model, $class)
    {
        // 用户开启接收消息推送并且是本地环境以外的环境才进行极光消息推送
        if (config('app.env') !== 'local') {
            $userids[]  = $model->user_id ?? $model->id;
            $content = Notification::transformContent($class, $model, false);

            // \Log::info('========================xs ss===================');
            // \Log::info($class);
            // \Log::info($content);

            //$content = Notification::transformContentZH($class, $model);
            $content = str_replace('{title}', $model->title, $content);
            $content = str_replace('{nickname}', $model->follow_user_name, $content);
            // \Log::info($content);
            //\Log::info($userids);
            return JpushService::send($userids, $content);
        }
    }

    /**
     * 新增通知提醒
     *
     * @param array  $userids  发送用户
     * @param string $content  发送的正文内容,是一个数组;
     * @param string $usertype 用户类型
     * @param string $audience 消息类型
     *
     * @return \Illuminate\Http\Response 是否成功
     */
    public static function send($userids, $content, $usertype = 'client', $audience = 'alias')
    {
        $jpush      = new Jpush();

        // 默认为tag方式
        if ($audience == 'alias') {
            // 组合用户的标签
            $tag_options  = config('jpush.audience_alias');
            $tag_prefix   = $tag_options[$usertype]['prefix'];
            $alias       = null;
            $notifys    = [];
            foreach ($userids as $key => $value) {
                $notify['user_id']      = $value;
                $notify['content']      = $content;
                $notify['sendtime']     = date('Y-m-d H:i:s');

                $alias[] = $tag_prefix . $value;

                $notifys[] = $notify;
            }

            $path = storage_path().'/logs/Jpush';
            \File::isDirectory($path) or \File::makeDirectory($path, 0777, true, true);
            $fileName = $path.'/Jpush_alias_'. date('Ymd') .'.log';
            //dump($fileName);
            // 设置权限
            if (file_exists($fileName)) {
                @chmod($fileName, 0777);
            }

            // 打开文件
            $logFile = fopen($fileName, 'a+');

            // 写入分割线
            fwrite(
                $logFile,
                '============================================================================================' . PHP_EOL
            );

            // 写入当前的操作的时间
            fwrite($logFile, date('Y-m-d H:i:s').PHP_EOL);
            // 写入当前的操作
            fwrite($logFile, json_encode($alias, JSON_UNESCAPED_UNICODE) . PHP_EOL);
            // 关闭文件
            fclose($logFile);

            //Storage::append($file, json_encode($alias,JSON_UNESCAPED_UNICODE));
            // 进行推送
            if ($alias) {
                $result = $jpush -> sendByAlias($alias, $content);
                $storage['code']    = $result;
                $storage['users']   = $userids;
                $storage['notify']  = $notifys;

                // $file = 'Jpush_'.date('Ymd').'.json';
                // Storage::append($file, json_encode($storage,JSON_UNESCAPED_UNICODE));

                // 创建日志
                $logFileContent = storage_path('logs' . DIRECTORY_SEPARATOR . 'Jpush/Jpush_content_'. date('Ymd') .'.log');
                // 设置权限
                if (file_exists($logFileContent)) {
                    @chmod($logFileContent, 0777);
                }
                // 打开文件
                $logFile = fopen($logFileContent, 'a+');

                // 写入分割线
                fwrite(
                    $logFile,
                    '========================================================================================' . PHP_EOL
                );

                // 写入当前的操作的时间
                fwrite($logFile, date('Y-m-d H:i:s').PHP_EOL);
                // 写入当前的操作
                fwrite($logFile, json_encode($storage, JSON_UNESCAPED_UNICODE) . PHP_EOL);
                // 关闭文件
                fclose($logFile);

                @chmod($logFileContent, 0777);

                //批量增加
                //$this -> model -> insertBatch($notifys);
                return $result;
            }
        }
    }
}