OpenCart是一款開源的 PHP 開源電子商務建站系統。OpenCart獨立站建站系統安裝方便、功能強大、操作簡單。支持多語言、多貨幣、多店鋪等功能。今天小編就以新增短信接口為例,給大家講解一下如何進行二次開發,使用的短信接口是我們短信寶短信群發平臺的短信接口,我們短信寶短信群發平臺的接口非常穩定,發送速度快,注冊就送測試短信,推薦大家使用。
1:打開項目:\admin\view\template\setting\setting.twig 增加短信寶設置頁面大概在40行和1235行
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
|
<ul class= "nav nav-tabs" > <li class= "active" ><a href= "#tab-general" data-toggle= "tab" >{{ tab_general }}</a></li> <li><a href= "#tab-store" data-toggle= "tab" >{{ tab_store }}</a></li> <li><a href= "#tab-local" data-toggle= "tab" >{{ tab_local }}</a></li> <li><a href= "#tab-option" data-toggle= "tab" >{{ tab_option }}</a></li> <li><a href= "#tab-image" data-toggle= "tab" >{{ tab_image }}</a></li> <li><a href= "#tab-mail" data-toggle= "tab" >{{ tab_mail }}</a></li> <li><a href= "#tab-server" data-toggle= "tab" >{{ tab_server }}</a></li> <li><a href= "#tab-sms" data-toggle= "tab" >短信寶配置</a></li> </ul> <div class= "tab-pane" id= "tab-sms" > <div class= "form-group required" > <label class= "col-sm-2 control-label" for = "input-name" >短信寶用戶名</label> <div class= "col-sm-10" > <input type= "text" name= "config_smsbaouser" value= "{{ config_smsbaouser }}" placeholder= "短信寶用戶名" id= "input-name" class= "form-control" /> </div> </div> <div class= "form-group required" > <label class= "col-sm-2 control-label" for = "input-owner" >短信寶密碼</label> <div class= "col-sm-10" > <input type= "text" name= "config_smsbaopass" value= "{{ config_smsbaopass }}" placeholder= "短信寶密碼" id= "input-owner" class= "form-control" /> </div> </div> <div class= "form-group required" > <label class= "col-sm-2 control-label" for = "input-owner" >短信寶簽名</label> <div class= "col-sm-10" > <input type= "text" name= "config_smsbaosign" value= "{{ config_smsbaosign }}" placeholder= "短信寶簽名" id= "input-owner" class= "form-control" /> </div> </div> </div> |
2:打開項目:admin\controller\setting\setting.php 修改代碼大概224行左右
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
if (isset($ this ->request->post[ 'config_smsbaouser' ])) { $data[ 'config_smsbaouser' ] = $ this ->request->post[ 'config_smsbaouser' ]; } else { $data[ 'config_smsbaouser' ] = $ this ->config->get( 'config_smsbaouser' ); } if (isset($ this ->request->post[ 'config_smsbaopass' ])) { $data[ 'config_smsbaopass' ] = $ this ->request->post[ 'config_smsbaopass' ]; } else { $data[ 'config_smsbaopass' ] = $ this ->config->get( 'config_smsbaopass' ); } if (isset($ this ->request->post[ 'config_smsbaosign' ])) { $data[ 'config_smsbaosign' ] = $ this ->request->post[ 'config_smsbaosign' ]; } else { $data[ 'config_smsbaosign' ] = $ this ->config->get( 'config_smsbaosign' ); } if (isset($ this ->request->post[ 'config_name' ])) { $data[ 'config_name' ] = $ this ->request->post[ 'config_name' ]; } else { $data[ 'config_name' ] = $ this ->config->get( 'config_name' ); } |
3:打開項目:catalog\view\theme\default\template\account\register.twig 修改代碼大概97行
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
|
<div class= "form-group required " > <label class= "col-sm-2 control-label" for = "input-mobiles" >短信驗證碼</label> <div class= "col-sm-10" > <input type= "text" name= "mobiles" value= "" placeholder= "短信驗證碼" id= "input-mobiles" class= "form-control" /> <b class= "huoqu" style= "height: 28px; width: 120px;cursor: pointer;" >獲取驗證碼</b> {% if error_mobiles %} <div class= "text-danger" >{{ error_mobiles }}</div> {% endif %} </div> </div> <script> var time = 0; // 倒計時時間 var res = null ; // 倒計時資源,釋放時使用 /** * 執行倒計時的方法 */ function sendTime() { clearTimeout(res); // 先清空一下倒計時資源。 time--; // 倒計時時間遞減。 // 如果倒計時到達0時,則恢復按鈕原來的內容 if (time <= 0) { time = "獲取驗證碼" ; $( '.huoqu' ).text(time); clearTimeout(res); time = 0; return ; } // 倒計時的內容寫到按鈕里面 $( '.huoqu' ).text( "剩余" + time + "秒" ); res = setTimeout( "sendTime()" , 1000); } /** * 調用處 */ $( function () { var flg = true ; // 防止ajax重復提交的標記 /** * 點擊發送短信,觸發事件 */ $( '.huoqu' ).on( "click" , function () { var mobile = $( "#input-telephone" ).val(); var reg = /^1[3,4,5,7,8]\d{9}$/; if (!reg.test(mobile)) {alert( '手機號碼不正確' ); return ret;} // 如果當前倒計時結束,則收集表單數據,并ajax提交到服務端 if (0 == time) { var data = { "mobile" : mobile}; var err = "" ; if (flg == true ) { flg = false ; // ajax提交請求 $.ajax({ "url" : "index.php?route=account/register/sendsms" , "type" : "post" , "data" : data, "dataType" : "json" , "success" : function (msg) { if (msg == '0' ){ time = 60; sendTime(); alert( '短信發送成功!' ); ret = true ; } else if (msg == '2' ){ alert( '短信配置不正確' ); } else { alert(msg); } } }); } return false ; } }); }); </script> |
4:打開項目:catalog\view\theme\default\template\account\forgotten.twig 修改密碼增加短信頁面 大概32行
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
|
<fieldset class= "{{ register_type != 'mobile' ? 'hidden' }} mobile" > <legend class= "mobile" >您的手機號</legend> <div class= "form-group required mobile" > <label class= "col-sm-2 control-label" for = "input-telephone" >手機號碼</label> <div class= "col-sm-10" > <input type= "tel" name= "telephone" value= "" placeholder= "手機號碼" id= "input-telephone" class= "form-control" /> {% if error_telephone %} <div class= "text-danger" >{{ error_telephone }}</div> {% endif %} </div> </div> <div class= "form-group required mobile" > <label class= "col-sm-2 control-label" for = "input-mobiles" >短信驗證碼</label> <div class= "col-sm-10" > <input type= "text" name= "mobiles" value= "" placeholder= "短信驗證碼" id= "input-mobiles" class= "form-control" /> <b class= "huoqu" style= "height: 28px; width: 120px;cursor: pointer;" >獲取驗證碼</b> {% if error_mobiles %} <div class= "text-danger" >{{ error_mobiles }}</div> {% endif %} </div> </div> </fieldset> <script> var time = 0; // 倒計時時間 var res = null ; // 倒計時資源,釋放時使用 /** * 執行倒計時的方法 */ function sendTime() { clearTimeout(res); // 先清空一下倒計時資源。 time--; // 倒計時時間遞減。 // 如果倒計時到達0時,則恢復按鈕原來的內容 if (time <= 0) { time = "獲取驗證碼" ; $( '.huoqu' ).text(time); clearTimeout(res); time = 0; return ; } // 倒計時的內容寫到按鈕里面 $( '.huoqu' ).text( "剩余" + time + "秒" ); res = setTimeout( "sendTime()" , 1000); } /** * 調用處 */ $( function () { var flg = true ; // 防止ajax重復提交的標記 /** * 點擊發送短信,觸發事件 */ $( '.huoqu' ).on( "click" , function () { var mobile = $( "#input-telephone" ).val(); var reg = /^1[3,4,5,7,8]\d{9}$/; if (!reg.test(mobile)) {alert( '手機號碼不正確' ); return ret;} // 如果當前倒計時結束,則收集表單數據,并ajax提交到服務端 if (0 == time) { var data = { "mobile" : mobile}; var err = "" ; if (flg == true ) { flg = false ; // ajax提交請求 $.ajax({ "url" : "index.php?route=account/register/sendsms" , "type" : "post" , "data" : data, "dataType" : "json" , "success" : function (msg) { if (msg == '0' ){ time = 60; sendTime(); alert( '短信發送成功!' ); ret = true ; } else if (msg == '2' ){ alert( '短信配置不正確' ); } else { alert(msg); } } }); } return false ; } }); }); </script> |
5:打開項目:catalog\controller\account\register.php 增加短信發送函數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public function sendsms() { $mobile = $_POST[ 'mobile' ]; $ this ->load->model( 'setting/setting' ); $user = $ this ->config->get( 'config_smsbaouser' ); $pass = $ this ->config->get( 'config_smsbaopass' ); $sign = $ this ->config->get( 'config_smsbaosign' ); if (empty($user) && empty($pass) && empty($sign)) { echo json_encode(2);exit; } $code = rand(1000,9999); $content = '【' .$sign. '】您的驗證碼為:' .$code. '請妥善保存!' ; $result = file_get_contents($url); if ($result == 0) { $ this ->session->data[ 'mobile_code' ] = $code; echo json_encode(0);exit; } else { echo json_encode($result);exit; } } |
6:當前項目235行左右增加校驗短信代碼
1
2
3
4
5
6
|
if ($ this ->request->post[ 'mobiles' ] != $ this ->session->data[ 'mobile_code' ] && $ this ->request->post[ 'type' ] == 'mobile' ) { $ this ->error[ 'mobiles' ] = '短信驗證碼錯誤' ; } if ((utf8_strlen(trim($ this ->request->post[ 'firstname' ])) < 1) || (utf8_strlen(trim($ this ->request->post[ 'firstname' ])) > 32)) { $ this ->error[ 'firstname' ] = $ this ->language->get( 'error_firstname' ); } |
7:打開項目:catalog\controller\account\forgotten.php 修改代碼大概19行
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
|
if (($ this ->request->server[ 'REQUEST_METHOD' ] == 'POST' ) && $ this ->validate()) { if ($ this ->request->post[ 'type' ] == 'mobile' ) { $ this ->checksms($ this ->request->post[ 'telephone' ]); $customer_info = $ this ->model_account_customer->getCustomerByMobile($ this ->request->post[ 'telephone' ]); $ this ->model_account_customer->editPassword($customer_info[ 'customer_id' ], $ this ->session->data[ 'checkpassword' ]); $ this ->session->data[ 'success' ] = '重設密碼鏈接已發送到您的手機!' ; } else { $customer_info = $ this ->model_account_customer->getCustomerByEmail($ this ->request->post[ 'email' ]); $ this ->model_account_customer->editCode($customer_info[ 'customer_id' ], token(40)); $ this ->session->data[ 'success' ] = $ this ->language->get( 'text_success' ); } $ this ->response->redirect($ this ->url->link( 'account/login' )); } protected function checksms($mobile) { $ this ->load->model( 'setting/setting' ); $user = $ this ->config->get( 'config_smsbaouser' ); $pass = $ this ->config->get( 'config_smsbaopass' ); $sign = $ this ->config->get( 'config_smsbaosign' ); if (empty($user) && empty($pass) && empty($sign)) { return 2; exit; } $code = rand(1000, 9999); $content = '【' . $sign . '】您的新密碼為:' . $code . '請妥善保存!' ; $result = file_get_contents($url); if ($result == 0) { $ this ->session->data[ 'checkpassword' ] = $code; return 0; exit; } else { return $result; exit; } } |
8:當前項目93行左右增加校驗短信代碼
1
2
3
4
5
6
7
8
|
if ($ this ->request->post[ 'type' ] == 'mobile' ) { if ($ this ->request->post[ 'mobiles' ] != $ this ->session->data[ 'mobile_code' ]) { $ this ->error[ 'mobiles' ] = '短信驗證碼錯誤' ; } if ($ this ->request->post[ 'telephone' ] && !$ this ->model_account_customer->getTotalCustomersByTelephone($ this ->request->post[ 'telephone' ])) { $ this ->error[ 'warning' ] = $ this ->language->get( 'error_exists_telephone' ); } } |
經過上面的替換,短信寶的短信平臺已經替換成功了,可以正常使用了。進行測試發送:
報備一下短信寶的VIP模板,這樣就可以走短信寶的優質通道了,即便遇到敏感文字我們都不會人工審核,短信內容3~5秒就可送達。
另外:我們已經開發好完整的OpenCartV3.8系統短信寶插件,點擊此鏈接 下載及查看安裝流程。
最新更新
電商類
CMS類
微信類