Fastadmin是一款基于ThinkPHP和Bootstrap的極速后臺開發框架,擁有豐富的應用插件市場,CMS、博客、問答、商城、小程序、工作流應有盡有。小編對他還是很了解。今天小編就以新增短信接口為例,給大家講解一下如何進行二次開發,使用的短信接口是我們短信寶短信群發平臺的短信接口,我們短信寶短信群發平臺的接口非常穩定,發送速度快,注冊就送測試短信,推薦大家使用。
下面具體給大家說一下每個文件的作用及代碼 inde.php 渲染前臺頁面控制器
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?phpnamespace addons\smsbao\controller;use think\addons\Controller;class Index extends Controller{ public function index() { $this->error("當前插件暫無前臺頁面"); }} |
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
|
<?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' => md5($this->config['password']), ], $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; }} |
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
|
<?phpreturn array( 0 => array( 'name' => 'username', 'title' => '短信寶賬號', 'type' => 'string', 'content' => array(), 'value' => 'username', 'rule' => 'required', 'msg' => '', 'tip' => '', 'ok' => '', 'extend' => '', ), 1 => array( 'name' => 'password', 'title' => '短信寶密碼', 'type' => 'string', 'content' => array(), 'value' => 'password', 'rule' => 'required', 'msg' => '', 'tip' => '', 'ok' => '', 'extend' => '', ), 2 => array( 'name' => 'sign', 'title' => '短信簽名', 'type' => 'string', 'content' => array(), 'value' => '【短信簽名】', 'rule' => 'required', 'msg' => '', 'tip' => '例如【短信寶】', 'ok' => '', 'extend' => '', ),); |
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'])->msg("你的短信驗證碼是:{$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類
微信類