CRMEB Admin框架采用TP6+Mysql+Element前后端分離全部100%開源。方便二開,更有詳細使用文檔、接口文檔、數據字典、二開文檔/視頻教程。為開發者賦能,助力企業發展、國家富強,致力于打造最受歡迎的Admin框架項目。今天小編就以新增短信接口為例,給大家講解一下如何進行二次開發,使用的短信接口是我們短信寶短信群發平臺的短信接口,我們短信寶短信群發平臺的接口非常穩定,發送速度快,注冊就送測試短信,推薦大家使用。
首先執行以下SQL增加短信寶配置參數字典
1
2
3
4
5
6
7
8
9
|
INSERT INTO `eb_system_config_tab` (`id`, `pid`, `title`, `eng_title`, `status`, `info`, `icon`, `type`, `sort`) VALUES ( '999' , '96' , '短信寶配置' , 'smsbao' , '1' , '0' , 'ios-chatboxes' , '0' , '0' ); INSERT INTO `eb_system_config` ( `menu_name`, `type`, `input_type`, `config_tab_id`, `parameter`, `upload_type`, `required`, `width`, `high`, `value`, `info`, ` desc `, `sort`, `status`) VALUES ( 'smsbao_user' , 'text' , 'input' , '999' , '' , 0, '' , '999' , 0, '' , '短信寶賬號' , '短信寶賬號' , '0' , '1' ); INSERT INTO `eb_system_config` ( `menu_name`, `type`, `input_type`, `config_tab_id`, `parameter`, `upload_type`, `required`, `width`, `high`, `value`, `info`, ` desc `, `sort`, `status`) VALUES ( 'smsbao_pwd' , 'text' , 'input' , '999' , '' , 0, '' , '999' , 0, '' , '短信寶APIKEY' , '短信寶APIKEY' , '0' , '1' ); INSERT INTO `eb_system_config` ( `menu_name`, `type`, `input_type`, `config_tab_id`, `parameter`, `upload_type`, `required`, `width`, `high`, `value`, `info`, ` desc `, `sort`, `status`) VALUES ( 'sms_sign_name' , 'text' , 'input' , '999' , '' , 0, '' , '999' , 0, '' , '短信簽名' , '短信簽名' , '0' , '1' ); UPDATE `eb_system_config` SET `parameter`= '0=>一號通\r\n1=>阿里云\r\n2=>騰訊云\r\n3=>短信寶' WHERE (`id`= '393' ); |
接著打開項目app\services\message\notice\SmsService.php文件,增加短信類型,代碼如下
1
|
private $smsType = [ 'yihaotong' , 'aliyun' , 'tencent' , 'smsbao' ]; |
接著打開項目config\sms.php文件,增加短信寶驅動配置:
1
2
3
4
5
|
'smsbao' =>[ 'smsbao_user' => '' , 'smsbao_pwd' => '' , 'sign_name' => '' , ] |
接著在\crmeb\services\sms\storage目錄下 新增Smsbao.php短信寶核心發送代碼
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
<?php namespace crmeb\services\sms\storage; use crmeb\services\sms\BaseSms; use crmeb\services\HttpService; use think\exception\ValidateException; use think\facade\Config; use app\services\message\SystemNotificationServices; class Smsbao extends BaseSms { protected $status ; /** * 發送模板id * @var array */ protected $templateIds = []; /** * @var string */ protected $accessKeyId = '' ; /** * @var string */ protected $accessKeySecret = '' ; protected $signName = '' ; /** * @var string */ protected $region = "ap-guangzhou" ; /** * @param array $config * @return mixed|void */ protected function initialize( array $config = []) { parent::initialize( $config ); $this ->accessKeyId = sys_config( 'smsbao_user' , '' ); $this ->accessKeySecret = sys_config( 'smsbao_pwd' , '' ); if ( $this ->accessKeyId && $this ->accessKeySecret) { $this ->status = true; } else { $this ->status = false; } $this ->signName = $config [ 'sign_name' ] ?? null; if ( $this ->signName == null){ $this ->signName = sys_config( 'sms_sign_name' , '' ); } } /** * 提取模板code * @param string $templateId * @return null */ protected function getTemplateCode(string $templateId ) { $notifyServices = app()->make(SystemNotificationServices:: class ); $template = $notifyServices ->value([ 'sms_id' => $templateId , ], 'system_text' ) ?? 0; return $template ?? null; } /** * @param string $phone * @param string $templateId * @param array $data * @return mixed|void */ public function send(string $phone , string $templateId , array $data ) { $statusStr = array ( "0" => "短信發送成功" , "-1" => "參數不全" , "-2" => "服務器空間不支持,請確認支持curl或者fsocket,聯系您的空間商解決或者更換空間!" , "30" => "密碼錯誤" , "40" => "賬號不存在" , "41" => "余額不足" , "42" => "帳戶已過期" , "43" => "IP地址限制" , "50" => "內容含有敏感詞" ); $user = $this ->accessKeyId; $pass = md5( $this ->accessKeySecret); $content = $this ->getTemplateCode( $templateId ); foreach ( array_keys ( $data ) as $k => $v ){ $search = '{' . $v . '}' ; $content = str_replace ( $search , $data [ $v ], $content ); } $content = '【' . $this ->signName. '】' . $content ; $sendurl = $smsapi . "sms?u=" . $user . "&p=" . $pass . "&m=" . $phone . "&c=" .urlencode( $content ); $result = $this ->fetchContent( $sendurl , 'POST' , '' ) ; try { if ( $result == '0' ){ return true; } else { throw new ValidateException( $statusStr [ $result ]); } } catch (\Exception $e ) { throw new ValidateException( $e ->getMessage()); } } private function fetchContent( $url , $method , $body ) { $ch = curl_init(); if ( $method == 'POST' ) { curl_setopt( $ch , CURLOPT_POST, 1); //post提交方式 curl_setopt( $ch , CURLOPT_POSTFIELDS, $body ); } else { $url .= '?' . $body ; } curl_setopt( $ch , CURLOPT_URL, $url ); curl_setopt( $ch , CURLOPT_TIMEOUT, 5); curl_setopt( $ch , CURLOPT_RETURNTRANSFER, 1); curl_setopt( $ch , CURLOPT_HTTPHEADER, array ( "x-sdk-client" => "php/2.0.0" )); if ( substr ( $url , 0,5) == 'https' ) { curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER, false); curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST, false); } $rtn = curl_exec( $ch ); if ( $rtn === false) { // 大多由設置等原因引起,一般無法保障后續邏輯正常執行, // 所以這里觸發的是E_USER_ERROR,會終止腳本執行,無法被try...catch捕獲,需要用戶排查環境、網絡等故障 trigger_error( "[CURL_" . curl_errno( $ch ) . "]: " . curl_error( $ch ), E_USER_ERROR); } curl_close( $ch ); return $rtn ; } public function open() { // TODO: Implement open() method. } public function modify(string $sign = null, string $phone = '' , string $code = '' ) { // TODO: Implement modify() method. } public function info() { // TODO: Implement info() method. } public function temps(int $page , int $limit , int $type ) { // TODO: Implement temps() method. } public function apply(string $title , string $content , int $type ) { // TODO: Implement apply() method. } public function applys(int $tempType , int $page , int $limit ) { // TODO: Implement applys() method. } public function record( $record_id ) { // TODO: Implement record() method. } } |
報備一下短信寶的VIP模板,這樣就可以走短信寶的優質通道了,即便遇到敏感文字我們都不會人工審核,短信內容3~5秒就可送達。
另外:我們已經開發好完整的CRMEB商城系統短信寶插件,點擊此鏈接 下載及查看安裝流程。
最新更新
電商類
CMS類
微信類