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

274 lines
9.6 KiB
PHP
Executable File

<?php
namespace app\usmobile\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;
public function __construct() {
parent::__construct();
}
// 初始化
protected function _initialize() {
parent::_initialize();
$this->country_code = 'US';
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();
}
$website_email = (string) Config::get('website_email');
$this->view->assign('website_email', $website_email);
$website_phone = (string) Config::get('website_phone');
$this->view->assign('website_phone', $website_phone);
$business_email = (string) Config::get('business_email');
$this->view->assign('business_email', $business_email);
$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', 'm_icon', 'image']);
$this->cacheTag('ProductCategoryTag')->set('productCategoryList', $this->categoryList);
}
$this->productCategory = $this->list_to_tree($this->categoryList);
// tiaoshi($this->productCategory[0]['child']);die;
//导航
$navigation =self::navInit();
$this->nav_header = $navigation['header'];
$this->nav_footer = $navigation['footer'];
$this->assign('nav_header', $this->nav_header);
$this->assign('nav_footer', $this->nav_footer);
$product_country_code = "products_".strtolower($this->country_code)."_color";
$productColor = config($product_country_code);
$this->view->assign('productColor', $productColor);
$this->view->assign('productCategory', $this->productCategory);
$this->view->assign('allCategoryList', $this->categoryList);
}
//导航初始化
private function navInit(){
// 读取缓存数据
//$header = $this->cacheGet('cache_common_nav_header_key');
// $footer = $this->cacheGet('cache_common_nav_footer_key');
// 导航模型
$field = array('id', 'pid', 'name', 'url', 'value', 'data_type', 'is_new_window_open');
// 缓存没数据则从数据库重新读取,顶部菜单
//if(empty($header))
//{
$headerData = Loader::model('Navigation')->field($field)->where(array('nav_type'=>'header', 'stat'=>0, 'pid'=>0, 'country_code' => COUNTRY_CODE))->order('sort')->select();
$header = self::navDataDealWith($headerData);
if(!empty($header))
{
foreach($header as &$v)
{
$childData = Loader::model('Navigation')->field($field)->where(array('nav_type'=>'header', 'stat'=>0, 'pid'=>$v['id'], 'country_code' => COUNTRY_CODE))->order('sort')->select();
$v['items'] = self::navDataDealWith($childData);
}
}
//$this->cacheSet('cache_common_nav_header_key', $header, 3600);
// }
// 底部导航
//if(empty($footer))
// {
$footerdata = Loader::model('Navigation')->field($field)->where(array('nav_type'=>'footer', 'stat'=>0, 'pid'=>0, 'country_code' => COUNTRY_CODE))->order('sort')->select();
$footer = self::navDataDealWith($footerdata);
if(!empty($footer))
{
foreach($footer as &$v)
{
$childData = Loader::model('Navigation')->field($field)->where(array('nav_type'=>'footer', 'stat'=>0, 'pid'=>$v['id'], 'country_code' => COUNTRY_CODE))->order('sort')->select();
$v['items'] = self::navDataDealWith($childData);
}
}
//$this->cacheSet('cache_common_nav_footer_key', $footer, 3600);
//}
return [
'header' => $header,
'footer' => $footer,
];
}
/**
* [NavDataDealWith 导航数据处理]
* @author martin
* @version 0.0.1
* @datetime 2023-11-23T21:36:46+0800
* @param [array] $data [需要处理的数据]
* @return [array] [处理好的数据]
*/
public function NavDataDealWith($data)
{
if(!empty($data) && is_array($data))
{
foreach($data as $k=>&$v)
{
// url处理
switch($v['data_type'])
{
// 文章分类
case 'article':
$v['url'] = 'article/detail/'.$v['value'].'.html';
break;
// 博客
case 'blog':
$v['url'] = 'blog/detail/id/'.$v['value'].'.html';
break;
// 商品分类
case 'goods_category':
$category = Loader::model('ProductCategory')->getRow(['stat' => 0, 'id' => $v['value'], 'country_code' => $this->country_code], null, ['id' => 'asc']);
if(!empty($category) && $category['pid'] == 0) {
$v['url'] = 'product/catelists/id/'.$v['value'];
}
else{
$v['url'] = 'product/subcatelists/id/'.$v['value'];
}
break;
}
$data[$k] = $v;
}
}
return $data;
}
/**
* 节点遍历
* @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)
{
$expire = 86400 * 30;
if (time() - $curr_time > $expire)
return false;
$temp_p = $this->make_pwd($customer_id, $curr_time);
if ($temp_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);
$customer_info['telephone'] = $customer_info['telephone'] != '' ? substr_replace($customer_info['telephone'], '****', 3, 4) : '';
$customer_info['email'] = $customer_info['email'] != '' ? substr_replace($customer_info['email'], '****', 2, 6) : '';
$customer_info['have_pwd'] = $customer_info['password'] != '' ? 1 : 0;
unset($customer_info['password']);
$customer_info['ct'] = $curr_time;
$customer_info['p'] = $p;
$expire = 86400 * 30;
Cookie::init(['expire' => $expire]);
Cookie::set('c', $customer_info);
}
private function make_pwd($customer_id, $curr_time)
{
$salt = 'Orico2019.';
$p = md5(md5($customer_id . $curr_time . $salt));
return $p;
}
private function check_login()
{
return Cookie::has('c');
}
protected function check_true_login()
{
// 校验用户是否登录,且校验cookie合法性
if (!$this->check_login())
{
return false;
}
$customer_info = json_decode(Cookie::get('c'), true);
$ct = isset($customer_info['ct']) ? $customer_info['ct'] : '';
$p = isset($customer_info['p']) ? $customer_info['p'] : '';
return $this->check_login_token($customer_info['id'], $ct, $p);
}
protected function _logout()
{
if (Cookie::has('c'))
Cookie::delete('c');
$this->customer_id = 0;
}
}