You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.6 KiB
119 lines
3.6 KiB
<?php
|
|
|
|
// +----------------------------------------------------------------------
|
|
// | ThinkAdmin
|
|
// +----------------------------------------------------------------------
|
|
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网站: http://demo.thinkadmin.top
|
|
// +----------------------------------------------------------------------
|
|
// | 开源协议 ( https://mit-license.org )
|
|
// +----------------------------------------------------------------------
|
|
// | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
|
|
// | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
|
|
// +----------------------------------------------------------------------
|
|
|
|
use app\admin\service\NodeService;
|
|
use app\admin\service\OplogService;
|
|
use library\File;
|
|
use think\Db;
|
|
use think\facade\Middleware;
|
|
use think\Request;
|
|
|
|
if (!function_exists('auth')) {
|
|
/**
|
|
* 节点访问权限检查
|
|
* @param string $node
|
|
* @return boolean
|
|
* @throws ReflectionException
|
|
*/
|
|
function auth($node)
|
|
{
|
|
return NodeService::checkAuth($node);
|
|
}
|
|
}
|
|
|
|
if (!function_exists('sysdata')) {
|
|
/**
|
|
* JSON 数据读取与存储
|
|
* @param string $name 数据名称
|
|
* @param array|null $value 数据内容
|
|
* @return mixed
|
|
* @throws \think\Exception
|
|
* @throws \think\exception\PDOException
|
|
*/
|
|
function sysdata($name, array $value = null)
|
|
{
|
|
if (is_null($value)) {
|
|
$data = json_decode(Db::name('SystemData')->where(['name' => $name])->value('value'), true);
|
|
return empty($data) ? [] : $data;
|
|
} else {
|
|
return data_save('SystemData', ['name' => $name, 'value' => json_encode($value, JSON_UNESCAPED_UNICODE)], 'name');
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!function_exists('sysoplog')) {
|
|
/**
|
|
* 写入系统日志
|
|
* @param string $action 日志行为
|
|
* @param string $content 日志内容
|
|
* @return boolean
|
|
*/
|
|
function sysoplog($action, $content)
|
|
{
|
|
return OplogService::write($action, $content);
|
|
}
|
|
}
|
|
|
|
if (!function_exists('local_image')) {
|
|
/**
|
|
* 下载远程文件到本地
|
|
* @param string $url 远程图片地址
|
|
* @return string
|
|
*/
|
|
function local_image($url)
|
|
{
|
|
$result = File::down($url);
|
|
if (isset($result['url'])) {
|
|
return $result['url'];
|
|
} else {
|
|
return $url;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!function_exists('base64_image')) {
|
|
/**
|
|
* base64 图片上传接口
|
|
* @param string $content
|
|
* @param string $predir
|
|
* @return string
|
|
*/
|
|
function base64_image($content, $predir = 'base64/')
|
|
{
|
|
try {
|
|
if (preg_match('|^data:image/(.*?);base64,|i', $content)) {
|
|
list($ext, $base) = explode('|||', preg_replace('|^data:image/(.*?);base64,|i', '$1|||', $content));
|
|
$info = File::save($predir . md5($base) . '.' . (empty($ext) ? 'tmp' : $ext), base64_decode($base));
|
|
return $info['url'];
|
|
} else {
|
|
return $content;
|
|
}
|
|
} catch (\Exception $e) {
|
|
return $content;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 访问权限检查中间键
|
|
Middleware::add(function (Request $request, \Closure $next) {
|
|
// 验证访问节点权限
|
|
if (NodeService::forceAuth()) {
|
|
return $next($request);
|
|
} elseif (NodeService::islogin()) {
|
|
return json(['code' => 0, 'msg' => '抱歉,没有访问该操作的权限!']);
|
|
} else {
|
|
return json(['code' => 0, 'msg' => '抱歉,需要登录获取访问权限!', 'url' => url('@admin/login')]);
|
|
}
|
|
});
|
|
|