AreaForm.php
4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 管理端表单 trait层:渲染生成 小区 form 表单
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Forms\Area
* @package App\Admin\Forms\Area
* @author Richer <yangzi1028@163.com>
* @date 2021年1月27日11:03:37
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Forms\Area;
use App\Models\Area\Area;
use App\Models\Area\City;
use App\Models\Area\County;
use App\Models\Area\Group;
use App\Models\Area\Province;
use App\Models\Area\Township;
use App\Models\Area\Village;
use Encore\Admin\Form;
use Illuminate\Support\Facades\Hash;
/**
* Class AreaForm.
*
* @category App\Admin\Forms\Area
* @package App\Admin\Forms\Area
* @author Richer <yangzi1028@163.com>
* @date 2021年1月27日11:03:37
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
trait AreaForm
{
/**
* 渲染表单字段
*
* @param Int $id 用户id
*
* @return void
*/
public function renderFormFields($id)
{
if ($this->level > Area::LEVEL_PROVINCE) {
$this->generateFormSingleSelectField($this->form, 'province_id', __(Province::OBJ_NAME), Province::getSelectOptions(), 1)->load("city_id", "/admin/cities/select-options");
}
if ($this->level > Area::LEVEL_CITY) {
$this->generateFormSingleSelectField($this->form, 'city_id', __(City::OBJ_NAME), [], 1)->load("county_id", "/admin/counties/select-options");
}
if ($this->level > Area::LEVEL_COUNTY) {
$this->generateFormSingleSelectField($this->form, 'county_id', __(County::OBJ_NAME), [], 1)->load("township_id", "/admin/townships/select-options");
}
if ($this->level > Area::LEVEL_TOWNSHIP) {
$this->generateFormSingleSelectField($this->form, 'township_id', __(Township::OBJ_NAME), [], 1)->load("village_id", "/admin/villages/select-options");
}
if ($this->level > Area::LEVEL_VILLAGE) {
$this->generateFormSingleSelectField($this->form, 'village_id', __(Village::OBJ_NAME), [], 1);
}
$this->generateFormTextField($this->form, 'name', __($this->title). ' ' . strtolower(__('name')), 128, 1)
->creationRules(['required', "unique:{$this->table},name,NULL,id"])
->updateRules(['required', "unique:{$this->table},name,{{id}},id"]);
$this->generateFormNumberField($this->form, 'sort', __('sort'));
// $this->form->html("<a class='text text-success' style='margin-left: 5px' href='javascript:;' onclick='showMap()'>坐标拾取器</a>", "")->setWidth(10, 1);
// 在表单提交前调用
$this->form->submitted(function (Form $form) {
// $form->ignore(['username','password','password_confirmation','gid','ggid']);
});
// 保存前回调:
$this->form->saving(function (Form $form) {
// dd(request()->all());
// $form->ignore(['username','password','password_confirmation','gid','ggid']);
// 获取操作
$actions = request()->route()->getAction();
// 如果是以@store结尾则为新增操作
if (\Str::endsWith($actions['uses'], '@store')) {
// 获取code
// $code = Area::generateCode($this->level, $pcode);
// $form->model()->setAttribute('level', $this->level ?? 1);
}
});
// 保存后回调
$this->form->saved(
function (Form $form) {
// 获取操作
$actions = request()->route()->getAction();
// 设置 user 表 supplier_id
$model = $form->model();
// 如果是以@store结尾则为新增操作
if (\Str::endsWith($actions['uses'], '@store')) {
} elseif (\Str::endsWith($actions['uses'], '@update')) {
}
$model->save();
}
);
}
}