Skip to content

Commit c5ea982

Browse files
committed
feat: Crud的index,read,delete支持扩展
1 parent f931769 commit c5ea982

File tree

6 files changed

+67
-16
lines changed

6 files changed

+67
-16
lines changed

Diff for: .example.env

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ REDIS_SELECT = 0
2323

2424
[FILESYSTEM]
2525
DRIVER = qiniu
26-
QINIU_ACCESS_KEY = aaaaa
27-
QINIU_SECRET_KEY = sssss
28-
QINIU_BUCKET = resstdp
29-
QINIU_DOMAIN = resstdp.51os.xyz
26+
QINIU_ACCESS_KEY = ak
27+
QINIU_SECRET_KEY = sk
28+
QINIU_BUCKET = res-gen-51os
29+
QINIU_DOMAIN = res.gen.51os.xyz
3030

3131
[JWT]
3232
SECRET = c884cf72e91cd5cb5fdc2e76d424257c

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
标准化项目实践
1+
Gen接口端
22
===============
33

44
> 运行环境要求PHP7.4+,兼容PHP8.0。

Diff for: admin.http

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,29 @@ content-type: application/json
1515

1616
###
1717
#CRUD - CRUD列表
18-
GET {{baseUrl}}/crud/admin_log
18+
GET {{baseUrl}}/crud/product_brand
1919
Authorization: Bearer {{token}}
2020

2121
###
2222
#CRUD - CRUD读取
23-
GET {{baseUrl}}/crud/admin_log/1
23+
GET {{baseUrl}}/crud/product_brand/1
2424
Authorization: Bearer {{token}}
2525

2626

2727
###
2828
#CRUD - CRUD更新
29-
PUT {{baseUrl}}/crud/admin_log/1
29+
PUT {{baseUrl}}/crud/product_brand/1
3030
Authorization: Bearer {{token}}
3131

3232

3333
###
3434
#REST - GET操作
35-
GET {{baseUrl}}/rest/admin_log/act/1
35+
GET {{baseUrl}}/rest/product_brand/act/1
3636
Authorization: Bearer {{token}}
3737

3838
###
3939
#REST - POST操作
40-
POST {{baseUrl}}/rest/admin_log/act/1
40+
POST {{baseUrl}}/rest/product_brand/act/1
4141
Authorization: Bearer {{token}}
4242

4343
###

Diff for: app/controller/admin/Crud.php

+41-5
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,31 @@ public function index()
3434
$table->cols->filter(fn($col) => empty($col->hide_in_search))->column('data_index'),
3535
['filter', 'sorter']
3636
), 'get');
37+
3738
$total = $this->model->withSearch(array_keys($search), $search)->count();
38-
$list = $this->model->withSearch(array_keys($search), $search)->with(
39+
$objs = $this->model->withSearch(array_keys($search), $search)->with(
3940
$table->cols->filter(fn($col) => empty($col->hide_in_table) && !empty($col->relation_name))->column('relation_name') ?: []
4041
)->page($current, $pageSize)->select();
42+
$data = [];
43+
foreach ($objs as $obj) {
44+
$data[] = array_merge_recursive($obj->visible(array_merge([$this->model->getPk()], $table->crud_index_cols))->toArray(), $this->mergeIndex($obj));
45+
}
4146

4247
return $this->success([
4348
'total' => $total,
44-
'data' => $list->visible(array_merge([$this->model->getPk()], $table->crud_index_cols))->toArray()
49+
'data' => $data
4550
]);
4651
}
52+
/**
53+
* 列表的合并数据(扩展列表返回)
54+
* @access public
55+
* @param \think\Model $obj 模型对象
56+
* @return array
57+
*/
58+
public function mergeIndex($obj)
59+
{
60+
return [];
61+
}
4762

4863
/**
4964
* @api {POST} /crud/:table 创建
@@ -57,7 +72,6 @@ public function create()
5772
$table = TableModel::find(parse_name(string_remove_prefix($this->request->controller(), 'admin.'), 0));
5873
$data = $this->request->post($table->cols->filter(fn($col) => empty($col->hide_in_form))->column('data_index'));
5974

60-
// 创建
6175
$this->model->create($data);
6276

6377
return $this->success();
@@ -78,7 +92,19 @@ public function read($id)
7892
throw new ModelNotFoundException('数据不存在');
7993
}
8094

81-
return $this->success($obj->visible(array_merge([$this->model->getPk()], $table->crud_read_cols))->toArray());
95+
return $this->success(
96+
array_merge_recursive($obj->visible(array_merge([$this->model->getPk()], $table->crud_read_cols))->toArray(), $this->mergeRead($obj))
97+
);
98+
}
99+
/**
100+
* 读取的合并数据(扩展读取返回)
101+
* @access public
102+
* @param \think\Model $obj 模型对象
103+
* @return array
104+
*/
105+
public function mergeRead($obj)
106+
{
107+
return [];
82108
}
83109

84110
/**
@@ -116,8 +142,18 @@ public function delete($ids)
116142
}
117143

118144
foreach ($objs as $obj) {
119-
$obj->delete();
145+
$this->eachDelete($obj);
120146
}
147+
121148
return $this->success();
122149
}
150+
/**
151+
* 删除的模型处理(扩展删除操作)
152+
* @access public
153+
* @param \think\Model $obj 模型对象
154+
*/
155+
public function eachDelete($obj)
156+
{
157+
$obj->delete();
158+
}
123159
}

Diff for: app/model/Col.php

+15
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ public function getFormilySchemaAttr($value, $data)
9494
array_keys(system_dict($data['value_enum_dict_key'])),
9595
array_values(system_dict($data['value_enum_dict_key']))
9696
);
97+
if ($data['value_type'] == 'avatar') {
98+
$schema['x-component-props'] = [
99+
'accept' => 'image/*',
100+
'listType' => 'picture-card',
101+
'multiple' => false,
102+
'maxCount' => 1,
103+
];
104+
}
105+
if ($data['value_type'] == 'image') {
106+
$schema['x-component-props'] = [
107+
'accept' => 'image/*',
108+
'listType' => 'picture-card',
109+
'multiple' => true,
110+
];
111+
}
97112
return $schema;
98113
}
99114

Diff for: composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "stdp/stdp-tp6",
2+
"name": "3xdev/gen-tp6",
33
"description": "standard project by thinkphp6",
44
"type": "project",
55
"keywords": [

0 commit comments

Comments
 (0)