-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathSmsManagerServiceProvider.php
More file actions
113 lines (98 loc) · 3.5 KB
/
SmsManagerServiceProvider.php
File metadata and controls
113 lines (98 loc) · 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
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace Toplan\Sms;
use DB;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\ServiceProvider;
use PhpSms;
class SmsManagerServiceProvider extends ServiceProvider
{
use DispatchesJobs;
/**
* 启动服务
*/
public function boot()
{
$this->publishes([
__DIR__ . '/../../config/laravel-sms.php' => config_path('laravel-sms.php'),
], 'config');
$this->publishes([
__DIR__ . '/../../../migrations/' => database_path('/migrations'),
], 'migrations');
if (config('laravel-sms.route.enable', true)) {
require __DIR__ . '/routes.php';
}
require __DIR__ . '/validations.php';
$this->phpSms();
}
/**
* 注册服务
*/
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../../config/laravel-sms.php', 'laravel-sms');
$this->app->singleton('Toplan\\Sms\\SmsManager', function ($app) {
$token = $app->request->header('access-token', null);
if (empty($token)) {
$token = $app->request->input('access_token', null);
if(empty($token)) {
$token = $app->request->input('phone', null);
}
}
$input = $app->request->all();
return new SmsManager($token, $input);
});
}
/**
* 配置PhpSms
*/
protected function phpSms()
{
$queueJob = config('laravel-sms.queueJob', 'Toplan\Sms\SendReminderSms');
PhpSms::queue(false, function ($sms) use ($queueJob) {
if (!class_exists($queueJob)) {
throw new LaravelSmsException("Class [$queueJob] does not exists.");
}
$this->dispatch(new $queueJob($sms));
});
PhpSms::beforeSend(function ($task) {
if (!config('laravel-sms.dbLogs', false)) {
return true;
}
$data = $task->data ?: [];
$to = is_array($data['to']) ? json_encode($data['to']) : $data['to'];
$id = DB::table('laravel_sms')->insertGetId([
'to' => $to ?: '',
'temp_id' => json_encode($data['templates']),
'data' => json_encode($data['data']),
'content' => $data['content'] ?: '',
'voice_code' => $data['code'] ?: '',
'created_at' => date('Y-m-d H:i:s', time()),
]);
$data['_sms_id'] = $id;
$task->data($data);
});
PhpSms::afterSend(function ($task, $result) {
if (!config('laravel-sms.dbLogs', false)) {
return true;
}
$microTime = $result['time']['finished_at'];
$finishedAt = explode(' ', $microTime)[1];
$data = $task->data;
if (!isset($data['_sms_id'])) {
return true;
}
DB::beginTransaction();
$dbData = [];
$dbData['updated_at'] = date('Y-m-d H:i:s', $finishedAt);
$dbData['result_info'] = json_encode($result['logs']);
if ($result['success']) {
$dbData['sent_time'] = $finishedAt;
} else {
DB::table('laravel_sms')->where('id', $data['_sms_id'])->increment('fail_times');
$dbData['last_fail_time'] = $finishedAt;
}
DB::table('laravel_sms')->where('id', $data['_sms_id'])->update($dbData);
DB::commit();
});
}
}