Esempio n. 1
0
 public function actionRun()
 {
     $sync_list = BlogSyncQueue::find()->where(['status' => -1])->orderBy("id asc")->limit(count(SyncBlogService::$type_mapping))->all();
     if (!$sync_list) {
         return $this->echoLog("no data need to handle");
     }
     $date_now = date("Y-m-d H:i:s");
     foreach ($sync_list as $_item) {
         sleep(1);
         $this->echoLog("--------queue_id:{$_item['id']}---------");
         $tmp_res = SyncBlogService::doSync($_item['type'], $_item['blog_id']);
         $_item->status = $tmp_res ? 1 : 0;
         $_item->updated_time = $date_now;
         $_item->update(0);
         if (!$tmp_res) {
             $this->echoLog(SyncBlogService::getLastErrorMsg());
         }
     }
     return $this->echoLog(" Done ");
 }
Esempio n. 2
0
 public function actionSet()
 {
     $request = Yii::$app->request;
     if ($request->isGet) {
         $id = trim($this->get("id", 0));
         $info = [];
         if ($id) {
             $post_info = Posts::findOne(['id' => $id]);
             if ($post_info) {
                 $domains = Yii::$app->params['domains'];
                 $info = ["id" => $post_info['id'], "title" => DataHelper::encode($post_info['title']), "content" => DataHelper::encode($post_info['content']), "type" => $post_info['type'], "status" => $post_info['status'], "original" => $post_info['original'], "tags" => DataHelper::encode($post_info['tags']), 'view_url' => $domains['blog'] . Url::toRoute("/default/{$post_info['id']}")];
             }
         }
         //set or add
         return $this->render("set", ["info" => $info, "posts_type" => Constant::$posts_type, "status_desc" => Constant::$status_desc, "original_desc" => Constant::$original_desc]);
     }
     $uid = $this->current_user->uid;
     $id = trim($this->post("id", 0));
     $title = trim($this->post("title"));
     $content = trim($this->post("content"));
     $tags = trim($this->post("tags"));
     $type = trim($this->post("type"));
     $status = trim($this->post("status", 0));
     $original = trim($this->post("original", 0));
     if (mb_strlen($title, "utf-8") <= 0 || mb_strlen($title, "utf-8") > 100) {
         return $this->renderJSON([], "请输入博文标题并且少于100个字符", -1);
     }
     if (mb_strlen($content, "utf-8") <= 0) {
         return $this->renderJSON([], "请输入更多点博文内容", -1);
     }
     if (mb_strlen($tags, "utf-8") <= 0) {
         return $this->renderJSON([], "请输入博文tags", -1);
     }
     if (intval($type) <= 0) {
         return $this->renderJSON([], "请选择类型", -1);
     }
     $date_now = date("Y-m-d H:i:s");
     if ($id) {
         $model_posts = Posts::findOne(['id' => $id]);
     } else {
         $model_posts = new Posts();
         $model_posts->uid = $uid;
         $model_posts->created_time = $date_now;
     }
     $tags_arr = [];
     $tags = explode(",", $tags);
     if ($tags) {
         foreach ($tags as $_tag) {
             if (!in_array($_tag, $tags_arr)) {
                 $tags_arr[] = $_tag;
             }
         }
     }
     preg_match('/<\\s*img\\s+[^>]*?src\\s*=\\s*(\'|\\")(.*?)\\1[^>]*?\\/?\\s*>/i', $content, $match_img);
     if ($match_img && count($match_img) == 3) {
         $model_posts->image_url = $match_img[2];
     } else {
         $model_posts->image_url = "";
     }
     $model_posts->title = $title;
     $model_posts->content = $content;
     $model_posts->type = $type;
     $model_posts->original = $original;
     $model_posts->status = $status;
     $model_posts->tags = $tags_arr ? implode(",", $tags_arr) : "";
     $model_posts->updated_time = $date_now;
     $model_posts->save(0);
     $post_id = $model_posts->id;
     BlogService::buildTags($post_id);
     CacheHelperService::buildFront(true);
     if ($status == 1) {
         //只有在线的才进入同步队列
         SyncBlogService::addQueue($post_id);
         RecommendService::addQueue($post_id);
     }
     return $this->renderJSON(['post_id' => $post_id], "博文发布成功");
 }