TPshop是用thinkphp开发的一款免费开源网店系统,二次开发非常方便,代码清晰简洁,通俗易懂,丰富的插件和多套模板支持,易扩展,是目前国内首家最为完善的开源商城系统。系统支持多语言版本,操作简单,安全稳定,是广大用户二次开发的最佳选择,我以替换短信接口为例,一步一步的手把手教大家开发过程,我们短信宝短信平台非常稳定,而且快速,注册还送免费测试短信,大家可以在我们短信宝平台注册一个账号,进行测试。
首先我们要更换后台的显示界面文件。打开模版文件,替换一下模版文件。打开项目/application/admin/view2/system/sms.html文件,修改代码26~56行,代码如下图所示:
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
|
< div class = "explanation" id = "explanation" > < div class = "title" id = "checkZoom" >< i class = "fa fa-lightbulb-o" ></ i > < h4 title = "提示相关设置操作时应注意的要点" >操作提示</ h4 > < span id = "explanationZoom" title = "收起提示" ></ span ></ div > < ul > < li >系统平台全局设置,包括基础设置、购物、短信、邮件、水印和分销等相关模块。</ li > < li >有任何疑问,请咨询短信宝客服</ li > </ ul > </ div > < form method = "post" enctype = "multipart/form-data" name = "form1" action = "{:U('System/handle')}" > < input type = "hidden" name = "inc_type" value = "{$inc_type}" > < div class = "ncap-form-default" > < dl class = "row" > < dt class = "tit" > < label for = "sms_appkey" >短信宝账号</ label > </ dt > < dd class = "opt" > < input id = "sms_appkey" name = "sms_appkey" value = "{$config.sms_appkey}" class = "input-txt" type = "text" /> < p class = "notic" >短信宝的用户名,还未注册短信宝的用户请点击< a href = "http://www.smsbao.com/reg" target = "_blank" >免费注册</ a ></ p > </ dd > </ dl > < dl class = "row" > < dt class = "tit" > < label for = "sms_secretKey" >短信宝密码</ label > </ dt > < dd class = "opt" > < input id = "sms_secretKey" name = "sms_secretKey" value = "{$config.sms_secretKey}" class = "input-txt" type = "password" /> < p class = "notic" >短信宝账号密码</ p > </ dd > </ dl > </ div > </ form > |
项目/application/admin/view2/sms_template/sms_template_list.html文件,修改代码6~13行,代码如下图所示:
1
2
3
4
5
6
7
8
|
< div class = "fixed-bar" > < div class = "item-title" > < div class = "subject" > < h3 >短信配置模板</ h3 > </ div > </ div > </ div > |
项目/application/admin/view2/sms_template/_sms_template.html文件,修改代码15~61行,代码如下图所示:
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
|
< form class = "form-horizontal" id = "addEditSmsTemplate" method = "post" > < div class = "ncap-form-default" > < dl class = "row" > < dt class = "tit" > < label for = "sms_sign" >< em >*</ em >短信签名</ label > </ dt > < dd class = "opt" > < input type = "text" value = "{$smsTpl.sms_sign}" name = "sms_sign" id = "sms_sign" class = "input-txt" > < span class = "err" id = "err_sms_sign" style = "display:none;" >短信签名不能为空 </ span > < p class = "notic" >短信的签名,如smsbao,签名不能包含特殊字符,长度3-8个字符。</ p > </ dd > </ dl > < dl class = "row" > < dt class = "tit" > < label for = "send_scene" >< em >*</ em >发送场景</ label > </ dt > < dd class = "opt" > < if condition = "$send_scene_id gt 0 " > {$send_name} < input type = "hidden" value = "{$send_scene_id}" name = "send_scene" id = "send_scene" class = "form-control" style = "width:250px;" /> < else /> < select class = "small form-control" name = "send_scene" id = "send_scene" onchange = "changeContent(this.value);" > < option value = "-1" >请选择使用场景</ option > < foreach name = "send_scene" item = "v" key = "k" > < option value = "{$k}" <if condition = " $k eq $smsTpl.send_scene " >selected="selected"</ if >>{$v[0]}</ option > </ foreach > </ select > < span class = "err" id = "err_send_scene" style = "display:none;" >请选择使用场景</ span > </ if > < span class = "err" ></ span > < p class = "notic" >使用场景</ p > </ dd > </ dl > < dl class = "row" > < dt class = "tit" > < label for = "tpl_content" >< em >*</ em >短信模板内容</ label > </ dt > < dd class = "opt" > < textarea id = "tpl_content" name = "tpl_content" class = "tarea" rows = "6" readonly = "readonly" >{$smsTpl.tpl_content}</ textarea > < span class = "err" id = "err_tpl_content" style = "display:none;" >短信模板内容不能为空</ span > < p class = "notic" >短信的模板内容</ p > </ dd > </ dl > < div class = "bot" >< a href = "JavaScript:void(0);" onclick = "checkForm();" class = "ncap-btn-big ncap-btn-green" id = "submitBtn" >确认提交</ a ></ div > </ div > < input type = "hidden" name = "tpl_id" value = "{$smsTpl.tpl_id}" ></ label > </ form > |
经过替换之后,所有的显示都变成短信宝短信平台的了,第一步完成。接下来替换发送短信的接口文件,项目/application/home/controller/Api.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
|
/** * 前端发送短信方法: APP/WAP/PC 共用发送方法 */ public function send_validate_code(){ $this ->send_scene = C( 'SEND_SCENE' ); $type = I( 'type' ); $scene = I( 'scene' ); //发送短信验证码使用场景 $mobile = I( 'mobile' ); $sender = I( 'send' ); $verify_code = I( 'verify_code' ); $mobile = ! empty ( $mobile ) ? $mobile : $sender ; $session_id = I( 'unique_id' , session_id()); session( "scene" , $scene ); //注册 if ( $scene == 1 && ! empty ( $verify_code )){ $verify = new Verify(); if (! $verify ->check( $verify_code , 'user_reg' )) { ajaxReturn( array ( 'status' =>-1, 'msg' => '图像验证码错误' )); } } if ( $type == 'email' ){ //发送邮件验证码 $logic = new UsersLogic(); $res = $logic ->send_email_code( $sender ); ajaxReturn( $res ); } else { //发送短信验证码 $res = checkEnableSendSms( $scene ); if ( $res [ 'status' ] != 1){ ajaxReturn( $res ); } //判断是否存在验证码 $data = M( 'sms_log' )->where( array ( 'mobile' => $mobile , 'session_id' => $session_id , 'status' =>1))->order( 'id DESC' )->find(); //获取时间配置 $sms_time_out = tpCache( 'sms.sms_time_out' ); $sms_time_out = $sms_time_out ? $sms_time_out : 120; //120秒以内不可重复发送 if ( $data && (time() - $data [ 'add_time' ]) < $sms_time_out ){ $return_arr = array ( 'status' =>-1, 'msg' => $sms_time_out . '秒内不允许重复发送' ); ajaxReturn( $return_arr ); } //随机一个验证码 $code = rand(1000, 9999); $user = session( 'user' ); if ( $scene == 6){ if (! $user [ 'user_id' ]){ //登录超时 ajaxReturn( array ( 'status' =>-1, 'msg' => '登录超时' )); } $params = array ( 'code' => $code ); if ( $user [ 'nickname' ]){ $params [ 'user_name' ] = $user [ 'nickname' ]; } } $params [ 'code' ] = $code ; //发送短信 $resp = $this ->sendsms_smsbao( $scene , $mobile , $params , $session_id ); if ( $resp == '0' ){ //发送成功, 修改发送状态位成功 M( 'sms_log' )->where( array ( 'mobile' => $mobile , 'code' => $code , 'session_id' => $session_id , 'status' => 0))->save( array ( 'status' => 1)); $return_arr = array ( 'status' =>1, 'msg' => '发送成功,请注意查收' ); } else { $return_arr = array ( 'status' =>-1, 'msg' => '发送失败' . $resp ); } ajaxReturn( $return_arr ); } } /** * 短信宝发送方法 */ private function sendsms_smsbao( $scene , $sender , $params , $session_id ){ $product = $config [ 'sms_product' ]; $code = ! empty ( $params [ 'code' ]) ? $params [ 'code' ] : false; $consignee = ! empty ( $params [ 'consignee' ]) ? $params [ 'consignee' ] : false; $user_name = ! empty ( $params [ 'user_name' ]) ? $params [ 'user_name' ] : false; $order_sn = $params [ 'order_sn' ]; $mobile = ! empty ( $params [ 'mobile' ]) ? $params [ 'mobile' ] : false; if ( empty ( $unique_id )){ $session_id = session_id(); } else { $session_id = $unique_id ; } $smsParams = array ( 1 => "{\"code\":\"$code\",\"product\":\"$product\"}" , //1. 用户注册 2 => "{\"code\":\"$code\"}" , //2. 用户找回密码 3 => "{\"consignee\":\"$consignee\",\"phone\":\"$mobile\"}" , //3. 客户下单 4 => "{\"order_sn\":\"$order_sn\"}" , //4. 客户支付 5 => "{\"user_name\":\"$user_name\",\"order_sn\":\"$order_sn\",\"consignee\":\"$consignee\"}" , //5.商家发货 6 => "{\"user_name\":\"$user_name\",\"code\":\"$code\"}" //6. 修改手机号码 ); $smsParam = $smsParams [ $scene ]; //提取发送短信内容 $scenes = C( 'SEND_SCENE' ); $msg = $scenes [ $scene ][1]; $params_arr = json_decode( $smsParam ); foreach ( $params_arr as $k => $v ) { $msg = str_replace ( '${' . $k . '}' , $v , $msg ); } //发送记录存储数据库 $log_id = M( 'sms_log' )->insertGetId( array ( 'mobile' => $sender , 'code' => $code , 'add_time' => time(), 'session_id' => $session_id , 'status' => 0, 'scene' => $scene , 'msg' => $msg )); $config = tpCache( 'sms' ); $sms_get_param [ 'u' ]= $config [ 'sms_appkey' ]; $sms_get_param [ 'p' ]=md5( $config [ 'sms_secretKey' ]); $sms_get_param [ 'm' ]= $sender ; $sms_get_param [ 'c' ]= "【" . $config [ 'sms_product' ]. "】" . $msg ; if ( $resp == '0' ) { M( 'sms_log' )->where( array ( 'id' => $log_id ))->save( array ( 'status' => 1)); //修改发送状态为成功 } else { M( 'sms_log' )->where( array ( 'id' => $log_id ))->update( array ( 'error_msg' => $resp )); //发送失败, 将发送失败信息保存数据库 } return $resp ; } |
经过上面的替换,短信宝的短信平台已经替换成功了,可以正常使用了。
报备一下短信宝的VIP模版,这样就可以走短信宝的优质通道,并且免审核了,短信内容3~5秒就可送达。
最新更新
电商类
CMS类
微信类