ServiceHelper.php
2.24 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
<?php
namespace App\Helpers;
use Carbon\Carbon;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
class ServiceHelper
{
/**
* @return string
*/
public static function getServiceHost(): string
{
return config('services.provider.service_host', '');
}
/**
* @return string
*/
public static function getServiceSecret(): string
{
return config('services.provider.service_secret', '');
}
/**
* 推送微信服务号
* @param array<string,mixed> $data
* @return \Illuminate\Http\Client\Response
* @throws \JsonException
*/
public static function sendWechatOfficial(array $data): Response
{
return self::send('WechatOfficialMessage', $data);
}
/**
* 推送微信小程序
* @param array<string,mixed> $data
* @return \Illuminate\Http\Client\Response
* @throws \JsonException
*/
public static function sendWechatApp(array $data): Response
{
return self::send('WechatAppMessage', $data);
}
/**
* 推送极光消息
* @param array<string,mixed> $data
* @return \Illuminate\Http\Client\Response
* @throws \JsonException
*/
public static function sendJPush(array $data): Response
{
return self::send('JPushMessage', $data);
}
/**
* @param string $channel
* @param array<string,mixed> $data
* @return \Illuminate\Http\Client\Response
* @throws \JsonException
*/
public static function send(string $channel, array $data): Response
{
$payload = ['time' => Carbon::now()->timestamp, 'channel' => $channel, 'data' => $data];
$payload['sign'] = self::getSign($payload);
return Http::asJson()->post(self::getServiceHost() . '/api/provider', $payload);
}
/**
* @param array<string,mixed> $payload
* @return string
* @throws \JsonException
*/
private static function getSign(array $payload): string
{
$payload['data'] = json_encode($payload['data'], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
$payload['secret'] = self::getServiceSecret();
ksort($payload);
return md5(base64_encode(implode('&', $payload)));
}
}