Base.php
3.5 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
<?php
namespace app\api\controller\tencentim;
use app\common\controller\Api;
use app\common\helper\HttpHelper;
use think\Db;
use think\Request;
header('Content-type:text/html; Charset=utf-8');
date_default_timezone_set('PRC');
class Base extends Api
{
protected $noNeedLogin = ['*'];
public $tim_url;
protected $tim_sdkappid;
protected $tim_key;
protected $tim_userId;
protected $userSig;
public function __construct(Request $request = null)
{
parent::__construct($request);
$this->tim_url = config("site.tim_url");//推送域名URL
$this->tim_sdkappid = config("site.tim_sdkappid");//应用sdkappid
$this->tim_key = config("site.tim_key");//应用密钥
$this->tim_userId = config("site.tim_userId");//管理员账号
Vendor("tencent.TLSSigAPIv2");
$this->api = new \TLSSigAPIv2($this->tim_sdkappid, $this->tim_key);
$where = [
"sdkappid" => $this->tim_sdkappid,
"userId" => $this->tim_userId,
];
$tim_sign_info = Db::name("tencent_tim")->where($where)->find();
// if (!empty($tim_sign_info) && $tim_sign_info['failuretime'] > time()) {
// //存过sign信息并且未失效
// $this->userSig = $tim_sign_info['userSig'];
// } else {
//已失效或者未存过
$expire = 86400 * 180;//存180天
$nowtm = time();
$this->userSig = $this->api->genUserSig($this->tim_userId, $expire);
if ($this->userSig) {
if (!empty($tim_sign_info)) {
//更新
$upt_data = [
"expire" => $expire,
"failuretime" => $expire + $nowtm,
"updatetime" => $nowtm
];
Db::name("tencent_tim")->where("id", $tim_sign_info['id'])->update($upt_data);
} else {
//插入
$insert_data = [
"sdkappid" => $this->tim_sdkappid,
"userId" => $this->tim_userId,
"userSig" => $this->userSig,
"expire" => $expire,
"failuretime" => $expire + $nowtm,
"createtime" => $nowtm
];
Db::name("tencent_tim")->insert($insert_data);
}
// }
}
}
public function send($url, $data)
{
$url = $this->tim_url . $url
. "?usersig=" . $this->userSig
. "&identifier=" . $this->tim_userId
. "&sdkappid=" . $this->tim_sdkappid
. "&random=" . mt_rand(0, 4294967295)
. "&contenttype=json";
// file_put_contents("kevin_head.log", $url . PHP_EOL, FILE_APPEND);
// file_put_contents("kevin_head.log", json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
// $res = $this->curlPost($url,json_encode($data,JSON_UNESCAPED_UNICODE));
$res = HttpHelper::curl($url, "POST", $data);
return $res;
}
function curlPost($Url, $data)
{
$ch = curl_init($Url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//$data JSON类型字符串
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($data)));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}