梵音定版初始化
This commit is contained in:
462
application/admin/controller/Addon.php
Normal file
462
application/admin/controller/Addon.php
Normal file
@@ -0,0 +1,462 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use fast\Http;
|
||||
use think\addons\AddonException;
|
||||
use think\addons\Service;
|
||||
use think\Cache;
|
||||
use think\Config;
|
||||
use think\Db;
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
* 插件管理
|
||||
*
|
||||
* @icon fa fa-cube
|
||||
* @remark 可在线安装、卸载、禁用、启用、配置、升级插件,插件升级前请做好备份。
|
||||
*/
|
||||
class Addon extends Backend
|
||||
{
|
||||
protected $model = null;
|
||||
protected $noNeedRight = ['get_table_list'];
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
if (!$this->auth->isSuperAdmin() && in_array($this->request->action(), ['install', 'uninstall', 'local', 'upgrade', 'authorization', 'testdata'])) {
|
||||
$this->error(__('Access is allowed only to the super management group'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 插件列表
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$addons = get_addon_list();
|
||||
foreach ($addons as $k => &$v) {
|
||||
$config = get_addon_config($v['name']);
|
||||
$v['config'] = $config ? 1 : 0;
|
||||
$v['url'] = str_replace($this->request->server('SCRIPT_NAME'), '', $v['url']);
|
||||
}
|
||||
$this->assignconfig(['addons' => $addons, 'api_url' => config('fastadmin.api_url'), 'faversion' => config('fastadmin.version'), 'domain' => request()->host(true)]);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置
|
||||
*/
|
||||
public function config($name = null)
|
||||
{
|
||||
$name = $name ? $name : $this->request->get("name");
|
||||
if (!$name) {
|
||||
$this->error(__('Parameter %s can not be empty', 'name'));
|
||||
}
|
||||
if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
|
||||
$this->error(__('Addon name incorrect'));
|
||||
}
|
||||
$info = get_addon_info($name);
|
||||
$config = get_addon_fullconfig($name);
|
||||
if (!$info) {
|
||||
$this->error(__('Addon not exists'));
|
||||
}
|
||||
if ($this->request->isPost()) {
|
||||
$params = $this->request->post("row/a", [], 'trim');
|
||||
if ($params) {
|
||||
foreach ($config as $k => &$v) {
|
||||
if (isset($params[$v['name']])) {
|
||||
if ($v['type'] == 'array') {
|
||||
$params[$v['name']] = is_array($params[$v['name']]) ? $params[$v['name']] : (array)json_decode($params[$v['name']], true);
|
||||
$value = $params[$v['name']];
|
||||
} else {
|
||||
$value = is_array($params[$v['name']]) ? implode(',', $params[$v['name']]) : $params[$v['name']];
|
||||
}
|
||||
$v['value'] = $value;
|
||||
}
|
||||
}
|
||||
try {
|
||||
$addon = get_addon_instance($name);
|
||||
//插件自定义配置实现逻辑
|
||||
if (method_exists($addon, 'config')) {
|
||||
$addon->config($name, $config);
|
||||
} else {
|
||||
//更新配置文件
|
||||
set_addon_fullconfig($name, $config);
|
||||
Service::refresh();
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$this->error(__($e->getMessage()));
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$tips = [];
|
||||
$groupList = [];
|
||||
$ungroupList = [];
|
||||
foreach ($config as $index => &$item) {
|
||||
//如果有设置分组
|
||||
if (isset($item['group']) && $item['group']) {
|
||||
if (!in_array($item['group'], $groupList)) {
|
||||
$groupList["custom" . (count($groupList) + 1)] = $item['group'];
|
||||
}
|
||||
} elseif ($item['name'] != '__tips__') {
|
||||
$ungroupList[] = $item['name'];
|
||||
}
|
||||
if ($item['name'] == '__tips__') {
|
||||
$tips = $item;
|
||||
unset($config[$index]);
|
||||
}
|
||||
}
|
||||
if ($ungroupList) {
|
||||
$groupList['other'] = '其它';
|
||||
}
|
||||
$this->view->assign("groupList", $groupList);
|
||||
$this->view->assign("addon", ['info' => $info, 'config' => $config, 'tips' => $tips]);
|
||||
$configFile = ADDON_PATH . $name . DS . 'config.html';
|
||||
$viewFile = is_file($configFile) ? $configFile : '';
|
||||
return $this->view->fetch($viewFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 安装
|
||||
*/
|
||||
public function install()
|
||||
{
|
||||
$name = $this->request->post("name");
|
||||
$force = (int)$this->request->post("force");
|
||||
if (!$name) {
|
||||
$this->error(__('Parameter %s can not be empty', 'name'));
|
||||
}
|
||||
if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
|
||||
$this->error(__('Addon name incorrect'));
|
||||
}
|
||||
|
||||
$info = [];
|
||||
try {
|
||||
$uid = $this->request->post("uid");
|
||||
$token = $this->request->post("token");
|
||||
$version = $this->request->post("version");
|
||||
$faversion = $this->request->post("faversion");
|
||||
$extend = [
|
||||
'uid' => $uid,
|
||||
'token' => $token,
|
||||
'version' => $version,
|
||||
'faversion' => $faversion
|
||||
];
|
||||
$info = Service::install($name, $force, $extend);
|
||||
} catch (AddonException $e) {
|
||||
$this->result($e->getData(), $e->getCode(), __($e->getMessage()));
|
||||
} catch (Exception $e) {
|
||||
$this->error(__($e->getMessage()), $e->getCode());
|
||||
}
|
||||
$this->success(__('Install successful'), '', ['addon' => $info]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 卸载
|
||||
*/
|
||||
public function uninstall()
|
||||
{
|
||||
$name = $this->request->post("name");
|
||||
$force = (int)$this->request->post("force");
|
||||
$droptables = (int)$this->request->post("droptables");
|
||||
if (!$name) {
|
||||
$this->error(__('Parameter %s can not be empty', 'name'));
|
||||
}
|
||||
if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
|
||||
$this->error(__('Addon name incorrect'));
|
||||
}
|
||||
//只有开启调试且为超级管理员才允许删除相关数据库
|
||||
$tables = [];
|
||||
if ($droptables && Config::get("app_debug") && $this->auth->isSuperAdmin()) {
|
||||
$tables = get_addon_tables($name);
|
||||
}
|
||||
try {
|
||||
Service::uninstall($name, $force);
|
||||
if ($tables) {
|
||||
$prefix = Config::get('database.prefix');
|
||||
//删除插件关联表
|
||||
foreach ($tables as $index => $table) {
|
||||
//忽略非插件标识的表名
|
||||
if (!preg_match("/^{$prefix}{$name}/", $table)) {
|
||||
continue;
|
||||
}
|
||||
Db::execute("DROP TABLE IF EXISTS `{$table}`");
|
||||
}
|
||||
}
|
||||
} catch (AddonException $e) {
|
||||
$this->result($e->getData(), $e->getCode(), __($e->getMessage()));
|
||||
} catch (Exception $e) {
|
||||
$this->error(__($e->getMessage()));
|
||||
}
|
||||
$this->success(__('Uninstall successful'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用启用
|
||||
*/
|
||||
public function state()
|
||||
{
|
||||
$name = $this->request->post("name");
|
||||
$action = $this->request->post("action");
|
||||
$force = (int)$this->request->post("force");
|
||||
if (!$name) {
|
||||
$this->error(__('Parameter %s can not be empty', 'name'));
|
||||
}
|
||||
if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
|
||||
$this->error(__('Addon name incorrect'));
|
||||
}
|
||||
try {
|
||||
$action = $action == 'enable' ? $action : 'disable';
|
||||
//调用启用、禁用的方法
|
||||
Service::$action($name, $force);
|
||||
Cache::rm('__menu__');
|
||||
} catch (AddonException $e) {
|
||||
$this->result($e->getData(), $e->getCode(), __($e->getMessage()));
|
||||
} catch (Exception $e) {
|
||||
$this->error(__($e->getMessage()));
|
||||
}
|
||||
$this->success(__('Operate successful'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 本地上传
|
||||
*/
|
||||
public function local()
|
||||
{
|
||||
Config::set('default_return_type', 'json');
|
||||
|
||||
$info = [];
|
||||
$file = $this->request->file('file');
|
||||
try {
|
||||
$uid = $this->request->post("uid");
|
||||
$token = $this->request->post("token");
|
||||
$faversion = $this->request->post("faversion");
|
||||
$force = $this->request->post("force");
|
||||
if (!$uid || !$token) {
|
||||
throw new Exception(__('Please login and try to install'));
|
||||
}
|
||||
$extend = [
|
||||
'uid' => $uid,
|
||||
'token' => $token,
|
||||
'faversion' => $faversion
|
||||
];
|
||||
$info = Service::local($file, $extend, $force);
|
||||
} catch (AddonException $e) {
|
||||
$this->result($e->getData(), $e->getCode(), __($e->getMessage()));
|
||||
} catch (Exception $e) {
|
||||
$this->error(__($e->getMessage()));
|
||||
}
|
||||
$this->success(__('Offline installed tips'), '', ['addon' => $info]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新插件
|
||||
*/
|
||||
public function upgrade()
|
||||
{
|
||||
$name = $this->request->post("name");
|
||||
$addonTmpDir = RUNTIME_PATH . 'addons' . DS;
|
||||
if (!$name) {
|
||||
$this->error(__('Parameter %s can not be empty', 'name'));
|
||||
}
|
||||
if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
|
||||
$this->error(__('Addon name incorrect'));
|
||||
}
|
||||
if (!is_dir($addonTmpDir)) {
|
||||
@mkdir($addonTmpDir, 0755, true);
|
||||
}
|
||||
|
||||
$info = [];
|
||||
try {
|
||||
$info = get_addon_info($name);
|
||||
$uid = $this->request->post("uid");
|
||||
$token = $this->request->post("token");
|
||||
$version = $this->request->post("version");
|
||||
$faversion = $this->request->post("faversion");
|
||||
$extend = [
|
||||
'uid' => $uid,
|
||||
'token' => $token,
|
||||
'version' => $version,
|
||||
'oldversion' => $info['version'] ?? '',
|
||||
'faversion' => $faversion
|
||||
];
|
||||
//调用更新的方法
|
||||
$info = Service::upgrade($name, $extend);
|
||||
Cache::rm('__menu__');
|
||||
} catch (AddonException $e) {
|
||||
$this->result($e->getData(), $e->getCode(), __($e->getMessage()));
|
||||
} catch (Exception $e) {
|
||||
$this->error(__($e->getMessage()));
|
||||
}
|
||||
$this->success(__('Operate successful'), '', ['addon' => $info]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试数据
|
||||
*/
|
||||
public function testdata()
|
||||
{
|
||||
$name = $this->request->post("name");
|
||||
if (!$name) {
|
||||
$this->error(__('Parameter %s can not be empty', 'name'));
|
||||
}
|
||||
if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
|
||||
$this->error(__('Addon name incorrect'));
|
||||
}
|
||||
|
||||
try {
|
||||
Service::importsql($name, 'testdata.sql');
|
||||
} catch (AddonException $e) {
|
||||
$this->result($e->getData(), $e->getCode(), __($e->getMessage()));
|
||||
} catch (Exception $e) {
|
||||
$this->error(__($e->getMessage()), $e->getCode());
|
||||
}
|
||||
$this->success(__('Import successful'), '');
|
||||
}
|
||||
|
||||
/**
|
||||
* 已装插件
|
||||
*/
|
||||
public function downloaded()
|
||||
{
|
||||
$offset = (int)$this->request->get("offset");
|
||||
$limit = (int)$this->request->get("limit");
|
||||
$filter = $this->request->get("filter", '');
|
||||
$search = $this->request->get("search", '', 'strip_tags,htmlspecialchars');
|
||||
$onlineaddons = $this->getAddonList();
|
||||
$filter = (array)json_decode($filter, true);
|
||||
$addons = get_addon_list();
|
||||
$list = [];
|
||||
foreach ($addons as $k => $v) {
|
||||
if ($search && stripos($v['name'], $search) === false && stripos($v['title'], $search) === false && stripos($v['intro'], $search) === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($onlineaddons[$v['name']])) {
|
||||
$v = array_merge($v, $onlineaddons[$v['name']]);
|
||||
$v['price'] = '-';
|
||||
} else {
|
||||
$v['category_id'] = 0;
|
||||
$v['flag'] = '';
|
||||
$v['banner'] = '';
|
||||
$v['image'] = '';
|
||||
$v['demourl'] = '';
|
||||
$v['price'] = __('None');
|
||||
$v['screenshots'] = [];
|
||||
$v['releaselist'] = [];
|
||||
$v['url'] = addon_url($v['name']);
|
||||
$v['url'] = str_replace($this->request->server('SCRIPT_NAME'), '', $v['url']);
|
||||
}
|
||||
$v['createtime'] = filemtime(ADDON_PATH . $v['name']);
|
||||
if ($filter && isset($filter['category_id']) && is_numeric($filter['category_id']) && $filter['category_id'] != $v['category_id']) {
|
||||
continue;
|
||||
}
|
||||
$list[] = $v;
|
||||
}
|
||||
$total = count($list);
|
||||
if ($limit) {
|
||||
$list = array_slice($list, $offset, $limit);
|
||||
}
|
||||
$result = array("total" => $total, "rows" => $list);
|
||||
|
||||
$callback = $this->request->get('callback') ? "jsonp" : "json";
|
||||
return $callback($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测
|
||||
*/
|
||||
public function isbuy()
|
||||
{
|
||||
$name = $this->request->post("name");
|
||||
$uid = $this->request->post("uid");
|
||||
$token = $this->request->post("token");
|
||||
$version = $this->request->post("version");
|
||||
$faversion = $this->request->post("faversion");
|
||||
$extend = [
|
||||
'uid' => $uid,
|
||||
'token' => $token,
|
||||
'version' => $version,
|
||||
'faversion' => $faversion
|
||||
];
|
||||
try {
|
||||
$result = Service::isBuy($name, $extend);
|
||||
} catch (Exception $e) {
|
||||
$this->error(__($e->getMessage()));
|
||||
}
|
||||
return json($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新授权
|
||||
*/
|
||||
public function authorization()
|
||||
{
|
||||
$params = [
|
||||
'uid' => $this->request->post('uid'),
|
||||
'token' => $this->request->post('token'),
|
||||
'faversion' => $this->request->post('faversion'),
|
||||
];
|
||||
try {
|
||||
Service::authorization($params);
|
||||
} catch (Exception $e) {
|
||||
$this->error(__($e->getMessage()));
|
||||
}
|
||||
$this->success(__('Operate successful'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插件相关表
|
||||
*/
|
||||
public function get_table_list()
|
||||
{
|
||||
$name = $this->request->post("name");
|
||||
if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
|
||||
$this->error(__('Addon name incorrect'));
|
||||
}
|
||||
$tables = get_addon_tables($name);
|
||||
$prefix = Config::get('database.prefix');
|
||||
foreach ($tables as $index => $table) {
|
||||
//忽略非插件标识的表名
|
||||
if (!preg_match("/^{$prefix}{$name}/", $table)) {
|
||||
unset($tables[$index]);
|
||||
}
|
||||
}
|
||||
$tables = array_values($tables);
|
||||
$this->success('', null, ['tables' => $tables]);
|
||||
}
|
||||
|
||||
protected function getAddonList()
|
||||
{
|
||||
$onlineaddons = Cache::get("onlineaddons");
|
||||
if (!is_array($onlineaddons) && config('fastadmin.api_url')) {
|
||||
$onlineaddons = [];
|
||||
$params = [
|
||||
'uid' => $this->request->post('uid'),
|
||||
'token' => $this->request->post('token'),
|
||||
'version' => config('fastadmin.version'),
|
||||
'faversion' => config('fastadmin.version'),
|
||||
];
|
||||
$json = [];
|
||||
try {
|
||||
$json = Service::addons($params);
|
||||
} catch (\Exception $e) {
|
||||
|
||||
}
|
||||
$rows = $json['rows'] ?? [];
|
||||
foreach ($rows as $index => $row) {
|
||||
if (!isset($row['name'])) {
|
||||
continue;
|
||||
}
|
||||
$onlineaddons[$row['name']] = $row;
|
||||
}
|
||||
Cache::set("onlineaddons", $onlineaddons, 600);
|
||||
}
|
||||
return $onlineaddons;
|
||||
}
|
||||
|
||||
}
|
||||
327
application/admin/controller/Ajax.php
Normal file
327
application/admin/controller/Ajax.php
Normal file
@@ -0,0 +1,327 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\exception\UploadException;
|
||||
use app\common\library\Upload;
|
||||
use fast\Random;
|
||||
use think\addons\Service;
|
||||
use think\Cache;
|
||||
use think\Config;
|
||||
use think\Db;
|
||||
use think\Lang;
|
||||
use think\Loader;
|
||||
use think\Response;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* Ajax异步请求接口
|
||||
* @internal
|
||||
*/
|
||||
class Ajax extends Backend
|
||||
{
|
||||
protected $noNeedLogin = ['lang'];
|
||||
protected $noNeedRight = ['*'];
|
||||
protected $layout = '';
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
|
||||
//设置过滤方法
|
||||
$this->request->filter(['trim', 'strip_tags', 'htmlspecialchars']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载语言包
|
||||
*/
|
||||
public function lang()
|
||||
{
|
||||
$this->request->get(['callback' => 'define']);
|
||||
$header = ['Content-Type' => 'application/javascript'];
|
||||
if (!config('app_debug')) {
|
||||
$offset = 30 * 60 * 60 * 24; // 缓存一个月
|
||||
$header['Cache-Control'] = 'public';
|
||||
$header['Pragma'] = 'cache';
|
||||
$header['Expires'] = gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
|
||||
}
|
||||
|
||||
$controllername = $this->request->get('controllername');
|
||||
$lang = $this->request->get('lang');
|
||||
if (!$lang || !in_array($lang, config('allow_lang_list')) || !$controllername || !preg_match("/^[a-z0-9_\.]+$/i", $controllername)) {
|
||||
return jsonp(['errmsg' => '参数错误'], 200, [], ['json_encode_param' => JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE]);
|
||||
}
|
||||
|
||||
$controllername = input("controllername");
|
||||
$className = Loader::parseClass($this->request->module(), 'controller', $controllername, false);
|
||||
|
||||
//存在对应的类才加载
|
||||
if (class_exists($className)) {
|
||||
$this->loadlang($controllername);
|
||||
}
|
||||
|
||||
return jsonp(Lang::get(), 200, $header, ['json_encode_param' => JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*/
|
||||
public function upload()
|
||||
{
|
||||
Config::set('default_return_type', 'json');
|
||||
|
||||
//必须还原upload配置,否则分片及cdnurl函数计算错误
|
||||
Config::load(APP_PATH . 'extra/upload.php', 'upload');
|
||||
|
||||
$chunkid = $this->request->post("chunkid");
|
||||
if ($chunkid) {
|
||||
if (!Config::get('upload.chunking')) {
|
||||
$this->error(__('Chunk file disabled'));
|
||||
}
|
||||
$action = $this->request->post("action");
|
||||
$chunkindex = $this->request->post("chunkindex/d");
|
||||
$chunkcount = $this->request->post("chunkcount/d");
|
||||
$filename = $this->request->post("filename");
|
||||
$method = $this->request->method(true);
|
||||
if ($action == 'merge') {
|
||||
$attachment = null;
|
||||
//合并分片文件
|
||||
try {
|
||||
$upload = new Upload();
|
||||
$attachment = $upload->merge($chunkid, $chunkcount, $filename);
|
||||
} catch (UploadException $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
$this->success(__('Uploaded successful'), '', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
|
||||
} elseif ($method == 'clean') {
|
||||
//删除冗余的分片文件
|
||||
try {
|
||||
$upload = new Upload();
|
||||
$upload->clean($chunkid);
|
||||
} catch (UploadException $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
$this->success();
|
||||
} else {
|
||||
//上传分片文件
|
||||
//默认普通上传文件
|
||||
$file = $this->request->file('file');
|
||||
try {
|
||||
$upload = new Upload($file);
|
||||
$upload->chunk($chunkid, $chunkindex, $chunkcount);
|
||||
} catch (UploadException $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
} else {
|
||||
$attachment = null;
|
||||
//默认普通上传文件
|
||||
$file = $this->request->file('file');
|
||||
try {
|
||||
$upload = new Upload($file);
|
||||
$attachment = $upload->upload();
|
||||
} catch (UploadException $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
$this->success(__('Uploaded successful'), '', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用排序
|
||||
*/
|
||||
public function weigh()
|
||||
{
|
||||
//排序的数组
|
||||
$ids = $this->request->post("ids");
|
||||
//拖动的记录ID
|
||||
$changeid = $this->request->post("changeid");
|
||||
//操作字段
|
||||
$field = $this->request->post("field");
|
||||
//操作的数据表
|
||||
$table = $this->request->post("table");
|
||||
if (!Validate::is($table, "alphaDash")) {
|
||||
$this->error();
|
||||
}
|
||||
//主键
|
||||
$pk = $this->request->post("pk");
|
||||
//排序的方式
|
||||
$orderway = strtolower($this->request->post("orderway", ""));
|
||||
$orderway = $orderway == 'asc' ? 'ASC' : 'DESC';
|
||||
$sour = $weighdata = [];
|
||||
$ids = explode(',', $ids);
|
||||
$prikey = $pk && preg_match("/^[a-z0-9\-_]+$/i", $pk) ? $pk : (Db::name($table)->getPk() ?: 'id');
|
||||
$pid = $this->request->post("pid", "");
|
||||
//限制更新的字段
|
||||
$field = in_array($field, ['weigh']) ? $field : 'weigh';
|
||||
|
||||
// 如果设定了pid的值,此时只匹配满足条件的ID,其它忽略
|
||||
if ($pid !== '') {
|
||||
$hasids = [];
|
||||
$list = Db::name($table)->where($prikey, 'in', $ids)->where('pid', 'in', $pid)->field("{$prikey},pid")->select();
|
||||
foreach ($list as $k => $v) {
|
||||
$hasids[] = $v[$prikey];
|
||||
}
|
||||
$ids = array_values(array_intersect($ids, $hasids));
|
||||
}
|
||||
|
||||
$list = Db::name($table)->field("$prikey,$field")->where($prikey, 'in', $ids)->order($field, $orderway)->select();
|
||||
foreach ($list as $k => $v) {
|
||||
$sour[] = $v[$prikey];
|
||||
$weighdata[$v[$prikey]] = $v[$field];
|
||||
}
|
||||
$position = array_search($changeid, $ids);
|
||||
$desc_id = $sour[$position] ?? end($sour); //移动到目标的ID值,取出所处改变前位置的值
|
||||
$sour_id = $changeid;
|
||||
$weighids = [];
|
||||
$temp = array_values(array_diff_assoc($ids, $sour));
|
||||
foreach ($temp as $m => $n) {
|
||||
if ($n == $sour_id) {
|
||||
$offset = $desc_id;
|
||||
} else {
|
||||
if ($sour_id == $temp[0]) {
|
||||
$offset = $temp[$m + 1] ?? $sour_id;
|
||||
} else {
|
||||
$offset = $temp[$m - 1] ?? $sour_id;
|
||||
}
|
||||
}
|
||||
if (!isset($weighdata[$offset])) {
|
||||
continue;
|
||||
}
|
||||
$weighids[$n] = $weighdata[$offset];
|
||||
Db::name($table)->where($prikey, $n)->update([$field => $weighdata[$offset]]);
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空系统缓存
|
||||
*/
|
||||
public function wipecache()
|
||||
{
|
||||
try {
|
||||
$type = $this->request->request("type");
|
||||
switch ($type) {
|
||||
case 'all':
|
||||
case 'content':
|
||||
//内容缓存
|
||||
rmdirs(CACHE_PATH, false);
|
||||
Cache::clear();
|
||||
if ($type == 'content') {
|
||||
break;
|
||||
}
|
||||
// no break
|
||||
case 'template':
|
||||
// 模板缓存
|
||||
rmdirs(TEMP_PATH, false);
|
||||
if ($type == 'template') {
|
||||
break;
|
||||
}
|
||||
// no break
|
||||
case 'addons':
|
||||
// 插件缓存
|
||||
Service::refresh();
|
||||
if ($type == 'addons') {
|
||||
break;
|
||||
}
|
||||
// no break
|
||||
case 'browser':
|
||||
// 浏览器缓存
|
||||
// 只有生产环境下才修改
|
||||
if (!config('app_debug')) {
|
||||
$version = config('site.version');
|
||||
$newversion = preg_replace_callback("/(.*)\.([0-9]+)\$/", function ($match) {
|
||||
return $match[1] . '.' . ($match[2] + 1);
|
||||
}, $version);
|
||||
if ($newversion && $newversion != $version) {
|
||||
Db::startTrans();
|
||||
try {
|
||||
\app\common\model\Config::where('name', 'version')->update(['value' => $newversion]);
|
||||
\app\common\model\Config::refreshFile();
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($type == 'browser') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
\think\Hook::listen("wipecache_after");
|
||||
$this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取分类数据,联动列表
|
||||
*/
|
||||
public function category()
|
||||
{
|
||||
$type = $this->request->get('type', '');
|
||||
$pid = $this->request->get('pid', '');
|
||||
$where = ['status' => 'normal'];
|
||||
$categorylist = null;
|
||||
if ($pid || $pid === '0') {
|
||||
$where['pid'] = $pid;
|
||||
}
|
||||
if ($type) {
|
||||
$where['type'] = $type;
|
||||
}
|
||||
|
||||
$categorylist = Db::name('category')->where($where)->field('id as value,name')->order('weigh desc,id desc')->select();
|
||||
|
||||
$this->success('', '', $categorylist);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取省市区数据,联动列表
|
||||
*/
|
||||
public function area()
|
||||
{
|
||||
$params = $this->request->get("row/a");
|
||||
if (!empty($params)) {
|
||||
$province = isset($params['province']) ? $params['province'] : null;
|
||||
$city = isset($params['city']) ? $params['city'] : null;
|
||||
} else {
|
||||
$province = $this->request->get('province');
|
||||
$city = $this->request->get('city');
|
||||
}
|
||||
$where = ['pid' => 0, 'level' => 1];
|
||||
$provincelist = null;
|
||||
if ($province !== null) {
|
||||
$where['pid'] = $province;
|
||||
$where['level'] = 2;
|
||||
if ($city !== null) {
|
||||
$where['pid'] = $city;
|
||||
$where['level'] = 3;
|
||||
}
|
||||
}
|
||||
$provincelist = Db::name('area')->where($where)->field('id as value,name')->select();
|
||||
$this->success('', '', $provincelist);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成后缀图标
|
||||
*/
|
||||
public function icon()
|
||||
{
|
||||
$suffix = $this->request->request("suffix");
|
||||
$suffix = $suffix ? $suffix : "FILE";
|
||||
$data = build_suffix_image($suffix);
|
||||
$header = ['Content-Type' => 'image/svg+xml'];
|
||||
$offset = 30 * 60 * 60 * 24; // 缓存一个月
|
||||
$header['Cache-Control'] = 'public';
|
||||
$header['Pragma'] = 'cache';
|
||||
$header['Expires'] = gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
|
||||
$response = Response::create($data, '', 200, $header);
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
158
application/admin/controller/Category.php
Normal file
158
application/admin/controller/Category.php
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\model\Category as CategoryModel;
|
||||
use fast\Tree;
|
||||
|
||||
/**
|
||||
* 分类管理
|
||||
*
|
||||
* @icon fa fa-list
|
||||
* @remark 用于管理网站的所有分类,分类可进行无限级分类,分类类型请在常规管理->系统配置->字典配置中添加
|
||||
*/
|
||||
class Category extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \app\common\model\Category
|
||||
*/
|
||||
protected $model = null;
|
||||
protected $categorylist = [];
|
||||
protected $noNeedRight = ['selectpage'];
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = model('app\common\model\Category');
|
||||
|
||||
$tree = Tree::instance();
|
||||
$tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'pid');
|
||||
$this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
|
||||
$categorydata = [0 => ['type' => 'all', 'name' => __('None')]];
|
||||
foreach ($this->categorylist as $k => $v) {
|
||||
$categorydata[$v['id']] = $v;
|
||||
}
|
||||
$typeList = CategoryModel::getTypeList();
|
||||
$this->view->assign("flagList", $this->model->getFlagList());
|
||||
$this->view->assign("typeList", $typeList);
|
||||
$this->view->assign("parentList", $categorydata);
|
||||
$this->assignconfig('typeList', $typeList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags']);
|
||||
if ($this->request->isAjax()) {
|
||||
$search = $this->request->request("search");
|
||||
$type = $this->request->request("type");
|
||||
|
||||
//构造父类select列表选项数据
|
||||
$list = [];
|
||||
|
||||
foreach ($this->categorylist as $k => $v) {
|
||||
if ($search) {
|
||||
if ($v['type'] == $type && stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false) {
|
||||
if ($type == "all" || $type == null) {
|
||||
$list = $this->categorylist;
|
||||
} else {
|
||||
$list[] = $v;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($type == "all" || $type == null) {
|
||||
$list = $this->categorylist;
|
||||
} elseif ($v['type'] == $type) {
|
||||
$list[] = $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$total = count($list);
|
||||
$result = array("total" => $total, "rows" => $list);
|
||||
|
||||
return json($result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
}
|
||||
return parent::add();
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
$row = $this->model->get($ids);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$adminIds = $this->getDataLimitAdminIds();
|
||||
if (is_array($adminIds)) {
|
||||
if (!in_array($row[$this->dataLimitField], $adminIds)) {
|
||||
$this->error(__('You have no permission'));
|
||||
}
|
||||
}
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
$params = $this->request->post("row/a");
|
||||
if ($params) {
|
||||
$params = $this->preExcludeFields($params);
|
||||
|
||||
if ($params['pid'] != $row['pid']) {
|
||||
$childrenIds = Tree::instance()->init(collection(\app\common\model\Category::select())->toArray())->getChildrenIds($row['id'], true);
|
||||
if (in_array($params['pid'], $childrenIds)) {
|
||||
$this->error(__('Can not change the parent to child or itself'));
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
//是否采用模型验证
|
||||
if ($this->modelValidate) {
|
||||
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
||||
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
|
||||
$row->validate($validate);
|
||||
}
|
||||
$result = $row->allowField(true)->save($params);
|
||||
if ($result !== false) {
|
||||
$this->success();
|
||||
} else {
|
||||
$this->error($row->getError());
|
||||
}
|
||||
} catch (\think\exception\PDOException $e) {
|
||||
$this->error($e->getMessage());
|
||||
} catch (\think\Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$this->view->assign("row", $row);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Selectpage搜索
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function selectpage()
|
||||
{
|
||||
return parent::selectpage();
|
||||
}
|
||||
}
|
||||
84
application/admin/controller/Dashboard.php
Normal file
84
application/admin/controller/Dashboard.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\admin\model\Admin;
|
||||
use app\admin\model\User;
|
||||
use app\common\controller\Backend;
|
||||
use app\common\model\Attachment;
|
||||
use fast\Date;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 控制台
|
||||
*
|
||||
* @icon fa fa-dashboard
|
||||
* @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
|
||||
*/
|
||||
class Dashboard extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
\think\Db::execute("SET @@sql_mode='';");
|
||||
} catch (\Exception $e) {
|
||||
|
||||
}
|
||||
$column = [];
|
||||
$starttime = Date::unixtime('day', -6);
|
||||
$endtime = Date::unixtime('day', 0, 'end');
|
||||
$joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
|
||||
->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
|
||||
->group('join_date')
|
||||
->select();
|
||||
for ($time = $starttime; $time <= $endtime;) {
|
||||
$column[] = date("Y-m-d", $time);
|
||||
$time += 86400;
|
||||
}
|
||||
$userlist = array_fill_keys($column, 0);
|
||||
foreach ($joinlist as $k => $v) {
|
||||
$userlist[$v['join_date']] = $v['nums'];
|
||||
}
|
||||
|
||||
$dbTableList = Db::query("SHOW TABLE STATUS");
|
||||
$addonList = get_addon_list();
|
||||
$totalworkingaddon = 0;
|
||||
$totaladdon = count($addonList);
|
||||
foreach ($addonList as $index => $item) {
|
||||
if ($item['state']) {
|
||||
$totalworkingaddon += 1;
|
||||
}
|
||||
}
|
||||
$this->view->assign([
|
||||
'totaluser' => User::count(),
|
||||
'totaladdon' => $totaladdon,
|
||||
'totaladmin' => Admin::count(),
|
||||
'totalcategory' => \app\common\model\Category::count(),
|
||||
'todayusersignup' => User::whereTime('jointime', 'today')->count(),
|
||||
'todayuserlogin' => User::whereTime('logintime', 'today')->count(),
|
||||
'sevendau' => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
|
||||
'thirtydau' => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
|
||||
'threednu' => User::whereTime('jointime', '-3 days')->count(),
|
||||
'sevendnu' => User::whereTime('jointime', '-7 days')->count(),
|
||||
'dbtablenums' => count($dbTableList),
|
||||
'dbsize' => array_sum(array_map(function ($item) {
|
||||
return $item['Data_length'] + $item['Index_length'];
|
||||
}, $dbTableList)),
|
||||
'totalworkingaddon' => $totalworkingaddon,
|
||||
'attachmentnums' => Attachment::count(),
|
||||
'attachmentsize' => Attachment::sum('filesize'),
|
||||
'picturenums' => Attachment::where('mimetype', 'like', 'image/%')->count(),
|
||||
'picturesize' => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
|
||||
]);
|
||||
|
||||
$this->assignconfig('column', array_keys($userlist));
|
||||
$this->assignconfig('userdata', array_values($userlist));
|
||||
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
}
|
||||
39
application/admin/controller/Epay.php
Normal file
39
application/admin/controller/Epay.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use think\Config;
|
||||
|
||||
class Epay extends Backend
|
||||
{
|
||||
protected $noNeedRight = ['upload'];
|
||||
|
||||
/**
|
||||
* 上传本地证书
|
||||
* @return void
|
||||
*/
|
||||
public function upload()
|
||||
{
|
||||
Config::set('default_return_type', 'json');
|
||||
|
||||
$certname = $this->request->post('certname', '');
|
||||
$certPathArr = [
|
||||
'cert_client' => '/addons/epay/certs/apiclient_cert.pem', //微信支付api
|
||||
'cert_key' => '/addons/epay/certs/apiclient_key.pem', //微信支付api
|
||||
'app_cert_public_key' => '/addons/epay/certs/appCertPublicKey.crt',//应用公钥证书路径
|
||||
'alipay_root_cert' => '/addons/epay/certs/alipayRootCert.crt', //支付宝根证书路径
|
||||
'ali_public_key' => '/addons/epay/certs/alipayCertPublicKey.crt', //支付宝公钥证书路径
|
||||
];
|
||||
if (!isset($certPathArr[$certname])) {
|
||||
$this->error("证书错误");
|
||||
}
|
||||
$url = $certPathArr[$certname];
|
||||
$file = $this->request->file('file');
|
||||
if (!$file) {
|
||||
$this->error("未上传文件");
|
||||
}
|
||||
$file->move(dirname(ROOT_PATH . $url), basename(ROOT_PATH . $url), true);
|
||||
$this->success(__('上传成功'), '', ['url' => $url]);
|
||||
}
|
||||
}
|
||||
141
application/admin/controller/Index.php
Normal file
141
application/admin/controller/Index.php
Normal file
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\admin\model\AdminLog;
|
||||
use app\common\controller\Backend;
|
||||
use think\Config;
|
||||
use think\Hook;
|
||||
use think\Session;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* 后台首页
|
||||
* @internal
|
||||
*/
|
||||
class Index extends Backend
|
||||
{
|
||||
|
||||
protected $noNeedLogin = ['login'];
|
||||
protected $noNeedRight = ['index', 'logout'];
|
||||
protected $layout = '';
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
//移除HTML标签
|
||||
$this->request->filter('trim,strip_tags,htmlspecialchars');
|
||||
}
|
||||
|
||||
/**
|
||||
* 后台首页
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$cookieArr = ['adminskin' => "/^skin\-([a-z\-]+)\$/i", 'multiplenav' => "/^(0|1)\$/", 'multipletab' => "/^(0|1)\$/", 'show_submenu' => "/^(0|1)\$/"];
|
||||
foreach ($cookieArr as $key => $regex) {
|
||||
$cookieValue = $this->request->cookie($key);
|
||||
if (!is_null($cookieValue) && preg_match($regex, $cookieValue)) {
|
||||
config('fastadmin.' . $key, $cookieValue);
|
||||
}
|
||||
}
|
||||
//左侧菜单
|
||||
list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
|
||||
'dashboard' => 'hot',
|
||||
'addon' => ['new', 'red', 'badge'],
|
||||
'auth/rule' => __('Menu'),
|
||||
], $this->view->site['fixedpage']);
|
||||
$action = $this->request->request('action');
|
||||
if ($this->request->isPost()) {
|
||||
if ($action == 'refreshmenu') {
|
||||
$this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]);
|
||||
}
|
||||
}
|
||||
$this->assignconfig('cookie', ['prefix' => config('cookie.prefix')]);
|
||||
$this->view->assign('menulist', $menulist);
|
||||
$this->view->assign('navlist', $navlist);
|
||||
$this->view->assign('fixedmenu', $fixedmenu);
|
||||
$this->view->assign('referermenu', $referermenu);
|
||||
$this->view->assign('title', __('Home'));
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员登录
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
$url = $this->request->get('url', '', 'url_clean');
|
||||
$url = $url ?: 'index/index';
|
||||
if ($this->auth->isLogin()) {
|
||||
$this->success(__("You've logged in, do not login again"), $url);
|
||||
}
|
||||
//保持会话有效时长,单位:小时
|
||||
$keeyloginhours = 24;
|
||||
if ($this->request->isPost()) {
|
||||
$username = $this->request->post('username');
|
||||
$password = $this->request->post('password', '', null);
|
||||
$keeplogin = $this->request->post('keeplogin');
|
||||
$token = $this->request->post('__token__');
|
||||
$rule = [
|
||||
'username' => 'require|length:3,30',
|
||||
'password' => 'require|length:3,30',
|
||||
'__token__' => 'require|token',
|
||||
];
|
||||
$data = [
|
||||
'username' => $username,
|
||||
'password' => $password,
|
||||
'__token__' => $token,
|
||||
];
|
||||
if (Config::get('fastadmin.login_captcha')) {
|
||||
$rule['captcha'] = 'require|captcha';
|
||||
$data['captcha'] = $this->request->post('captcha');
|
||||
}
|
||||
$validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
|
||||
$result = $validate->check($data);
|
||||
if (!$result) {
|
||||
$this->error($validate->getError(), $url, ['token' => $this->request->token()]);
|
||||
}
|
||||
AdminLog::setTitle(__('Login'));
|
||||
$result = $this->auth->login($username, $password, $keeplogin ? $keeyloginhours * 3600 : 0);
|
||||
if ($result === true) {
|
||||
Hook::listen("admin_login_after", $this->request);
|
||||
$this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
|
||||
} else {
|
||||
$msg = $this->auth->getError();
|
||||
$msg = $msg ? $msg : __('Username or password is incorrect');
|
||||
$this->error($msg, $url, ['token' => $this->request->token()]);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据客户端的cookie,判断是否可以自动登录
|
||||
if ($this->auth->autologin()) {
|
||||
Session::delete("referer");
|
||||
$this->redirect($url);
|
||||
}
|
||||
$background = Config::get('fastadmin.login_background');
|
||||
$background = $background ? (stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background) : '';
|
||||
$this->view->assign('keeyloginhours', $keeyloginhours);
|
||||
$this->view->assign('background', $background);
|
||||
$this->view->assign('title', __('Login'));
|
||||
Hook::listen("admin_login_init", $this->request);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*/
|
||||
public function logout()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$this->auth->logout();
|
||||
Hook::listen("admin_logout_after", $this->request);
|
||||
$this->success(__('Logout successful'), 'index/login');
|
||||
}
|
||||
$html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
|
||||
$html .= "<script>document.forms['logout_submit'].submit();</script>";
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
}
|
||||
26
application/admin/controller/Version.php
Normal file
26
application/admin/controller/Version.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
|
||||
use think\Controller;
|
||||
use think\Request;
|
||||
|
||||
/**
|
||||
* 版本管理
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Version extends Backend
|
||||
{
|
||||
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = model('Version');
|
||||
}
|
||||
|
||||
}
|
||||
297
application/admin/controller/auth/Admin.php
Normal file
297
application/admin/controller/auth/Admin.php
Normal file
@@ -0,0 +1,297 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\auth;
|
||||
|
||||
use app\admin\model\AuthGroup;
|
||||
use app\admin\model\AuthGroupAccess;
|
||||
use app\common\controller\Backend;
|
||||
use fast\Random;
|
||||
use fast\Tree;
|
||||
use think\Db;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* 管理员管理
|
||||
*
|
||||
* @icon fa fa-users
|
||||
* @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
|
||||
*/
|
||||
class Admin extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \app\admin\model\Admin
|
||||
*/
|
||||
protected $model = null;
|
||||
protected $selectpageFields = 'id,username,nickname,avatar';
|
||||
protected $searchFields = 'id,username,nickname';
|
||||
protected $childrenGroupIds = [];
|
||||
protected $childrenAdminIds = [];
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = model('Admin');
|
||||
|
||||
$this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isSuperAdmin());
|
||||
$this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin());
|
||||
|
||||
$groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
|
||||
|
||||
Tree::instance()->init($groupList);
|
||||
$groupdata = [];
|
||||
if ($this->auth->isSuperAdmin()) {
|
||||
$result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
|
||||
foreach ($result as $k => $v) {
|
||||
$groupdata[$v['id']] = $v['name'];
|
||||
}
|
||||
} else {
|
||||
$result = [];
|
||||
$groups = $this->auth->getGroups();
|
||||
foreach ($groups as $m => $n) {
|
||||
$childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
|
||||
$temp = [];
|
||||
foreach ($childlist as $k => $v) {
|
||||
$temp[$v['id']] = $v['name'];
|
||||
}
|
||||
$result[__($n['name'])] = $temp;
|
||||
}
|
||||
$groupdata = $result;
|
||||
}
|
||||
|
||||
$this->view->assign('groupdata', $groupdata);
|
||||
$this->assignconfig("admin", ['id' => $this->auth->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
if ($this->request->isAjax()) {
|
||||
//如果发送的来源是Selectpage,则转发到Selectpage
|
||||
if ($this->request->request('keyField')) {
|
||||
return $this->selectpage();
|
||||
}
|
||||
$childrenGroupIds = $this->childrenGroupIds;
|
||||
$groupName = AuthGroup::where('id', 'in', $childrenGroupIds)
|
||||
->column('id,name');
|
||||
$authGroupList = AuthGroupAccess::where('group_id', 'in', $childrenGroupIds)
|
||||
->field('uid,group_id')
|
||||
->select();
|
||||
|
||||
$adminGroupName = [];
|
||||
foreach ($authGroupList as $k => $v) {
|
||||
if (isset($groupName[$v['group_id']])) {
|
||||
$adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
|
||||
}
|
||||
}
|
||||
$groups = $this->auth->getGroups();
|
||||
foreach ($groups as $m => $n) {
|
||||
$adminGroupName[$this->auth->id][$n['id']] = $n['name'];
|
||||
}
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
|
||||
$list = $this->model
|
||||
->where($where)
|
||||
->where('id', 'in', $this->childrenAdminIds)
|
||||
->field(['password', 'salt', 'token'], true)
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
|
||||
foreach ($list as $k => &$v) {
|
||||
$groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
|
||||
$v['groups'] = implode(',', array_keys($groups));
|
||||
$v['groups_text'] = implode(',', array_values($groups));
|
||||
}
|
||||
unset($v);
|
||||
$result = array("total" => $list->total(), "rows" => $list->items());
|
||||
|
||||
return json($result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
$params = $this->request->post("row/a");
|
||||
if ($params) {
|
||||
Db::startTrans();
|
||||
try {
|
||||
if (!Validate::is($params['password'], '\S{6,30}')) {
|
||||
exception(__("Please input correct password"));
|
||||
}
|
||||
$params['salt'] = Random::alnum();
|
||||
$params['password'] = $this->auth->getEncryptPassword($params['password'], $params['salt']);
|
||||
$params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
|
||||
$result = $this->model->validate('Admin.add')->save($params);
|
||||
if ($result === false) {
|
||||
exception($this->model->getError());
|
||||
}
|
||||
$group = $this->request->post("group/a");
|
||||
|
||||
//过滤不允许的组别,避免越权
|
||||
$group = array_intersect($this->childrenGroupIds, $group);
|
||||
if (!$group) {
|
||||
exception(__('The parent group exceeds permission limit'));
|
||||
}
|
||||
|
||||
$dataset = [];
|
||||
foreach ($group as $value) {
|
||||
$dataset[] = ['uid' => $this->model->id, 'group_id' => $value];
|
||||
}
|
||||
model('AuthGroupAccess')->saveAll($dataset);
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
$row = $this->model->get(['id' => $ids]);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
if (!in_array($row->id, $this->childrenAdminIds)) {
|
||||
$this->error(__('You have no permission'));
|
||||
}
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
$params = $this->request->post("row/a");
|
||||
if ($params) {
|
||||
Db::startTrans();
|
||||
try {
|
||||
if ($params['password']) {
|
||||
if (!Validate::is($params['password'], '\S{6,30}')) {
|
||||
exception(__("Please input correct password"));
|
||||
}
|
||||
$params['salt'] = Random::alnum();
|
||||
$params['password'] = $this->auth->getEncryptPassword($params['password'], $params['salt']);
|
||||
} else {
|
||||
unset($params['password'], $params['salt']);
|
||||
}
|
||||
//这里需要针对username和email做唯一验证
|
||||
$adminValidate = \think\Loader::validate('Admin');
|
||||
$adminValidate->rule([
|
||||
'username' => 'require|regex:\w{3,30}|unique:admin,username,' . $row->id,
|
||||
'email' => 'require|email|unique:admin,email,' . $row->id,
|
||||
'mobile' => 'regex:1[3-9]\d{9}|unique:admin,mobile,' . $row->id,
|
||||
'password' => 'regex:\S{32}',
|
||||
]);
|
||||
$result = $row->validate('Admin.edit')->save($params);
|
||||
if ($result === false) {
|
||||
exception($row->getError());
|
||||
}
|
||||
|
||||
// 先移除所有权限
|
||||
model('AuthGroupAccess')->where('uid', $row->id)->delete();
|
||||
|
||||
$group = $this->request->post("group/a");
|
||||
|
||||
// 过滤不允许的组别,避免越权
|
||||
$group = array_intersect($this->childrenGroupIds, $group);
|
||||
if (!$group) {
|
||||
exception(__('The parent group exceeds permission limit'));
|
||||
}
|
||||
|
||||
$dataset = [];
|
||||
foreach ($group as $value) {
|
||||
$dataset[] = ['uid' => $row->id, 'group_id' => $value];
|
||||
}
|
||||
model('AuthGroupAccess')->saveAll($dataset);
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$grouplist = $this->auth->getGroups($row['id']);
|
||||
$groupids = [];
|
||||
foreach ($grouplist as $k => $v) {
|
||||
$groupids[] = $v['id'];
|
||||
}
|
||||
$this->view->assign("row", $row);
|
||||
$this->view->assign("groupids", $groupids);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
public function del($ids = "")
|
||||
{
|
||||
if (!$this->request->isPost()) {
|
||||
$this->error(__("Invalid parameters"));
|
||||
}
|
||||
$ids = $ids ? $ids : $this->request->post("ids");
|
||||
if ($ids) {
|
||||
$ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $ids)));
|
||||
// 避免越权删除管理员
|
||||
$childrenGroupIds = $this->childrenGroupIds;
|
||||
$adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
|
||||
$query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
|
||||
})->select();
|
||||
if ($adminList) {
|
||||
$deleteIds = [];
|
||||
foreach ($adminList as $k => $v) {
|
||||
$deleteIds[] = $v->id;
|
||||
}
|
||||
$deleteIds = array_values(array_diff($deleteIds, [$this->auth->id]));
|
||||
if ($deleteIds) {
|
||||
Db::startTrans();
|
||||
try {
|
||||
$this->model->destroy($deleteIds);
|
||||
model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
$this->error(__('No rows were deleted'));
|
||||
}
|
||||
}
|
||||
$this->error(__('You have no permission'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新
|
||||
* @internal
|
||||
*/
|
||||
public function multi($ids = "")
|
||||
{
|
||||
// 管理员禁止批量操作
|
||||
$this->error();
|
||||
}
|
||||
|
||||
/**
|
||||
* 下拉搜索
|
||||
*/
|
||||
public function selectpage()
|
||||
{
|
||||
$this->dataLimit = 'auth';
|
||||
$this->dataLimitField = 'id';
|
||||
return parent::selectpage();
|
||||
}
|
||||
}
|
||||
138
application/admin/controller/auth/Adminlog.php
Normal file
138
application/admin/controller/auth/Adminlog.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\auth;
|
||||
|
||||
use app\admin\model\AuthGroup;
|
||||
use app\common\controller\Backend;
|
||||
|
||||
/**
|
||||
* 管理员日志
|
||||
*
|
||||
* @icon fa fa-users
|
||||
* @remark 管理员可以查看自己所拥有的权限的管理员日志
|
||||
*/
|
||||
class Adminlog extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \app\admin\model\AdminLog
|
||||
*/
|
||||
protected $model = null;
|
||||
protected $childrenAdminIds = [];
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = model('AdminLog');
|
||||
$this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
if ($this->request->isAjax()) {
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
$isSuperAdmin = $this->auth->isSuperAdmin();
|
||||
$childrenAdminIds = $this->childrenAdminIds;
|
||||
$list = $this->model
|
||||
->where($where)
|
||||
->where(function ($query) use ($isSuperAdmin, $childrenAdminIds) {
|
||||
if (!$isSuperAdmin) {
|
||||
$query->where('admin_id', 'in', $childrenAdminIds);
|
||||
}
|
||||
})
|
||||
->field('content,useragent', true)
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
|
||||
$result = array("total" => $list->total(), "rows" => $list->items());
|
||||
|
||||
return json($result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 详情
|
||||
*/
|
||||
public function detail($ids)
|
||||
{
|
||||
$row = $this->model->get(['id' => $ids]);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
if (!$this->auth->isSuperAdmin()) {
|
||||
if (!$row['admin_id'] || !in_array($row['admin_id'], $this->childrenAdminIds)) {
|
||||
$this->error(__('You have no permission'));
|
||||
}
|
||||
}
|
||||
$this->view->assign("row", $row->toArray());
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
* @internal
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
$this->error();
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
* @internal
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
$this->error();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
public function del($ids = "")
|
||||
{
|
||||
if (!$this->request->isPost()) {
|
||||
$this->error(__("Invalid parameters"));
|
||||
}
|
||||
$ids = $ids ? $ids : $this->request->post("ids");
|
||||
if ($ids) {
|
||||
$isSuperAdmin = $this->auth->isSuperAdmin();
|
||||
$childrenAdminIds = $this->childrenAdminIds;
|
||||
$adminList = $this->model->where('id', 'in', $ids)
|
||||
->where(function ($query) use ($isSuperAdmin, $childrenAdminIds) {
|
||||
if (!$isSuperAdmin) {
|
||||
$query->where('admin_id', 'in', $childrenAdminIds);
|
||||
}
|
||||
})
|
||||
->select();
|
||||
if ($adminList) {
|
||||
$deleteIds = [];
|
||||
foreach ($adminList as $k => $v) {
|
||||
$deleteIds[] = $v->id;
|
||||
}
|
||||
if ($deleteIds) {
|
||||
$this->model->destroy($deleteIds);
|
||||
$this->success();
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->error();
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新
|
||||
* @internal
|
||||
*/
|
||||
public function multi($ids = "")
|
||||
{
|
||||
// 管理员禁止批量操作
|
||||
$this->error();
|
||||
}
|
||||
|
||||
}
|
||||
317
application/admin/controller/auth/Group.php
Normal file
317
application/admin/controller/auth/Group.php
Normal file
@@ -0,0 +1,317 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\auth;
|
||||
|
||||
use app\admin\model\AuthGroup;
|
||||
use app\common\controller\Backend;
|
||||
use fast\Tree;
|
||||
use think\Db;
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
* 角色组
|
||||
*
|
||||
* @icon fa fa-group
|
||||
* @remark 角色组可以有多个,角色有上下级层级关系,如果子角色有角色组和管理员的权限则可以派生属于自己组别下级的角色组或管理员
|
||||
*/
|
||||
class Group extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \app\admin\model\AuthGroup
|
||||
*/
|
||||
protected $model = null;
|
||||
//当前登录管理员所有子组别
|
||||
protected $childrenGroupIds = [];
|
||||
//当前组别列表数据
|
||||
protected $grouplist = [];
|
||||
protected $groupdata = [];
|
||||
//无需要权限判断的方法
|
||||
protected $noNeedRight = ['roletree'];
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = model('AuthGroup');
|
||||
|
||||
$this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
|
||||
|
||||
$groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
|
||||
|
||||
Tree::instance()->init($groupList);
|
||||
$groupList = [];
|
||||
if ($this->auth->isSuperAdmin()) {
|
||||
$groupList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
|
||||
} else {
|
||||
$groups = $this->auth->getGroups();
|
||||
$groupIds = [];
|
||||
foreach ($groups as $m => $n) {
|
||||
if (in_array($n['id'], $groupIds) || in_array($n['pid'], $groupIds)) {
|
||||
continue;
|
||||
}
|
||||
$groupList = array_merge($groupList, Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['pid'])));
|
||||
foreach ($groupList as $index => $item) {
|
||||
$groupIds[] = $item['id'];
|
||||
}
|
||||
}
|
||||
}
|
||||
$groupName = [];
|
||||
foreach ($groupList as $k => $v) {
|
||||
$groupName[$v['id']] = $v['name'];
|
||||
}
|
||||
|
||||
$this->grouplist = $groupList;
|
||||
$this->groupdata = $groupName;
|
||||
$this->assignconfig("admin", ['id' => $this->auth->id, 'group_ids' => $this->auth->getGroupIds()]);
|
||||
|
||||
$this->view->assign('groupdata', $this->groupdata);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if ($this->request->isAjax()) {
|
||||
$list = $this->grouplist;
|
||||
$total = count($list);
|
||||
$result = array("total" => $total, "rows" => $list);
|
||||
|
||||
return json($result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
$params = $this->request->post("row/a", [], 'strip_tags');
|
||||
$params['rules'] = explode(',', $params['rules']);
|
||||
if (!in_array($params['pid'], $this->childrenGroupIds)) {
|
||||
$this->error(__('The parent group exceeds permission limit'));
|
||||
}
|
||||
$parentmodel = model("AuthGroup")->get($params['pid']);
|
||||
if (!$parentmodel) {
|
||||
$this->error(__('The parent group can not found'));
|
||||
}
|
||||
// 父级别的规则节点
|
||||
$parentrules = explode(',', $parentmodel->rules);
|
||||
// 当前组别的规则节点
|
||||
$currentrules = $this->auth->getRuleIds();
|
||||
$rules = $params['rules'];
|
||||
// 如果父组不是超级管理员则需要过滤规则节点,不能超过父组别的权限
|
||||
$rules = in_array('*', $parentrules) ? $rules : array_intersect($parentrules, $rules);
|
||||
// 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
|
||||
$rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
|
||||
$params['rules'] = implode(',', $rules);
|
||||
if ($params) {
|
||||
$this->model->create($params);
|
||||
$this->success();
|
||||
}
|
||||
$this->error();
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
if (!in_array($ids, $this->childrenGroupIds)) {
|
||||
$this->error(__('You have no permission'));
|
||||
}
|
||||
$row = $this->model->get(['id' => $ids]);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
$params = $this->request->post("row/a", [], 'strip_tags');
|
||||
//父节点不能是非权限内节点
|
||||
if (!in_array($params['pid'], $this->childrenGroupIds)) {
|
||||
$this->error(__('The parent group exceeds permission limit'));
|
||||
}
|
||||
// 父节点不能是它自身的子节点或自己本身
|
||||
if (in_array($params['pid'], Tree::instance()->getChildrenIds($row->id, true))) {
|
||||
$this->error(__('The parent group can not be its own child or itself'));
|
||||
}
|
||||
$params['rules'] = explode(',', $params['rules']);
|
||||
|
||||
$parentmodel = model("AuthGroup")->get($params['pid']);
|
||||
if (!$parentmodel) {
|
||||
$this->error(__('The parent group can not found'));
|
||||
}
|
||||
// 父级别的规则节点
|
||||
$parentrules = explode(',', $parentmodel->rules);
|
||||
// 当前组别的规则节点
|
||||
$currentrules = $this->auth->getRuleIds();
|
||||
$rules = $params['rules'];
|
||||
// 如果父组不是超级管理员则需要过滤规则节点,不能超过父组别的权限
|
||||
$rules = in_array('*', $parentrules) ? $rules : array_intersect($parentrules, $rules);
|
||||
// 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
|
||||
$rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
|
||||
$params['rules'] = implode(',', $rules);
|
||||
if ($params) {
|
||||
Db::startTrans();
|
||||
try {
|
||||
$row->save($params);
|
||||
$children_auth_groups = model("AuthGroup")->all(['id' => ['in', implode(',', (Tree::instance()->getChildrenIds($row->id)))]]);
|
||||
$childparams = [];
|
||||
foreach ($children_auth_groups as $key => $children_auth_group) {
|
||||
$childparams[$key]['id'] = $children_auth_group->id;
|
||||
$childparams[$key]['rules'] = implode(',', array_intersect(explode(',', $children_auth_group->rules), $rules));
|
||||
}
|
||||
model("AuthGroup")->saveAll($childparams);
|
||||
Db::commit();
|
||||
$this->success();
|
||||
} catch (Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
$this->error();
|
||||
return;
|
||||
}
|
||||
$this->view->assign("row", $row);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
public function del($ids = "")
|
||||
{
|
||||
if (!$this->request->isPost()) {
|
||||
$this->error(__("Invalid parameters"));
|
||||
}
|
||||
$ids = $ids ? $ids : $this->request->post("ids");
|
||||
if ($ids) {
|
||||
$ids = explode(',', $ids);
|
||||
$grouplist = $this->auth->getGroups();
|
||||
$group_ids = array_map(function ($group) {
|
||||
return $group['id'];
|
||||
}, $grouplist);
|
||||
// 移除掉当前管理员所在组别
|
||||
$ids = array_diff($ids, $group_ids);
|
||||
|
||||
// 循环判断每一个组别是否可删除
|
||||
$grouplist = $this->model->where('id', 'in', $ids)->select();
|
||||
$groupaccessmodel = model('AuthGroupAccess');
|
||||
foreach ($grouplist as $k => $v) {
|
||||
// 当前组别下有管理员
|
||||
$groupone = $groupaccessmodel->get(['group_id' => $v['id']]);
|
||||
if ($groupone) {
|
||||
$ids = array_diff($ids, [$v['id']]);
|
||||
continue;
|
||||
}
|
||||
// 当前组别下有子组别
|
||||
$groupone = $this->model->get(['pid' => $v['id']]);
|
||||
if ($groupone) {
|
||||
$ids = array_diff($ids, [$v['id']]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (!$ids) {
|
||||
$this->error(__('You can not delete group that contain child group and administrators'));
|
||||
}
|
||||
$count = $this->model->where('id', 'in', $ids)->delete();
|
||||
if ($count) {
|
||||
$this->success();
|
||||
}
|
||||
}
|
||||
$this->error();
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新
|
||||
* @internal
|
||||
*/
|
||||
public function multi($ids = "")
|
||||
{
|
||||
// 组别禁止批量操作
|
||||
$this->error();
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取角色权限树
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function roletree()
|
||||
{
|
||||
$this->loadlang('auth/group');
|
||||
|
||||
$model = model('AuthGroup');
|
||||
$id = $this->request->post("id");
|
||||
$pid = $this->request->post("pid");
|
||||
$parentGroupModel = $model->get($pid);
|
||||
$currentGroupModel = null;
|
||||
if ($id) {
|
||||
$currentGroupModel = $model->get($id);
|
||||
}
|
||||
if (($pid || $parentGroupModel) && (!$id || $currentGroupModel)) {
|
||||
$id = $id ? $id : null;
|
||||
$ruleList = collection(model('AuthRule')->order('weigh', 'desc')->order('id', 'asc')->select())->toArray();
|
||||
//读取父类角色所有节点列表
|
||||
$parentRuleList = [];
|
||||
if (in_array('*', explode(',', $parentGroupModel->rules))) {
|
||||
$parentRuleList = $ruleList;
|
||||
} else {
|
||||
$parentRuleIds = explode(',', $parentGroupModel->rules);
|
||||
foreach ($ruleList as $k => $v) {
|
||||
if (in_array($v['id'], $parentRuleIds)) {
|
||||
$parentRuleList[] = $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$ruleTree = new Tree();
|
||||
$groupTree = new Tree();
|
||||
//当前所有正常规则列表
|
||||
$ruleTree->init($parentRuleList);
|
||||
//角色组列表
|
||||
$groupTree->init(collection(model('AuthGroup')->where('id', 'in', $this->childrenGroupIds)->select())->toArray());
|
||||
|
||||
//读取当前角色下规则ID集合
|
||||
$adminRuleIds = $this->auth->getRuleIds();
|
||||
//是否是超级管理员
|
||||
$superadmin = $this->auth->isSuperAdmin();
|
||||
//当前拥有的规则ID集合
|
||||
$currentRuleIds = $id ? explode(',', $currentGroupModel->rules) : [];
|
||||
|
||||
if (!$id || !in_array($pid, $this->childrenGroupIds) || !in_array($pid, $groupTree->getChildrenIds($id, true))) {
|
||||
$parentRuleList = $ruleTree->getTreeList($ruleTree->getTreeArray(0), 'name');
|
||||
$hasChildrens = [];
|
||||
foreach ($parentRuleList as $k => $v) {
|
||||
if ($v['haschild']) {
|
||||
$hasChildrens[] = $v['id'];
|
||||
}
|
||||
}
|
||||
$parentRuleIds = array_map(function ($item) {
|
||||
return $item['id'];
|
||||
}, $parentRuleList);
|
||||
$nodeList = [];
|
||||
foreach ($parentRuleList as $k => $v) {
|
||||
if (!$superadmin && !in_array($v['id'], $adminRuleIds)) {
|
||||
continue;
|
||||
}
|
||||
if ($v['pid'] && !in_array($v['pid'], $parentRuleIds)) {
|
||||
continue;
|
||||
}
|
||||
$state = array('selected' => in_array($v['id'], $currentRuleIds) && !in_array($v['id'], $hasChildrens));
|
||||
$nodeList[] = array('id' => $v['id'], 'parent' => $v['pid'] ? $v['pid'] : '#', 'text' => __($v['title']), 'type' => 'menu', 'state' => $state);
|
||||
}
|
||||
$this->success('', null, $nodeList);
|
||||
} else {
|
||||
$this->error(__('Can not change the parent to child'));
|
||||
}
|
||||
} else {
|
||||
$this->error(__('Group not found'));
|
||||
}
|
||||
}
|
||||
}
|
||||
157
application/admin/controller/auth/Rule.php
Normal file
157
application/admin/controller/auth/Rule.php
Normal file
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\auth;
|
||||
|
||||
use app\admin\model\AuthRule;
|
||||
use app\common\controller\Backend;
|
||||
use fast\Tree;
|
||||
use think\Cache;
|
||||
|
||||
/**
|
||||
* 规则管理
|
||||
*
|
||||
* @icon fa fa-list
|
||||
* @remark 规则通常对应一个控制器的方法,同时左侧的菜单栏数据也从规则中体现,通常建议通过控制台进行生成规则节点
|
||||
*/
|
||||
class Rule extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \app\admin\model\AuthRule
|
||||
*/
|
||||
protected $model = null;
|
||||
protected $rulelist = [];
|
||||
protected $multiFields = 'ismenu,status';
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
if (!$this->auth->isSuperAdmin()) {
|
||||
$this->error(__('Access is allowed only to the super management group'));
|
||||
}
|
||||
$this->model = model('AuthRule');
|
||||
// 必须将结果集转换为数组
|
||||
$ruleList = \think\Db::name("auth_rule")->field('type,condition,remark,createtime,updatetime', true)->order('weigh DESC,id ASC')->select();
|
||||
foreach ($ruleList as $k => &$v) {
|
||||
$v['title'] = __($v['title']);
|
||||
}
|
||||
unset($v);
|
||||
Tree::instance()->init($ruleList)->icon = [' ', ' ', ' '];
|
||||
$this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
|
||||
$ruledata = [0 => __('None')];
|
||||
foreach ($this->rulelist as $k => &$v) {
|
||||
if (!$v['ismenu']) {
|
||||
continue;
|
||||
}
|
||||
$ruledata[$v['id']] = $v['title'];
|
||||
unset($v['spacer']);
|
||||
}
|
||||
unset($v);
|
||||
$this->view->assign('ruledata', $ruledata);
|
||||
$this->view->assign("menutypeList", $this->model->getMenutypeList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if ($this->request->isAjax()) {
|
||||
$list = $this->rulelist;
|
||||
$total = count($this->rulelist);
|
||||
$result = array("total" => $total, "rows" => $list);
|
||||
|
||||
return json($result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
$params = $this->request->post("row/a", [], 'strip_tags');
|
||||
if ($params) {
|
||||
if (!$params['ismenu'] && !$params['pid']) {
|
||||
$this->error(__('The non-menu rule must have parent'));
|
||||
}
|
||||
$result = $this->model->validate()->save($params);
|
||||
if ($result === false) {
|
||||
$this->error($this->model->getError());
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
$this->error();
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
$row = $this->model->get(['id' => $ids]);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
$params = $this->request->post("row/a", [], 'strip_tags');
|
||||
if ($params) {
|
||||
if (!$params['ismenu'] && !$params['pid']) {
|
||||
$this->error(__('The non-menu rule must have parent'));
|
||||
}
|
||||
if ($params['pid'] == $row['id']) {
|
||||
$this->error(__('Can not change the parent to self'));
|
||||
}
|
||||
if ($params['pid'] != $row['pid']) {
|
||||
$childrenIds = Tree::instance()->init(collection(AuthRule::select())->toArray())->getChildrenIds($row['id']);
|
||||
if (in_array($params['pid'], $childrenIds)) {
|
||||
$this->error(__('Can not change the parent to child'));
|
||||
}
|
||||
}
|
||||
//这里需要针对name做唯一验证
|
||||
$ruleValidate = \think\Loader::validate('AuthRule');
|
||||
$ruleValidate->rule([
|
||||
'name' => 'require|unique:AuthRule,name,' . $row->id,
|
||||
]);
|
||||
$result = $row->validate()->save($params);
|
||||
if ($result === false) {
|
||||
$this->error($row->getError());
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
$this->error();
|
||||
}
|
||||
$this->view->assign("row", $row);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
public function del($ids = "")
|
||||
{
|
||||
if (!$this->request->isPost()) {
|
||||
$this->error(__("Invalid parameters"));
|
||||
}
|
||||
$ids = $ids ? $ids : $this->request->post("ids");
|
||||
if ($ids) {
|
||||
$delIds = [];
|
||||
foreach (explode(',', $ids) as $k => $v) {
|
||||
$delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, true));
|
||||
}
|
||||
$delIds = array_unique($delIds);
|
||||
$count = $this->model->where('id', 'in', $delIds)->delete();
|
||||
if ($count) {
|
||||
Cache::rm('__menu__');
|
||||
$this->success();
|
||||
}
|
||||
}
|
||||
$this->error();
|
||||
}
|
||||
}
|
||||
160
application/admin/controller/general/Attachment.php
Normal file
160
application/admin/controller/general/Attachment.php
Normal file
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\general;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
|
||||
/**
|
||||
* 附件管理
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
* @remark 主要用于管理上传到服务器或第三方存储的数据
|
||||
*/
|
||||
class Attachment extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \app\common\model\Attachment
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
protected $searchFields = 'id,filename,url';
|
||||
protected $noNeedRight = ['classify'];
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = model('Attachment');
|
||||
$this->view->assign("mimetypeList", \app\common\model\Attachment::getMimetypeList());
|
||||
$this->view->assign("categoryList", \app\common\model\Attachment::getCategoryList());
|
||||
$this->assignconfig("categoryList", \app\common\model\Attachment::getCategoryList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
if ($this->request->isAjax()) {
|
||||
$mimetypeQuery = [];
|
||||
$filter = $this->request->request('filter');
|
||||
$filterArr = (array)json_decode($filter, true);
|
||||
if (isset($filterArr['category']) && $filterArr['category'] == 'unclassed') {
|
||||
$filterArr['category'] = ',unclassed';
|
||||
$this->request->get(['filter' => json_encode(array_diff_key($filterArr, ['category' => '']))]);
|
||||
}
|
||||
if (isset($filterArr['mimetype']) && preg_match("/(\/|\,|\*)/", $filterArr['mimetype'])) {
|
||||
$mimetype = $filterArr['mimetype'];
|
||||
$filterArr = array_diff_key($filterArr, ['mimetype' => '']);
|
||||
$mimetypeQuery = function ($query) use ($mimetype) {
|
||||
$mimetypeArr = array_filter(explode(',', $mimetype));
|
||||
foreach ($mimetypeArr as $index => $item) {
|
||||
$query->whereOr('mimetype', 'like', '%' . str_replace("/*", "/", $item) . '%');
|
||||
}
|
||||
};
|
||||
}
|
||||
$this->request->get(['filter' => json_encode($filterArr)]);
|
||||
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
|
||||
$list = $this->model
|
||||
->where($mimetypeQuery)
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
|
||||
$cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
|
||||
foreach ($list as $k => &$v) {
|
||||
$v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
|
||||
}
|
||||
unset($v);
|
||||
$result = array("total" => $list->total(), "rows" => $list->items());
|
||||
|
||||
return json($result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 选择附件
|
||||
*/
|
||||
public function select()
|
||||
{
|
||||
if ($this->request->isAjax()) {
|
||||
return $this->index();
|
||||
}
|
||||
$mimetype = $this->request->get('mimetype', '');
|
||||
$mimetype = substr($mimetype, -1) === '/' ? $mimetype . '*' : $mimetype;
|
||||
$this->view->assign('mimetype', $mimetype);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if ($this->request->isAjax()) {
|
||||
$this->error();
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除附件
|
||||
* @param array $ids
|
||||
*/
|
||||
public function del($ids = "")
|
||||
{
|
||||
if (!$this->request->isPost()) {
|
||||
$this->error(__("Invalid parameters"));
|
||||
}
|
||||
$ids = $ids ? $ids : $this->request->post("ids");
|
||||
if ($ids) {
|
||||
\think\Hook::add('upload_delete', function ($params) {
|
||||
if ($params['storage'] == 'local') {
|
||||
$attachmentFile = ROOT_PATH . '/public' . $params['url'];
|
||||
if (is_file($attachmentFile)) {
|
||||
@unlink($attachmentFile);
|
||||
}
|
||||
}
|
||||
});
|
||||
$attachmentlist = $this->model->where('id', 'in', $ids)->select();
|
||||
foreach ($attachmentlist as $attachment) {
|
||||
\think\Hook::listen("upload_delete", $attachment);
|
||||
$attachment->delete();
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', 'ids'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 归类
|
||||
*/
|
||||
public function classify()
|
||||
{
|
||||
if (!$this->auth->check('general/attachment/edit')) {
|
||||
\think\Hook::listen('admin_nopermission', $this);
|
||||
$this->error(__('You have no permission'), '');
|
||||
}
|
||||
if (!$this->request->isPost()) {
|
||||
$this->error(__("Invalid parameters"));
|
||||
}
|
||||
$category = $this->request->post('category', '');
|
||||
$ids = $this->request->post('ids');
|
||||
if (!$ids) {
|
||||
$this->error(__('Parameter %s can not be empty', 'ids'));
|
||||
}
|
||||
$categoryList = \app\common\model\Attachment::getCategoryList();
|
||||
if ($category && !isset($categoryList[$category])) {
|
||||
$this->error(__('Category not found'));
|
||||
}
|
||||
$category = $category == 'unclassed' ? '' : $category;
|
||||
\app\common\model\Attachment::where('id', 'in', $ids)->update(['category' => $category]);
|
||||
$this->success();
|
||||
}
|
||||
|
||||
}
|
||||
311
application/admin/controller/general/Config.php
Normal file
311
application/admin/controller/general/Config.php
Normal file
@@ -0,0 +1,311 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\general;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\Email;
|
||||
use app\common\model\Config as ConfigModel;
|
||||
use think\Cache;
|
||||
use think\Db;
|
||||
use think\Exception;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* 系统配置
|
||||
*
|
||||
* @icon fa fa-cogs
|
||||
* @remark 可以在此增改系统的变量和分组,也可以自定义分组和变量,如果需要删除请从数据库中删除
|
||||
*/
|
||||
class Config extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \app\common\model\Config
|
||||
*/
|
||||
protected $model = null;
|
||||
protected $noNeedRight = ['check', 'rulelist', 'selectpage', 'get_fields_list'];
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
// $this->model = model('Config');
|
||||
$this->model = new ConfigModel;
|
||||
ConfigModel::event('before_write', function ($row) {
|
||||
if (isset($row['name']) && $row['name'] == 'name' && preg_match("/fast" . "admin/i", $row['value'])) {
|
||||
throw new Exception(__("Site name incorrect"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$siteList = [];
|
||||
$groupList = ConfigModel::getGroupList();
|
||||
foreach ($groupList as $k => $v) {
|
||||
$siteList[$k]['name'] = $k;
|
||||
$siteList[$k]['title'] = $v;
|
||||
$siteList[$k]['list'] = [];
|
||||
}
|
||||
|
||||
foreach ($this->model->all() as $k => $v) {
|
||||
if (!isset($siteList[$v['group']])) {
|
||||
continue;
|
||||
}
|
||||
$value = $v->toArray();
|
||||
$value['title'] = __($value['title']);
|
||||
if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio'])) {
|
||||
$value['value'] = explode(',', $value['value']);
|
||||
}
|
||||
$value['content'] = json_decode($value['content'], true);
|
||||
if (in_array($value['name'], ['categorytype', 'configgroup', 'attachmentcategory'])) {
|
||||
$dictValue = (array)json_decode($value['value'], true);
|
||||
foreach ($dictValue as $index => &$item) {
|
||||
$item = __($item);
|
||||
}
|
||||
unset($item);
|
||||
$value['value'] = json_encode($dictValue, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
$value['tip'] = htmlspecialchars($value['tip']);
|
||||
if ($value['name'] == 'cdnurl') {
|
||||
//cdnurl不支持在线修改
|
||||
continue;
|
||||
}
|
||||
$siteList[$v['group']]['list'][] = $value;
|
||||
}
|
||||
$index = 0;
|
||||
foreach ($siteList as $k => &$v) {
|
||||
$v['active'] = !$index ? true : false;
|
||||
$index++;
|
||||
}
|
||||
$this->view->assign('siteList', $siteList);
|
||||
$this->view->assign('typeList', ConfigModel::getTypeList());
|
||||
$this->view->assign('ruleList', ConfigModel::getRegexList());
|
||||
$this->view->assign('groupList', ConfigModel::getGroupList());
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if (!config('app_debug')) {
|
||||
$this->error(__('Only work at development environment'));
|
||||
}
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
$params = $this->request->post("row/a", [], 'trim');
|
||||
if ($params) {
|
||||
foreach ($params as $k => &$v) {
|
||||
$v = is_array($v) && $k !== 'setting' ? implode(',', $v) : $v;
|
||||
}
|
||||
if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) {
|
||||
$params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
|
||||
} else {
|
||||
$params['content'] = '';
|
||||
}
|
||||
try {
|
||||
$result = $this->model->create($params);
|
||||
} catch (Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($result !== false) {
|
||||
try {
|
||||
ConfigModel::refreshFile();
|
||||
} catch (Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
$this->success();
|
||||
} else {
|
||||
$this->error($this->model->getError());
|
||||
}
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
* @param null $ids
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
$row = $this->request->post("row/a", [], 'trim');
|
||||
if ($row) {
|
||||
$configList = [];
|
||||
foreach ($this->model->all() as $v) {
|
||||
if (isset($row[$v['name']])) {
|
||||
$value = $row[$v['name']];
|
||||
if (is_array($value) && isset($value['field'])) {
|
||||
$value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
|
||||
} else {
|
||||
$value = is_array($value) ? implode(',', $value) : $value;
|
||||
}
|
||||
$v['value'] = $value;
|
||||
$configList[] = $v->toArray();
|
||||
}
|
||||
}
|
||||
try {
|
||||
$this->model->allowField(true)->saveAll($configList);
|
||||
} catch (Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
try {
|
||||
ConfigModel::refreshFile();
|
||||
} catch (Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
$this->success();
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @param string $ids
|
||||
*/
|
||||
public function del($ids = "")
|
||||
{
|
||||
if (!config('app_debug')) {
|
||||
$this->error(__('Only work at development environment'));
|
||||
}
|
||||
$name = $this->request->post('name');
|
||||
$config = ConfigModel::getByName($name);
|
||||
if ($name && $config) {
|
||||
try {
|
||||
$config->delete();
|
||||
ConfigModel::refreshFile();
|
||||
} catch (Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
$this->success();
|
||||
} else {
|
||||
$this->error(__('Invalid parameters'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测配置项是否存在
|
||||
* @internal
|
||||
*/
|
||||
public function check()
|
||||
{
|
||||
$params = $this->request->post("row/a");
|
||||
if ($params) {
|
||||
$config = $this->model->get($params);
|
||||
if (!$config) {
|
||||
$this->success();
|
||||
} else {
|
||||
$this->error(__('Name already exist'));
|
||||
}
|
||||
} else {
|
||||
$this->error(__('Invalid parameters'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 规则列表
|
||||
* @internal
|
||||
*/
|
||||
public function rulelist()
|
||||
{
|
||||
//主键
|
||||
$primarykey = $this->request->request("keyField");
|
||||
//主键值
|
||||
$keyValue = $this->request->request("keyValue", "");
|
||||
|
||||
$keyValueArr = array_filter(explode(',', $keyValue));
|
||||
$regexList = \app\common\model\Config::getRegexList();
|
||||
$list = [];
|
||||
foreach ($regexList as $k => $v) {
|
||||
if ($keyValueArr) {
|
||||
if (in_array($k, $keyValueArr)) {
|
||||
$list[] = ['id' => $k, 'name' => $v];
|
||||
}
|
||||
} else {
|
||||
$list[] = ['id' => $k, 'name' => $v];
|
||||
}
|
||||
}
|
||||
return json(['list' => $list]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送测试邮件
|
||||
* @internal
|
||||
*/
|
||||
public function emailtest()
|
||||
{
|
||||
$row = $this->request->post('row/a');
|
||||
$receiver = $this->request->post("receiver");
|
||||
if ($receiver) {
|
||||
if (!Validate::is($receiver, "email")) {
|
||||
$this->error(__('Please input correct email'));
|
||||
}
|
||||
\think\Config::set('site', array_merge(\think\Config::get('site'), $row));
|
||||
$email = new Email;
|
||||
$result = $email
|
||||
->to($receiver)
|
||||
->subject(__("This is a test mail", config('site.name')))
|
||||
->message('<div style="min-height:550px; padding: 100px 55px 200px;">' . __('This is a test mail content', config('site.name')) . '</div>')
|
||||
->send();
|
||||
if ($result) {
|
||||
$this->success();
|
||||
} else {
|
||||
$this->error($email->getError());
|
||||
}
|
||||
} else {
|
||||
$this->error(__('Invalid parameters'));
|
||||
}
|
||||
}
|
||||
|
||||
public function selectpage()
|
||||
{
|
||||
$id = $this->request->get("id/d");
|
||||
$config = \app\common\model\Config::get($id);
|
||||
if (!$config) {
|
||||
$this->error(__('Invalid parameters'));
|
||||
}
|
||||
$setting = $config['setting'];
|
||||
//自定义条件
|
||||
$custom = isset($setting['conditions']) ? (array)json_decode($setting['conditions'], true) : [];
|
||||
$custom = array_filter($custom);
|
||||
|
||||
$this->request->request(['showField' => $setting['field'], 'keyField' => $setting['primarykey'], 'custom' => $custom, 'searchField' => [$setting['field'], $setting['primarykey']]]);
|
||||
$this->model = \think\Db::connect()->setTable($setting['table']);
|
||||
return parent::selectpage();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取表列表
|
||||
* @internal
|
||||
*/
|
||||
public function get_table_list()
|
||||
{
|
||||
$tableList = [];
|
||||
$dbname = \think\Config::get('database.database');
|
||||
$tableList = \think\Db::query("SELECT `TABLE_NAME` AS `name`,`TABLE_COMMENT` AS `title` FROM `information_schema`.`TABLES` where `TABLE_SCHEMA` = '{$dbname}';");
|
||||
$this->success('', null, ['tableList' => $tableList]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取表字段列表
|
||||
* @internal
|
||||
*/
|
||||
public function get_fields_list()
|
||||
{
|
||||
$table = $this->request->request('table');
|
||||
$dbname = \think\Config::get('database.database');
|
||||
//从数据库中获取表字段信息
|
||||
$sql = "SELECT `COLUMN_NAME` AS `name`,`COLUMN_COMMENT` AS `title`,`DATA_TYPE` AS `type` FROM `information_schema`.`columns` WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? ORDER BY ORDINAL_POSITION";
|
||||
//加载主表的列
|
||||
$fieldList = Db::query($sql, [$dbname, $table]);
|
||||
$this->success("", null, ['fieldList' => $fieldList]);
|
||||
}
|
||||
}
|
||||
93
application/admin/controller/general/Crontab.php
Normal file
93
application/admin/controller/general/Crontab.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\general;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use Cron\CronExpression;
|
||||
|
||||
/**
|
||||
* 定时任务
|
||||
*
|
||||
* @icon fa fa-tasks
|
||||
* @remark 按照设定的时间进行任务的执行,目前支持三种任务:请求URL、执行SQL、执行Shell。
|
||||
*/
|
||||
class Crontab extends Backend
|
||||
{
|
||||
|
||||
protected $model = null;
|
||||
protected $noNeedRight = ['check_schedule', 'get_schedule_future'];
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = model('Crontab');
|
||||
$this->view->assign('typeList', \app\admin\model\Crontab::getTypeList());
|
||||
$this->assignconfig('typeList', \app\admin\model\Crontab::getTypeList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if ($this->request->isAjax()) {
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
$total = $this->model
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->count();
|
||||
|
||||
$list = $this->model
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->limit($offset, $limit)
|
||||
->select();
|
||||
$time = time();
|
||||
foreach ($list as $k => &$v) {
|
||||
$cron = CronExpression::factory($v['schedule']);
|
||||
$v['nexttime'] = $time > $v['endtime'] ? __('None') : $cron->getNextRunDate()->getTimestamp();
|
||||
}
|
||||
$result = array("total" => $total, "rows" => $list);
|
||||
|
||||
return json($result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断Crontab格式是否正确
|
||||
* @internal
|
||||
*/
|
||||
public function check_schedule()
|
||||
{
|
||||
$row = $this->request->post("row/a");
|
||||
$schedule = $row['schedule'] ?? '';
|
||||
if (CronExpression::isValidExpression($schedule)) {
|
||||
$this->success();
|
||||
} else {
|
||||
$this->error(__('Crontab format invalid'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据Crontab表达式读取未来七次的时间
|
||||
* @internal
|
||||
*/
|
||||
public function get_schedule_future()
|
||||
{
|
||||
$time = [];
|
||||
$schedule = $this->request->post('schedule');
|
||||
$days = (int)$this->request->post('days');
|
||||
try {
|
||||
$cron = CronExpression::factory($schedule);
|
||||
for ($i = 0; $i < $days; $i++) {
|
||||
$time[] = $cron->getNextRunDate(null, $i)->format('Y-m-d H:i:s');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
|
||||
}
|
||||
|
||||
$this->success("", null, ['futuretime' => $time]);
|
||||
}
|
||||
|
||||
}
|
||||
61
application/admin/controller/general/CrontabLog.php
Normal file
61
application/admin/controller/general/CrontabLog.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\general;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
|
||||
/**
|
||||
* 定时任务
|
||||
*
|
||||
* @icon fa fa-tasks
|
||||
* @remark 类似于Linux的Crontab定时任务,可以按照设定的时间进行任务的执行
|
||||
*/
|
||||
class CrontabLog extends Backend
|
||||
{
|
||||
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = model('CrontabLog');
|
||||
$this->view->assign('statusList', $this->model->getStatusList());
|
||||
$this->assignconfig('statusList', $this->model->getStatusList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if ($this->request->isAjax()) {
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
$total = $this->model
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->count();
|
||||
|
||||
$list = $this->model
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->limit($offset, $limit)
|
||||
->select();
|
||||
$list = collection($list)->toArray();
|
||||
$result = array("total" => $total, "rows" => $list);
|
||||
|
||||
return json($result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
public function detail($ids = null)
|
||||
{
|
||||
$row = $this->model->get($ids);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$this->view->assign("row", $row);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
}
|
||||
84
application/admin/controller/general/Profile.php
Normal file
84
application/admin/controller/general/Profile.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\general;
|
||||
|
||||
use app\admin\model\Admin;
|
||||
use app\common\controller\Backend;
|
||||
use fast\Random;
|
||||
use think\Session;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* 个人配置
|
||||
*
|
||||
* @icon fa fa-user
|
||||
*/
|
||||
class Profile extends Backend
|
||||
{
|
||||
|
||||
protected $searchFields = 'id,title';
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
if ($this->request->isAjax()) {
|
||||
$this->model = model('AdminLog');
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
|
||||
$list = $this->model
|
||||
->where($where)
|
||||
->where('admin_id', $this->auth->id)
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
|
||||
$result = array("total" => $list->total(), "rows" => $list->items());
|
||||
|
||||
return json($result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新个人信息
|
||||
*/
|
||||
public function update()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
$params = $this->request->post("row/a");
|
||||
$params = array_filter(array_intersect_key(
|
||||
$params,
|
||||
array_flip(array('email', 'nickname', 'password', 'avatar'))
|
||||
));
|
||||
unset($v);
|
||||
if (!Validate::is($params['email'], "email")) {
|
||||
$this->error(__("Please input correct email"));
|
||||
}
|
||||
if (isset($params['password'])) {
|
||||
if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
|
||||
$this->error(__("Please input correct password"));
|
||||
}
|
||||
$params['salt'] = Random::alnum();
|
||||
$params['password'] = md5(md5($params['password']) . $params['salt']);
|
||||
}
|
||||
$exist = Admin::where('email', $params['email'])->where('id', '<>', $this->auth->id)->find();
|
||||
if ($exist) {
|
||||
$this->error(__("Email already exists"));
|
||||
}
|
||||
if ($params) {
|
||||
$admin = Admin::get($this->auth->id);
|
||||
$admin->save($params);
|
||||
//因为个人资料面板读取的Session显示,修改自己资料后同时更新Session
|
||||
Session::set("admin", $admin->toArray());
|
||||
Session::set("admin.safecode", $this->auth->getEncryptSafecode($admin));
|
||||
$this->success();
|
||||
}
|
||||
$this->error();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
52
application/admin/controller/user/Group.php
Normal file
52
application/admin/controller/user/Group.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\user;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
|
||||
/**
|
||||
* 会员组管理
|
||||
*
|
||||
* @icon fa fa-users
|
||||
*/
|
||||
class Group extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \app\admin\model\UserGroup
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = model('UserGroup');
|
||||
$this->view->assign("statusList", $this->model->getStatusList());
|
||||
}
|
||||
|
||||
public function add()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
}
|
||||
$nodeList = \app\admin\model\UserRule::getTreeList();
|
||||
$this->assign("nodeList", $nodeList);
|
||||
return parent::add();
|
||||
}
|
||||
|
||||
public function edit($ids = null)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
}
|
||||
$row = $this->model->get($ids);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$rules = explode(',', $row['rules']);
|
||||
$nodeList = \app\admin\model\UserRule::getTreeList($rules);
|
||||
$this->assign("nodeList", $nodeList);
|
||||
return parent::edit($ids);
|
||||
}
|
||||
|
||||
}
|
||||
108
application/admin/controller/user/Rule.php
Normal file
108
application/admin/controller/user/Rule.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\user;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use fast\Tree;
|
||||
|
||||
/**
|
||||
* 会员规则管理
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Rule extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \app\admin\model\UserRule
|
||||
*/
|
||||
protected $model = null;
|
||||
protected $rulelist = [];
|
||||
protected $multiFields = 'ismenu,status';
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = model('UserRule');
|
||||
$this->view->assign("statusList", $this->model->getStatusList());
|
||||
// 必须将结果集转换为数组
|
||||
$ruleList = collection($this->model->order('weigh', 'desc')->select())->toArray();
|
||||
foreach ($ruleList as $k => &$v) {
|
||||
$v['title'] = __($v['title']);
|
||||
$v['remark'] = __($v['remark']);
|
||||
}
|
||||
unset($v);
|
||||
Tree::instance()->init($ruleList)->icon = [' ', ' ', ' '];
|
||||
$this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
|
||||
$ruledata = [0 => __('None')];
|
||||
foreach ($this->rulelist as $k => &$v) {
|
||||
if (!$v['ismenu']) {
|
||||
continue;
|
||||
}
|
||||
$ruledata[$v['id']] = $v['title'];
|
||||
}
|
||||
$this->view->assign('ruledata', $ruledata);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
if ($this->request->isAjax()) {
|
||||
$list = $this->rulelist;
|
||||
$total = count($this->rulelist);
|
||||
|
||||
$result = array("total" => $total, "rows" => $list);
|
||||
|
||||
return json($result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
}
|
||||
return parent::add();
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
}
|
||||
return parent::edit($ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
public function del($ids = "")
|
||||
{
|
||||
if (!$this->request->isPost()) {
|
||||
$this->error(__("Invalid parameters"));
|
||||
}
|
||||
$ids = $ids ? $ids : $this->request->post("ids");
|
||||
if ($ids) {
|
||||
$delIds = [];
|
||||
foreach (explode(',', $ids) as $k => $v) {
|
||||
$delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, true));
|
||||
}
|
||||
$delIds = array_unique($delIds);
|
||||
$count = $this->model->where('id', 'in', $delIds)->delete();
|
||||
if ($count) {
|
||||
$this->success();
|
||||
}
|
||||
}
|
||||
$this->error();
|
||||
}
|
||||
|
||||
}
|
||||
105
application/admin/controller/user/User.php
Normal file
105
application/admin/controller/user/User.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\user;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\Auth;
|
||||
|
||||
/**
|
||||
* 会员管理
|
||||
*
|
||||
* @icon fa fa-user
|
||||
*/
|
||||
class User extends Backend
|
||||
{
|
||||
|
||||
protected $relationSearch = true;
|
||||
protected $searchFields = 'id,username,nickname';
|
||||
|
||||
/**
|
||||
* @var \app\admin\model\User
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\User;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
if ($this->request->isAjax()) {
|
||||
//如果发送的来源是Selectpage,则转发到Selectpage
|
||||
if ($this->request->request('keyField')) {
|
||||
return $this->selectpage();
|
||||
}
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
$list = $this->model
|
||||
->with('group')
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
foreach ($list as $k => $v) {
|
||||
$v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
|
||||
$v->hidden(['password', 'salt']);
|
||||
}
|
||||
$result = array("total" => $list->total(), "rows" => $list->items());
|
||||
|
||||
return json($result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
}
|
||||
return parent::add();
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$this->token();
|
||||
}
|
||||
$row = $this->model->get($ids);
|
||||
$this->modelValidate = true;
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
|
||||
return parent::edit($ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
public function del($ids = "")
|
||||
{
|
||||
if (!$this->request->isPost()) {
|
||||
$this->error(__("Invalid parameters"));
|
||||
}
|
||||
$ids = $ids ? $ids : $this->request->post("ids");
|
||||
$row = $this->model->get($ids);
|
||||
$this->modelValidate = true;
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
Auth::instance()->delete($row['id']);
|
||||
$this->success();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user