萤火小程序商城是B2C模式的电子商城,是在Thinkphp5基础上搭建的一个PHP项目,前后端全部开源。Thinkphp5以易学易用著称,同时也方便二次开发,让您快速搭建个性化独立商城。小编今天就以新增短信接口为例为大家讲解一下如何进行二次开发,我们使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台非常稳定,发送速度快,注册就送测试短信,推荐大家使用。
首先我们打开项目:\source\application\store\view\setting\sms.php文件,替换8~33行代码
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
|
< div class = "widget-head am-cf" > < div class = "widget-title am-fl" >短信通知</ div > </ div > < input type = "hidden" name = "sms[default]" value = "smsbao" > < div class = "am-form-group" > < label class = "am-u-sm-3 am-form-label form-require" > 短信宝账号: </ label > < div class = "am-u-sm-9" > < input type = "text" class = "tpl-form-input" name = "sms[engine][smsbao][smsbao_user]" value="<?= $values['engine']['smsbao']['smsbao_user'] ?>" required> </ div > </ div > < div class = "am-form-group" > < label class = "am-u-sm-3 am-form-label form-require" > 短信宝密码: </ label > < div class = "am-u-sm-9" > < input type = "text" class = "tpl-form-input" name = "sms[engine][smsbao][smsbao_pwd]" value="<?= $values['engine']['smsbao']['smsbao_pwd'] ?>" required> </ div > </ div > < div class = "am-form-group" > < label class = "am-u-sm-3 am-form-label form-require" > 短信宝签名 </ label > < div class = "am-u-sm-9" > < input type = "text" class = "tpl-form-input" name = "sms[engine][smsbao][smsbao_sign]" value="<?= $values['engine']['smsbao']['smsbao_sign'] ?>" required> </ div > </ div > |
接着打开项目:\source\application\store\controller\Setting.php文件,替换smsTest方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public function smsTest( $AccessKeyId , $AccessKeySecret , $sign , $msg_type , $template_code , $accept_phone ) { $SmsDriver = new SmsDriver([ 'default' => 'smsbao' , 'engine' => [ 'smsbao' => [ 'user' => $AccessKeyId , 'pwd' => $AccessKeySecret , 'sign' => $sign , $msg_type => compact( 'template_code' , 'accept_phone' ), ], ], ]); $templateParams = []; if ( $msg_type === 'order_pay' ) { $templateParams = [ 'order_no' => '2018071200000000' ]; } $res = $SmsDriver ->sendSms( $msg_type , $templateParams , true); if ( $res == '0' ) { return $this ->renderSuccess( '发送成功' ); } return $this ->renderError( '发送失败 ' . $SmsDriver ->getCode( $res )); } |
接着打开项目:\source\application\common\model\Setting.php文件,替换115~133行左右代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
'sms' => [ 'key' => 'sms' , 'describe' => '短信通知' , 'values' => [ 'default' => 'smsbao' , 'engine' => [ 'smsbao' => [ 'smsbao_user' => '' , 'smsbao_pwd' => '' , 'smsbao_sign' => '萤火科技' , 'order_pay' => [ 'is_enable' => '0' , 'template_code' => '' , 'accept_phone' => '' , ], ], ], ], ] |
接着打开项目:\source\application\common\library\sms\Driver.php文件,增加getCode方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public function getCode( $code ) { switch ( $code ) { case 0: return '短信发送成功' ; case -1: return '参数不全' ; case -2: return '服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!' ; case 30: return '密码错误' ; case 40: return '账号不存在' ; case 41: return '余额不足' ; case 42: return '帐户已过期' ; case 43: return 'IP地址限制' ; case 50: return '内容含有敏感词' ; } } |
最后在项目:\source\application\common\library\sms\engine文件夹中创建一个命名为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
|
<?php namespace app\common\library\sms\engine; class Smsbao { private $config ; /** * 构造方法 * Qiniu constructor. * @param $config */ public function __construct( $config ) { $this ->config = $config ; } /** * 发送短信通知 * @param $msgType * @param $templateParams * @return bool|\stdClass */ public function sendSms( $msgType , $templateParams ) { $data [ 'u' ] = $this ->config[ 'user' ]; $data [ 'p' ] = md5( $this ->config[ 'pwd' ]); $data [ 'm' ] = $this ->config[ 'order_pay' ][ 'accept_phone' ]; $content = "您有一条新订单,订单号为:" . $templateParams [ 'order_no' ]. ",请注意查看。" ; $data [ 'c' ] = "【" . $this ->config[ 'sign' ]. "】" . $content ; $res = $this ->post( $url , $data ); return $res ; } private function post( $url , $data ) { $curl = curl_init(); curl_setopt( $curl , CURLOPT_URL, $url ); curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER, false); curl_setopt( $curl , CURLOPT_SSL_VERIFYHOST, false); curl_setopt( $curl , CURLOPT_POST, 1); curl_setopt( $curl , CURLOPT_POSTFIELDS, $data ); curl_setopt( $curl , CURLOPT_RETURNTRANSFER, 1); $ret = curl_exec( $curl ); curl_close( $curl ); return $ret ; } } |
好了,经过以上的替换,短信宝的短信平台已经替换成功了,可以正常使用了。我们进行测试发送。
报备一下短信宝的VIP模板,这样就可以走短信宝的优质通道了,即便遇到敏感文字我们都不会人工审核,短信内容3~5秒就可送达。
另外:我们已经开发好完整的萤火小程序商城短信宝插件,点击此链接 下载及查看安装流程。