6
0
Fork 0
jiqu-library-server/app/Services/Push/UniPushService.php

252 lines
6.3 KiB
PHP

<?php
namespace App\Services\Push;
use App\Helpers\Order as OrderHelper;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
class UniPushService
{
protected $appId;
protected $appKey;
protected $appSecret;
protected $masterSecret;
protected $cacheKey;
protected $uri = 'https://restapi.getui.com/v2/';
public function __construct(string $appId, string $appKey, string $appSecret, string $masterSecret)
{
$this->appId = $appId;
$this->appKey = $appKey;
$this->appSecret = $appSecret;
$this->masterSecret = $masterSecret;
$this->cacheKey = 'uni-push-'.$this->appId;
}
/**
* 获取当前实例token
*
* @return void
*/
public function getToken()
{
if (Cache::has($this->cacheKey)) {
return Cache::get($this->cacheKey);
} else {
return $this->getTokenFromServer();
}
}
protected function getUri($uri)
{
return $this->uri.$this->appId.'/'.$uri;
}
/**
* 获取unix毫秒时间戳
*
* @return void
*/
protected function msectime()
{
list($msec, $sec) = explode(' ', microtime());
$msectime = (float) sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
return $msectime;
}
/**
* 创建签名
*
* @return void
*/
protected function makeSign($param)
{
return hash('sha256', $param['appkey'].$param['timestamp'].$this->masterSecret);
}
/**
* 重新获取token
*
* @return void
*/
protected function getTokenFromServer()
{
$token = '';
$param = [
'appkey'=>$this->appKey,
'timestamp'=>$this->msectime(),
];
$param['sign'] = $this->makeSign($param);
$response = Http::withHeaders([
'Accept' => 'application/json',
])->post($this->getUri('auth'), $param);
if ($response->successful()) {
$data = $response->json();
$token = $data['data']['token'];
Cache::put($this->cacheKey, $data['data']['token'], $data['data']['expire_time']);
}
return $token;
}
/**
* 封装公共请求
*
* @return void
*/
protected function post($uri, $params)
{
$token = $this->getToken();
// dd($params);
$response = Http::withHeaders([
'Accept' => 'application/json',
'token' => $token,
])->post($this->getUri($uri), $params);
$resData = [];
if ($response->successful()) {
$resData = $response->json();
}
return $resData;
}
protected function get($uri, $params = null)
{
$token = $this->getToken();
$response = Http::withHeaders([
'Accept' => 'application/json',
'token' => $token,
])->get($this->getUri($uri), $params);
$resData = [];
if ($response->successful()) {
$resData = $response->json();
}
return $resData;
}
protected function createPushData($title, $body, $params = [])
{
$payload = urlencode(json_encode($params));
return [
'request_id' => OrderHelper::serialNumber(),
'settings'=>[
'ttl'=> 3600000,
'strategy'=> [
'default'=>1,
'ios'=>4,
],
],
'push_message'=>[
'transmission'=> json_encode([
'title' => $title,
'content'=> $body,
'payload'=> $payload,
]),
// 'notification'=>[
// 'title' => $title,
// 'body' => $body,
// 'click_type'=> 'payload', //默认打开首页
// 'payload'=> $payload,
// ],
],
'push_channel'=>[
'android'=>[
'ups'=> [
'transmission'=> json_encode([
'title' => $title,
'content'=> $body,
'payload'=> $payload,
]),
// 'notification'=>[
// 'title' => $title,
// 'body' => $body,
// 'click_type'=> 'payload', //默认打开首页
// 'payload'=> $payload,
// ],
],
],
'ios'=>[
'type'=>'notify',
'payload' => $payload,
'aps'=>[
'alert'=>[
'title'=>$title,
'body'=>$body,
],
'content-available'=> 0,
],
],
],
];
}
/**
* 绑定cid和user_id
*
* @return void
*/
public function bindCid($userId, $cid)
{
$this->post('user/alias', [
'data_list'=>[
'cid' => $cid,
'alias'=>$userId,
],
]);
}
/**
* 解绑所有与该别名绑定的cid
*/
public function unbindCid($userId)
{
$res = $this->get('user/alias/'.$userId);
if (Arr::get($res, 'code', 1) === 0) {
return true;
}
return false;
}
/**
* 根据cid单推
*
* @return void
*/
public function pushCid(string $cid, string $title, string $body, array $params = [])
{
$this->post('push/single/cid', array_merge([
'audience'=>[
'cid'=>[
$cid,
],
],
], $this->createPushData($title, $body, $params)));
}
/**
* 给所有人推送
*
* @param string $title
* @param string $body
* @param array $params
* @return void
*/
public function pushAll(string $title, string $body, array $params = [])
{
$this->post('push/all', array_merge([
'audience'=>'all',
], $this->createPushData($title, $body, $params)));
}
}