p2 project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.3 KiB

<?php
namespace app\admin\controller;
use app\admin\service\NoticeService;
use app\model\PusherLogModel;
class Notice extends AdminBaseController
{
// 弹窗通知消息
public function popUp()
{
$returnData = (new NoticeService())->popUp($this->request->param());
return json($returnData);
}
// 向一个或多个设备兴趣推送通知
public function publishToInterest()
{
$param = $this->request->post();
if (empty($param['title']) || empty($param['body'])) {
return json([
'code' => 400,
'message' => "缺少参数",
'data' => []
]);
}
$interestName = ["interests-1"]; // 订阅兴趣的名称, 一次推送最多100个兴趣名称
$res = (new \app\utility\Pusher())->publishToInterest($interestName, $param['title'], $param['body']);
return json([
'code' => 0,
'message' => 'ok',
'data' => $res
]);
}
// 向一个或多个用户推送通知
public function publishToUsers()
{
$param = $this->request->post();
if (empty($param['users']) || empty($param['title']) || empty($param['body'])) {
return json([
'code' => 400,
'message' => "缺少参数",
'data' => []
]);
}
// 将用户ID构建为数组
$explodeUser = explode(',', $param['users']);
$buildUserArr = [];
$insertData = [];
foreach ($explodeUser as $uid) {
if (is_numeric($uid)) {
$buildUserArr[] = strval($uid);
} else {
$buildUserArr[] = $uid;
}
$insertData[] = [
'user_id' => $uid,
'title' => $param['title'],
'content' => $param['body'],
];
}
// 发送通知
$res = (new \app\utility\Pusher())->publishToUsers($buildUserArr, $param['title'], $param['body']);
// 保存每个用户推送的消息内容
if (!empty($insertData)) {
PusherLogModel::insertAll($insertData);
}
return json([
'code' => 0,
'message' => 'ok',
'data' => $res
]);
}
}