feat: index - 首页及顶部导航处理
This commit is contained in:
210
app/index/controller/Common.php
Normal file
210
app/index/controller/Common.php
Normal file
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\index\controller;
|
||||
|
||||
use app\BaseController;
|
||||
use app\index\model\LanguageModel;
|
||||
use app\index\model\ProductCategoryModel;
|
||||
use app\index\model\ProductModel;
|
||||
use app\index\model\SysConfigModel;
|
||||
use app\index\model\SysNavigationItemModel;
|
||||
use think\facade\Lang;
|
||||
use think\facade\View;
|
||||
|
||||
abstract class Common extends BaseController
|
||||
{
|
||||
// 当前语言id
|
||||
protected $lang_id = 1;
|
||||
|
||||
/**
|
||||
* 控制器初始化
|
||||
* @access public
|
||||
*/
|
||||
public function initialize()
|
||||
{
|
||||
// 获取国家/语言列表
|
||||
$languages = $this->getLanguages();
|
||||
// 输出国家/语言列表
|
||||
View::assign('header_languages', $languages);
|
||||
|
||||
// 获取当前语言
|
||||
$current_language = $this->getCurrentLanguage($languages);
|
||||
if (!empty($current_language)) {
|
||||
$this->lang_id = $current_language['id'];
|
||||
}
|
||||
|
||||
// 获取产品分类
|
||||
$categorys = $this->getProductCategory($this->lang_id);
|
||||
// 输出产品分类
|
||||
View::assign('header_categorys', $categorys);
|
||||
|
||||
// 获取热销产品
|
||||
$hot_products = $this->getHotProducts($this->lang_id);
|
||||
// 输出热销产品
|
||||
View::assign('header_hot_products', $hot_products);
|
||||
|
||||
// 输出顶部导航
|
||||
View::assign('header_navigation', $this->getNavigation('NAV_67f3701f3e831', $this->lang_id));
|
||||
|
||||
// 获取系统配置
|
||||
$configs = $this->getSysConfig($this->lang_id, ['basic', 'contact', 'media']);
|
||||
// 输出系统配置
|
||||
View::assign('basic_config', $configs['basic']);
|
||||
View::assign('contact_config', $configs['contact']);
|
||||
View::assign('media_config', $configs['media']);
|
||||
|
||||
// 获取底部导航
|
||||
$footer_navigation = $this->getNavigation('NAV_67f60be43df8d', $this->lang_id);
|
||||
// 输出底部导航
|
||||
View::assign('footer_navigation', $footer_navigation);
|
||||
}
|
||||
|
||||
// 获取当前语言
|
||||
private function getCurrentLanguage($languages)
|
||||
{
|
||||
$current_code = Lang::getLangSet();
|
||||
foreach ($languages as $item) {
|
||||
if ($item['lang_code'] == $current_code) {
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
// 获取产品分类
|
||||
protected function getProductCategory($language = 1)
|
||||
{
|
||||
$categorys = ProductCategoryModel::field([
|
||||
'id',
|
||||
'pid',
|
||||
'name',
|
||||
'icon',
|
||||
'level'
|
||||
])
|
||||
->language($language)
|
||||
->displayed()
|
||||
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||
->select();
|
||||
if ($categorys->isEmpty()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_to_tree($categorys->toArray(), 0, 'pid', 1, false);
|
||||
}
|
||||
|
||||
// 获取顶部导航
|
||||
protected function getNavigation($unique_label, $language = 1)
|
||||
{
|
||||
$nav = SysNavigationItemModel::hasWhere('navigation', [
|
||||
'unique_label' => $unique_label,
|
||||
'language_id' => $language,
|
||||
'status' => 1
|
||||
])
|
||||
->order(['sort' => 'asc', 'id' => 'asc'])
|
||||
->select();
|
||||
if ($nav->isEmpty()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_to_tree($nav->toArray(), 0, 'pid', 1, false);
|
||||
}
|
||||
|
||||
// 获取热销产品
|
||||
protected function getHotProducts($language = 1, $limit = 3)
|
||||
{
|
||||
$products = ProductModel::field([
|
||||
'id',
|
||||
'name',
|
||||
'cover_image',
|
||||
])
|
||||
->language($language)
|
||||
->onSale(true)
|
||||
->onShelves(true)
|
||||
->hot(true)
|
||||
->limit($limit)
|
||||
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||
->select();
|
||||
if ($products->isEmpty()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $products->toArray();
|
||||
}
|
||||
|
||||
// 获取国家/语言列表
|
||||
protected function getLanguages()
|
||||
{
|
||||
$languages = LanguageModel::alias('l')
|
||||
->field([
|
||||
'l.id',
|
||||
'l.name' => 'lang_name',
|
||||
'l.en_name' => 'lang_en_name',
|
||||
'l.code' => 'lang_code',
|
||||
'l.icon' => 'lang_icon',
|
||||
'l.url' => 'lang_url',
|
||||
'c.id' => 'country_id',
|
||||
'c.name' => 'country_zh_name',
|
||||
'c.en_name' => 'country_en_name',
|
||||
'c.code' => 'country_code',
|
||||
])
|
||||
->join('sys_country c', 'c.id = l.country_id')
|
||||
->where('l.status', '=', 1)
|
||||
->where('c.status', '=', 1)
|
||||
->order(['l.sort' => 'asc', 'l.id' => 'asc'])
|
||||
->select();
|
||||
|
||||
return $languages;
|
||||
}
|
||||
|
||||
// 获取系统联系方式配置
|
||||
protected function getSysConfig($language, $group = [])
|
||||
{
|
||||
$configs = SysConfigModel::alias('c')
|
||||
->field([
|
||||
'c.id',
|
||||
'c.title',
|
||||
'c.name',
|
||||
'c.value',
|
||||
'c.extra',
|
||||
'c.type',
|
||||
'g.unique_label'
|
||||
])
|
||||
->join('sys_config_group g', 'g.id = c.group_id')
|
||||
->where('g.unique_label', 'in', $group)
|
||||
->where('g.language_id', '=', $language)
|
||||
->order(['c.sort' => 'asc', 'c.id' => 'desc'])
|
||||
->select();
|
||||
if ($configs->isEmpty()) {
|
||||
return [];
|
||||
}
|
||||
$configs = $configs->toArray();
|
||||
$data = [];
|
||||
foreach ($configs as $cfg) {
|
||||
$current = &$data;
|
||||
if (!isset($current[$cfg['unique_label']])) {
|
||||
$current[$cfg['unique_label']] = [];
|
||||
}
|
||||
$current = &$current[$cfg['unique_label']];
|
||||
|
||||
// 根据name中"."拆分为多维数组
|
||||
$parts = explode('.', $cfg['name']);
|
||||
foreach ($parts as $part) {
|
||||
if (!isset($current[$part])) {
|
||||
$current[$part] = [];
|
||||
}
|
||||
$current = &$current[$part];
|
||||
}
|
||||
$current = [
|
||||
'title' => $cfg['title'],
|
||||
'type' => $cfg['type'],
|
||||
'value' => $cfg['value'],
|
||||
'extra' => $cfg['extra'],
|
||||
];
|
||||
}
|
||||
unset($current);
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user