init
This commit is contained in:
163
app/admin/common.php
Executable file
163
app/admin/common.php
Executable file
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
use think\Request;
|
||||
use think\Loader;
|
||||
|
||||
/**
|
||||
* 记录日志
|
||||
*/
|
||||
function action_log($title = '日志名称', $content = null, $user_id = 0, $param = array()) {
|
||||
$request = Request::instance();
|
||||
//参数检查
|
||||
if (empty($title) || empty($content)) {
|
||||
return '参数不能为空';
|
||||
}
|
||||
$user_id = $user_id > 0 ? $user_id : (isset($user_id) ? 0 : is_session_login('user'));
|
||||
// if ($user_id == 1) {
|
||||
// return 'debug';
|
||||
// }
|
||||
//插入日志
|
||||
$data['title'] = $title;
|
||||
$data['user_id'] = $user_id;
|
||||
$data['ip'] = $request->ip();
|
||||
$data['url'] = $request->url(true);
|
||||
$data['module'] = $request->module();
|
||||
$data['controller'] = $request->controller();
|
||||
$data['action'] = $request->action();
|
||||
$data['param'] = empty($param) ? serialize($request->param()) : serialize($param);
|
||||
$data['content'] = is_array($content) ? serialize($content) : $content;
|
||||
$data['addtime'] = time();
|
||||
Loader::model('ActionLog')->data($data)->save();
|
||||
}
|
||||
|
||||
//获取指定模块中的控制器名
|
||||
function get_ctrl_names($module, $retrun = 'string') {
|
||||
$ctrlPath = APP_PATH . ltrim($module, '/') . '/controller';
|
||||
$ctrlOption = '';
|
||||
$ctrlArray = [];
|
||||
if (is_dir($ctrlPath)) {
|
||||
$dirResource = opendir($ctrlPath);
|
||||
while ($dir = readdir($dirResource)) {
|
||||
if (!in_array($dir, array('.', '..', '.svn'))) {
|
||||
$ctrlArray[] = $val = basename($dir, '.php');
|
||||
$ctrlOption .= "<option value='" . $val . "'>" . $val . "</option>";
|
||||
}
|
||||
}
|
||||
closedir($dirResource);
|
||||
}
|
||||
|
||||
if ($retrun == 'string') {
|
||||
return $ctrlOption;
|
||||
} else {
|
||||
return $ctrlArray;
|
||||
}
|
||||
}
|
||||
|
||||
//获取指定控制器中的动作名
|
||||
function get_action_names($controller, $namespace = '', $retrun = 'string') {
|
||||
$class_name = is_object($controller) ? $controller : $namespace . str_replace('.php', '', $controller);
|
||||
$current_actions = get_class_methods($class_name);
|
||||
if (!isset($current_actions)) {
|
||||
return '指定的控制器无效';
|
||||
}
|
||||
|
||||
$parent_class_name = get_parent_class($class_name);
|
||||
$parent_actions = $parent_class_name ? get_class_methods($parent_class_name) : ['__construct', '_initialize'];
|
||||
$actionArray = array_diff($current_actions, $parent_actions);
|
||||
$actionOption = '';
|
||||
foreach ($actionArray as $val) {
|
||||
$actionOption .= "<option value='" . $val . "'>" . $val . "</option>";
|
||||
}
|
||||
if ($retrun == 'string') {
|
||||
return $actionOption;
|
||||
} else {
|
||||
return $actionArray;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('is_administrator')) {
|
||||
|
||||
/**
|
||||
* 检测当前用户是否为管理员
|
||||
* @param null $uid
|
||||
* @return bool true-管理员,false-非管理员
|
||||
*/
|
||||
function is_administrator($uid = null) {
|
||||
$user_id = !empty($uid) ? $uid : is_session_login('user');
|
||||
return is_int($user_id) && ((int) $user_id === (int) config('user_administrator'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 分析枚举类型配置值
|
||||
* 格式 a:名称1,b:名称2
|
||||
* @param $string 配置值
|
||||
* @return array
|
||||
*/
|
||||
function parse_config_attr($string) {
|
||||
$array = preg_split('/[,;\r\n]+/', trim($string, ",;\r\n"));
|
||||
if (strpos($string, ':')) {
|
||||
$value = [];
|
||||
foreach ($array as $val) {
|
||||
list($k, $v) = explode(':', $val);
|
||||
$value[$k] = $v;
|
||||
}
|
||||
} else {
|
||||
$value = $array;
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
function editor($selector = '#content', $type = 'string') {
|
||||
$rootDir = request()->root();
|
||||
if (is_array($selector)) {
|
||||
$KindEditor = "\n";
|
||||
foreach ($selector as $s) {
|
||||
$KindEditor .="KindEditor.ready(function(K) {K.create('{$s}', {allowFileManager: true});});\n";
|
||||
}
|
||||
} else {
|
||||
$KindEditor = "KindEditor.ready(function(K) {K.create('{$selector}', {allowFileManager: true});});";
|
||||
}
|
||||
$editor = <<<EDITOR
|
||||
<link rel="stylesheet" href="{$rootDir}/backend/adminlte/kindeditor/themes/default/default.min.css" />
|
||||
<link rel="stylesheet" href="{$rootDir}/backend/adminlte/kindeditor/themes/default/custom.css" />
|
||||
<script charset="utf-8" src="{$rootDir}/backend/adminlte/kindeditor/kindeditor-min.js"></script>
|
||||
<script type="text/javascript">
|
||||
$KindEditor
|
||||
</script>
|
||||
EDITOR;
|
||||
return $editor;
|
||||
}
|
||||
/**
|
||||
* 国家选择
|
||||
* @cit $string 国家
|
||||
* @view $string 表
|
||||
* */
|
||||
function city($cit = '',$view = ''){
|
||||
$cit = strtolower($cit);
|
||||
switch ($cit){
|
||||
case '':
|
||||
return Loader::model($view);
|
||||
break;
|
||||
case 'zh':
|
||||
return Loader::model($view);
|
||||
break;
|
||||
case 'en':
|
||||
return Loader::model($view.'En');
|
||||
break;
|
||||
case 'us':
|
||||
return Loader::model($view.'Us');
|
||||
break;
|
||||
case 'id':
|
||||
return Loader::model($view.'Id');
|
||||
break;
|
||||
case 'th':
|
||||
return Loader::model($view.'Th');
|
||||
break;
|
||||
case 'vn':
|
||||
return Loader::model($view.'Vn');
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user