NIUSHOPV5 采用 ThinkPHP6.0 + MySQL 开发语言框架采用 Thinkphp6 + Uni-app 设计开发,代码全新升级;支持实物商品、虚拟商品、电子卡密、服务项目及卡项套餐五大商品类型;支持拼团、砍价、秒杀、超级会员卡、礼品卡、分销、直播等多种会员营销方案。完全开源,二次开发非常方便,小编对他还是比较了解的,今天小编为大家讲解 V5 版本的短信接口替换,使用的接口是我们短信宝群发平台的短信接口,我们短信宝群发短信平台非常稳定,发送速度快,注册还送测试短信,推荐大家使用。
插件的目录结构如下
├─smsbao插件目录
│ ├─config 配置目录
│ │ ├─diy_view.php 页面DIY相关配置
│ │ ├─event.php 事件相关配置
│ │ ├─info.php 插件信息配置
│ │ ├─menu_shop.php 菜单栏配置
│ ├─event 事件目录
│ │ ├─ DoEditSmsMessage.php 短信模板事件
│ │ ├─EnableCallBack.php 短信开关事件
│ │ ├─EnableSms.php 查询启用的短信插件
│ │ ├─Install.php 应用安装
│ │ ├─SendSms.php 短信发送
│ │ ├─SmsType.php 短信方式
│ │ ├─UnInstall.php 应用卸载
│ └─model
│ │ ├─Config.php 短信核心配置
│ │ ├─Sms.php 核心发送类
│ └─shop
│ │ ├─controller 控制器
│ │ │ ├─Message.php 模板编辑类
│ │ │ ├─ Sms.php 核心发送类
│ │ ├─view 视图
│ │ │ ├─message 模板编辑类
│ │ │ │ ├─edit.html 模板视图
│ │ │ ├─ sms 核心发送类
│ │ │ │ ├─config.html 短信视图
下面具体给大家说一下重要文件的作用及代码
1:打开项目:addon\smsbao\config\info.php 填写当前插件的基本信息
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php return [ 'name' => 'smsbao' , 'title' => '短信宝短信' , 'description' => '短信宝短信功能' , 'type' => 'system' , //插件类型 system :系统插件(自动安装), business:业务插件 promotion:营销插件 tool:工具插件 'status' => 1, 'author' => '' , 'version' => '5.0.3' , 'version_no' => '520220907001' , 'content' => '' , ]; |
2:打开项目:addon\smsbao\config\event.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
|
<?php // 事件定义文件 return [ 'bind' => [ ], 'listen' => [ //短信方式 'SmsType' => [ 'addon\smsbao\event\SmsType' ], 'DoEditSmsMessage' => [ 'addon\smsbao\event\DoEditSmsMessage' ], 'SendSms' => [ 'addon\smsbao\event\SendSms' ], //启用回调,使用这个短信,就要关闭其他短信插件 'EnableCallBack' => [ 'addon\smsbao\event\EnableCallBack' ], //查询启用的短信插件 'EnableSms' => [ 'addon\smsbao\event\EnableSms' ], 'SmsTemplateInfo' => [ 'addon\smsbao\event\SmsTemplateInfo' ] ], 'subscribe' => [ ], ]; |
3:打开项目:\addon\smsbao\model\Sms.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
|
class Sms extends BaseModel { /** * 短信发送 * @param array $param * @return array|mixed * @throws InvalidArgumentException */ public function send($param = []) { $config_model = new Config(); $config_result = $config_model->getSmsConfig(); if ($config_result[ "data" ][ "is_use" ]) { $config = $config_result[ "data" ][ "value" ]; $sms_info = $param[ "message_info" ][ "sms_json_array" ]; //消息类型模板 短信模板信息 if (empty($sms_info[ "smsbao" ])) return $ this ->error([], "消息模板尚未配置" ); $sms_info = $sms_info[ "smsbao" ]; $var_parse = $param[ "var_parse" ]; //变量解析 $smscontent = $sms_info[ 'content' ]; foreach ($var_parse as $k=>$v){ $smscontent=str_replace( '{' .$k. '}' ,$v,$smscontent); } $account = $param[ "sms_account" ]; //发送手机号 $sms_config = [ // HTTP 请求的超时时间(秒) 'timeout' => 5.0, // 默认发送配置 'default' => [ // 网关调用策略,默认:顺序调用 'strategy' => OrderStrategy::class, // 默认可用的发送网关 'gateways' => [ 'smsbao' ], ], // 可用的网关配置 'gateways' => [ "smsbao" => [ 'user' => $config[ "smsbao_key_id" ], 'password' => $config[ "smsbao_key_secret" ], ] ], ]; try { $easySms = new EasySms($sms_config); $easySms->send($account, [ 'content' => '【' .$config[ "smsbaosign" ]. '】' .$smscontent, // 'template' => $sms_info[ "template_id" ], 'data' => $var_parse, ]); return $ this ->success([ "addon" => "smsbao" , "addon_name" => "短信宝短信" , "content" => $sms_info[ "content" ] ]); } catch (NoGatewayAvailableException $exception) { $message = $exception->getException( 'smsbao' )->getMessage(); return $ this ->error([ "content" => $sms_info[ "content" ] ], $message ? : '短信发送异常' ); } } } } |
4:打开项目:addon\smsbao\shop\view\message\edit.html 增加短信宝模板视图
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
|
{extend name= "app/shop/view/base.html" /} {block name= "resources" } <style> .layui-btn-primary:hover {border-color: #C9C9C9;} .number-con {margin-right: 10px;} .form-wrap {margin-top: 0;} </style> {/block} {block name= "main" } <div class= "layui-form form-wrap" > <div class= "layui-form-item" > <label class= "layui-form-label" >是否开启:</label> <div class= "layui-input-block" > <input type= "checkbox" name= "sms_is_open" value= "1" { if $sms_is_open == 1}checked{/ if } lay-skin= "switch" > </div> </div> <div class= "layui-form-item" > <label class= "layui-form-label" >数据值:</label> <div class= "layui-input-block" > {foreach $message_variable_list as $message_variable_k => $message_variable_v} <button class= "layui-btn layui-btn-primary number-con" onclick= "clickBtn('{$message_variable_k}')" >{$message_variable_v}</button> {/foreach} </div> </div> <div class= "layui-form-item" > <label class= "layui-form-label" >短信内容:</label> <div class= "layui-input-block" > <textarea id= "text_area" name= "content" class= "layui-textarea len-long" >{ if !empty($info)}{$info.content}{/ if }</textarea> </div> <div class= "word-aux" >变量只能使用上方数据值中的变量,否则不会被解析</div> </div> <div class= "form-row" > <button class= "layui-btn" lay-submit lay-filter= "save" >保存</button> <button class= "layui-btn layui-btn-primary" onclick= "back()" >返回</button> </div> <input type= "hidden" name= "keywords" value= "{$keywords}" > </div> {/block} {block name= "script" } <script> layui.use( 'form' , function () { var form = layui.form; var repeat_flag = false ; //防重复标识 form.render(); /** * 监听提交 */ form.on( 'submit(save)' , function (data) { if (repeat_flag) return ; repeat_flag = true ; $.ajax({ dataType: 'JSON' , type: 'POST' , data: data.field, success: function (res){ repeat_flag = false ; if (res.code == 0) { layer.confirm( '编辑成功' , { title: '操作提示' , btn: [ '返回列表' , '继续操作' ], yes: function (){ location.href = ns.url( "shop/message/lists" ) }, btn2: function () { location.reload(); } }); } else { layer.msg(res.message); } } }); }); }); function back(){ location.href = ns.url( "shop/message/lists" ); } function clickBtn(con) { var txtArea = $( "#text_area" )[0]; var content = txtArea.value; //文本域内容 var start = txtArea.selectionStart; //光标的初始位置,selectionStart:选区开始位置;selectionEnd:选区结束位置。 txtArea.value = content.substring(0, txtArea.selectionStart) + '{' + con + '}' + content.substring(txtArea.selectionEnd, content.length); var position = start + con.length; $( "#text_area" ).focus(); txtArea.setSelectionRange(position+1, position+1); //setSelectionRange()方法用来设置<input>元素当前选中的文本的开始和结束位置 } </script> {/block} |
5:打开项目:addon\smsbao\shop\view\sms\config.html 增加短信宝短信视图
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
|
{extend name= "app/shop/view/base.html" /} {block name= "resources" } <style> .form-wrap {margin-top: 0;} </style> {/block} {block name= "main" } <div class= "layui-form form-wrap" > <div class= "layui-form-item" > <label class= "layui-form-label" >是否开启:</label> <div class= "layui-input-block" id= "isOpen" > <input type= "checkbox" name= "status" lay-filter= "isOpen" value= "1" lay-skin= "switch" { if condition= "$info.is_use == 1" } checked {/ if } /> </div> <div class= "word-aux" >当前使用短信宝配置</div> </div> <div class= "layui-form-item" > <label class= "layui-form-label" >短信宝账号:</label> <div class= "layui-input-block" > <input type= "text" name= "smsbao_key_id" placeholder= "请输入短信宝账号" { if $info.value } value= "{$info.value.smsbao_key_id}" {/ if } autocomplete= "off" class= "layui-input len-long" > </div> </div> <div class= "layui-form-item" > <label class= "layui-form-label" >短信宝密码:</label> <div class= "layui-input-block" > <input type= "text" name= "smsbao_key_secret" placeholder= "请输入短信宝密码" { if $info.value } value= "{$info.value.smsbao_key_secret}" {/ if } autocomplete= "off" class= "layui-input len-long" > </div> </div> <div class= "layui-form-item" > <label class= "layui-form-label" >短信内容签名:</label> <div class= "layui-input-block" > <input type= "text" name= "smsbaosign" placeholder= "请输入短信内容签名" { if $info.value } value= "{$info.value.smsbaosign}" {/ if } autocomplete= "off" class= "layui-input len-long" > </div> </div> <!-- 表单操作 --> <div class= "form-row" > <button class= "layui-btn" lay-submit lay-filter= "save" >保存</button> <button class= "layui-btn layui-btn-primary" onclick= "back()" >返回</button> </div> </div> {/block} {block name= "script" } <script> layui.use( 'form' , function () { var form = layui.form, repeat_flag = false ; //防重复标识 form.render(); form.on( 'submit(save)' , function (data) { if (repeat_flag) return ; repeat_flag = true ; $.ajax({ data: data.field, dataType: 'JSON' , type: 'POST' , success: function (res) { repeat_flag = false ; if (res.code == 0) { layer.confirm( '编辑成功' , { title: '操作提示' , btn: [ '返回列表' , '继续操作' ], yes: function (){ location.href = ns.url( "shop/message/sms" ) }, btn2: function () { location.reload(); } }); } else { layer.msg(res.message); } } }); }); }); function back() { location.href = ns.url( "shop/message/sms" ); } </script> {/block} |
经过上面的替换,短信宝的短信平台已经替换成功了,可以正常使用了。进行测试发送:
报备一下短信宝的VIP模板,这样就可以走短信宝的优质通道了,即便遇到敏感文字我们都不会人工审核,短信内容3~5秒就可送达。
另外:我们已经开发好完整的NIUSHOPV5系统短信宝插件,点击此链接 下载及查看安装流
最新更新
电商类
CMS类
微信类