下载鸥 > 网站下载 > 插件下载 > 帝国CMS

帝国cms7.5短信宝接口插件

4352021-05-02 17:09:52

收藏
帝国cms7.5短信宝接口插件
帝国cms是一款无论安全还是性能方面都表现很出色的cms程序,许多企业建设了基于帝国cms开发的站点。帝国cms默认是不带短信登陆注册的,此时我们就可以通过短信宝接口进行完善。

帝国cms7.5短信宝接口插件

短信宝接口注意事项

由于使用短信登陆功能的帝国cms用户95%以上都进行过二开,我们建议是按照文章所述进行修改,不建议直接下载安装包做短信功能,否则一个函数出错就可能导致程序无法运行。
 

短信宝接口修改项

打开项目/e/admin创建一个smsbao文件夹,在smsbao文件夹下创建一个smsbao.php文件,文件代码如下:
<?php
header('Content-Type:text/html;charset=UTF-8');
/**
 * SmsBao实现类
 * @category   帝国cms
 * @package  帝国cmsV7.2
 * @subpackage  Sms
 * @author    linf
 */
class Sms{
    private $account;//短信宝账户
    private $password;//密码
    private $balance;//短信剩余条数
    const sendSmsUrl = "http://api.smsbao.com/sms";
    const queryBalanceUrl = "http://api.smsbao.com/query";
    /**
     * 架构函数
     * @access public
     * @param string $account  在短信宝注册的账户名
     * @param string $password  在短信宝注册的账户名的密码
     */
    public function __construct($account,$password) {
        if (empty($account) || empty($password)) {
           echo "用户名和密码不可为空!";
        }
        $this->account = $account;
        $this->password = $password;
    }
    
    /**
     * 发送短信函数
     * @access public
     * @param string $mobile  手机号,多个手机号用英文逗号分隔
     * @param string $content  发送内容
     * @return array 返回值为数组,其中status为0表明发送成功,其他情况下发送失败,失败原因为msg
     */
    public function sendSms($mobile,$content){
        $param['u'] = $this->account;
        $param['p'] = md5($this->password);
        $param['m'] = $mobile;
        $param['c'] = $content;
        //$param['c'] = iconv('UTF-8', 'GBK2312//IGNORE', $content);
        $ret = self::http(self::sendSmsUrl, $param);
        //$data['status'] = $ret;
        $data = $ret == 0 ?'0' : self::getResult($ret);
        return $data;
    }
    
    /**
     * 获取短信剩余条数函数
     * @access public
     */
    public function getBalance(){
        $param['u'] = $this->account;
        $param['p'] = md5($this->password);
        $ret = self::http(self::queryBalanceUrl, $param);
        $retArr = split("\n", $ret);
        $balanceArr = split(",", $retArr[1]);
        $this->balance = $retArr[0] == 0 ? $balanceArr[1] : self::getResult($ret);
        return $this->balance;
    }
   /**
     * 发送http请求
     * @access protected
     * @param string $url  请求地址
     * @param string $param  get方式请求内容,数组形式,post方式时无效
     * * @param string $data  post请求方式时的内容,get方式时无效
     * @param string $method  请求方式,默认get
     */
    protected static function http($url, $param, $data = '', $method = 'GET'){
        $opts = array(
            CURLOPT_TIMEOUT        => 30,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
        );
    
        /* 根据请求类型设置特定参数 */
        $opts[CURLOPT_URL] = $url . '?' . http_build_query($param);
    
        if(strtoupper($method) == 'POST'){
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = $data;
    
            if(is_string($data)){ //发送JSON数据
                $opts[CURLOPT_HTTPHEADER] = array(
                    'Content-Type: application/json; charset=utf-8',
                    'Content-Length: ' . strlen($data),
                );
            }
        }
    
        /* 初始化并执行curl请求 */
        $ch = curl_init();
        curl_setopt_array($ch, $opts);
        $data  = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
    
        //发生错误,抛出异常
        if($error) throw new Exception('请求发生错误:' . $error);
    
        return  $data;
    }
    private function getResult($key){
        $rst['30'] = '密码错误';
        $rst['40'] = '账号不存在';
        $rst['41'] = '余额不足';
        $rst['42'] = '帐号过期';
        $rst['43'] = 'IP地址限制';
        $rst['50'] = '内容含有敏感词';
        $rst['51'] = '手机号码不正确';
        return $rst[$key];
    }
}
?>

然后我们修改前端的模板页面,打开项目/e/template/member/register.php文件,修改63~72行左右的代码:
   <tr>
      <td height="25" bgcolor="#FFFFFF"> <div align='left'>手机号码</div></td>
      <td height="25" bgcolor="#FFFFFF">
          <input name='phone' type='text' id='phone' maxlength='50' value="<?=$ecmsfirstpost==1?"":ehtmlspecialchars(stripSlashes($addr[phone]))?>">
        *</td>
    </tr>
    <tr> 
      <td height="25" bgcolor="#FFFFFF"> <div align='left'>验证码</div></td>
      <td height="25" bgcolor="#FFFFFF"> <input name='rzm' type='text' id='rzm' maxlength='30'>
        *<input  type="button" style="font-size: 12px; height: 22px; line-height: 19px;" value="发送验证码" onclick="sendrzm()" id="sendag"  ></td>
    </tr>

再引入jquery文件添加jquery代码:
<script src="/js/jquery.min-1.7.2.js"></script>
<SCRIPT language=javascript>
<!--
var secs = 120;
function sendrzm(){
     var tel=$("#phone").val();
     <?
    if($public_r['regkey_ok'])
    {
    ?>
        if($('#key').val()==''||$('#key').val().length<4){
            alert('请先填写图形验证码');
            return false;
        }
    <?
    }  
    ?>
    if($.trim(tel)==''||$.trim(tel).length!=11){
        alert('请填写正确的手机号码');
    }else{
        $.getJSON('/e/member/doaction.php?enews=Rzsj&phone=' + tel + '',
            function(data) {
                if(data.d=='2'){
                    alert(data.n);
                }else{
                    document.userinfoform.sendag.disabled=true;
                                for(i=1;i<=secs;i++) {
                                 window.setTimeout("update(" + i + ")", i * 1000);
                                }
                }
                             
            });
    }
    
}
function update(num) {
 if(num == secs) {
 document.userinfoform.sendag.value ="重新发送验证码";
 document.userinfoform.sendag.disabled=false;
 }
else {
 printnr = secs-num;
 document.userinfoform.sendag.value = "(" + printnr +")重新发送验证码";
 }
}
//-->
</SCRIPT>

修改项目/e/member/doaction.php文件的177~185行左右添加认证手机代码:
elseif($enews=='Rzsj')//认证手机号码
{
    $tel=$_GET['phone'];
    F_rzm($tel);
}elseif($enews=='Rzsjq')//认证手机号码取回密码
{
    $add=$_GET;
    F_rzmq($add);
}

添加完成之后,我们修改/e/class/connet.php文件中的4958~5213行左右,添加短信接口验证代码:
//发送手机验证码
function F_rzm($tel){
    global $empire,$dbtbpre,$public_r;
    $ism=is_mobile($tel);
    if($ism == 2)
    {
        G_return("手机号码为空或者错误!");
    }
    $time=time();
    $zrc=$empire->fetch1("select * from {$dbtbpre}enewsmemberadd where phone='$tel'  limit 1");
    if($zrc['userid']){
        G_return("该号码注册,请更换其他号码!");
    }
    //识别码
    $gzwc=RepPostVar(getcvar('gzwrz'));
    $zr=$empire->fetch1("select * from {$dbtbpre}rz where bsm='$gzwc' and sj='$tel'  limit 1");
    if($zr['id']){
        //验证次数
        if($zr['c']>10)
        {
            G_return("该号码已多次接收不了验证码,建议更换手机重新注册!");
        }
        //验证间隔时间
        $gtime2=$zr['t']+60*2;
        if($time<$gtime2)
        {
            G_return("发送过程中,手机接收验证码需要1-2分钟,请耐心等待!");
        }
        $rm=no_make_password(6);
        $content=$rm;
        $fh=api_sendsms($tel,$content);
        if($fh){
            $sql=$empire->query("update {$dbtbpre}rz set t='$time',rzm='$rm',c=c+1 where id='$zr[id]'");
            $logincookie=$time+3600*2;
            $set1=esetcookie("gzwrz",$gzwc,$logincookie);
            G_return("验证码已发送成功,请查收!",1);
        }else{
            G_return("发送失败,请联系管理员!");
        }
 
    }else{
        $rm=no_make_password(6);
        $content=$rm;
        $fh=api_sendsms($tel,$content);
        if($fh){
            $gzwc=make_password(20);
            $sql=$empire->query("insert into {$dbtbpre}rz(bsm,rzm,sj,t,c) values('$gzwc','$rm','$tel','$time','1');");
            $logincookie=$time+3600*2;
            $set1=esetcookie("gzwrz",$gzwc,$logincookie);
            G_return("验证码已发送成功,请查收!",1);
        }else{
            G_return("发送失败,请联系管理员!");
        }
 
    }
 
}
 
//发送手机验证码(找回密码)function F_rzmq($add){
    global $empire,$dbtbpre,$public_r;
    $tel=$add['phone'];
    //if(!$public_r['opengetpass'])
    //{
    //  G_return("系统关闭取回密码!");
    //}
    $username=trim($add[username]);
    if(!$username)
    {
        G_return("用户名不能为空!");
    }
 
 
    $ism=is_mobile($tel);
    if($ism == 2)
    {
        G_return("手机号码为空或者错误!");
    }
    $username=RepPostVar($username);
    $ur=$empire->fetch1("select ".eReturnSelectMemberF('userid,username')." from ".eReturnMemberTable()." where ".egetmf('username')."='$username' limit 1");
    if(!$ur['userid'])
    {
        G_return("该用户不存在!");
    }
    $aur=$empire->fetch1("select phone from {$dbtbpre}enewsmemberadd where userid='".$ur['userid']."' limit 1");
    if($tel!=$aur['phone'])
    {
        G_return("手机号码不正确!");
    }
 
 
    $time=time();
    //识别码
    $gzwc=RepPostVar(getcvar('gzwrzq'));
    $zr=$empire->fetch1("select * from {$dbtbpre}rzq where sj='$tel' and username='".$username."'  limit 1");
    if($zr['id']){
        //时间判断
        $gtime1=date("Y-m-d",$zr['t']);
        $gddd=date("Y-m-d",time());
        //验证次数
        if($zr['c']>10&&$gtime1==$gddd)
        {
            G_return("该号码已多次接收不了验证码,建议更换手机重新注册!");
        }
        //验证间隔时间
        $gtime2=$zr['t']+60*2;
        if($time<$gtime2)
        {
            G_return("发送过程中,手机接收验证码需要1-2分钟,请耐心等待!");
        }
        $rm=no_make_password(6);
        $content=$rm;
        $fh=api_sendsms($tel,$content);
        if($fh){
            if($gtime1!=$gddd){
                $sql=$empire->query("update {$dbtbpre}rzq set t='$time',rzm='$rm',c=c+1 where id='$zr[id]'");
            }else{
                $sql=$empire->query("update {$dbtbpre}rzq set t='$time',rzm='$rm',c=1 where id='$zr[id]'");
            }
            $logincookie=$time+3600*2;
            $set1=esetcookie("gzwrzq",$gzwc,$logincookie);
            G_return("认证码已发送成功,请查收!",1);
        }else{
            G_return("发送失败,请联系管理员!");
        }
 
    }else{
 
        $rm=no_make_password(6);
        $content=$rm;
        $fh=api_sendsms($tel,$content);
        if($fh){
            $gzwc=make_password(20);
            $sql=$empire->query("insert into {$dbtbpre}rzq(bsm,rzm,sj,t,c,username) values('$gzwc','$rm','$tel','$time','1','".$username."');");
            $logincookie=$time+3600*2;
            $set1=esetcookie("gzwrzq",$gzwc,$logincookie);
            G_return("认证码已发送成功,请查收!",1);
        }else{
            G_return("发送失败,请联系管理员!");
        }
 
    }
 
}

//替换手机短信内容变量
function T_sjn($rzm){
    global $empire,$dbtbpre,$public_r;
    $date=date("Y-m-d");
    $time=date("Y-m-d H:i:s");
    $content=$public_r['add_rznr'];
    $content=str_replace('[!--rzm--]',$rzm,$content);
    return  iconv('UTF-8', 'GB2312', $content);//utf-8
    //return $content; //gbk
}
 
//替换手机短信内容变量(找回密码)
function T_sjnq($rzm){
    global $empire,$dbtbpre,$public_r;
    $date=date("Y-m-d");
    $time=date("Y-m-d H:i:s");
    $content=$public_r['add_rznrq'];
    $content=str_replace('[!--rzm--]',$rzm,$content);
    return  iconv('UTF-8', 'GB2312', $content);//utf-8
    //return  $content;
}
 
 
//注册时手机验证
function C_sjrz($tel,$rzm){
    global $empire,$dbtbpre,$public_r;
    $ism=is_mobile($tel);
    if($ism==2)
    {
       printerror('手机号码为空或者错误!'.$tel.$rzm,'',1,0,1);
    }
    $zrc=$empire->fetch1("select * from {$dbtbpre}enewsmemberadd where phone='$tel'  limit 1");
    if($zrc['id']){
        printerror('该号码注册,请更换其他号码!','',1,0,1);
    }
    //识别码
    $gzwc=RepPostVar(getcvar('gzwrz'));
 
    $rzm=RepPostVar($rzm);
    $zr=$empire->fetch1("select * from {$dbtbpre}rz where bsm='$gzwc' and sj='$tel' and rzm='$rzm'  order by id desc limit 1");
    if(!$zr['id']){
        printerror('手机号码验证失败!','',1,0,1);
    }
    $time=time();
    $gtime2=$zr['t']+3600*12;
    if($time>$gtime2)
    {
        printerror('手机验证码已超过有效期!','',1,0,1);
    }
    $set2=esetcookie("gzwrz","",0);
    $gsql=$empire->query("delete from {$dbtbpre}rz where id=$zr[id] ");
    return '';
}
 
 
//验证手机号码
function is_mobile($tel){
    if($tel=="")
    { return 2; }
    //验证长度
    if(strlen($tel)!="11")
    {  return 2;  }
    //验证开头3位格式
    if(!preg_match("/13[0123456789]\d{8}|14[57]\d{8}|15[012356789]\d{8}|18[012356789]\d{8}|17[17]\d{8}/",$tel))///1\d{10}/
    { return 2; }
 
    return 1;
}
 
//返回信息
function G_return($n,$d=2){
    $gf['n']=Gbkbm($n);
    $gf['d']=$d;
    echo json_encode($gf);
    exit();
}
 
//中文编码
function Gbkbm($g){
    //$g=iconv("GB2312","UTF-8//IGNORE",$g);
    return $g;
}
 
//短信发送接口
function api_sendsms($moblie,$content){
    global $empire,$dbtbpre,$public_r;
    //$uid=mb_convert_encoding($uid,'GB2312','UTF-8'); //内容为UTF-8时转码成GB2312
    //$msg=mb_convert_encoding($msg,'GB2312','UTF-8'); //内容为UTF-8时转码成GB2312
    require("../admin/smsbao/Smsbao.php");
    //取出短信配置信息
    $smsbao_config=file_get_contents("../admin/smsbao/config.txt");
    $smsbao_config=explode(",", $smsbao_config);
    $smsbao=new Sms($smsbao_config[0],$smsbao_config[1]);
    $content="【".$smsbao_config[2]."】您好,你的验证码为:".$content.',请勿告诉他人';
    $sms_res=$smsbao->sendSms($moblie,$content);
    if($sms_res=='0'){
        return true;
    }else{
        return false;
    }
    /* 提交成功返回值格式:
    0发送成功! */
}

最后我们进行发送测试:
帝国cms7.5短信宝接口插件


经过以上的添加,短信宝短信接口就已经安装成功,可以正常使用了。
 

短信宝插件问题汇总

1、安装短信宝插件后,修改系统默认目录admin会出现会员投稿白屏的问题,查看详情可点击这里:帝国cms会员投稿后白屏怎么办?

2、修改默认目录admin还会导致无法发送短信的问题,查看详情可点击这里:帝国cms短信宝接口点击发送验证码没反应
郑重承诺

帝国cms7.5短信宝接口插件为官方正版,下载鸥未增删过任意一个字节,请放心下载。

下载地址 立即下载 导出教程
发表评论 共有条评论
插件下载分类

插件信息

  • 分类帝国CMS
  • 程序版本v7.5
  • 编号No.12-50
  • 价格免费
  • 编码UTF-8
  • 官网下载 前往官网
推荐帝国CMS插件下载
最新帝国CMS插件下载
客服QQ:341553759
扫码咨询 常见问题 >
官方交流群:90432500
点击加入