JpushService.php
5.5 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
<?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;
}
}
}
}