Skip to content

Commit bd80022

Browse files
authored
Update TencentPushTpns.php
1 parent a27cdd6 commit bd80022

1 file changed

Lines changed: 68 additions & 34 deletions

File tree

src/TencentPushTpns.php

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,21 @@ class TencentPushTpns
66
{
77
private $parm_url = [
88
'APP_PUSH' => "v3/push/app", //推送通知与应用内消息接口
9+
"TAG" => 'v3/device/tag', //标签绑定
10+
'DELETE_TAG' => 'v3/device/tag/delete_all_device', //删除标签下所有设备
911
'ACCOUNT_TOKEN_UPLOAD' => 'v3/push/package/upload', //1.号码包上传接口 2.token包上传接口
1012
'ACCOUNT_BIND' => 'v3/device/account/batchoperate', // 账号绑定与解绑接口
1113
];
1214

13-
//推送目标
14-
const AUDIENCE_ALL = "all"; //全量推送
15-
const AUDIENCE_TAG = "tag"; //标签推送
16-
const AUDIENCE_TOKEN = "token"; //单设备推送
17-
const AUDIENCE_TOKEN_LIST = "token_list"; //设备列表推送
18-
const AUDIENCE_ACCOUNT = "account"; //单账号推送
19-
const AUDIENCE_ACCOUNT_LIST = "account_list"; //账号列表推送
20-
const AUDIENCE_ACCOUNT_PACKAGE = "package_account_push"; //号码包推送
21-
const AUDIENCE_TOKEN_PACKAGE = "package_token_push"; //token 文件包推送
22-
23-
public $tag_type = "all";
2415
public $url = 'https://api.tpns.tencent.com/';
25-
public $sign;
2616
public $appid;
2717
public $secretKey;
2818

2919
public function __construct($config)
3020
{
3121
$this->appid = $config['appid'];
3222
$this->secretKey = $config['secretKey'];
23+
$this->environment = empty($config['environment']) ? "product" : $config['environment'];
3324
}
3425
/**
3526
* 全员发送消息【安装App的】
@@ -39,38 +30,92 @@ public function __construct($config)
3930
* @param string $message_type
4031
*
4132
*/
42-
public function send_all($content, $message_type = 'notify')
33+
public function send_all($content, $message_type = 'notify')
34+
{
35+
$arr = [
36+
'audience_type' => "all",
37+
'message' => $content,
38+
'message_type' => $message_type,
39+
'environment' => $this->environment,
40+
];
41+
42+
return $this->Push($arr, $this->get_url('APP_PUSH'));
43+
}
44+
/**
45+
* 标签推送
46+
*
47+
* @param string $content
48+
* @param array $tag_array
49+
* @param string $message_type
50+
*/
51+
public function send_tag($content, $tag_array, $message_type = 'notify')
52+
{
53+
$arr = [
54+
'audience_type' => "tag",
55+
'tag_items' => $tag_array,
56+
'message' => $content,
57+
'message_type' => $message_type,
58+
'environment' => $this->environment,
59+
];
60+
return $this->Push($arr, $this->get_url('APP_PUSH'));
61+
}
62+
/**
63+
* 设备推送
64+
* @param string $content
65+
* @param array $token_array
66+
* @param string $message_type
67+
*/
68+
public function send_token($content, $token_array, $message_type = 'notify')
4369
{
4470
$arr = [
45-
'audience_type' => 'all',
71+
'audience_type' => (count($token_array) > 1) ? "token_list" : "token",
72+
'token_list' => $token_array,
4673
'message' => $content,
4774
'message_type' => $message_type,
48-
'environment' => 'product',
75+
'environment' => $this->environment,
4976
];
5077
return $this->Push($arr, $this->get_url('APP_PUSH'));
5178
}
5279
/**
53-
* 一对一发送消息
80+
* 账号推送发送消息
5481
*
5582
* @param array $content 内容
5683
* @param string $account 用户账号
5784
* @param string $message_type 类型
5885
*
5986
*/
60-
public function send_account($content, $account, $message_type = 'notify')
87+
88+
public function send_account($content, $account_array, $message_type = 'notify')
6189
{
6290

6391
$arr = [
64-
'audience_type' => self::AUDIENCE_ACCOUNT,
92+
'audience_type' => (count($account_array) > 1) ? "account_list" : "account",
6593
'message' => $content,
6694
'message_type' => $message_type,
67-
'environment' => 'product',
68-
'account_list' => [
69-
$account
70-
],
95+
'environment' => $this->environment,
96+
'account_list' => $account_array,
7197
];
7298
return $this->Push($arr, $this->get_url('APP_PUSH'));
7399
}
100+
/**
101+
* 标签绑定与解绑
102+
*
103+
* @param int $operator_type
104+
* @param array $array
105+
* @param array $tag_array
106+
*/
107+
public function set_tag($operator_type, $array = [], $tag_array = [])
108+
{
109+
$arr = [
110+
'operator_type' => $operator_type
111+
];
112+
return $this->Push(array_merge($arr, $array, $tag_array), $this->get_url('TAG'));
113+
}
114+
115+
public function delete_tag($array)
116+
{
117+
return $this->Push($array, $this->get_url('DELETE_TAG'));
118+
}
74119
/**
75120
* 获取 sign
76121
*
@@ -132,25 +177,14 @@ public function Push($data, $url)
132177
$ret = curl_exec($ch);
133178
$error = curl_error($ch);
134179
$info = curl_getinfo($ch);
135-
136180
curl_close($ch);
137-
138181
if ($error != "") {
139182
throw new \Exception($error);
140183
}
141-
142184
$code = $info["http_code"];
143-
144185
if ($code != 200) {
145186
throw new \Exception("status: " . $code . ", message: " . $ret);
146187
}
147-
148-
$arr = json_decode($ret, 1);
149-
return $arr;
150-
if ($arr['ret_code'] == 0) {
151-
return true;
152-
} else {
153-
return false;
154-
}
188+
return json_decode($ret, true);
155189
}
156190
}

0 commit comments

Comments
 (0)