pull/8/head
pppscn 2018-09-11 14:26:37 +08:00
commit 7dbe39ca9f
6 changed files with 152 additions and 31 deletions

View File

@ -329,8 +329,8 @@ class Crud extends Command
$controllerUrl = strtolower(implode('/', $controllerArr));
$controllerBaseName = strtolower(implode(DS, $controllerArr));
//视图文件
$viewDir = $adminPath . 'view' . DS . $controllerBaseName . DS;
//视图文件 -- 如果控制器名字为驼峰形式,目录名需要转为下划线
$viewDir = $adminPath . 'view' . DS . strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', implode(DS, $controllerArr))) . DS;
//最终将生成的文件路径
$javascriptFile = ROOT_PATH . 'public' . DS . 'assets' . DS . 'js' . DS . 'backend' . DS . $controllerBaseName . '.js';

View File

@ -197,6 +197,13 @@ class Menu extends Command
if (in_array('trashed', get_class_methods($model))) {
$withSofeDelete = true;
}
} else {
preg_match_all("/\\\$this\->model\s*=\s*new\s+([a-zA-Z\\\]+);/", $classContent, $matches);
\think\Request::instance()->module('admin');
$model = new $matches[1][0];
if (in_array('trashed', get_class_methods($model))) {
$withSofeDelete = true;
}
}
//忽略的类
if (stripos($classComment, "@internal") !== FALSE) {
@ -226,7 +233,13 @@ class Menu extends Command
$pid = 0;
foreach ($controllerArr as $k => $v) {
$key = $k + 1;
$name = strtolower(implode('/', array_slice($controllerArr, 0, $key)));
//驼峰转下划线
$controllerNameArr = array_slice($controllerArr, 0, $key);
foreach ($controllerNameArr as &$val) {
$val = strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $val), "_"));
}
unset($val);
$name = implode('/', $controllerNameArr);
$title = (!isset($controllerArr[$key]) ? $controllerTitle : '');
$icon = (!isset($controllerArr[$key]) ? $controllerIcon : 'fa fa-list');
$remark = (!isset($controllerArr[$key]) ? $controllerRemark : '');

View File

@ -113,7 +113,7 @@ if (!function_exists('build_toolbar')) {
'add' => ['javascript:;', 'btn btn-success btn-add', 'fa fa-plus', __('Add'), __('Add')],
'edit' => ['javascript:;', 'btn btn-success btn-edit btn-disabled disabled', 'fa fa-pencil', __('Edit'), __('Edit')],
'del' => ['javascript:;', 'btn btn-danger btn-del btn-disabled disabled', 'fa fa-trash', __('Delete'), __('Delete')],
'import' => ['javascript:;', 'btn btn-danger btn-import', 'fa fa-upload', __('Import'), __('Import')],
'import' => ['javascript:;', 'btn btn-info btn-import', 'fa fa-upload', __('Import'), __('Import')],
];
$btnAttr = array_merge($btnAttr, $attr);
$html = [];
@ -123,8 +123,39 @@ if (!function_exists('build_toolbar')) {
continue;
}
list($href, $class, $icon, $text, $title) = $btnAttr[$v];
$extend = $v == 'import' ? 'id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"' : '';
$html[] = '<a href="' . $href . '" class="' . $class . '" title="' . $title . '" ' . $extend . '><i class="' . $icon . '"></i> ' . $text . '</a>';
//$extend = $v == 'import' ? 'id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"' : '';
//$html[] = '<a href="' . $href . '" class="' . $class . '" title="' . $title . '" ' . $extend . '><i class="' . $icon . '"></i> ' . $text . '</a>';
if ($v == 'import') {
$template = str_replace('/', '_', $controller);
$download = '';
if (file_exists("./template/{$template}.xlsx")) {
$download .= "\n <li><a href=\"/template/{$template}.xlsx\" target=\"_blank\">XLSX模版</a></li>";
}
if (file_exists("./template/{$template}.xls")) {
$download .= "\n <li><a href=\"/template/{$template}.xls\" target=\"_blank\">XLS模版</a></li>";
}
if (file_exists("./template/{$template}.csv")) {
$download .= empty($download) ? '' : "\n <li class=\"divider\"></li>";
$download .= "\n <li><a href=\"/template/{$template}.csv\" target=\"_blank\">CSV模版</a></li>";
}
$download .= empty($download) ? '' : "\n ";
if (!empty($download)) {
$html[] = <<<EOT
<div class="btn-group">
<button type="button" href="{$href}" class="btn btn-info btn-import" title="{$title}" id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="{$icon}"></i> {$text}</button>
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" title="下载批量导入模版">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">{$download}</ul>
</div>
EOT;
} else {
$html[] = '<a href="' . $href . '" class="' . $class . '" title="' . $title . '" id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="' . $icon . '"></i> ' . $text . '</a>';
}
} else {
$html[] = '<a href="' . $href . '" class="' . $class . '" title="' . $title . '"><i class="' . $icon . '"></i> ' . $text . '</a>';
}
}
return implode(' ', $html);
}

View File

@ -10,6 +10,17 @@ use PhpOffice\PhpSpreadsheet\Reader\Csv;
trait Backend
{
//设置关联查询JOIN预查询
protected $withModel = [];
/**
* 空操作
* @internal
*/
public function _empty()
{
$this->error('好像迷路了……无法进行该操作!');
}
/**
* 查看
@ -24,6 +35,21 @@ trait Backend
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
if ($this->withModel) {
$total = $this->model
->with($this->withModel)
->where($where)
->order($sort, $order)
->count();
$list = $this->model
->with($this->withModel)
->where($where)
->order($sort, $order)
->limit($offset, $limit)
->select();
} else {
$total = $this->model
->where($where)
->order($sort, $order)
@ -34,6 +60,7 @@ trait Backend
->order($sort, $order)
->limit($offset, $limit)
->select();
}
$list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list);
@ -80,11 +107,13 @@ trait Backend
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
if (!isset($params['admin_id'])) {
$auth = Auth::instance();
$params['admin_id'] = $auth->isLogin() ? $auth->id : 0;
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
}
try {
//是否采用模型验证
if ($this->modelValidate) {
@ -116,11 +145,12 @@ trait Backend
/**
* 编辑
*/
public function edit($ids = NULL)
public function edit($ids = null)
{
$row = $this->model->get($ids);
if (!$row)
if (!$row) {
$this->error(__('No Results were found'));
}
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
if (!in_array($row[$this->dataLimitField], $adminIds)) {
@ -130,10 +160,12 @@ trait Backend
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$pk = $this->model->getPk();
$params[$pk] = $row[$pk];
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = basename(str_replace('\\', '/', get_class($this->model)));
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
$row->validate($validate);
}
@ -328,6 +360,7 @@ trait Backend
}
//加载文件
$insert = [];
try {
if (!$PHPExcel = $reader->load($filePath)) {
$this->error(__('Unknown data format'));
@ -336,17 +369,17 @@ trait Backend
$allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
$allRow = $currentSheet->getHighestRow(); //取得一共有多少行
$maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
$fields = [];
for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) {
for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
$val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
$fields[] = $val;
}
}
$insert = [];
for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
$values = [];
for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) {
for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
$val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
$values[] = is_null($val) ? '' : $val;
}
@ -369,6 +402,22 @@ trait Backend
}
try {
//是否包含admin_id字段
$has_admin_id = false;
foreach ($fieldArr as $name => $key) {
if ($key == 'admin_id') {
$has_admin_id = true;
break;
}
}
if ($has_admin_id) {
$auth = Auth::instance();
foreach ($insert as &$val) {
if (!isset($val['admin_id']) || empty($val['admin_id'])) {
$val['admin_id'] = $auth->isLogin() ? $auth->id : 0;
}
}
}
$this->model->saveAll($insert);
} catch (\think\exception\PDOException $exception) {
$msg = $exception->getMessage();

View File

@ -427,6 +427,7 @@ class Backend extends Controller
$custom = (array)$this->request->request("custom/a");
//是否返回树形结构
$istree = $this->request->request("isTree", 0);
$ishtml = $this->request->request("isHtml", 0);
if($istree) {
$word = [];
$pagesize = 99999;
@ -473,21 +474,48 @@ class Backend extends Controller
unset($item['password'], $item['salt']);
$list[] = [
$primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '',
$field => isset($item[$field]) ? $item[$field] : ''
$field => isset($item[$field]) ? $item[$field] : '',
'pid' => isset($item['pid']) ? $item['pid'] : 0
];
}
if($istree) {
$tree = Tree::instance();
$tree->init(collection($list)->toArray(), 'pid');
$list = $tree->getTreeList($tree->getTreeArray(0), 'name');
$list = $tree->getTreeList($tree->getTreeArray(0), $field);
if(!$ishtml){
foreach ($list as &$item) {
$item = str_replace('&nbsp;', ' ', $item);
}
unset($item);
}
}
}
//这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
return json(['list' => $list, 'total' => $total]);
}
/**
* 获取楼宇列表
*/
protected function building()
{
//TODO权限隔离
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
$where[] = ['admin_id', 'in', $adminIds];
}
$where[] = ['status', 'gt', 0];
$where = function ($query) use ($where) {
foreach ($where as $k => $v) {
if (is_array($v)) {
call_user_func_array([$query, 'where'], $v);
} else {
$query->where($v);
}
}
};
$building = model('building')->where($where)->field('id,building_no,building_name,image,address,total_room_area,total_room_num')->select();
$this->view->assign('building', $building);
}
}

View File

@ -134,7 +134,7 @@ class Menu
if ($menu)
{
// 必须将结果集转换为数组
$ruleList = collection(AuthRule::order('weigh', 'desc')->field('id,pid,name')->select())->toArray();
$ruleList = collection(AuthRule::order('weigh', 'desc')->order('id', 'asc')->field('id,pid,name')->select())->toArray();
// 构造菜单数据
$ids = Tree::instance()->init($ruleList)->getChildrenIds($menu['id'], true);
}