refactor: 修改nas移动端帮助中心
This commit is contained in:
@@ -147,6 +147,7 @@ class TopsNas extends BaseController
|
||||
'id',
|
||||
'pid',
|
||||
'name',
|
||||
'picture'
|
||||
])
|
||||
->where('isshow', '=', 1)
|
||||
->where('country_code', '=', $this->country_code)
|
||||
@@ -263,4 +264,171 @@ class TopsNas extends BaseController
|
||||
|
||||
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($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(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(127);
|
||||
if (!empty($banners)) {
|
||||
$banners = $banners['typeid_127']['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'));
|
||||
}
|
||||
|
||||
return $this->fetch();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user