AbstractService.class.php
9.09 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
<?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", "↵", "↵"];
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;
}
}