Fastadmin 新增短信宝短信接口
发表日期:2020-06-04 文章编辑:短信宝小编 浏览次数:
Fastadmin是一款基于ThinkPHP和Bootstrap的极速后台开发框架,拥有丰富的应用插件市场,CMS、博客、问答、商城、小程序、工作流应有尽有。小编对他还是很了解。今天小编就以新增短信接口为例,给大家讲解一下如何进行二次开发,使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台的接口非常稳定,发送速度快,注册就送测试短信,推荐大家使用。
插件的目录结构如下:
├─smsbao插件目录
│ ├─controller
│ │ ├─index.php
│ ├─library
│ │ ├─Smsbao.php
│ ├─config.php
│ ├─info.ini
│ ├─Smsbao.php
下面具体给大家说一下每个文件的作用及代码 inde.php 渲染前台页面控制器
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?phpnamespace addons\smsbao\controller;use think\addons\Controller;class Index extends Controller{ public function index() { $this->error("当前插件暂无前台页面"); }} |
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
|
<?phpnamespace 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' ) ]; 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' => md5($this->config['password']), ], $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; }} |
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
|
<?phpreturn array( 0 => array( 'name' => 'username', 'title' => '短信宝账号', 'type' => 'string', 'content' => array(), 'value' => 'username', 'rule' => 'required', 'msg' => '', 'tip' => '', 'ok' => '', 'extend' => '', ), 1 => array( 'name' => 'password', 'title' => '短信宝密码', 'type' => 'string', 'content' => array(), 'value' => 'password', 'rule' => 'required', 'msg' => '', 'tip' => '', 'ok' => '', 'extend' => '', ), 2 => array( 'name' => 'sign', 'title' => '短信签名', 'type' => 'string', 'content' => array(), 'value' => '【短信签名】', 'rule' => 'required', 'msg' => '', 'tip' => '例如【短信宝】', 'ok' => '', 'extend' => '', ),); |
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
|
<?phpnamespace 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'])->msg("你的短信验证码是:{$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系统短信宝插件,点击此链接 下载及查看安装流程。

