ArticleService.class.php
11.2 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
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 17/4/12
* Time: 10:46
*/
namespace Common\Service;
use Com\Rpc;
use Common\Common\Constant;
use Common\Common\NewsHelper;
use Common\Model\ArticleModel;
use Common\Model\ClassModel;
use VcySDK\Cron;
use VcySDK\Service;
class ArticleService extends AbstractService
{
// 新闻二维码地址
const NEWS_QRCODE_URL = 'News/Frontend/NewsQrcode';
// 构造方法
public function __construct()
{
parent::__construct();
$this->_d = new ArticleModel();
$this->_class = new ClassModel();
}
/**
* 格式化数据
* @author liyifei
* @param array $article 新闻数据
* @param array $attachs 附件数据数组
* @return array
*/
public function formatData($article, $attachs = null)
{
// unset多余数据
$unsetParam = [
'ea_id',
'ea_name',
'top_time',
'update_time',
'unread_total',
'domain',
'status',
'created',
'updated',
'deleted',
];
foreach ($article as $k => $v) {
if (in_array($k, $unsetParam)) {
unset($article[$k]);
}
}
$article['parent_id'] = 0;
$article['parent_name'] = '';
// 返回父分类ID,方便前端定位
$class = $this->_class->get($article['class_id']);
if ($class) {
$parent_class = $this->_class->get($class['parent_id']);
if ($parent_class) {
$article['parent_id'] = $parent_class['class_id'];
$article['parent_name'] = $parent_class['class_name'];
}
}
// 反序列化积分策略数据
if (!empty($article['strategys'])) {
$formatStrategys = [];
$strategys = unserialize($article['strategys']);
foreach ($strategys as $action_key => $strategy_id) {
$formatStrategys[] = [
'action_key' => $action_key,
'strategy_id' => $strategy_id,
];
}
$article['strategys'] = $formatStrategys;
}
// 反序列化学分策略数据
if (!empty($article['credit_strategys'])) {
$formatStrategys = [];
$strategys = unserialize($article['credit_strategys']);
foreach ($strategys as $action_key => $strategy_id) {
$formatStrategys[] = [
'action_key' => $action_key,
'strategy_id' => $strategy_id,
];
}
$article['credit_strategys'] = $formatStrategys;
}
// 新闻内容格式化,图文混排标签转换
if (!empty($article['content']) && is_array($attachs) && !empty($attachs)) {
$attach_list = [];
foreach ($attachs as $list) {
foreach ($list as $k => $v) {
$attach_list[$k] = $v;
}
}
$pattern = '/<img[^<]*(news_video|news_audio)="(\w+)".*\/>/iU';
$matches = [];
preg_match_all($pattern, $article['content'], $matches, PREG_SET_ORDER);
foreach ($matches as $matche) {
// $matche[0] = 将要被替换的字符串
// $matche[1] = news_video 或 news_audio
// $matche[2] = 附件ID
if (count($matche) != 3 || !isset($attach_list[$matche[2]])) {
continue;
}
$attach = $attach_list[$matche[2]];
$html = '';
switch ($matche[1]) {
// 视频
case 'news_video':
$html = "<{$matche[1]} at_url=\"{$attach['at_convert_url']}\" cover=\"{$attach['at_cover_url']}\" />";
break;
// 音频
case 'news_audio':
$html = "<{$matche[1]} at_name=\"{$attach['at_name']}\" at_time=\"{$attach['at_time']}\" at_url=\"{$attach['at_url']}\" />";
break;
}
$article['content'] = str_replace($matche[0], $html, $article['content']);
}
}
return $article;
}
/**
* @desc 新增新闻推荐
* @author tangxingguo
* @param int $articleId 新闻ID
* @param array $postRight 用户提交的权限数据
* @return mixed
*/
public function addNewsRpc($articleId, $postRight)
{
// 预发布与草稿不推送
$articleInfo = $this->get($articleId);
if (in_array($articleInfo['news_status'], [Constant::NEWS_STATUS_DRAFT, Constant::NEWS_STATUS_READY_SEND, Constant::NEWS_STATUS_TIMING_RELEASE])) {
return false;
}
// 权限数据
$newsHelper = &NewsHelper::instance();
$articleInfo['right'] = $newsHelper->formatRightForRPC($postRight);
// 格式化RPC参数
$param_arr = $this->formatRpcParams($articleInfo, 1);
// 推送
$url = rpcUrl('/Public/Rpc/Recommender/ArticleNew');
$res = Rpc::phprpc($url)->invoke('Index', $param_arr);
return $res;
}
/**
* @desc 更新新闻推荐
* @author tangxingguo
* @param Int $articleId 新闻ID
* @param array $articleInfo 新闻信息
* @param array $postRight 用户提交的权限数据
* @return mixed
*/
public function updateNewsRpc($articleId, $articleInfo, $postRight)
{
// 预发布与草稿不推送
if (in_array($articleInfo['news_status'], [Constant::NEWS_STATUS_DRAFT, Constant::NEWS_STATUS_READY_SEND])) {
return false;
}
$info = $this->get($articleId);
if (empty($info)) {
return false;
}
$articleInfo['article_id'] = $articleId;
// 权限数据
$newsHelper = &NewsHelper::instance();
$articleInfo['right'] = $newsHelper->formatRightForRPC($postRight);
// 格式化RPC参数
$param_arr = $this->formatRpcParams($articleInfo, 1);
// 推送
$url = rpcUrl('/Public/Rpc/Recommender/ArticleUpdate');
$res = Rpc::phprpc($url)->invoke('Index', $param_arr);
return $res;
}
/**
* @desc 删除新闻推荐
* @author tangxingguo
* @param int $articleId 新闻ID
* @return mixed
*/
public function delNewsRpc($articleId)
{
$articleInfo = $this->get($articleId);
// 格式化RPC参数
$param_arr = $this->formatRpcParams($articleInfo, 2);
// 推送
$url = rpcUrl('/Public/Rpc/Recommender/ArticleDelete');
$res = Rpc::phprpc($url)->invoke('Index', $param_arr);
return $res;
}
/**
* @desc 格式化RPC数据
* @author tangxingguo
* @param array $articleInfo 新闻信息
* @param int $paramType 数据类型(1=添加、更新;2=删除)
* @return array RPC参数
*/
private function formatRpcParams($articleInfo, $paramType)
{
$param_arr = [];
switch ($paramType) {
// 更新或添加
case 1:
$param_arr = [
// 被推荐数据所在应用模块目录标识名
'app'=>'News',
// 被推荐数据所属的分类Id,可以为空,但必须提供该参数
'dataCategoryId' => $articleInfo['class_id'],
// 被推荐数据的原始数据 Id
'dataId' => $articleInfo['article_id'],
// 文章标题
'title' => $articleInfo['title'],
// 文章摘要
'summary' => $articleInfo['summary'],
// 封面图片附件 ID
'attachId' => $articleInfo['cover_id'],
// 封面图片 url
'pic' => $articleInfo['cover_url'],
// 文章链接
'url' => 'News/Frontend/Index/Detail/Index?article_id=' . $articleInfo['article_id'],
// 文章发布时间戳,不设置或者为空,则以推荐时间为准
'dateline' => $articleInfo['send_time'],
// 文章阅读权限范围,为空则表示全部人员,否则需要构造数组
'right' => $articleInfo['right'],
];
break;
// 删除
case 2:
$param_arr = [
// 被推荐数据所在应用模块目录标识名
'app'=>'News',
// 被推荐数据所属的分类Id,可以为空,但必须提供该参数
'dataCategoryId' => $articleInfo['class_id'],
// 被推荐数据的原始数据 Id
'dataId' => $articleInfo['article_id'],
];
break;
}
return $param_arr;
}
/**
* 创建数据标识
* @author zhonglei
* @return string
*/
public function buildDataID()
{
$data_id = md5(sprintf('%s_%s_%s', QY_DOMAIN, APP_DIR, MILLI_TIME));
$count = $this->count_by_conds(['data_id' => $data_id]);
if ($count == 0) {
return $data_id;
}
return $this->buildDataID();
}
/**
* 根据动作Key获取策略ID
* @author zhonglei
* @param array $article 新闻数据
* @param string $action_key 动作Key
* @return string
*/
public function getStrategyID($article, $action_key)
{
$strategy_id = '';
if (!is_array($article) || empty($article)) {
return $strategy_id;
}
// 非自定义策略
if ($article['strategy_setting'] != Constant::NEWS_STRATEGY_CUSTOM) {
return $strategy_id;
}
// 反序列化
if (!is_array($article['strategys'])) {
$article['strategys'] = empty($article['strategys']) ? [] : unserialize($article['strategys']);
}
// 根据动作Key获取策略ID
if (isset($article['strategys'][$action_key])) {
$strategy_id = $article['strategys'][$action_key];
}
return $strategy_id;
}
/**
* 增加定时任务
* @param int $id 新闻ID
* @param int $cronTime 执行时间
* @return bool
*/
public function cronAdd($id, $cronTime)
{
// 实例化定时任务类
$cron_serv = new Cron(Service::instance());
// 参数
$conds_remind = [
'crRemark' => 'news_cron',
'crType' => 2,
'crParams' => json_encode(['article_id' => $id]),
'crMethod' => 'POST',
'crReqUrl' => oaUrl('Frontend/Callback/Release/Index'), // 回调地址
'crTimes' => 1,
'crCron' => rgmdate((String)$cronTime, 's i G j n ? Y'),
'crMonitorExecution' => 0,
'crDescription' => '头条定时发布'
];
// 创建定时任务
$res_publish = $cron_serv->add($conds_remind);
$cron_publish = $res_publish['crId'];
$this->update($id, ['release_crid' => $cron_publish]);
return true;
}
}