public function actionIndex() { $list = UserMessageHistory::find()->where(['status' => 1])->orderBy("id desc")->limit(5)->all(); $data = []; if ($list) { $user_mapping = DataHelper::getDicByRelateID($list, User::className(), "uid", "uid", ["nickname", "avatar"]); foreach ($list as $_item) { if (!isset($user_mapping[$_item['uid']])) { continue; } $tmp_user_info = $user_mapping[$_item['uid']]; $data[] = ["id" => $_item['id'], "nickname" => DataHelper::encode($tmp_user_info['nickname']), "avatar" => $tmp_user_info['avatar'] ? $tmp_user_info['avatar'] : GlobalUrlService::buildStaticUrl("/images/wap/no_avatar.png"), "content" => DataHelper::encode($_item['content']), "created_time" => date("Y-m-d H:i", strtotime($_item['created_time']))]; } } $this->layout = "main"; return $this->render("index", ["list" => $data]); }
public function actionDo() { $kw = trim($this->get("kw", "")); $p = intval($this->get("p", 1)); if (!$p) { $p = 1; } $data = []; if (!$kw) { return $this->redirect("/"); } $this->setTitle($kw); $pagesize = 10; $offset = ($p - 1) * $pagesize; $search_key = ['LIKE', 'search_key', '%' . strtr($kw, ['%' => '\\%', '_' => '\\_', '\\' => '\\\\']) . '%', false]; $query = IndexSearch::find()->where($search_key); $total_count = $query->count(); $list = $query->orderBy("id desc")->limit($pagesize)->offset($offset)->all(); if ($list) { $book_mapping = DataHelper::getDicByRelateID($list, Book::className(), "book_id", "id", ["subtitle", "summary", "origin_image_url", "tags"]); $post_mapping = DataHelper::getDicByRelateID($list, Posts::className(), "post_id", "id", ["title", "content", "tags"]); foreach ($list as $_item) { if ($_item['book_id']) { $tmp_target = $book_mapping[$_item['book_id']]; $tmp_content = mb_substr($tmp_target['summary'], 0, 105, "utf-8"); $tmp_title = DataHelper::encode($tmp_target['subtitle']); $tmp_view_url = Url::toRoute("/library/detail/{$_item['book_id']}"); } else { $tmp_target = $post_mapping[$_item['post_id']]; $tmp_content = UtilHelper::blog_summary($tmp_target['content'], 105); $tmp_title = DataHelper::encode($tmp_target['title']); $tmp_view_url = Url::toRoute("/default/{$_item['post_id']}"); } $tags = explode(",", $tmp_target['tags']); $data[] = ['title' => $tmp_title, 'content' => nl2br($tmp_content), 'tags' => $tags, 'date' => date("Y年m月d日"), 'view_url' => $tmp_view_url]; } } $page_info = DataHelper::ipagination(["total_count" => $total_count, "page_size" => $pagesize, "page" => $p, "display" => 5]); return $this->render("result", ["data" => $data, "page_info" => $page_info, "urls" => ["page_base" => Url::toRoute(["/search/do", "kw" => $kw])]]); }