JAVA版百度主动推送POST提交

  |   3 评论   |   974 浏览

     最近随便看了看关于百度的推送,查了下资料发现网上Java版的百度主动提交案列好少的说,铺天盖地的都是php的推送分享,所以觉得还是得去写写。

        这是文章发布时的js代码自动选择是否提交百度链接:

    /**
         * @@description 添加文章
         * @param {Boolean} articleIsPublished 文章是否发布过
         * @param {Boolean} isAuto 是否为自动保存
         */
        add: function (articleIsPublished, isAuto) {
            if (admin.article.validate()) {
                var that = this;
                that._addDisabled();
    
                $("#loadMsg").text(Label.loadingLabel);
                $("#tipMsg").text("");
                var signId = "";
                $(".signs button").each(function () {
                    if (this.className === "selected") {
                        signId = this.id.substr(this.id.length - 1, 1);
                    }
                });
    
                var articleContent = admin.editors.articleEditor.getContent(),
                        articleAbstract = admin.editors.abstractEditor.getContent();
    
                var requestJSONObject = {
                    "article": {
                        "articleTitle": $("#title").val(),
                        "articleContent": articleContent,
                        "articleAbstract": articleAbstract,
                        "articleTags": this.trimUniqueArray($("#tag").val()).toString(),
                        "articlePermalink": $("#permalink").val(),
                        "articleIsPublished": articleIsPublished,
                        "articleSignId": signId,
                        "postToCommunity": $("#postToCommunity").prop("checked"),
                        "articleCommentable": $("#articleCommentable").prop("checked"),
                        "articleViewPwd": $("#viewPwd").val()
                    }
                }; 
    			var Aid="";//文章id
                $.ajax({
                    url: latkeConfig.servePath + "/console/article/",
                    type: "POST",
                    cache: false,
                    data: JSON.stringify(requestJSONObject),
                    success: function (result, textStatus) {
                        if (isAuto) {
                            $("#tipMsg").text(Label.autoSaveLabel);
                            admin.article.status.id = result.oId;
                            return;
                        } 
    					Aid=result.oId;
                        $("#tipMsg").text(result.msg);
                        if (!result.sc) {
                            return;
                        }
    
                        if (articleIsPublished) {
                            admin.article.status.id = undefined;
                            admin.selectTab("article/article-list");
                        } else {
                            admin.selectTab("article/draft-list");
                        }
    					//成功提示是否推送
    					if (articleIsPublished) {  
    						 $.tzConfirm({title:"温馨提示",content:"您需要将连接提交百度吗?",callback:function(ok){
    								if(ok){
    									 $.ajax({
    										url:"/articles/toPull*",
    										type: "GET",
    										data: {"articleId":Aid,"path":latkeConfig.servePath},      
    										error: function(data) { 
    										},
    										success: function(data) { 
    											 var data = $.parseJSON(data);  
    											  console.log(data.remain+"==="+data.success); 
    											  //提示推送结果
    											  if(data.success=="0"){
    												  loading("推送失败",4);
    											  }else{
    												  loading("推送成功",4);
    											  }
    										}
        								}); 
    								 }
    					     }});	
    					};
                        admin.article.isConfirm = false;
                    },
                    complete: function (jqXHR, textStatus) {
                        that._removeDisabled();
                        $("#loadMsg").text("");
                        if (jqXHR.status === 403) {
                            $.get("/admin-index.do");
                            that.add(articleIsPublished);
                        }
                    }
                });
            }
        },

    后台请求处理代码:

    推送处理

    核心方法:

    核心代码

    最后看看演示效果:

    点击确定后再控制台输出信息:

         这是文章路径发生错误,所以推送失败。


     这样的提示说明文章推送成功了。

    百度的每天文章推送限额500条,今天推送了3条,所以还有497条可以推送。success为零时则表示推送失败,反之,则推送成功。具体可参照官方文档体验。 

     


    by 猿码阁     


    >