待发短信

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

4001-021-502

工作时间

9:00-21:00

FastAdmin管理系统新增短信宝短信接口

FastAdmin是一款基于ThinkPHP+Bootstrap的极速后台开发框架。强大的插件扩展功能,在线安装卸载升级插件。小编带着大家一起开发fastadmin的短信宝插件接口。我们使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台非常稳定,发送速度快,注册就送测试短信,推荐大家使用。
1.首先完成渲染前台控制器代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
 
namespace addons\smsbao\controller;
 
use think\addons\Controller;
 
class Index extends Controller
{
 
    public function index()
    {
        $this->error("当前插件暂无前台页面");
    }
 
}

2.接着在library目录下的创建短信发送类Smsbao.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
<?php
 
namespace addons\smsbao\library;
 
class Smsbao
{
    private $_params = [];
    protected $error '';
    protected $config = [];
    protected static $instance = null;
    protected $statusStr array(
        "0"  => "短信发送成功",
        "-1" => "参数不全",
        "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
        "30" => "密码错误",
        "40" => "账号不存在",
        "41" => "余额不足",
        "42" => "帐户已过期",
        "43" => "IP地址限制",
        "50" => "内容含有敏感词"
    );
 
    public function __construct($options = [])
    {
        if ($config = get_addon_config('smsbao')) {
            $this->config = array_merge($this->config, $config);
        }
        $this->config = array_merge($this->config, is_array($options) ? $options : []);
    }
 
    /**
     * 单例
     * @param array $options 参数
     * @return Smsbao
     */
    public static function instance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance new static($options);
        }
        return self::$instance;
    }
 
    /**
     * 立即发送短信
     *
     * @return boolean
     */
    public function send()
    {
        $this->error = '';
        $params $this->_params();
        $postArr array(
            'u' => $params['u'],
            'p' => $params['p'],
            'm' => $params['mobile'],
            'c' => $params['msg']
        );
        $options = [
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json; charset=utf-8'
            )
        ];
        $result = \fast\Http::sendRequest('http://api.smsbao.com/sms'$postArr'GET'$options);
        if ($result['ret']) {
            if (isset($result['msg']) && $result['msg'] == '0')
                return TRUE;
            $this->error = isset($this->statusStr[$result['msg']]) ? $this->statusStr[$result['msg']] : 'InvalidResult';
        else {
            $this->error = $result['msg'];
        }
        return FALSE;
    }
 
    private function _params()
    {
        return array_merge([
            'u' => $this->config['username'],
            'p' => $this->config['apikey'],
        ], $this->_params);
    }
 
    /**
     * 获取错误信息
     * @return string
     */
    public function getError()
    {
        return $this->error;
    }
 
    /**
     * 接收手机
     * @param   string $mobile 手机号码
     * @return Smsbao
     */
    public function mobile($mobile '')
    {
        $this->_params['mobile'] = $mobile;
        return $this;
    }
 
    /**
     * 短信内容
     * @param   string $msg 短信内容
     * @return Smsbao
     */
    public function msg($msg '')
    {
        $this->_params['msg'] = $this->config['sign'] . $msg;
        return $this;
    }
     
    /**
     * 短信内容
     * @param   string $msgCode 短信code
     * @return Smsbao
     */
    public function msgCode($code '')
    {
        $this->_params['msg'] = $this->config['sign'] . str_replace("{code}"$code$this->config['template']);
        return $this;
    }
}

3.接着创建短信配置文件config.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
<?php
 
return [
    [
        'name' => 'username',
        'title' => '短信宝账号',
        'type' => 'string',
        'content' => [],
        'value' => '短信宝账号',
        'rule' => 'required',
        'msg' => '',
        'tip' => '',
        'ok' => '',
        'extend' => '',
    ],
    [
        'name' => 'apikey',
        'title' => '短信宝APIKEY',
        'type' => 'string',
        'content' => [],
        'value' => '短信宝APIKEY',
        'rule' => 'required',
        'msg' => '',
        'tip' => '',
        'ok' => '',
        'extend' => '',
    ],
    [
        'name' => 'sign',
        'title' => '短信签名',
        'type' => 'string',
        'content' => [],
        'value' => '【安全锁】',
        'rule' => 'required',
        'msg' => '',
        'tip' => '例如【安全锁】',
        'ok' => '',
        'extend' => '',
    ],
    [
        'name' => 'template',
        'title' => '短信模板',
        'type' => 'string',
        'content' => [],
        'value' => '你的短信验证码是:{code}',
        'rule' => 'required',
        'msg' => '',
        'tip' => '示例:你的短信验证码是:{code}',
        'ok' => '',
        'extend' => '',
    ],
];

4.接着是有关插件启用禁用等操作Smsbao.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
<?php
 
namespace addons\smsbao;
 
use app\common\library\Menu;
use think\Addons;
 
/**
 * Smsbao插件
 */
class Smsbao extends Addons
{
 
    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
 
        return true;
    }
 
    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
 
        return true;
    }
 
    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {
 
        return true;
    }
 
    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {
 
        return true;
    }
 
    /**
     * 短信发送
     * @param Sms $params
     * @return mixed
     */
    public function smsSend(&$params)
    {
        $smsbao new library\Smsbao();
        $result $smsbao->mobile($params['mobile'])->msgCode($params['code'])->send();
        return $result;
    }
 
    /**
     * 短信发送通知(msg参数直接构建实际短信内容即可)
     * @param   array $params
     * @return  boolean
     */
    public function smsNotice(&$params)
    {
        $smsbao new library\Smsbao();
        $result $smsbao->mobile($params['mobile'])->msg($params['msg'])->send();
        return $result;
    }
 
    /**
     * 检测验证是否正确
     * @param   Sms $params
     * @return  boolean
     */
    public function smsCheck(&$params)
    {
        return TRUE;
    }
}

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

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

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

开源插件

最新更新

电商类

CMS类

微信类

文章标签