Basic.php
6.4 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* Steps 表单: Goods Step Form-基本信息
+-----------------------------------------------------------------------------------------------------------------------
* PHP version 7
*
* @category App\Admin\Forms\Steps\Goods
* @package App\Admin\Forms\Steps\Goods
* @author Richer <yangzi1028@163.com>
* @date 2022年5月7日15:14:47
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Forms\Steps\Goods;
use App\Admin\Traits\FormFieldTrait;
use App\Models\Goods\Goods;
use App\Models\Goods\GoodsCategory;
use Encore\Admin\Facades\Admin;
use App\Admin\Rewrite\Widgets\StepForm;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
/**
* Class Basic
*
* @category App\Admin\Forms\Steps\Goods
* @package App\Admin\Forms\Steps\Goods
* @author Richer <yangzi1028@163.com>
* @date 2022年5月7日15:14:47
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class Basic extends StepForm
{
use FormFieldTrait;
/**
* The form title.
*
* @var string
*/
public $title = '基本信息';
/**
* The form model.
*
* @var string
*/
public $model = null;
/**
* The form.
*
* @var string
*/
public $form = null;
/**
* Basic constructor.
*
* @param array $data
*/
public function __construct($data = [])
{
$this->model = new Goods();
$this->form = $this;
parent::__construct($data);
}
/**
* Handle the form request.
*
* @param Request $request
*
* @return RedirectResponse
*/
public function handle(Request $request)
{
session()->put("steps.goods.model", $this->model);
$basic = $this->save($request);
admin_toastr('分类设置成功,请维护商品规格!');
return $this->next($basic);
}
/**
* Build a form here.
*/
public function form()
{
Admin::script(' $(".step-next").on("click", function() {$(this).text("Loading...").addClass("disabled");});');
$id = $this->getId() ;
if ($id) {
$this->hidden('id')->default($this->getId());
}
$this->formDisplayField('id', __('ID'));
// $this->form->divider();
// $this->form->html('', '<div class="row" style="clear:both;"><h4 class="pull-left" style="font-weight: bold">'
// . __('goods_category') .
// '</h4></div>')->setWidth(1, 10)->setGroupClass('col-md-12');
$this->formSingleSelectField('category_id', __('goods_category_main'), GoodsCategory::getSelectOptions(), 1)
->load('specification_id', '/'.config('admin.route.prefix').'/goods-categories/specifications');
$this->formMultipleSelectField('category_ids', __('goods_category_other'), GoodsCategory::getSelectOptions(), 0);
// $this->form->divider();
// $this->form->html('', '<div class="row" style="clear:both;"><h4 class="pull-left" style="font-weight: bold">'
// . __('goods_specification') .
// '</h4></div>')->setWidth(1, 10)->setGroupClass('col-md-12');
$this->formSingleSelectField('specification_id', __('goods_specification'), [], 1);
// $this->formSingleImageField('cover_image', '商品封面图', 0);
// $this->formMultipleImageField('images', '商品主图', 0);
$this->formTextField('name', __('goods_name'), 100, 1, 1);
$this->formTextField('subname', __('goods_subname'), 100, 1, 1);
// $this->formTextField('origin_place', __('origin_place'), 100, 1, 2);
// $this->formTextField('shipment_place', __('shipment_place'), 100, 1, 2);
// $this->formTextAreaField('server', __('server'), 255, 3);
// $this->formTextAreaField('non_delivery_area', __('non_delivery_area'), 255, 3);
//
// $this->formRichTextField();
// $this->formTextAreaField();
$this->formCommonFields(1, 1);
// // 设置专用字段
// $this->renderSpecialFields();
}
/**
* 设置专业字段
*/
public function renderSpecialFields()
{
//
}
/**
* The data of the form.
*
* @return array $data
*/
public function data()
{
return optional($this->getBase())->toArray();
}
/**
* 获取 id
*
* @return int|null
*/
private function getId()
{
return optional($this->getBase())->id;
}
/**
* @return array
*/
protected function getBase()
{
// 获取上一部的数据
$url = str_replace('_pjax=%23pjax-container', '', request()->getUri()) ;
$id = request('id');
if (Str::contains($url, 'create') && Str::contains($url, 'step=base')) {
$model = Arr::get(session('steps'), 'base');
} else {
$model = $this->model->find($id);
}
return $model;
}
/**
* 保存方案:新增或者更新
*
* @param $request
* @return mixed
*/
public function save($request)
{
$data = $request->all();
$id = $request->id;
if (array_key_exists('images', $data)) {
unset($data['images']);
}
if (array_key_exists('url_images', $data)) {
// 文件
$data['images'] = my_json_encode($request->url_images);
unset($data['url_images']);
}
if (array_key_exists('name_images', $data)) {
unset($data['name_images']);
}
if (array_key_exists('file', $data)) {
unset($data['file']);
}
if ($id) {
// 更新
$model = $this->model->find($id);
$model->update($data);
} else {
$user = Admin::user();
// 设置公共参数
$data['user_id'] = $user->id;
$data['shop_id'] = $user->shop_id ? : 0;
// 默认为下架状态
$data['unshelved_status'] = config('constants.UNSHELVED');
// 保存方案
$model = $this->model->create($data);
}
return $model;
}
}