|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Odeen |
|
5
|
|
|
* Date: 2016/5/8 |
|
6
|
|
|
* Time: 23:42 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace LaravelSms\sms; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use LaravelSms\lib\Rest; |
|
13
|
|
|
|
|
14
|
|
|
class Sms |
|
15
|
|
|
{ |
|
16
|
|
|
protected $smsData = [ |
|
17
|
|
|
'to' => null, |
|
18
|
|
|
'templates' => [], |
|
19
|
|
|
'templateData' => [], |
|
20
|
|
|
'content' => null |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
protected $agent; |
|
24
|
|
|
|
|
25
|
|
|
protected $config; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct($config) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->config = $config; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get all the data of SMS/voice verify. |
|
34
|
|
|
* |
|
35
|
|
|
* @param null|string $name |
|
36
|
|
|
* |
|
37
|
|
|
* @return mixed |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getData($name = null) |
|
40
|
|
|
{ |
|
41
|
|
|
if (is_string($name) && isset($this->smsData["$name"])) { |
|
42
|
|
|
return $this->smsData[$name]; |
|
43
|
|
|
} |
|
44
|
|
|
return $this->smsData; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* send mobile |
|
49
|
|
|
* @param $mobile |
|
50
|
|
|
* @return $this |
|
51
|
|
|
*/ |
|
52
|
|
|
public function to($mobile) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->smsData['to'] = $mobile; |
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* send content for part of sms-providers support content-sms |
|
60
|
|
|
* @param $content |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
|
|
public function content($content) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->smsData['content'] = $content; |
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* set template-id for part of sms-providers support template-sms |
|
71
|
|
|
* @param $agentName |
|
72
|
|
|
* @param null $tempId |
|
73
|
|
|
* @return $this |
|
74
|
|
|
*/ |
|
75
|
|
|
public function template($agentName='SMS_AGENT', $tempId = null) |
|
76
|
|
|
{ |
|
77
|
|
|
if ($agentName === 'SMS_AGENT'){ |
|
78
|
|
|
$agentName = $this->config; |
|
79
|
|
|
} |
|
80
|
|
|
if (is_array($agentName)) { |
|
81
|
|
|
foreach ($agentName as $k => $v) { |
|
82
|
|
|
$this->template($k, $v); |
|
83
|
|
|
} |
|
84
|
|
|
} elseif ($agentName && $tempId) { |
|
85
|
|
|
if (!isset($this->smsData['templates']) || !is_array($this->smsData['templates'])) { |
|
86
|
|
|
$this->smsData['templates'] = []; |
|
87
|
|
|
} |
|
88
|
|
|
$this->smsData['templates']["$agentName"] = $tempId; |
|
89
|
|
|
} |
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* set template-data for part of sms-providers support template-sms |
|
95
|
|
|
* @param array $data |
|
96
|
|
|
* @return $this |
|
97
|
|
|
*/ |
|
98
|
|
|
public function data(array $data) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->smsData['templateData'] = $data; |
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return mixed |
|
106
|
|
|
* @throws \Exception |
|
107
|
|
|
*/ |
|
108
|
|
|
public function send() |
|
109
|
|
|
{ |
|
110
|
|
|
if($this->config=='YunTongXun'){ |
|
111
|
|
|
$rest=new Rest(config('sms.agents.'.$this->config)); |
|
112
|
|
|
return $rest->sendTemplateSMS($this->smsData['to'],$this->smsData['templateData'],$this->smsData['templates']['YunTongXun']); |
|
113
|
|
|
}else{ |
|
114
|
|
|
throw new \Exception('make sure you have choose a right agent'); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |