待发短信

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

4001-021-502

工作时间

9:00-21:00

shopwindV3.4.3新增短信宝短信接口

shopwind是一款免费开源的轻量级电商系统。稳定的架构内核,基于Yii2深度开发,代码开源,遵循Apache2.0协议,完善的开发文档,功能模块化,拒绝臃肿繁杂,源码易读方便二次开发,丰富的API接口,数据通讯安全加签,可以满足任何功能插件开发的需要。小编对他还是比较了解的,今天小编就以新增短信接口为例,给大家讲解一下如何进行二次开发,我们今天讲解的是v3.4.3版本,使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台的接口非常稳定,发送速度快,注册就送测试短信,推荐大家使用
1:打开 shopwind/common/plugins/sms 新增短信宝smsbao目录
插件的目录结构如下:
├─smsbao插件目录
│ ├─plugin.info.php 短信插件配置信息
│ ├─SDK.php 继承系统短信基类
│ ├─smsbao.plugin.php 短信宝核心发送短信类
1:下面具体给大家说一下每个文件的作用及代码,plugin.info.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
<?php
/**
 * @copyright Copyright (c) 2018 ShopWind Inc. All Rights Reserved.
 *
 * This is not free software. Do not use it for commercial purposes. 
 * If you need commercial operation, please contact us to purchase a license.
 */
namespace common\plugins\sms\smsbao;
use yii;
use yii\helpers\Url;
use common\library\Language;
/**
 * @Id plugin.info.php 2018.8.3 $
 */
return array(
    'code' => 'smsbao',
    'name' => '短信宝',
    'desc' => '短信宝短信插件',
    'author' => 'smsbao',
    'website' => 'https://www.smsbao.com',
    'version' => '1.0',
    'buttons' => array(
        array(
            'label' => Language::get('manage'),
            'url' => Url::toRoute(['msg/index'])
        )
    ),
    'config' => array(
        'uid' => array(
            'type' => 'text',
            'text' => '短信宝账号'
        ),
        'key' => array(
            'type' => 'text',
            'text' => '短信宝密码'
        ),
        'scene' => array(
            'name' => 'config[scene]',
            'type' => 'checkbox',
            'text' => '启用场景',
            'items' => array(
                'register' => '用户注册',
                'find_password' => '找回密码'
            )
        )
    )
);

2:SDK.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
<?php
/**
 * @copyright Copyright (c) 2018 ShopWind Inc. All Rights Reserved.
 *
 * This is not free software. Do not use it for commercial purposes. 
 * If you need commercial operation, please contact us to purchase a license.
 */
namespace common\plugins\sms\smsbao;
use yii;
use yii\base\InvalidConfigException;
/**
 */
class SDK {
}

3:smsbao.plugin.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
<?php
/**
 * @copyright Copyright (c) 2018 ShopWind Inc. All Rights Reserved.
 *
 * This is not free software. Do not use it for commercial purposes. 
 * If you need commercial operation, please contact us to purchase a license.
 */
namespace common\plugins\sms\smsbao;
use yii;
use common\models\MsgTemplateModel;
use common\library\Basewind;
use common\library\Language;
use common\plugins\BaseSms;
/**
 */
class Smsbao extends BaseSms
{
    /**
    * 网关地址
    * @var string $gateway
    */
    protected $gateway = 'http://api.smsbao.com/';
   /**
     * 短信实例
    * @var string $code
    */
    protected $code = 'smsbao';
    protected $smbaostatus = [
                "0" => "短信发送成功",
                "-1" => "参数不全",
                "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
                "30" => "密码错误",
                "40" => "账号不存在",
                "41" => "余额不足",
                "42" => "帐户已过期",
                "43" => "IP地址限制",
                "50" => "内容含有敏感词"
    ];
    /**
    * 发送短信
     * @param bool $valid 发送频次等的校验,如果是系统发送的短信,可以适当的不做该校验以确保发送成功
    */
   public function send($valid = true)
   {
        if(!$this->verify()) {
            return false;
        }
        if($valid === true && !$this->validSend()) {
            return false;
        }
        // 发送的短信信息校验
        if($this->validData() == false) {
            return false;
        }
        
        // 等于0说明发送成功,$result即为发送的短信条数(当短信内容过长,会分成2条来发送)
        $result = $this->submit();
        if($result == '0') {
            $codekey = $this->insert(1);
            return $codekey;
        }
        $this->errors = $this->smbaostatus[$result];
        $this->insert(0, $this->errors);
        return false;
    }
    /**
     * 测试短信发送(仅做测试用,不要用作正式场合)
     */
    public function testsend($content = '') {
        $this->content = $content;
        $result = $this->submit();
        if($result > 0) {
            return true;
        }
        $this->errors = $this->getMessage($result);
        return false;
    }
    
    /**
     * 执行短信发生
     */
    private function submit()
    {
        $sendurl = $this->gateway."sms?u=".$this->config['uid']."&p=".md5($this->config['key'])."&m=".$this->receiver."&c=".urlencode('【'.$this->signName.'】'.$this->content);
        return Basewind::curl($sendurl);
    }
    /**
     * 检测是否配置
     * @var boolean $force 是否验证短信模板内容
     */
    public function verify($force = true)
    {
        if(!$this->config['uid']) {
            $this->errors = '短信设置错误';
            return false;
        }
        if(!$this->config['key']) {
            $this->errors = '短信设置错误';
            return false;
        }
        // 如果是验证非具体短信创建,可以不用验证短信模板
        // 比如某个地方仅仅需要判断密钥是否配置,从而进行开关控制
        if(!$force) {
            return true;
        }
        // 传递具体短信场景参数,则验证短信模板
        if(($template = $this->getTemplate()) === false) {
            return false;
        }
        if(!$template || empty($template->content)) {
            $this->errors = Language::get('The "content" property must be set');
            return false;
        }
        if(empty($template->signName)) {
            $this->errors = Language::get('The "signName" property must be set');
            return false;
        }
        // 此处为必须赋值,避免无法发送短信。ID和签名该短信接口不需要赋值
        //$this->templateId = $template->templateId;
        $this->signName = $template->signName;
        $this->content = $this->getContent($template);
        return true;
    }
    public function getTemplate() 
   {
        if(($template = parent::getTemplate())) {
            return $template;
        }
        // 有异常(非空的情况)
        if($template === false) {
            return false;
        }
        
        // 使用其他平台的短信模板
      $query = MsgTemplateModel::find()->where(['and', ['scene' => $this->scene], ['!=''content''']])->orderBy(['id' => SORT_DESC])->one();
        return $query ? $query : null;
    }
}

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

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

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

开源插件

最新更新

电商类

CMS类

微信类

文章标签