480 lines
16 KiB
PHP
Executable File
480 lines
16 KiB
PHP
Executable File
<?php
|
||
namespace app\index\controller;
|
||
|
||
use think\Loader;
|
||
|
||
class TopsNas extends BaseController
|
||
{
|
||
public function _initialize()
|
||
{
|
||
parent::_initialize();
|
||
parent::nasNavigation();
|
||
}
|
||
|
||
private function getBanners($typeids)
|
||
{
|
||
$banners = Loader::model("Banner")->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;
|
||
}
|
||
|
||
/**
|
||
* nas专题首页
|
||
*/
|
||
public function index()
|
||
{
|
||
$chucks = $this->getBanners([107, 108, 109, 110, 111, 112]);
|
||
$data = [
|
||
'swiper' => $chucks['typeid_107'],
|
||
'product' => $chucks['typeid_108'],
|
||
'video' => $chucks['typeid_109'],
|
||
'plan' => $chucks['typeid_110'],
|
||
'weline' => $chucks['typeid_111'],
|
||
'download' => $chucks['typeid_112'],
|
||
];
|
||
$this->assign("data", $data);
|
||
|
||
return $this->fetch();
|
||
}
|
||
|
||
/**
|
||
* nas新品公测
|
||
*/
|
||
public function beta()
|
||
{
|
||
return $this->fetch();
|
||
}
|
||
|
||
public function download()
|
||
{
|
||
return $this->fetch();
|
||
}
|
||
|
||
public function download_cyber()
|
||
{
|
||
return $this->fetch();
|
||
}
|
||
|
||
/**
|
||
* nas 客户合作
|
||
*/
|
||
public function cooperation()
|
||
{
|
||
$chucks = $this->getBanners([119, 120, 121]);
|
||
$data = [
|
||
'image_text' => $chucks['typeid_119'],
|
||
'advantage' => $chucks['typeid_120'],
|
||
'expectation' => $chucks['typeid_121'],
|
||
];
|
||
$data['expectation']['banners'] = array_chunk($data['expectation']['banners'], 3);
|
||
$this->assign("data", $data);
|
||
|
||
return $this->fetch();
|
||
}
|
||
|
||
/**
|
||
* getCategoryChildren 获取子孙分类id
|
||
*/
|
||
private function getCategoryChildren($category)
|
||
{
|
||
return \think\Db::query("
|
||
SELECT t2.id
|
||
FROM (
|
||
SELECT
|
||
@r AS _id, (SELECT @r := GROUP_CONCAT(id) FROM cod_article_category WHERE FIND_IN_SET(pid, _id)) AS parent_id
|
||
FROM
|
||
(SELECT @r := $category) vars, cod_article_category h
|
||
WHERE @r <> 0) t1
|
||
JOIN cod_article_category t2
|
||
ON FIND_IN_SET(t2.pid, t1._id)
|
||
ORDER BY t2.id;
|
||
");
|
||
}
|
||
|
||
/**
|
||
* getProductSeriesIdMaps 获取文章系列型号
|
||
*/
|
||
private function getProductSeriesIdMaps($article)
|
||
{
|
||
$series = [];
|
||
$array_items = array_column($article, 'product_series');
|
||
array_walk($array_items, function ($val) use (&$series) {
|
||
$series = array_merge($series, explode(',', $val));
|
||
});
|
||
if (!empty($series)) {
|
||
$product_series = Loader::model('ProductSeries')
|
||
->where('id', 'in', array_unique($series))
|
||
->column('name', 'id');
|
||
foreach ($article as $key => $val) {
|
||
$p_series = explode(',', $val['product_series']);
|
||
$p_item = [];
|
||
foreach ($p_series as $k => $v) {
|
||
if (!empty($product_series[$v])) {
|
||
$p_item[] = $product_series[$v];
|
||
|
||
}
|
||
}
|
||
$article[$key]['product_series'] = $p_item;
|
||
}
|
||
}
|
||
return $article;
|
||
}
|
||
|
||
/**
|
||
* getProductSeries 获取产品系列
|
||
*/
|
||
private function getProductSeries()
|
||
{
|
||
return Loader::model('ProductSeries')
|
||
->field([
|
||
'id',
|
||
'name',
|
||
])
|
||
->where('stat', '=', 0)
|
||
->where('isshow', '=', 1)
|
||
->order(['id' => 'asc', 'sort' => 'asc'])
|
||
->select();
|
||
}
|
||
|
||
/**
|
||
* getCategoryTree 获取特定文章分类树
|
||
*/
|
||
private function getCategoryTree($cid = 36)
|
||
{
|
||
// 设备
|
||
$category = Loader::model('ArticleCategory')
|
||
->field([
|
||
'id',
|
||
'pid',
|
||
'name',
|
||
'picture'
|
||
])
|
||
->where('isshow', '=', 1)
|
||
->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;
|
||
}
|
||
|
||
/**
|
||
* guide 使用教程
|
||
*/
|
||
public function guide()
|
||
{
|
||
if ($this->request->isAjax()) {
|
||
try {
|
||
$param = [
|
||
'series' => input("get.series/d"),
|
||
'device' => input('get.device/d'),
|
||
'category' => input('get.category'),
|
||
'keywords' => input('get.keywords'),
|
||
'page' => input('get.page/d', 1),
|
||
'size' => input('get.size/d', 4),
|
||
];
|
||
$data = Loader::model('Article')
|
||
->field([
|
||
'id',
|
||
'name',
|
||
'description',
|
||
'product_series',
|
||
'content',
|
||
])
|
||
->where('stat', '=', 0)
|
||
->where('country_code', '=', $this->country_code)
|
||
->where(function ($query) use ($param) {
|
||
if (!empty($param['series'])) {
|
||
$query->where(sprintf('find_in_set(%s, `product_series`)', $param['series']));
|
||
}
|
||
|
||
if (!empty($param['keywords'])) {
|
||
$query->where(function ($q) use ($param) {
|
||
$q->where('name', 'like', '%' . $param['keywords'] . '%')
|
||
->whereOr(sprintf('MATCH(content) AGAINST("%s" IN BOOLEAN MODE)', $param['keywords']));
|
||
});
|
||
}
|
||
|
||
$category = [];
|
||
if (!empty($param['device']) && empty($param['category'])) {
|
||
$category = array_merge([$param['device']], array_column($this->getCategoryChildren($param['device']), 'id'));
|
||
}
|
||
if (!empty($param['category'])) {
|
||
$category[] = $param['category'];
|
||
}
|
||
if (!empty($category)) {
|
||
$query->where('cid', 'in', $category);
|
||
}
|
||
})
|
||
->order(['id' => 'desc', 'sort' => 'desc'])
|
||
->paginate($param['size']);
|
||
|
||
// 组装系列型号
|
||
$article = $this->getProductSeriesIdMaps($data->items());
|
||
|
||
return json([
|
||
'code' => 0,
|
||
'data' => [
|
||
'article' => $article,
|
||
'page' => [
|
||
'current_page' => $data->currentPage(),
|
||
'total_page' => $data->lastPage(),
|
||
'list_size' => $data->listRows(),
|
||
'total_size' => $data->total(),
|
||
],
|
||
],
|
||
'message' => 'success',
|
||
]);
|
||
} catch (\Throwable $th) {
|
||
return json([
|
||
'code' => 1,
|
||
'data' => [],
|
||
'message' => $th->getMessage(),
|
||
]);
|
||
}
|
||
}
|
||
|
||
// 型号
|
||
$this->assign('product_series', $this->getProductSeries());
|
||
|
||
// 设备
|
||
$this->assign('category', $this->getCategoryTree(36));
|
||
|
||
// 文章列表
|
||
$article = Loader::model('Article')
|
||
->field([
|
||
'id',
|
||
'name',
|
||
'description',
|
||
'product_series',
|
||
'content',
|
||
])
|
||
->where('stat', '=', 0)
|
||
->where('country_code', '=', $this->country_code)
|
||
->where('cid', 'in', array_merge([38], array_column($this->getCategoryChildren(38), 'id')))
|
||
->order(['id' => 'desc', 'sort' => 'desc'])
|
||
->paginate(4);
|
||
$this->assign('article', $this->getProductSeriesIdMaps($article->items()));
|
||
$this->assign('page', [
|
||
'current_page' => $article->currentPage(),
|
||
'total_page' => $article->lastPage(),
|
||
'list_size' => $article->listRows(),
|
||
'total_size' => $article->total(),
|
||
]);
|
||
|
||
return $this->fetch();
|
||
}
|
||
|
||
// 根据分类获取文章
|
||
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($key, $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);
|
||
}
|
||
// 嵌套子查询,解决union没有limit时排序问题
|
||
$query->table($query->buildSql() . 'a' . $key);
|
||
});
|
||
}
|
||
|
||
$map = [];
|
||
$data = $data = $model->select();
|
||
foreach ($data as $key => $val) {
|
||
$map['cid_' . $val['cid']][] = $val;
|
||
}
|
||
return $map;
|
||
}
|
||
// 帮忙中心
|
||
public function helper()
|
||
{
|
||
// 获取分类
|
||
$categorys = $this->getCategoryTree(76);
|
||
// 获取文章
|
||
$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);
|
||
|
||
$banners = $this->getBanners(126);
|
||
if (!empty($banners)) {
|
||
$banners = $banners['typeid_126']['banners'];
|
||
}
|
||
$this->assign('banners', $banners);
|
||
$this->assign('banners_size', count($banners));
|
||
|
||
return $this->fetch();
|
||
}
|
||
|
||
// 搜索帮助文章
|
||
public function helper_search()
|
||
{
|
||
$base_category = 76;
|
||
$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(76);
|
||
// 获取文章
|
||
$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();
|
||
}
|
||
}
|