極致CMS是開源免費的PHPCMS網站內容管理系統,無商業授權,簡單易用,提供豐富的插件,實現零基礎搭建不同類型網站(企業站,門戶站,個人博客站等),是個建站的好幫手。小編帶著大家一起開發極致CMS的短信寶插件接口。我們使用的短信接口是我們短信寶短信群發平臺的短信接口,我們短信寶短信群發平臺非常穩定,發送速度快,注冊就送測試短信,推薦大家使用。
1.首先打開項目app\admin\exts,新增smsbao 文件并創建config.php
1
2
3
4
5
6
7
8
9
10
|
<?php return [ 'name' => '短信寶短信注冊' , //插件名 'desc' => '使用短信寶短信注冊或者登錄系統' , //插件介紹 'author' => '短信寶小編' , //作者介紹,這里可以把自己的聯系方式帶上去,方便用戶溝通 'version' => '1.0' , //插件版本,默認1.0為最低版本 'update_time' => '2025-06-12' , //插件更新時間,格式:Y-m-d 'module' => 'Home' , //插件應用的模塊,Home表示前臺模塊,Admin表示后臺模塊.插件安裝的時候會據此加載控制器到對應的目錄中 ]; |
接著創建PluginsController.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
|
<?php namespace app\admin\exts; use frphp\lib\Controller; use frphp\extend\Page; class PluginsController extends Controller { //自動執行 public function _init(){ /** 繼承系統默認配置 **/ //檢查當前賬戶是否合乎操作 if (!isset( $_SESSION [ 'admin' ]) || $_SESSION [ 'admin' ][ 'id' ]==0){ Redirect(U( 'Login/index' )); } if ( $_SESSION [ 'admin' ][ 'isadmin' ]!=1){ if ( strpos ( $_SESSION [ 'admin' ][ 'paction' ], ',' .APP_CONTROLLER. ',' )!==false){ } else { $action = APP_CONTROLLER. '/' .APP_ACTION; if ( strpos ( $_SESSION [ 'admin' ][ 'paction' ], ',' . $action . ',' )===false){ $ac = M( 'Ruler' )->find( array ( 'fc' => $action )); if ( $this ->frparam( 'ajax' )){ JsonReturn([ 'code' =>1, 'msg' => '您沒有【' . $ac [ 'name' ]. '】的權限!' , 'url' =>U( 'Index/index' )]); } Error( '您沒有【' . $ac [ 'name' ]. '】的權限!' ,U( 'Index/index' )); } } } $webconf = webConf(); $this ->webconf = $webconf ; $customconf = get_custom(); $this ->customconf = $customconf ; //插件模板頁目錄 $this ->tpl = '@' .dirname( __FILE__ ). '/tpl/' ; /** 在下面添加自定義操作 **/ } //執行SQL語句在此處處理,或者移動文件也可以在此處理 public function install(){ //下面是新增test表的SQL操作 $this ->recurse_copy(APP_PATH. 'app/admin/exts/smsbao/file/' ,APP_PATH); return true; } //卸載程序,對新增字段、表等進行刪除SQL操作,或者其他操作 public function uninstall(){ if ( is_dir (APP_PATH. 'smsbao' )){ deldir(APP_PATH. 'smsbao' ); } return true; } // 原目錄,復制到的目錄 function recurse_copy( $src , $dst ) { $dir = opendir( $src ); @ mkdir ( $dst ); while (false !== ( $file = readdir( $dir )) ) { if (( $file != '.' ) && ( $file != '..' )) { if ( is_dir ( $src . '/' . $file ) ) { $this ->recurse_copy( $src . '/' . $file , $dst . '/' . $file ); } else { copy ( $src . '/' . $file , $dst . '/' . $file ); } } } closedir ( $dir ); } //安裝頁面介紹,操作說明 public function desc(){ $this ->display( $this ->tpl. 'plugins-description.html' ); } //配置文件,插件相關賬號密碼等操作 public function setconf( $plugins ){ //將插件賦值到模板中 $this ->plugins = $plugins ; $this ->config = json_decode( $plugins [ 'config' ],1); $this ->display( $this ->tpl. 'plugins-body.html' ); } //獲取插件內提交的數據處理 public function setconfigdata( $data ){ M( 'plugins' )->update([ 'id' => $data [ 'id' ]],[ 'config' =>json_encode( $data ,JSON_UNESCAPED_UNICODE)]); setCache( 'hook' ,null); //清空hook緩存 JsonReturn([ 'code' =>0, 'msg' => '設置成功!' ]); } } |
2.接著在app\admin\exts\smsbao目錄下創建tpl文件夾并創建plugins-body.html
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
|
<!doctype html> <html lang= "en" > <head> <meta charset= "UTF-8" > { include = "style" } <style> .layui-form-label { width: 230px; } </style> </head> <body > <div class = "layui-rows" style= " margin: 84px;" > <form class = "layui-form" action= "" > <input name= "id" value= "{$plugins['id']}" type= "hidden" > <div class = "layui-form-item" > <label class = "layui-form-label" >短信寶賬號:</label> <div class = "layui-input-inline" > <input type= "text" name= "username" required value= "{$config['username']}" lay-verify= "required" placeholder= "請輸入短信寶賬號" autocomplete= "off" class = "layui-input" > </div> <div class = "layui-form-mid layui-word-aux" >還沒注冊短信寶賬號? <a style= "color: blue;" target= "_blank" href= " </div> <div class = "layui-form-item" > <label class = "layui-form-label" >短信寶APIKEY:</label> <div class = "layui-input-inline" > <input type= "password" name= "apiKey" value= "{$config['apiKey']}" required lay-verify= "required" placeholder= "請輸入APIKEY" autocomplete= "off" class = "layui-input" > </div> <div class = "layui-form-mid layui-word-aux" ></div> </div> <div class = "layui-form-item" > <label class = "layui-form-label" >短信簽名:</label> <div class = "layui-input-inline" > <input type= "text" name= "SignName" required value= "{$config['SignName']}" lay-verify= "required" placeholder= "短信簽名" autocomplete= "off" class = "layui-input" > </div> </div> <div class = "layui-form-item" > <label class = "layui-form-label" >模板內容:</label> <div class = "layui-input-inline" > <input type= "text" name= "templateContent" required value= "{$config['templateContent']}" lay-verify= "required" placeholder= "模板內容" autocomplete= "off" class = "layui-input" > </div> <div class = "layui-form-mid layui-word-aux" >模板示例:您的驗證碼為: {code},如非本人操作,請忽略本短信</div> </div> <div class = "layui-form-item" > <div class = "layui-input-block" > <button class = "layui-btn" lay-submit lay-filter= "formDemo" >立即提交</button> <button type= "reset" class = "layui-btn layui-btn-primary" >重置</button> </div> </div> </form> </div> <script> $( function () { layui. use ( 'form' , function (){ var form = layui.form; //監聽提交 form.on( 'submit(formDemo)' , function (data){ $.post( "{fun U('setconf')}" ,data.field, function (res){ //console.log(res);return false; var res = JSON.parse(res); if (res.code==1){ layer.msg(res.msg); } else { layer.msg(res.msg, {icon: 6,time: 2000}, function (){ window.location.reload(); }); } }) return false; }); }); }) </script> </body> </html> |
3.接著在app\admin\exts\smsbao目錄下創建file\smsbao文件夾并創建Sms.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
|
<?php ini_set ( "display_errors" , "on" ); class Sms { private static $statusStr = array ( "0" => "短信發送成功" , "-1" => "參數不全" , "-2" => "服務器空間不支持,請確認支持curl或者fsocket,聯系您的空間商解決或者更換空間!" , "30" => "密碼錯誤" , "40" => "賬號不存在" , "41" => "余額不足" , "42" => "帳戶已過期" , "43" => "IP地址限制" , "50" => "內容含有敏感詞" , "51" => "手機號碼不正確" ); public static $username = '' ; public static $apiKey = '' ; public static $SignName = '' ; public static $templateContent = '' ; public static function setdata( $username , $apiKey , $SignName , $templateContent ){ static :: $username = $username ; static :: $apiKey = $apiKey ; static :: $SignName = $SignName ; static :: $templateContent = $templateContent ; } private static function curl_get( $sendurl ){ $ch = curl_init(); curl_setopt( $ch , CURLOPT_URL, $sendurl ); curl_setopt( $ch , CURLOPT_TIMEOUT, 1); curl_setopt( $ch , CURLOPT_USERAGENT, $_SERVER [ "HTTP_USER_AGENT" ]); curl_setopt( $ch , CURLOPT_FOLLOWLOCATION, 1); curl_setopt( $ch , CURLOPT_RETURNTRANSFER, true); $result = curl_exec( $ch ); curl_close( $ch ); return $result ; } /** * 發送短信 * @return stdClass */ public static function sendSms( $tel , $code ) { try { $statusStr = static :: $statusStr ; $user = static :: $username ; $apikey = static :: $apiKey ; $templateContent = str_replace ( '{code}' , $code , static :: $templateContent ); $content = '【' . static :: $SignName . '】' . $templateContent ; $sendurl = static :: $sendUrl . "u=" . $user . "&p=" . $apikey . "&m=" . $tel . "&c=" .urlencode( $content ); $result = static ::curl_get( $sendurl ); if ( $result == 0) { return $res [ 'code' ] = $result ; } else { $res [ 'code' ] = $result ; $res [ 'msg' ] = $statusStr [ $result ]; return $res ; } } catch (\Exception $error ) { $res [ 'code' ] = '-1' ; $res [ 'msg' ] = "發送失敗" ; return $res ; } return $res [ 'code' ] = '0' ; } } |
4.接著在app\admin\exts\smsbao目錄下創建controller\home文件夾并創建MsgController.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
|
<?php namespace app\home\plugins; use app\home\c\CommonController; class MsgController extends CommonController { function _init(){ parent::_init(); $w [ 'filepath' ] = 'smsbao' ; $w [ 'isopen' ] = 1; $res = M( 'plugins' )->find( $w ); if (! $res ){ JsonReturn([ 'code' =>1, 'msg' => '短信插件未開啟!' ]); } $res [ 'config' ] = json_decode( $res [ 'config' ],1); $this ->plugin = $res ; require_once APP_PATH. 'smsbao/Sms.php' ; \Sms::setdata( $res [ 'config' ][ 'username' ], $res [ 'config' ][ 'apiKey' ], $res [ 'config' ][ 'SignName' ], $res [ 'config' ][ 'templateContent' ]); } function index(){ $yzmname = $this ->frparam( 'codename' ,1); $yzm = $this ->frparam( 'yzm' ,1); if (isset( $GLOBALS [ 'Redis' ])){ $code = $GLOBALS [ 'Redis' ]->get( $yzmname ); if (! $yzm || md5(md5( $yzm ))!= $code ){ JsonReturn([ 'code' =>1, 'msg' => '驗證碼錯誤!' , 'data' =>[]]); } } else { if (! $yzm || md5(md5( $yzm ))!= $_SESSION [ $yzmname ]){ JsonReturn([ 'code' =>1, 'msg' => '驗證碼錯誤!' , 'data' =>[]]); } } $tel = $this ->frparam( 'tel' ,1); if ( strlen ( $tel )==11){ } else { JsonReturn([ 'code' =>1, 'msg' => '手機號格式錯誤!' , 'data' =>[]]); } $code = rand(100000,999999); $_SESSION [ 'tel_code' ] = $code ; if (isset( $GLOBALS [ 'Redis' ])){ $GLOBALS [ 'Redis' ]->setex( 'tel_code' ,5 * 60, $code ); } $response = \Sms::sendSms( $tel , $code ); //echo "發送短信(sendSms)接口返回的結果:\n"; //print_r($response); if ( $response [ 'code' ]== '0' ){ JsonReturn([ 'code' =>0, 'msg' => '發送成功!' , 'data' =>[]]); } else { JsonReturn([ 'code' =>1, 'msg' => $response [ 'msg' ], 'data' =>[]]); } } function sendmsg( $code , $tel ){ if ( strlen ( $tel )==11){ } else { JsonReturn([ 'code' =>1, 'msg' => '手機號格式錯誤!' , 'data' =>[]]); } $_SESSION [ 'tel_code' ] = $code ; if (isset( $GLOBALS [ 'Redis' ])){ $GLOBALS [ 'Redis' ]->setex( 'tel_code' ,5 * 60, $code ); } $response = \Sms::sendSms( $tel , $code ); //echo "發送短信(sendSms)接口返回的結果:\n"; //print_r($response); if ( $response [ 'code' ]== '0' ){ JsonReturn([ 'code' =>0, 'msg' => '發送成功!' , 'data' =>[]]); } else { JsonReturn([ 'code' =>1, 'msg' => $response [ 'msg' ], 'data' =>[]]); } } } |
5.接著在注冊、登錄頁面增加以下前端代碼
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
|
#################login.html頁面#################### <div class= "form-control" > <label for = "" >手機驗證碼:</label> <input type= "text" value= "" name= "telyzm" id= "telyzm" placeholder= "請輸入手機驗證碼" > <button type= "button" onclick= "sendmsg()" >發送驗證碼</button> </div> function sendmsg(){ var tel = $( "#tel" ).val(); if (tel== '' ){ alert( '手機號不能為空!' ); return false ; } var vercode = $( "#yzm" ).val(); if (vercode== '' ){ alert( '圖形驗證碼不能為空!' ); return false ; } $.post( '/msg/index' ,{tel:tel,yzm:vercode,codename: 'login_vercode' }, function (r){ alert(r.msg); }, 'json' ); } #################register.html頁面#################### <div class= "form-control" > <label for = "" >手機驗證碼:</label> <input type= "text" value= "" name= "telyzm" id= "telyzm" placeholder= "請輸入手機驗證碼" > <button type= "button" onclick= "sendmsg()" >發送驗證碼</button> </div> function sendmsg(){ var tel = $( "#tel" ).val(); if (tel== '' ){ alert( '手機號不能為空!' ); return false ; } var vercode = $( "#yzm" ).val(); if (vercode== '' ){ alert( '圖形驗證碼不能為空!' ); return false ; } $.post( '/msg/index' ,{tel:tel,yzm:vercode,codename: 'reg_vercode' }, function (r){ alert(r.msg); }, 'json' ); } |
經過上面的增加,短信寶的短信平臺已經對接成功了,可以正常使用了。
報備一下短信寶的VIP模板,這樣就可以走短信寶的優質通道了,即便遇到敏感文字我們都不會人工審核,短信內容3~5秒就可送達。
另外:我們已經開發好完整的極致CMS系統短信寶插件,點擊此鏈接 下載及查看安裝流程。
最新更新
電商類
CMS類
微信類