FastAdmin是一款基于ThinkPHP+Bootstrap的極速后臺開發框架。強大的插件擴展功能,在線安裝卸載升級插件。小編帶著大家一起開發fastadmin的短信寶插件接口。我們使用的短信接口是我們短信寶短信群發平臺的短信接口,我們短信寶短信群發平臺非常穩定,發送速度快,注冊就送測試短信,推薦大家使用。
1.首先完成渲染前臺控制器代碼:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?phpnamespace addons\smsbao\controller;use think\addons\Controller;class Index extends Controller{ public function index() { $this->error("當前插件暫無前臺頁面"); }} |
2.接著在library目錄下的創建短信發送類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
|
<?phpnamespace addons\smsbao\library;class Smsbao{ private $_params = []; protected $error = ''; protected $config = []; protected static $instance = null; protected $statusStr = array( "0" => "短信發送成功", "-1" => "參數不全", "-2" => "服務器空間不支持,請確認支持curl或者fsocket,聯系您的空間商解決或者更換空間!", "30" => "密碼錯誤", "40" => "賬號不存在", "41" => "余額不足", "42" => "帳戶已過期", "43" => "IP地址限制", "50" => "內容含有敏感詞" ); public function __construct($options = []) { if ($config = get_addon_config('smsbao')) { $this->config = array_merge($this->config, $config); } $this->config = array_merge($this->config, is_array($options) ? $options : []); } /** * 單例 * @param array $options 參數 * @return Smsbao */ public static function instance($options = []) { if (is_null(self::$instance)) { self::$instance = new static($options); } return self::$instance; } /** * 立即發送短信 * * @return boolean */ public function send() { $this->error = ''; $params = $this->_params(); $postArr = array( 'u' => $params['u'], 'p' => $params['p'], 'm' => $params['mobile'], 'c' => $params['msg'] ); $options = [ CURLOPT_HTTPHEADER => array( 'Content-Type: application/json; charset=utf-8' ) ]; if ($result['ret']) { if (isset($result['msg']) && $result['msg'] == '0') return TRUE; $this->error = isset($this->statusStr[$result['msg']]) ? $this->statusStr[$result['msg']] : 'InvalidResult'; } else { $this->error = $result['msg']; } return FALSE; } private function _params() { return array_merge([ 'u' => $this->config['username'], 'p' => $this->config['apikey'], ], $this->_params); } /** * 獲取錯誤信息 * @return string */ public function getError() { return $this->error; } /** * 接收手機 * @param string $mobile 手機號碼 * @return Smsbao */ public function mobile($mobile = '') { $this->_params['mobile'] = $mobile; return $this; } /** * 短信內容 * @param string $msg 短信內容 * @return Smsbao */ public function msg($msg = '') { $this->_params['msg'] = $this->config['sign'] . $msg; return $this; } /** * 短信內容 * @param string $msgCode 短信code * @return Smsbao */ public function msgCode($code = '') { $this->_params['msg'] = $this->config['sign'] . str_replace("{code}", $code, $this->config['template']); return $this; }} |
3.接著創建短信配置文件config.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
|
<?phpreturn [ [ 'name' => 'username', 'title' => '短信寶賬號', 'type' => 'string', 'content' => [], 'value' => '短信寶賬號', 'rule' => 'required', 'msg' => '', 'tip' => '', 'ok' => '', 'extend' => '', ], [ 'name' => 'apikey', 'title' => '短信寶APIKEY', 'type' => 'string', 'content' => [], 'value' => '短信寶APIKEY', 'rule' => 'required', 'msg' => '', 'tip' => '', 'ok' => '', 'extend' => '', ], [ 'name' => 'sign', 'title' => '短信簽名', 'type' => 'string', 'content' => [], 'value' => '【安全鎖】', 'rule' => 'required', 'msg' => '', 'tip' => '例如【安全鎖】', 'ok' => '', 'extend' => '', ], [ 'name' => 'template', 'title' => '短信模板', 'type' => 'string', 'content' => [], 'value' => '你的短信驗證碼是:{code}', 'rule' => 'required', 'msg' => '', 'tip' => '示例:你的短信驗證碼是:{code}', 'ok' => '', 'extend' => '', ],]; |
4.接著是有關插件啟用禁用等操作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
|
<?phpnamespace addons\smsbao;use app\common\library\Menu;use think\Addons;/** * Smsbao插件 */class Smsbao extends Addons{ /** * 插件安裝方法 * @return bool */ public function install() { return true; } /** * 插件卸載方法 * @return bool */ public function uninstall() { return true; } /** * 插件啟用方法 * @return bool */ public function enable() { return true; } /** * 插件禁用方法 * @return bool */ public function disable() { return true; } /** * 短信發送 * @param Sms $params * @return mixed */ public function smsSend(&$params) { $smsbao = new library\Smsbao(); $result = $smsbao->mobile($params['mobile'])->msgCode($params['code'])->send(); return $result; } /** * 短信發送通知(msg參數直接構建實際短信內容即可) * @param array $params * @return boolean */ public function smsNotice(&$params) { $smsbao = new library\Smsbao(); $result = $smsbao->mobile($params['mobile'])->msg($params['msg'])->send(); return $result; } /** * 檢測驗證是否正確 * @param Sms $params * @return boolean */ public function smsCheck(&$params) { return TRUE; }} |
經過上面的替換,短信寶的短信平臺已經替換成功了,可以正常使用了。進行測試發送:
報備一下短信寶的VIP模板,這樣就可以走短信寶的優質通道了,即便遇到敏感文字我們都不會人工審核,短信內容3~5秒就可送達。
另外:我們已經開發好完整的fastAdmin短信寶插件,點擊此鏈接?下載及查看安裝流
最新更新
電商類
CMS類
微信類