Слияние кода завершено, страница обновится автоматически
<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2020-9-28 10:55:00
// +----------------------------------------------------------------------
namespace buwang\traits;
use buwang\util\CommonTool;
use jianyan\excel\Excel;
use think\facade\Db;
use think\facade\View;
/**
* 后台CURD复用
* Trait Curd
* @package app\admin\traits
*/
trait Crud
{
/**
* @NodeAnotation(title="列表")
*/
public function index()
{
if ($this->request->isAjax()) {
if (input('selectFieds')) {
return $this->selectList();
}
list($page, $limit, $where) = $this->buildTableParames();
$count = $this->model
->where($where)
->count();
$list = $this->model
->where($where)
->page($page, $limit)
->order($this->sort)
->select();
$data = [
'total' => $count,
'list' => $list,
];
return $this->success('success', $data);
}
return view();
}
/**
* @NodeAnotation(title="添加")
*/
public function add()
{
if ($this->request->isAjax()) {
$post = $this->request->post();
$post = reform_keys($post);
$rule = [];
$this->validate($post, $rule);
try {
$save = $this->model->save($post);
} catch (\Exception $e) {
return $this->error('保存失败:' . $e->getMessage());
}
if ($save) return $this->success('保存成功');
else return $this->error('保存失败');
}
return view();
}
/**
* @NodeAnotation(title="编辑")
*/
public function edit($id)
{
$row = $this->model->find($id);
if ($row->isEmpty()) return $this->error('数据不存在');
if ($this->request->isAjax()) {
$post = $this->request->post();
$post = reform_keys($post);
$rule = [];
$this->validate($post, $rule);
try {
$save = $row->save($post);
} catch (\Exception $e) {
return $this->error('保存失败');
}
if ($save) return $this->success('保存成功');
else return $this->error('保存失败');
}
View::assign('row', $row);
return view();
}
/**
* @NodeAnotation(title="删除")
*/
public function del($id)
{
$row = $this->model->whereIn('id', $id)->select();
if ($row->isEmpty()) return $this->error('数据不存在');
try {
$save = $row->delete();
} catch (\Exception $e) {
return $this->error('删除失败');
}
if ($save) return $this->success('删除成功');
else return $this->error('删除失败');
}
/**
* @NodeAnotation(title="导出")
*/
public function export()
{
list($page, $limit, $where) = $this->buildTableParames();
$tableName = $this->model->getName();
$tableName = CommonTool::humpToLine(lcfirst($tableName));
$prefix = config('database.connections.mysql.prefix');
$dbList = Db::query("show full columns from {$prefix}{$tableName}");
$header = [];
foreach ($dbList as $vo) {
$comment = !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'];
if (!in_array($vo['Field'], $this->noExportFileds)) {
$header[] = [$comment, $vo['Field']];
}
}
$list = $this->model
->where($where)
->limit(100000)
->order('id', 'desc')
->select()
->toArray();
$fileName = time();
return Excel::exportData($list, $header, $fileName, 'xlsx');
}
/**
* @NodeAnotation(title="属性修改")
*/
public function modify()
{
$post = $this->request->post();
$rule = [
'id|ID' => 'require',
'field|字段' => 'require',
'value|值' => 'require',
];
$this->validate($post, $rule);
$row = $this->model->find($post['id']);
if (!$row) {
return $this->error('数据不存在');
}
if (!in_array($post['field'], $this->allowModifyFileds)) {
return $this->error('该字段不允许修改:' . $post['field']);
}
try {
$row->save([
$post['field'] => $post['value'],
]);
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
return $this->success('保存成功');
}
}
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Опубликовать ( 0 )