YzmCMS是一款轻量级开源内容管理系统,它采用OOP(面向对象)方式自主开发的框架。基于PHP+Mysql架构,并采用MVC框架式开发的一款高效开源的内容管理系统,可运行在Linux、Windows、MacOSX、Solaris等各种平台上。便于进行二次开发,小编对这款软件还是比较了解的,小编今天就以新增短信接口为例为大家讲解一下如何进行二次开发,我们使用的短信接口是短信宝短信群发平台的短信接口,短信宝短信群发平台非常稳定,发送速度快,注册就送测试短信,推荐大家使用。
首先我们打开项目:application\admin\view\system_set.html文件,在237~256行增加短信宝配置代码:
<div class="tabCon"><div class="row cl">
<label class="form-label col-xs-4 col-sm-2">短信宝用户名:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="text" name="smsbao_user" value="<?php echo $data["smsbao_user"];?>" class="input-text" style="width:50%">
</div></div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-2">短信宝密码:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="text" name="smsbao_pass" value="<?php echo $data["smsbao_pass"];?>" class="input-text" style="width:50%">
</div></div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-2">短信签名:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="text" name="smsbao_sign" value="<?php echo $data["smsbao_sign"];?>" class="input-text" style="width:50%">
</div>
</div>
</div>
接着打开项目:application\member\controller\index.class.php文件,替换register方法:
public function register(){
$config = get_config();
//检查是否允许新会员注册
if($config['member_register'] == 0) showmsg('管理员关闭了新会员注册!', 'stop');
if(isset($_SESSION['_userid']) && $_SESSION['_userid']){
showmsg(L('login_success'), U('member/index/init'), 1);
}
if(isset($_POST['dosubmit'])){
if(empty($_SESSION['code']) || strtolower($_POST['code']) != $_SESSION['code']){
$_SESSION['code'] = '';
showmsg(L('code_error'), '', 1);
}
if (empty($_SESSION['mobile_code']) || strtolower($_POST['mobile_code']) != $_SESSION['mobile_code']) {
$_SESSION['mobile_code'] = '';
showmsg(L('手机验证码不正确'),'',1);
}
$_SESSION['code'] = '';
$member = D('member');
$data = array();
$data['username'] = isset($_POST['username']) && is_username($_POST['username']) ? trim($_POST['username']) : showmsg(L('user_name_format_error'));
$data['password'] = isset($_POST['password']) && is_password($_POST['password']) ? trim($_POST['password']) : showmsg(L('password_format_error'));
$data['email'] = isset($_POST['email']) && is_email($_POST['email']) ? trim($_POST['email']) : showmsg(L('mail_format_error'));
$data['mobile'] = isset($_POST['mobile']) && is_mobile($_POST['mobile']) ? trim($_POST['mobile']) : showmsg(L('手机号不正确'));
$result = $member->where(array('username'=>$_POST['username']))->find();
if($result) showmsg(L('user_already_exists'));
$result = $member->where(array('email'=>$_POST['email']))->find();
if($result) showmsg("邮箱已存在!");
$results = $member->where(array('mobile'=>$_POST['mobile']))->find();
if($results) showmsg("手机已存在");
$data['nickname'] = $data['username'];
$data["password"] = password($data['password']);
$data['regdate'] = $data['lastdate'] = SYS_TIME;
$data['regip'] = $data['lastip'] = getip();
$data['groupid'] = '1';
$data['amount'] = '0.00';
$data['point'] = $data['experience'] = $config['member_point']; //经验和积分
$data['status'] = ($config['member_check'] || $config['member_email']) ? 0 : 1;
$data['userid'] = $member->insert($data, true);
if(!$data['userid']) showmsg("注册失败!");
D('member_detail')->insert($data, true, false); //插入附表
if($config['member_email']){ //是否需要邮件验证
$mail_code = $_SESSION['mail_code'] = md5(microtime(true).$data['userid']);
$_SESSION['userid'] = $data['userid'];
$url = SITE_URL."index.php?m=member&c=index&a=register&mail_code=$mail_code&userid={$data['userid']}&verify=1";
$message = '请点击邮箱验证地址:<a href="'. $url .'">'. $url .'</a>';
$res = sendmail($data['email'], '会员邮箱验证', $message);
if(!$res) showmsg('邮件发送失败,请联系网站管理员!', 'stop');
showmsg('我们已将邮件发送到您的邮箱,请尽快完成验证!');
}elseif($config['member_check']){ //是否需要管理员审核
showmsg('注册成功,由于管理员开启审核机制,请耐心等待!');
}
$_SESSION['_userid'] = $data['userid'];
$_SESSION['_username'] = $data['username'];
set_cookie('_userid', $data['userid']);
set_cookie('_username', $data['username']);
set_cookie('_nickname', $data['username']);
set_cookie('_groupid', $data['groupid']);
showmsg('注册成功!', U('member/index/init'), 1);
}else{
if(!empty($_GET['verify'])) {
$mail_code = isset($_GET['mail_code']) ? trim($_GET['mail_code']) : showmsg(L('illegal_operation'));
$userid = isset($_GET['userid']) ? intval($_GET['userid']) : showmsg(L('illegal_operation'));
if(isset($_SESSION['mail_code']) && $mail_code==$_SESSION['mail_code'] && $userid==$_SESSION['userid']){
unset($_SESSION['mail_code'], $_SESSION['userid']);
D('member')->update(array('status' => 1, 'email_status' => 1),array('userid'=>$userid));
showmsg('邮箱验证成功!', U('member/index/login'), 2);
}else{
showmsg('验证失败,可能是验证时间已过期!', U('member/index/register'));
}
}
include template('member', 'register');
}
}
接着打开项目:application\member\controller\reset.class.php文件,增加reset_mobile方法:
public function reset_mobile(){
session_start();
$_SESSION['step'] = isset($_SESSION['step']) ? $_SESSION['step'] : 1;
if ($_SESSION['step']==1 && isset($_POST['dosubmit'])) {
if(empty($_SESSION['code']) || strtolower($_POST['code']) != $_SESSION['code']){
$_SESSION['code'] = '';
showmsg(L('code_error'), '', 1);
}
$data = $this->_check($_POST['username']);
if(empty($data['mobile'])) showmsg('您没有绑定手机,请选择其他方式找回密码!', 'stop');
$ret = sendsms($data['mobile']);
if ($ret != 0) {
showmsg('短信发送失败,请联系网站管理员!');
}
$_SESSION['mobile'] = $data['mobile'];
$_SESSION['userid'] = $data['userid'];
$_SESSION['emc_times'] = 5;
$_SESSION['step'] = 2;
}elseif($_SESSION['step'] ==2 && isset($_POST['dosubmit'])){
if($_SESSION['emc_times']=='' || $_SESSION['emc_times']<=0){
$_SESSION['step'] = 1;
showmsg("验证次数超过5次,请重新获取短信验证码!");
}
if(!empty($_SESSION['mobile_code']) && strtolower($_POST['mobile_code']) == strtolower($_SESSION['mobile_code'])){
unset($_SESSION['emc_times']);
$_SESSION['step'] = 3;
}else{
$_SESSION['emc_times'] = $_SESSION['emc_times']-1;
showmsg('短信校验码错误!','',1);
}
}else if($_SESSION['step']==3 && isset($_POST['dosubmit'])){
if(!isset($_POST['password']) || !is_password($_POST['password'])) showmsg(L('password_format_error'));
D('member')->update(array('password' => password($_POST['password'])),array('userid'=>$_SESSION['userid']));
unset($_SESSION['step'], $_SESSION['code'], $_SESSION['mobile_code'], $_SESSION['mobile'], $_SESSION['userid']);
showmsg('更新密码成功!', U('member/index/login'));
}
include template('member', 'reset_mobile');
}
接着打开项目:application\member\view\default\register.html,在127行增加以下代码:
<script type="text/javascript">
var time = 0;
var res = null;
function sendTime(){
clearTimeout(res); // 先清空一下倒计时资源。
time--; // 倒计时时间递减。
// 如果倒计时到达0时,则恢复按钮原来的内容
if (time <= 0) {
time = "获取验证码";
$('.huoqu').text(time);
clearTimeout(res);
time = 0;
return;
}
// 倒计时的内容写到按钮里面
$('.huoqu').text("剩余" + time + "秒");
res = setTimeout("sendTime()", 1000);
}
$(function(){
var flg = true;
$('.huoqu').on('click',function(){
console.log(123);
var mobile = $("#mobile").val();
var code = $("#code").val();
var reg = /^1[3,4,5,7,8]\d{9}$/;
var ret = false;
if (code == '') {layer.msg('请填写验证码'); return ret;}
if (!reg.test(mobile)) {layer.msg('手机号码不正确');return ret;}
if (0 == time) {
var data = {"code" : code, "mobile" : mobile};
var err = "";
if (flg == true) {
flg = false;
// ajax提交请求
$.ajax({
type: "post",
url: "{U('public_checksms')}",
dataType: "html",
async: false,
data: data,
beforeSend: function(){
$("#dosubmit").attr({ disabled: "disabled" });
},
success: function(msg){
if(msg == '0'){
time = 60;
sendTime();
layer.msg('短信发送成功!');
ret = true;
}else if(msg == '1'){
layer.msg('验证码不正确');
}else if(msg == '2'){
layer.msg('短信配置不正确');
}else{
layer.msg(msg);
}
}
});
}
$('.codeimg').click();
return ret;
}
})
})
</script>
最后打开项目:common\function\system.func.php文件,增加sendsms方法
function sendsms($mobile)
{
$config = get_config();
$user = $config['smsbao_user'];
$pass = $config['smsbao_pass'];
$sign = $config['smsbao_sign'];
if (empty($user) && empty($pass) && empty($sign)) {
return 2;
}
$code = rand(1000,9999);
$content = '【'.$sign.'】您的验证码为:'.$code.'请妥善保存!';
$url = 'http://api.smsbao.com/sms?u='.$user.'&p='.md5($pass).'&m='.$mobile.'&c='.$content;
$result = file_get_contents($url);
if ($result == 0) {
$_SESSION['mobile_code'] = $code;
return '0';
}else{
return $result;
}
}
好了,经过以上的替换,短信宝的短信平台已经替换成功了,可以正常使用了。我们进行测试发送。
报备一下短信宝的VIP模板,这样就可以走短信宝的优质通道了,即便遇到敏感文字都不会人工审核,短信内容3~5秒就可送达。