2025-06-07 09:15:14 +08:00

145 lines
3.7 KiB
PHP

<?php
/**
* 商业使用授权协议
*
* Copyright (c) 2025 [云泽网]. 保留所有权利.
*
* 本软件仅供评估使用。任何商业用途必须获得书面授权许可。
* 未经授权商业使用本软件属于侵权行为,将承担法律责任。
*
* 授权购买请联系: 357099073@qq.com
* 官方网站: https://www.yunzer.cn
*
* 评估用户须知:
* 1. 禁止移除版权声明
* 2. 禁止用于生产环境
* 3. 禁止转售或分发
*/
/**
* 前台系统-基础控制器
*/
namespace app\index\controller;
use app\AppApi;
use think\facade\Db;
use think\facade\View;
use think\facade\Cookie;
use think\facade\Config;
use think\exception\HttpResponseException;
use think\facade\Request;
use app\index\model\User;
class Base
{
public $config = [];
public $userId = null;
public $user = [];
public function __construct()
{
date_default_timezone_set('PRC');
# 获取配置
$this->config = Db::table('yz_admin_config')->select()->toArray();
// 将配置数据转换为键值对形式
$configData = [];
foreach ($this->config as $item) {
// 使用正确的字段名 config_name 和 config_value
if (isset($item['config_name']) && isset($item['config_value'])) {
$configData[$item['config_name']] = $item['config_value'];
}
}
$this->config = $configData;
# 获取用户信息
$this->userId = Cookie::get('user_id');
if (!empty($this->userId)) {
$this->user = User::where('uid', $this->userId)->find();
}
View::assign([
'user' => $this->user,
'config' => $this->config
]);
}
/**
* 返回json对象
*/
protected function returnCode($code, $data = [], $count = 10)
{
header('Content-type:application/json');
if ($code == 0) {
$arr = array(
'code' => $code,
'msg' => '成功',
'count' => $count,
'data' => $data
);
} else if ($code >= 1 && $code <= 100) {
$arr = array(
'code' => $code,
'msg' => $data
);
} else {
$appapi = new AppApi();
$arr = array(
'code' => $code,
'msg' => $appapi::errorTip($code)
);
}
echo json_encode($arr);
if ($code != 0) {
exit;
}
}
/**
* 操作成功跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @return void
*/
protected function success($msg = '')
{
$result = [
'code' => 1,
'msg' => $msg
];
$type = $this->getResponseType();
if ($type == 'html') {
$response = view(Config::get('app.dispatch_success_tmpl'), $result);
} else if ($type == 'json') {
$response = json($result);
}
throw new HttpResponseException($response);
}
/**
* 操作错误跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @return void
*/
protected function error($msg = '')
{
$result = [
'code' => 0,
'msg' => $msg
];
$response = view(Config::get('app.dispatch_error_tmpl'), $result);
throw new HttpResponseException($response);
}
/**
* 获取当前的response 输出类型
* @access protected
* @return string
*/
protected function getResponseType()
{
return Request::isJson() || Request::isAjax() ? 'json' : 'html';
}
}