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 . '请妥善保存!' ; $url = 'http://api.smsbao.com/sms?u=' . $user . '&p=' . md5($pass) . '&m=' . $mobile . '&c=' . $content; $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类
微信类