XjPush.php
5.6 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
<?php
/**
* Created by PhpStorm.
* User: Kevin
* Date: 2022/03/16 1500
* Time: 15:00
*/
namespace app\api\controller\getui;
use think\Db;
use think\Request;
/**
* 巡检APP推送(unipush)
* @package app\api\controller\core
*/
class XjPush extends Base
{
/**
* 13. unipush推送隐患处理至巡检APP点击跳转通知详情
*/
public function pushPitfall($id)
{
if (empty($id)) {
return array_callback(false, "id不能为空");
}
// file_put_contents("1111.log", date("Y-m-d H:i:s") . $id . PHP_EOL, FILE_APPEND);
$warningModel = new \app\admin\model\inspection\Warning();
$warningInfo = $warningModel->where("id", $id)->find();
$sum_count = 0;//总共需要推送的人
$react_count = 0;//实际推送人数
// file_put_contents("1111.log", date("Y-m-d H:i:s") . $warningInfo->title . PHP_EOL, FILE_APPEND);
if (!empty($warningInfo)) {
$title = sub_str($warningInfo->title, 20);//通知消息标题,长度 ≤ 50
$content = sub_str($warningInfo->message, 50);//通知消息内容,长度 ≤ 256
$scene_type = "jump_to_pitfall_page";
$scene_data = ["id" => $warningInfo->id];//type 0未读
if (!empty($warningInfo->staff_id)) {
$user_id = Db::name("inspection_staff")->where("id", $warningInfo->staff_id)->value("user_id");
$user_ids = trim($user_id, ",");
if (!empty($user_ids)) {
$sum_count = count(explode(",", $user_ids));
// 查找需要推送人的clientid
$w1 = [];
$w1["id"] = ["in", $user_ids];
$w1["clientid"] = ["neq", ""];
$clientid_list = Db::name("user")->where($w1)->column("clientid");
if (!empty($clientid_list)) {
$react_count = count($clientid_list);
//请求推送
$push = new \GTPushRequest();
$push->setRequestId($this->micro_time);
$message = new \GTPushMessage();
$notify = new \GTNotification();
$notify->setTitle($title);
$notify->setBody($content);
//点击通知后续动作,目前支持以下后续动作:
//1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
$notify->setClickType("payload");
$array = ["scene_type" => $scene_type, "data" => $scene_data];
$notify->setPayload(json_encode($array, JSON_UNESCAPED_UNICODE));
$message->setNotification($notify);
$push->setPushMessage($message);
$push->setGroupName("message_" . $warningInfo->id);
$rr = $this->api->pushApi()->createListMsg($push);//【toList】创建消息;此接口用来创建消息体,并返回taskid,为批量推的前置步骤
if (!empty($rr)) {
if ($rr["code"] == 0) {
$taskid = $rr["data"]['taskid'];//任务编号,用于执行cid批量推和执行别名批量推,此taskid可以多次使用,有效期为用户设置的离线时间
if (!empty($taskid)) {
//执行cid批量推
$user = new \GTAudienceRequest();
$user->setTaskid($taskid);
$user->setCidList($clientid_list);
$result = $this->api->pushApi()->pushListByCid($user);
if (!empty($result)) {
if ($result['code'] == 0) {
// $this->success("推送成功", $result);
return array_callback(true, "推送成功", ["result" => $result, "sum_count" => $sum_count, "react_count" => $react_count]);
} else {
return array_callback(false, "推送异常[" . $result['code'] . $result['msg'] . "]");
}
} else {
return array_callback(false, "推送失败");
}
} else {
return array_callback(false, "消息体任务编号创建异常");
}
} else {
return array_callback(false, "创建消息体异常[" . $rr['code'] . $rr['msg'] . "]");
}
} else {
return array_callback(false, "创建消息体失败");
}
} else {
return array_callback(false, "巡检人员clientid都为空,建议让TA们重新登录APP");
}
}
}
return array_callback(false, "请先选择需要通知的巡检人员");
} else {
return array_callback(false, "未查找到需要推送的数据");
}
}
}