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

169 lines
5.3 KiB
PHP
Executable File

<?php
namespace app\index\controller;
use think\Lang;
use think\Loader;
use think\Config;
use think\Session;
use think\Cookie;
use app\common\controller\BaseController as Controller;
//<!--#include file="([0-9a-zA-Z/._-]+?)\.html" -->
class BaseController extends Controller {
//当前用户
protected $customer_id = 0;
# 当前国家编码
protected $country_code = 'ZH';
public function __construct() {
parent::__construct();
}
// 初始化
protected function _initialize() {
parent::_initialize();
if ($this->check_true_login())
{
$customer_info = json_decode(Cookie::get('c'), true);
$this->view->assign('customer_info', $customer_info);
$this->customer_id = $customer_info['id'];
$this->customer_info = $customer_info;
}
else
{
$this->_logout();
}
$this->view->assign('seo_title', (string) Config::get('website_seo_title'));
$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('ProductCategory')->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', 'unique_id']);
$this->cacheTag('ProductCategoryTag')->set('productCategoryList', $this->categoryList);
}
$this->productCategory = $this->list_to_tree($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);
}
$this->view->assign('country_list', $this->country_list);
$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) {
//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;
}
private function check_login_token($customer_id, $curr_time, $p)
{
// 校验登录的合法性
$tmp_p = $this->make_pwd($customer_id, $curr_time);
if ($tmp_p !== $p)
{
return false;
}
return true;
}
protected function set_login_token($customer_info)
{
$curr_time = time();
$p = $this->make_pwd($customer_info['id'], $curr_time);
$expire = 86400 * 30;
Cookie::init(['expire' => $expire]);
Cookie::set('p', $p);
Cookie::set('c', $customer_info);
Cookie::set('ct', $curr_time);
return;
}
private function make_pwd($customer_id, $curr_time)
{
$salt = 'Orico2019.';
$p = md5(md5($customer_id . $curr_time . $salt));
return $p;
}
private function check_login()
{
// 有cookie默认为已登录, 不校验cookie的合法性
return Cookie::has('p') && Cookie::has('c') && Cookie::has('ct');
}
protected function check_true_login()
{
// 校验用户是否登录,且校验cookie合法性
if (!$this->check_login())
{
$this->_logout();
return false;
}
$customer_info = json_decode(Cookie::get('c'), true);
$curr_time = Cookie::get('ct');
$p = Cookie::get('p');
return $this->check_login_token($customer_info['id'], $curr_time, $p);
}
protected function _logout()
{
if (Cookie::has('p'))
Cookie::delete('p');
if (Cookie::has('c'))
Cookie::delete('c');
if (Cookie::has('ct'))
Cookie::delete('ct');
$this->customer_id = 0;
return;
}
}