alias('b')->field([ 'b.id', 'b.typeid', 'bt.name' => 'typename', 'bt.description' => 'typedesc', 'b.name', 'b.categoryid', 'b.url', 'b.picture', 'b.alt', 'b.style', 'b.description' => 'desc', 'b.descolor', 'b.btncolor', 'b.videourl', ]) ->join('banner_type bt', 'bt.stat= 0 and bt.id=b.typeid') ->where('b.stat', '=', 0) ->where('b.typeid', 'in', $typeids) ->order(['sort' => 'asc', 'id' => 'asc']) ->select(); $chucks = []; foreach ($banners as $val) { if (empty($chucks['typeid_' . $val['typeid']])) { $chucks['typeid_' . $val['typeid']]['id'] = $val['typeid']; $chucks['typeid_' . $val['typeid']]['name'] = $val['typename']; $chucks['typeid_' . $val['typeid']]['desc'] = $val['typedesc']; $chucks['typeid_' . $val['typeid']]['banners'] = []; } $link = $val['url']; if (empty($val['url']) && !empty($val['categoryid'])) { $link = url_rewrite('productsub', ['id' => $val['categoryid']]); } $chucks['typeid_' . $val['typeid']]['banners'][] = [ 'id' => $val['id'], 'name' => $val['name'], 'link' => $link, 'picture' => $val['picture'], 'alt' => $val['alt'], 'style' => $val['style'], 'desc' => $val['desc'], 'desc_color' => $val['descolor'], 'btn_color' => $val['btncolor'], 'video_url' => $val['videourl'], ]; } return $chucks; } /** * getCategoryTree 获取特定文章分类树 */ private function getCategoryTree($cid) { // 设备 $category = Loader::model('ArticleCategory') ->field([ 'id', 'pid', 'name', 'picture' ]) ->where('isshow', '=', 1) ->where(function($query) use($cid) { $query->where('id', '=', $cid)->WhereOr('pid', '=', $cid); }) ->where('country_code', '=', $this->country_code) ->order(['sort' => 'asc']) ->select(); $category_array = collection($category)->toArray(); $category_tree = $this->buildTree($category_array, $cid); return $category_tree; } // 根据分类获取文章 private function getArticleByCategory($categorys, $limit = null) { if (!is_array($categorys)) { throw new \Exception('请确认分类正确'); } if (empty($categorys)) { return []; } $sub_query = Loader::model('Article') ->field([ 'id', 'cid', 'name', 'sort' ]) ->where('stat', '=', 0) ->where('cid', '=', $categorys[0]['id']) ->where('country_code', '=', $this->country_code) ->order(['sort' => 'asc', 'id' => 'desc']); if (!empty($limit)) { $sub_query = $sub_query->limit($limit); } $sub_query = $sub_query->buildSql(); $model = \think\Db::table($sub_query . ' a'); foreach ($categorys as $key => $val) { if ($key == 0) continue; $model->union(function ($query) use ($val, $limit) { $query->name('article')->field([ 'id', 'cid', 'name', 'sort' ]) ->where('stat', '=', 0) ->where('cid', '=', $val['id']) ->where('country_code', '=', $this->country_code) ->order(['sort' => 'asc', 'id' => 'desc']); if (!empty($limit)) { $query->limit($limit); } }); } $map = []; $data = $data = $model->select(); foreach ($data as $key => $val) { $map['cid_' . $val['cid']][] = $val; } return $map; } // 帮助中心 public function helper() { // 获取分类 $categorys = $this->getCategoryTree(77); // 获取文章 $articles = $this->getArticleByCategory($categorys, 3); // 组装数据 foreach ($categorys as $key => &$val) { if (!isset($val['articles'])) { $val['articles'] = []; } if (isset($articles['cid_' . $val['id']])) { $items = $articles['cid_' . $val['id']]; foreach ($items as $k => $v) { $val['articles'][] = [ 'id' => $v['id'], 'name' => $v['name'] ]; } } } unset($val); $this->assign('categorys', $categorys); // 获取banner $banners = $this->getBanners(128); if (!empty($banners)) { $banners = $banners['typeid_128']['banners']; } $this->assign('banners', $banners); $this->assign('banners_size', count($banners)); return $this->fetch(); } // 搜索帮助文章 public function helper_search() { $base_category = 77; $keywords = request()->param('keywords'); $articles = Loader::model('Article') ->field([ 'id', 'name', 'sort', ]) ->where('stat', '=', 0) ->where('country_code', '=', $this->country_code) ->where('cid', 'in', function ($query) use ($base_category) { $query->name('article_category') ->field(['id']) ->where('id', '=', $base_category) ->whereOr('pid', '=', $base_category); }) ->where(function ($query) use ($keywords) { if (!empty($keywords)) { $query->where('name', 'like', '%' . $keywords . '%'); } }) ->order(['sort' => 'asc', 'id' => 'desc']) ->select(); return json([ 'code' => 0, 'message' => '获取成功', 'data' => $articles ]); } // 帮助中心文章详细 public function helper_detail() { $id = request()->param('id'); $article = Loader::model('Article')->where('id', '=', $id)->find(); if (request()->isAjax()) { return json([ 'code' => 0, 'message' => '获取成功', 'data' => $article ]); } else { // 获取分类 $categorys = $this->getCategoryTree(77); // 获取文章 $articles = $this->getArticleByCategory($categorys); // 组装数据 foreach ($categorys as $key => &$val) { if (!isset($val['articles'])) { $val['articles'] = []; } if (isset($articles['cid_' . $val['id']])) { $items = $articles['cid_' . $val['id']]; foreach ($items as $k => $v) { $val['articles'][] = [ 'id' => $v['id'], 'name' => $v['name'] ]; } } } unset($val); $this->assign('categorys', $categorys); $this->assign('article', $article); $this->assign('cid', request()->param('cid', $article->cid)); } return $this->fetch(); } // 下载 public function download() { $downloads = []; // 获取banner $banners = $this->getBanners([129]); if (!empty($banners)) { $downloads = $banners['typeid_129']['banners']; } $this->assign('downloads', $downloads); return $this->fetch(); } }