TaoLer/app/common/controller/BaseController.php

145 lines
3.6 KiB
PHP
Raw Normal View History

2020-01-01 13:17:19 +08:00
<?php
2022-08-02 21:13:36 +08:00
/*
* @Author: TaoLer <alipay_tao@qq.com>
* @Date: 2021-12-06 16:04:50
2022-08-16 15:29:04 +08:00
* @LastEditTime: 2022-08-15 13:30:05
2022-08-02 21:13:36 +08:00
* @LastEditors: TaoLer
* @Description: 前端基础控制器设置
2022-08-16 15:29:04 +08:00
* @FilePath: \TaoLer\app\common\controller\BaseController.php
2022-08-02 21:13:36 +08:00
* Copyright (c) 2020~2022 https://www.aieok.com All rights reserved.
*/
2020-01-01 13:17:19 +08:00
declare (strict_types = 1);
namespace app\common\controller;
2022-08-02 21:13:36 +08:00
use think\facade\Request;
2020-01-01 13:17:19 +08:00
use think\facade\View;
use think\facade\Db;
use think\facade\Session;
use think\facade\Cache;
2021-07-16 17:42:07 +08:00
use app\BaseController as BaseCtrl;
2020-01-01 13:17:19 +08:00
/**
* 控制器基础类
*/
2021-07-16 17:42:07 +08:00
class BaseController extends BaseCtrl
2020-01-01 13:17:19 +08:00
{
2023-03-16 22:54:11 +08:00
protected $uid = '';
2021-12-15 15:46:04 +08:00
/**
* 初始化系统,导航,用户
*/
2020-01-01 13:17:19 +08:00
protected function initialize()
{
2021-07-16 17:42:07 +08:00
$this->uid = Session::get('user_id');
2020-01-01 13:17:19 +08:00
//系统配置
$this->showSystem();
2022-09-07 15:24:25 +08:00
//变量赋给模板
View::assign([
//显示子分类导航
'subcatelist' => $this->showSubnav(),
//当前登录用户
'user' => $this->showUser($this->uid),
]);
2020-01-01 13:17:19 +08:00
2021-07-16 17:42:07 +08:00
}
2020-01-01 13:17:19 +08:00
//判断是否已登录?
protected function isLogged()
{
if(Session::has('user_id')){
$this->success('您已登录','/index/index/index');
}
}
//判断是否需要登录?
protected function isLogin()
{
if(!Session::has('user_id')){
$this->error('请登录','/index/user/login');
}
}
2022-09-07 15:24:25 +08:00
// 显示子导航subnav
protected function showSubnav()
{
//1.查询父分类id
$pCate = Db::name('cate')->field('id,pid,ename,catename,is_hot')->where(['ename'=>input('ename'),'status'=>1,'delete_time'=>0])->find();
2023-05-05 12:09:57 +08:00
2022-09-07 15:24:25 +08:00
if(empty($pCate)) { // 没有点击任何分类,点击首页获取全部分类信息
2023-05-05 12:09:57 +08:00
$subCateList = [];
2022-09-07 15:24:25 +08:00
} else { // 点击分类,获取子分类信息
$parentId = $pCate['id'];
2022-11-18 10:31:44 +08:00
$subCate = Db::name('cate')->field('id,ename,catename,is_hot,pid')->where(['pid'=>$parentId,'status'=>1,'delete_time'=>0])->select()->toArray();
2022-09-07 15:24:25 +08:00
if(!empty($subCate)) { // 有子分类
$subCateList = array2tree($subCate);
} else { //无子分类
if($pCate['pid'] == 0) {
//一级菜单
$subCateList[] = $pCate;
} else {
//子菜单下如果无子菜单,则显示全部兄弟分类
$parament = Db::name('cate')->field('id,ename,catename,is_hot,pid')->where(['pid'=>$pCate['pid'],'status'=>1,'delete_time'=>0])->order(['sort' => 'asc'])->select()->toArray();
$subCateList = array2tree($parament);
}
}
}
return $subCateList;
2020-01-01 13:17:19 +08:00
}
2020-05-15 17:04:04 +08:00
//显示当前登录用户
2021-07-22 10:54:03 +08:00
protected function showUser($id)
2020-01-01 13:17:19 +08:00
{
2020-05-15 17:04:04 +08:00
$user = Cache::get('user'.$id);
if(!$user){
//1.查询用户
$user = Db::name('user')->field('id,name,nickname,user_img,sex,area_id,auth,city,email,active,sign,point,vip,create_time')->find($id);
2020-05-15 17:04:04 +08:00
Cache::tag('user')->set('user'.$id,$user,600);
}
2021-07-22 10:54:03 +08:00
return $user;
2020-01-01 13:17:19 +08:00
}
2022-08-02 21:13:36 +08:00
//热门标签
protected function getHotTag()
{
//热门标签
2022-08-16 15:29:04 +08:00
//return Article::getHotTags();
2022-08-02 21:13:36 +08:00
//转换为字符串
// $tagStr = implode(",",$tags);
//转换为数组并去重
// return array_unique(explode(",",$tagStr));
2022-08-16 15:29:04 +08:00
$allTags = Db::name('tag')->field('name,ename')->select();
$tagHot = [];
foreach($allTags as $v) {
$tagHot[] = ['name'=>$v['name'],'url'=> (string) url('tag_list',['ename'=>$v['ename']])];
}
return $tagHot;
2022-08-02 21:13:36 +08:00
}
2020-01-01 13:17:19 +08:00
2022-08-02 21:13:36 +08:00
//显示网站设置
2020-01-01 13:17:19 +08:00
protected function showSystem()
{
//1.查询分类表获取所有分类
2022-08-02 21:13:36 +08:00
$sysInfo = $this->getSystem();
//获取热门标签
$hotTag = $this->getHotTag();
$assign = [
'sysInfo' => $sysInfo,
'hotTag' => $hotTag,
'host' => Request::domain() . '/'
];
View::assign($assign);
2021-08-03 17:33:48 +08:00
return $sysInfo;
2020-01-01 13:17:19 +08:00
}
}