Files
orico-official-website-old/app/id/controller/BaseController.php
2024-10-29 14:04:59 +08:00

90 lines
3.5 KiB
PHP
Executable File

<?php
namespace app\id\controller;
use think\Lang;
use think\Loader;
use think\Config;
use think\Session;
use app\common\controller\BaseController as Controller;
//<!--#include file="([0-9a-zA-Z/._-]+?)\.html" -->
class BaseController extends Controller {
//当前用户
protected $customer_id = 0;
public function __construct() {
parent::__construct();
}
// 初始化
protected function _initialize() {
parent::_initialize();
$this->country_code = 'ID';
$this->customer_id = is_session_login('customer');
$this->current_customer = Session::get('customer_auth');
$this->view->assign('customer_id', $this->customer_id);
$this->view->assign('current_customer', $this->current_customer);
$this->view->assign('seo_title', (string) Config::get('website_seo_title_us'));
$this->view->assign('seo_keyword', (string) Config::get('website_seo_keyword'));
$this->view->assign('seo_description', (string) Config::get('website_seo_description'));
//$this->categoryList = $this->cacheGet('productCategoryList');
if (empty($this->productCategory)) {
$this->categoryList = Loader::model('product_category')->getList(['stat' => 0, 'siteid' => $this->siteid,'isshow'=>1, 'country_code' => $this->country_code], ['sort' => 'asc', 'id' => 'asc'], ['id', 'pid', 'haschild', 'name', 'shortname', 'sort', 'description', 'isshow', 'recommend', 'picture', 'icon', 'image', 'm_icon', 'unique_id']);
$this->cacheTag('ProductCategoryTag')->set('productCategoryList', $this->categoryList);
}
if ($this->cacheHas('country_list')) {
$this->country_list = $this->cacheGet('country_list');
} else {
$this->country_list = model('country')->where(['stat' => 0])->order(['sort' => 'asc'])->select();
$this->cacheSet('country_list', $this->country_list, 3600);
}
// tiaoshi($this->country_list);die;
$this->view->assign('country_list', $this->country_list);
$this->productCategory = $this->list_to_tree($this->categoryList);
$this->view->assign('productCategory', $this->productCategory);
$this->view->assign('allCategoryList', $this->categoryList);
}
/**
* 节点遍历
* @param $list
* @param string $pk
* @param string $pid
* @param string $child
* @param int $root
* return array
*/
protected function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = 'child', $root = 0) {
//dump($root);die;
//header('content-type:text/html;charset=utf-8;');
// 创建Tree
$tree = [];
if (is_array($list)) {
// 创建基于主键的数组引用
$refer = [];
foreach ($list as $key => $data) {
$list[$key] = $data->toArray();
$refer[$data[$pk]] = & $list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] = & $list[$key];
} else {
if (isset($refer[$parentId])) {
$parent = & $refer[$parentId];
$parent[$child][] = & $list[$key];
}
}
}
}
return $tree;
}
}