private function search($params = []) { $p = isset($params['p']) ? $params['p'] : 1; $type = isset($params['type']) ? $params['type'] : 1; $offset = ($p - 1) * $this->page_size; $query = Posts::find()->where(['status' => 1]); switch ($type) { case 2: $query->orderBy(['view_count' => SORT_DESC]); break; case 3: $query->andWhere(['original' => 1]); $query->orderBy(['id' => SORT_DESC]); break; default: $query->orderBy(['id' => SORT_DESC]); break; } $posts_info = $query->offset($offset)->limit($this->page_size)->all(); $data = []; if ($posts_info) { foreach ($posts_info as $_post) { $tmp_tags = explode(",", $_post['tags']); $data[] = ['title' => DataHelper::encode($_post['title']), 'content' => nl2br(UtilHelper::blog_short($_post['content'], 200)), "tags" => $tmp_tags, 'image_url' => $_post['image_url'], 'view_url' => UrlService::buildWapUrl("/default/info", ["id" => $_post['id']])]; } } return $data; }
public function actionInfo() { $id = intval($this->post("id", 0)); if (!$id) { return $this->renderJSON([], "指定博文不存在", -1); } $post_info = Posts::find()->where(['status' => 1, 'id' => $id])->one(); if (!$post_info) { return $this->renderJSON([], "指定博文不存在", -1); } $tmp_tags = explode(",", $post_info['tags']); $content = preg_replace("/brush:(\\w+);toolbar:false/", "prettyprint linenums", $post_info['content']); $info = ['author' => ['name' => DataHelper::getAuthorName()], 'title' => $post_info['title'], 'content' => $content, "tags" => $tmp_tags, 'updated_time' => date("Y-m-d H:i", strtotime($post_info['updated_time']))]; $share_info = ['title' => $post_info['title'], 'content' => UtilHelper::blog_short($post_info['content'], 200), 'url' => GlobalUrlService::buildWapUrl("/default/info", ['id' => $post_info['id']])]; return $this->renderJSON(['info' => $info, 'share_info' => $share_info]); }
public function actionDo() { $data = []; $kw = $this->get("kw", ""); if ($kw) { $this->setTitle($kw); $search_key = ['LIKE', 'search_key', '%' . strtr($kw, ['%' => '\\%', '_' => '\\_', '\\' => '\\\\']) . '%', false]; $query = IndexSearch::find()->where($search_key); $list = $query->orderBy("id desc")->all(); if ($list) { foreach ($list as $_item) { if ($_item['book_id']) { $tmp_title = DataHelper::encode($_item['title']); $tmp_view_url = UrlService::buildWapUrl("/library/info", ['id' => $_item['book_id']]); } else { $tmp_title = DataHelper::encode($_item['title']); $tmp_view_url = UrlService::buildWapUrl("/default/info", ['id' => $_item['post_id']]); } $data[] = ['title' => $tmp_title, 'content' => nl2br(UtilHelper::blog_short($_item['description'], 200)), 'image_url' => $_item['image'], 'view_url' => $tmp_view_url]; } } } return $this->render("do", ["post_list" => $data, 'kw' => $kw]); }