MembershipController.php
8.54 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
namespace App\Admin\Controllers;
use App\Admin\Renderable\MembershipGoodsEasyTable;
use App\Admin\Renderable\MembershipGoodsTable;
use App\Models\Membership;
use App\Models\MembershipGood;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Form\NestedForm;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Widgets\Table;
class MembershipController extends AdminController
{
protected $title = '会员';
/**
* Make a grid builder.
*
* @return Grid
*/
protected function grid()
{
return Grid::make(new Membership(), function (Grid $grid) {
// 设置自定义视图
$grid->setActionClass(Grid\Displayers\Actions::class);
$grid->column('id',__('ID'))->sortable();
$grid->column('title');
$grid->column('intro');
$grid->column('expand','展开')
->display('会员商品')
->expand(function (){
$th = ['id','price','line_price','limit_days','limit_unit'];
$data = MembershipGood::query()->where('membership_id',$this->id)->get($th)->toArray();
$th = ['id','价格','划线价格','有效期','单位'];
return Table::make($th, $data);
});
$grid->column('video_url');
$grid->column('video_cover')->image('/storage/');
$grid->column('bg_images')->gallery('/storage/');
$grid->column('terminal')->using([1 => '安卓', 2 => 'iOS'], '未知');
$grid->column('state','线上展示')->switch();
$grid->column('created_at');
$grid->column('updated_at')->sortable();
$grid->filter(function (Grid\Filter $filter) {
$filter->equal('id')->width(2);
$filter->like('terminal')->width(3);
$filter->panel();
});
});
}
/**
* Make a show builder.
*
* @param mixed $id
*
* @return Show
*/
protected function detail($id)
{
return Show::make($id, new Membership(), function (Show $show) {
$show->field('id');
$show->field('name');
$show->field('price');
$show->field('line_price');
$show->field('limited_days');
$show->field('limit_unit');
$show->field('intro');
$show->field('state');
$show->field('sn');
$show->field('is_video');
$show->field('video_url');
$show->field('video_cover');
$show->field('bg_images');
$show->field('visits');
$show->field('virtual_sales');
$show->field('sales');
$show->field('created_at');
$show->field('updated_at');
});
}
/**
* Make a form builder.
*
* @return Form
*/
protected function form()
{
$css = <<<CSS
.table tr td:first-child{
padding-left: 0 ;
}
.table tr td{
padding-left: 0 ;
}
.form-field .input-group .price{
width: 1% !important;
}
CSS;
\Admin::style($css);
return Form::make(new Membership(), function (Form $form) {
$form->display('id');
$form->block(8, function (Form\BlockForm $form) {
// 设置标题
$form->title('基本设置');
// 显示底部提交按钮
$form->showFooter();
// 设置字段宽度
$form->width(9, 2);
$form->radio('terminal')->addElementClass('terminal')
->options([1 => 'Android', 2 => 'IOS'])->default(2);
$form->text('title')->addElementClass('name');
$form->textarea('intro')->addElementClass('intro');
$form->radio('bg_type')->addElementClass('bg_type')
->options([1 => '单图', 2 => '轮播图', 3 => '视频'])
->when([1,2],function (Form\BlockForm $form){
$form->multipleImage('bg_images')
->limit(5)
->uniqueName()
->addElementClass('bg_images');
})
->when(3,function (Form\BlockForm $form){
$form->file('video_url')
->accept('mp4')
->autoUpload()
->uniqueName()
->addElementClass('video_url');
$form->image('video_cover')
->uniqueName()
->addElementClass('video_cover');
})->default(1);
$form->radio('state')->options(['不显示', '显示'])->default(0);
$form->radio('is_bind_old')->options(['新增会员商品', '选择已有未上架的商品'])->default(0)
->when(0,function (Form\BlockForm $form){
$form->table('new','新增', function (NestedForm $table) {
$table->text('price')->addElementClass('price');
$table->text('line_price')->addElementClass('line_price');
$table->text('limited_days')->addElementClass('limited_days');
$table->text('limit_unit')->addElementClass('limit_unit');
});
})
->when(1,function (Form\BlockForm $form){
$form->multipleSelectTable('old','已有')
->title('会员商品')
->from(MembershipGoodsTable::make())
->model(MembershipGood::class, 'id', 'price');
});
});
$form->block(4, function (Form\BlockForm $form) {
$form->width(9, 1);
$form->html(view('admin.form.membership'));
});
$form->display('created_at');
$form->display('updated_at');
});
}
public function store()
{
$all = request()->all();
//
if (isset($all['upload_column'])) return $this->form()->store();
// 检测是否已经有该平台的数据
$membership = Membership::query()->where('terminal',$all['terminal'])->first();
if ($membership && $all['state'] == '1') return $this->form()->response()->error('状态为显示会覆盖线上的设置,请修改后重试');
try{
DB::transaction(function ()use ($all){
$membership = Membership::query()->create([
'title' => $all['title'],
'intro' => $all['intro'],
'bg_type' => $all['bg_type'],
'bg_images' => $all['bg_images'],
'video_url' => $all['video_url'] ?? '',
'video_cover' => $all['video_cover'] ?? '',
'terminal' => $all['terminal'],
'state' => $all['state'],
]);
if ($all['is_bind_old'] === '0'){
// 新增
foreach ($all['new'] as $item) {
if ($item['_remove_'] === '1') continue;
MembershipGood::query()->create([
'membership_id' => $membership->id,
'price' => $item['price'],
'line_price' => $item['line_price'],
'limited_days' => $item['limited_days'],
'limit_unit' => $item['limit_unit'] ?? '天',
'terminal' => $all['terminal'],
'state' => 1,
]);
}
}elseif ($all['is_bind_old'] === '1'){
$old_ids = explode(',',$all['old']);
$membership_goods = MembershipGood::query()->whereIn('id',$old_ids)->get();
foreach ($membership_goods as $membership_good){
$membership_good->membership_id = $membership->id;
$membership_good->state = 1;
$membership_good->save();
}
}
});
}catch (\Exception $exception){
return $this->form()->response()->error($exception->getMessage());
}
return $this->form()->response()->refresh()->success(trans('admin.save_succeeded'));
}
}