WSTMall开源商城是一款基于ThinkPHP开发的多商户新零售开源系统,因为使用的是ThinkPHP框架,所以二次开发比较容易,小编对此系统还比较了解,今天小编就以替换短信接口为例为大家讲解一下如何进行二次开发,我们今天讲解的是1.9.5版本,我们使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台非常稳定,发送速度快,推荐大家使用。
因为短信配置是写在数据库中的所以我们先去修改数据库中的接口信息,我们在项目\Apps\Home\Action 下建一个文件,取名叫SmsbaoAction.class.php,文件代码为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?php namespace Home\Action; use think\Db; header( "Content-type:text/html;charset=utf-8" ); /** * 短信宝短信插件 */ class SmsbaoAction extends BaseAction { public function smsbao() { $smsbao = M( 'sys_configs' ); $data [ 'fieldName' ] = '短信宝账号' ; $smsbao ->where( 'fieldCode="smsKey"' )->save( $data ); echo '短信宝插件安装成功,请删除Apps\home\action\SmsbaoAction.class.php文件' ; } } |
新建完成后我们需要运行一下修改数据库,我们访问 域名/index.php?m=home&c=Smsbao&a=smsbao,看到短信宝插件安装成功,请删除Apps\home\action\SmsbaoAction.class.php文件 即为修改成功,修改成功我们去修改接口发送文件,打开项目\Apps\Common\Common\function.php,添加以下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/** * @param string $phoneNumer 手机号码 * @param string $content 短信内容 */ function SmsbaoSMS( $phoneNumer , $content ){ $url = 'http://api.smsbao.com/sms?u=' . $GLOBALS [ 'CONFIG' ][ 'smsKey' ]. '&p=' .md5( $GLOBALS [ 'CONFIG' ][ 'smsPass' ]). '&m=' . $phoneNumer . '&c=' . $content ; $ch =curl_init( $url ); curl_setopt( $ch , CURLOPT_RETURNTRANSFER, 1); //设置否输出到页面 curl_setopt( $ch , CURLOPT_CONNECTTIMEOUT, 30 ); //设置连接等待时间 curl_setopt( $ch , CURLOPT_ENCODING, "gzip" ); $data =curl_exec( $ch ); curl_close( $ch ); return $data ; } |
另一处短信接口文件,打开项目\Upgrade\Upgrade\Apps\Common\Common\function.php,添加一下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/** * @param unknown_type $phoneNumer * @param unknown_type $content */ function SmsbaoSMS( $phoneNumer , $content ){ $url = 'http://api.smsbao.com/sms?u=' . $GLOBALS [ 'CONFIG' ][ 'smsKey' ]. '&p=' .md5( $GLOBALS [ 'CONFIG' ][ 'smsPass' ]). '&m=' . $phoneNumer . '&c=' . $content ; $ch =curl_init( $url ); curl_setopt( $ch , CURLOPT_RETURNTRANSFER, 1); //设置否输出到页面 curl_setopt( $ch , CURLOPT_CONNECTTIMEOUT, 30 ); //设置连接等待时间 curl_setopt( $ch , CURLOPT_ENCODING, "gzip" ); $data =curl_exec( $ch ); curl_close( $ch ); return $data ; } |
接下来我们去修改调用短信接口的代码,打开项目\Apps\Home\Model\LogSmsModel.class.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
|
<?php namespace Home\Model; /** * ============================================================================ * WSTMall开源商城 * 官网地址:http://www.wstmall.net * 联系QQ:707563272 * ============================================================================ * 短信服务类 */ class LogSmsModel extends BaseModel { /** * 插入并发送短讯记录 */ public function sendSMS( $smsSrc , $phoneNumber , $content , $smsFunc , $verfyCode , $imgVerfy =1){ $USER = session( 'WST_USER' ); $userId = empty ( $USER )?0: $USER [ 'userId' ]; $m = M( 'log_sms' ); $ip = get_client_ip(); //检测短信验证码验证是否正确 if ( $GLOBALS [ 'CONFIG' ][ 'smsVerfy' ]==1 && $imgVerfy ==1){ $smsverfy = I( 'smsVerfy' ); $verify = new \Think\Verify( array ( 'reset' =>false)); $rs = $verify ->check( $smsverfy ); if (! $rs ){ return array ( 'status' =>-29999, 'msg' => '验证码不正确!' ); } } //检测是否超过每日短信发送数 $date = date ( 'Y-m-d' ); $sql = 'select count (smsId) counts,max(createTime) createTime from __PREFIX__log_sms where smsPhoneNumber= '.$phoneNumber.' and createTime> "'.$date.' 00:00:00" and createTime<= "'.$date.' 23:59:59" '; $smsRs = $this ->queryRow( $sql ); if ( $smsRs [ 'counts' ]>(int) $GLOBALS [ 'CONFIG' ][ 'smsLimit' ]){ return array ( 'status' =>-20000, 'msg' => '请勿频繁发送短信验证!' ); } if ( $smsRs [ 'createTime' ] != '' && ((time()- strtotime ( $smsRs [ 'createTime' ]))<120)){ return array ( 'status' =>-20001, 'msg' => '请勿频繁发送短信验证!' ); } //检测IP是否超过发短信次数 $sql = 'select count (smsId) counts,max(createTime) createTime from __PREFIX__log_sms where smsIP= "'.$ip.'" and createTime> "'.$date.' 00:00:00" and createTime<= "'.$date.' 23:59:59" '; $ipRs = $this ->queryRow( $sql ); if ( $ipRs [ 'counts' ]>(int) $GLOBALS [ 'CONFIG' ][ 'smsLimit' ]){ return array ( 'status' =>-20003, 'msg' => '请勿频繁发送短信验证!' ); } if ( $ipRs [ 'createTime' ]!= '' && ((time()- strtotime ( $ipRs [ 'createTime' ]))<120)){ return array ( 'status' =>-20004, 'msg' => '请勿频繁发送短信验证!' ); } $code = SmsbaoSMS( $phoneNumber , $content ); $data = array (); $data [ 'smsSrc' ] = $smsSrc ; $data [ 'smsUserId' ] = $userId ; $data [ 'smsPhoneNumber' ] = $phoneNumber ; $data [ 'smsContent' ] = $content ; $data [ 'smsReturnCode' ] = $code ; $data [ 'smsCode' ] = $verfyCode ; $data [ 'smsIP' ] = $ip ; $data [ 'smsFunc' ] = $smsFunc ; $data [ 'createTime' ] = date ( 'Y-m-d H:i:s' ); $m ->add( $data ); if ( intval ( $code ) == 0){ return array ( 'status' =>1, 'msg' => '短信发送成功!' ); } else { return array ( 'status' =>-1, 'msg' => '短信发送失败!' ); } } } |
好了,经过以上的替换,短信宝的短信平台已经替换成功了,我们去进行发送测试:
报备一下短信宝的VIP模板,这样就可以走短信宝的优质通道了,并且免审核了,短信内容3~5秒就可送达。
最新更新
电商类
CMS类
微信类