tp5百度推送

百度推送
前端代码:

bootstrap框架:
   <!-- 模态框(Modal) -->
    <div class="modal fade" id="pushList" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
	    <div class="modal-dialog">
		    <div class="modal-content">
			    <div class="modal-header">
				    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
					    &times;
				    </button>
				    <h4 class="modal-title" id="myModalLabel">
					    提交<span style="color:red;margin-left:10px;font-size:12px">注意: 在运行过程中请勿刷新浏览器</span>
				    </h4>
			    </div>
			    <div class="modal-body">
				    <div class="input-group">
                        <span class="input-group-addon">需提交</span>
                        <input type="text" class="form-control push_count" aria-label="Amount (to the nearest dollar)">
                        <span class="input-group-addon">条</span>
                    </div>
                    <div class="progress" style="margin:10px 0;display:none;">
                        <div class="push-progress progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="45" aria-valuemin="0" aria-valuemax="100"></div>
                    </div>
			    </div>
			    <div class="modal-footer">
			        <button type="button" class="btn btn-primary" onclick="execPush()">开始</button>
				    <button type="button" class="btn btn-default close-btn" data-dismiss="modal">关闭</button>
			    </div>
		    </div>
	    </div>
    </div>

js代码

var running = false;
function push(count,all){
    $.post("baidus/fa",{},function(res){
        if(res.remain){
            if(count > all){
                running = false;
                $('.push_count').val('');
                // 启用关闭按钮
                $('.close-btn').show();
                // 关闭进度条
                $('.progress').hide();
                alert("已全部提交完毕,剩余可用次数 " + res.remain + "次");
            }else{
                count++;
                /*进度条*/
                $('.push-progress').css('width', count / all * 100 + '%');
                console.log(res);
                push(count,all);
            }
        }else{
            running = false;
            alert("百度可提交的次数已达上限,请明天再来操作吧");
        }
    });
}

function execPush(){
    if(running) return;
    var push_count = $('.push_count').val();
    if(push_count && push_count > 0){
        running = true;
        // 开启进度条
        $('.progress').show();
        // 禁用关闭按钮
        $('.close-btn').hide();
        // 开始运行
        push(0, push_count);
    }else{
        alert("提交数必须为数字且大于0");
    }
}

后台代码:

 public function fa(){
        $request=Request::instance();
        // 推送信息表
        $push_info = Db::name('bai_status')->where('id', '=', 1)->find();
        if($push_info){
            // 最后推送数据的ID
            $last_id = $push_info['bid'];
            // 获取下一条需要推送的url
            $next_url = Db::name("bai")->where("id", ">", $last_id)->find();
            if(empty($next_url)){
                $next_url = Db::name("baidus")->find();
            }
            // 处理host
            $host = strstr($next_url['linkurl'], "/", true);
            if(!$host){
                $host = $next_url['linkurl'];
            }
            // 域名
            $site = "https://" . $host;
            // 推送token
            $token='####';
            // 推送api地址
            $api = "http://#####?site=$site&token=$token";
            // 初始化
            $ch = curl_init();
            // 参数
            $options =  array(
                CURLOPT_URL => $api,
                CURLOPT_POST => true,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POSTFIELDS => $next_url['linkurl'],
                CURLOPT_HTTPHEADER => array('Content-Type: text/plain')
            );
            // 设置参数
            curl_setopt_array($ch, $options);
            // 发送请求
            $result = (array)json_decode(curl_exec($ch));
            // 更新数据库
            Db::name('baidus_status')->update(array('id' => 1, 'bid' => $next_url['id'], 'btime' => time()));
            // 返回数据
            return array(
                'code' => $result['success'],
                'remain' => $result['remain'],
                'push_url' => $next_url['linkurl']
            );
        }else{
            return array('code' => '-1','msg' => '数据库信息有误,请联系管理员');
        }
    }

数据表设计

DROP TABLE IF EXISTS `fa_bai`;
CREATE TABLE `fa_bai` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `linkurl` varchar(100) COLLATE utf8_unicode_ci NOT NULL COMMENT '推送链接',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3994665 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='百度推送链接表';

DROP TABLE IF EXISTS `fa_bai_status`;
CREATE TABLE `fa_bai_status` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `bid` int(11) DEFAULT NULL COMMENT '上次发送ID',
  `bnum` int(11) DEFAULT NULL COMMENT '上次发送条数',
  `btime` int(11) DEFAULT NULL COMMENT '上次发送时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='发送记录';

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值