待发短信

在线客服
产品支持 短信宝客服
合作渠道 渠道合作
服务咨询

4001-021-502

工作时间

9:00-18:00

OpenCart网站系统新增短信宝短信接口

OpenCart是近年来国内外非常流行的 PHP 开源电子商务网站系统。该电商网站系统安装方便,功能强大,操作简单。支持多语言、多货币和多店铺。代码完全开源,代码结构清晰易懂,二次开发容易上手,入门门槛低。小编今天就以新增短信接口为例为大家讲解一下如何进行二次开发,我们使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台非常稳定,发送速度快,注册就送测试短信,推荐大家使用。

首先增加保存短信宝配置文件,打开项目:\admin\view\template\setting\setting.twig文件,在1206行增加以下代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<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>

接着打开项目:\admin\controller\setting\setting.php文件,在224~240行增加以下代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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');
}

接着打开项目:\catalog\view\theme\default\template\account\forgotten.twig文件,将以下代码进行替换

?
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
125
126
127
128
129
130
131
132
133
134
135
136
{{ header }}
<div id="account-forgotten" class="container">
  {{ include(helper.template('_partial/breadcrumb')) }}
  {% if error_warning %}
  <div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> {{ error_warning }}</div>
  {% endif %}
  <div class="row">{{ column_left }}
    {% if column_left and column_right %}
    {% set class = 'col-sm-6' %}
    {% elseif column_left or column_right %}
    {% set class = 'col-sm-9' %}
    {% else %}
    {% set class = 'col-sm-12' %}
    {% endif %}
    <div id="content" class="{{ class }}">{{ content_top }}
      <h1>{{ heading_title }}</h1>
      <p>{{ text_email }}</p>
 
      <ul class="nav nav-tabs">
        <li id="tab-email" {{ register_type != 'mobile' ? 'class="active"' }}><a href="#tab-general" data-toggle="tab">EMAIL找回</a></li>
        <li id="tab-mobile" {{ register_type == 'mobile' ? 'class="active"' }}><a href="#tab-general" data-toggle="tab">手机号找回</a></li>
      </ul>
 
      <form action="{{ action }}" method="post" enctype="multipart/form-data" class="form-horizontal">
        <fieldset class="{{ register_type == 'mobile' ? 'hidden' }} email">
          <legend class="email">{{ text_your_email }}</legend>
          <div class="form-group required email">
            <label class="col-sm-2 control-label" for="input-email">{{ entry_email }}</label>
            <div class="col-sm-10">
              <input type="text" name="email" value="{{ email }}" placeholder="{{ entry_email }}" id="input-email" class="form-control" />
            </div>
          </div>
        </fieldset>
        <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>
                      <div class="form-group hidden">
                  <input type="text" name="type" value="{{ register_type }}" class="form-control" />
              </div>
        <div class="buttons clearfix">
          <div class="pull-right">
            <input type="submit" value="{{ button_submit }}" class="btn btn-primary" />
          </div>
          <div class="pull-left"><a href="{{ back }}" class="btn btn-default">{{ button_back }}</a></div>
        </div>
      </form>
      {{ content_bottom }}</div>
    {{ column_right }}</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>
{{ footer }}

接着我们增加获取短信按钮,打开项目:\catalog\view\theme\default\template\account\register.twig文件,在70行左右添加以下代码

?
1
2
3
4
5
6
7
8
9
<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>  

接着在文件末尾增加获取短信倒计时代码

?
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
<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>

接着打开项目:\catalog\model\account\customer.php文件,在56行左右添加以下代码

?
1
2
3
4
5
public function getCustomerByMobile($mobile) {
    $query $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "customer WHERE LCASE(telephone) = '" $this->db->escape(utf8_strtolower($mobile)) . "'");
 
    return $query->row;
}

接着增加发送短信的方法,打开项目:\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
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.'请妥善保存!';
    $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['mobile_code'] = $code;
        echo json_encode(0);exit;
    }else{
        echo json_encode($result);exit;
    }
}

接着打开项目:\catalog\controller\account\forgotten.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
class ControllerAccountForgotten extends Controller {
private $error array();
 
public function index() {
    if ($this->customer->isLogged()) {
        $this->response->redirect($this->url->link('account/account'));
    }
 
    $this->load->language('account/forgotten');
 
    $this->document->setTitle($this->language->get('heading_title'));
 
    $this->load->model('account/customer');
 
    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'));
    }
 
    if (isset($this->request->post['type'])) {
        $data['register_type'] = $this->request->post['type'];
    else {
        $data['register_type'] = 'mobile';
    }
    $data['breadcrumbs'] = array();
 
    $data['breadcrumbs'][] = array(
        'text' => $this->language->get('text_home'),
        'href' => $this->url->link('common/home')
    );
 
    $data['breadcrumbs'][] = array(
        'text' => $this->language->get('text_account'),
        'href' => $this->url->link('account/account')
    );
 
    $data['breadcrumbs'][] = array(
        'text' => $this->language->get('text_forgotten'),
        'href' => $this->url->link('account/forgotten')
    );
 
    if (isset($this->error['warning'])) {
        $data['error_warning'] = $this->error['warning'];
    else {
        $data['error_warning'] = '';
    }
 
    if (isset($this->error['mobiles'])) {
        $data['error_mobiles'] = $this->error['mobiles'];
    else {
        $data['error_mobiles'] = '';
    }
 
    $data['action'] = $this->url->link('account/forgotten');
 
    $data['back'] = $this->url->link('account/login');
 
    if (isset($this->request->post['email'])) {
        $data['email'] = $this->request->post['email'];
    else {
        $data['email'] = '';
    }
    if (isset($this->error['telephone'])) {
        $data['error_telephone'] = $this->error['telephone'];
    else {
        $data['error_telephone'] = '';
    }
    $data['column_left'] = $this->load->controller('common/column_left');
    $data['column_right'] = $this->load->controller('common/column_right');
    $data['content_top'] = $this->load->controller('common/content_top');
    $data['content_bottom'] = $this->load->controller('common/content_bottom');
    $data['footer'] = $this->load->controller('common/footer');
    $data['header'] = $this->load->controller('common/header');
 
    $this->response->setOutput($this->load->view('account/forgotten'$data));
}
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;
    }
}
protected function validate() {
 
 
    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');
 
        }
    }
 
    if (!isset($this->request->post['email'])) {
        $this->error['warning'] = $this->language->get('error_email');
    elseif (!$this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
        $this->error['warning'] = $this->language->get('error_email');
    }
    
    // Check if customer has been approved.
    $customer_info $this->model_account_customer->getCustomerByEmail($this->request->post['email']);
 
    if ($customer_info && !$customer_info['status']) {
        $this->error['warning'] = $this->language->get('error_approved');
    }
    return !$this->error;
}
}

好了,经过以上的替换,短信宝的短信平台已经替换成功了,可以正常使用了。我们进行测试发送。

报备一下短信宝的VIP模板,这样就可以走短信宝的优质通道了,即便遇到敏感文字我们都不会人工审核,短信内容3~5秒就可送达。

另外:我们已经开发好完整的OpenCart网站系统短信宝插件,点击此链接 下载及查看安装流程。

 
开源插件
文章标签