refactor:优化后台基类、完善注释

This commit is contained in:
妙码生花 2023-07-08 20:12:52 +08:00
parent 8b87f99971
commit cb9a97a3dc
2 changed files with 14 additions and 17 deletions

View File

@ -6,11 +6,17 @@
public function index(): void
{
$this->request->filter(['strip_tags', 'trim']);
// 如果是select则转发到select方法,若select未重写,其实还是继续执行index
// 如果是 select 则转发到 select 方法,若未重写该方法,其实还是继续执行 index
if ($this->request->param('select')) {
$this->select();
}
/**
* 1. withJoin 不可使用 alias 方法设置表别名,别名将自动使用关联模型名称(小写下划线命名规则)
* 2. 以下的别名设置了主表别名,同时便于拼接查询参数等
* 3. paginate 数据集可使用链式操作 each(function($item, $key) {}) 遍历处理
*/
list($where, $alias, $limit, $order) = $this->queryBuilder();
$res = $this->model
->withJoin($this->withJoinTable, $this->withJoinType)

View File

@ -187,18 +187,18 @@ class Backend extends Api
$order = $this->request->get("order/s", '');
$search = $this->request->get("search/a", []);
$initKey = $this->request->get("initKey/s", $pk);
$initValue = $this->request->get("initValue/a", '');
$initValue = $this->request->get("initValue", '');
$where = [];
$modelTable = strtolower($this->model->getTable());
$alias[$modelTable] = parse_name(basename(str_replace('\\', '/', get_class($this->model))));
$tableAlias = $alias[$modelTable] . '.';
$mainTableAlias = $alias[$modelTable] . '.';
// 快速搜索
if ($quickSearch) {
$quickSearchArr = is_array($this->quickSearchField) ? $this->quickSearchField : explode(',', $this->quickSearchField);
foreach ($quickSearchArr as $k => $v) {
$quickSearchArr[$k] = stripos($v, ".") === false ? $tableAlias . $v : $v;
$quickSearchArr[$k] = str_contains($v, '.') ? $v : $mainTableAlias . $v;
}
$where[] = [implode("|", $quickSearchArr), "LIKE", '%' . str_replace('%', '\%', $quickSearch) . '%'];
}
@ -210,7 +210,7 @@ class Backend extends Api
// 排序
if ($order) {
$order = explode(',', $order);
if (isset($order[0]) && isset($order[1]) && ($order[1] == 'asc' || $order[1] == 'desc')) {
if (!empty($order[0]) && !empty($order[1]) && ($order[1] == 'asc' || $order[1] == 'desc')) {
$order = [$order[0] => $order[1]];
}
} else {
@ -218,7 +218,7 @@ class Backend extends Api
$order = $this->defaultSortField;
} else {
$order = explode(',', $this->defaultSortField);
if (isset($order[0]) && isset($order[1])) {
if (!empty($order[0]) && !empty($order[1])) {
$order = [$order[0] => $order[1]];
} else {
$order = [$pk => 'desc'];
@ -232,13 +232,7 @@ class Backend extends Api
continue;
}
if (stripos($field['field'], '.') !== false) {
$fieldArr = explode('.', $field['field']);
$alias[$fieldArr[0]] = $fieldArr[0];
$fieldName = $field['field'];
} else {
$fieldName = $tableAlias . $field['field'];
}
$fieldName = str_contains($field['field'], '.') ? $field['field'] : $mainTableAlias . $field['field'];
// 日期时间
if (isset($field['render']) && $field['render'] == 'datetime') {
@ -257,9 +251,6 @@ class Backend extends Api
// 范围查询
if ($field['operator'] == 'RANGE' || $field['operator'] == 'NOT RANGE') {
if (stripos($field['val'], ',') === false) {
continue;
}
$arr = explode(',', $field['val']);
// 重新确定操作符
if (!isset($arr[0]) || $arr[0] === '') {
@ -313,7 +304,7 @@ class Backend extends Api
// 数据权限
$dataLimitAdminIds = $this->getDataLimitAdminIds();
if ($dataLimitAdminIds) {
$where[] = [$tableAlias . $this->dataLimitField, 'in', $dataLimitAdminIds];
$where[] = [$mainTableAlias . $this->dataLimitField, 'in', $dataLimitAdminIds];
}
return [$where, $alias, $limit, $order];