SystemContentsController.php
6.6 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 管理端控制层: SystemContent 控制类
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Controllers\System
* @package App\Admin\Controllers\System
* @author Richer <yangzi1028@163.com>
* @date 2021年1月27日10:57:55
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Controllers\System;
use App\Admin\Controllers\BaseController;
use App\Admin\Rewrite\Admin;
use App\Admin\Rewrite\Form;
use App\Models\System\SystemContent;
use App\Models\User\User;
use Encore\Admin\Form\Tools;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Response;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
/**
* Class SystemContentsController.
*
* @category App\Admin\Controllers\System
* @package App\Admin\Controllers\System
* @author Richer <yangzi1028@163.com>
* @date 2021年1月27日10:57:55
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class SystemContentsController extends BaseController
{
protected $type = 1;
protected $url = 'user-agreement';
/**
* SystemContentsController constructor.
*
* @param SystemContent $model 注入model
*/
public function __construct(SystemContent $model)
{
// 资源显示的中名称
$this ->title = $model::OBJ_NAME;
// 是否可查看
$this->can_view = false;
// 是否可新增
$this->can_create = true;
// 是否可编辑
$this->can_edit = true;
// 是否可删除
$this->can_delete = true;
// 是否开启下拉菜单
$this->dropdownActions = false;
$actions = request()->route()->getAction();
switch (true) {
case Str::endsWith($actions['uses'], 'UserAgreement'):
$this->type = 1;
$this->url = 'user-agreement';
break;
case Str::endsWith($actions['uses'], 'WithdrawalRule'):
$this->type = 2;
$this->url = 'withdrawal-rule';
break;
case Str::endsWith($actions['uses'], 'DrawRule'):
$this->type = 3;
$this->url = 'draw-rule';
break;
}
// 执行父类构造方法
parent::__construct($model);
}
/**
* User setting page.
*
* @param Content $content
*
* @return Content
*/
public function getUserAgreement(Content $content)
{
return $this->getData($content);
}
/**
* Update user setting.
*
* @return bool|JsonResponse|RedirectResponse|Response|Redirector
*/
public function postUserAgreement()
{
return $this->settingForm()->store();
}
/**
* Update user setting.
*
* @return Response
*/
public function putUserAgreement()
{
$model = $this->model->where('type', $this->type)->first();
return $this->settingForm()->update(optional($model)->id);
}
/**
* User setting page.
*
* @param Content $content
*
* @return Content
*/
public function getWithdrawalRule(Content $content)
{
return $this->getData($content);
}
/**
* Update user setting.
*
* @return bool|JsonResponse|RedirectResponse|Response|Redirector
*/
public function postWithdrawalRule()
{
return $this->settingForm()->store();
}
/**
* Update user setting.
*
* @return Response
*/
public function putWithdrawalRule()
{
$model = $this->model->where('type', $this->type)->first();
return $this->settingForm()->update(optional($model)->id);
}
/**
* User setting page.
*
* @param Content $content
*
* @return Content
*/
public function getDrawRule(Content $content)
{
return $this->getData($content);
}
/**
* Update user setting.
*
* @return bool|JsonResponse|RedirectResponse|Response|Redirector
*/
public function postDrawRule()
{
return $this->settingForm()->store();
}
/**
* Update user setting.
*
* @return Response
*/
public function putDrawRule()
{
$model = $this->model->where('type', $this->type)->first();
return $this->settingForm()->update(optional($model)->id);
}
/**
* 新增
*
* @param $content
* @return mixed
*/
public function getData($content)
{
$model = $this->model->where('type', $this->type)->first();
$form = $this->settingForm();
$form->tools(
function (Tools $tools) {
$tools->disableList();
$tools->disableDelete();
$tools->disableView();
}
);
if ($model) {
return $content
->title(trans('admin.user_setting'))
->body($form->edit(optional($model)->id));
} else {
return $content
->title('')
->body($form);
}
}
/**
* Model-form for user setting.
*
* @return Form
*/
protected function settingForm()
{
$form = new Form($this->model);
$this->form = $form;
$form->hidden('type', $this->type);
$this->formTextField('title', '', 100, 1);
// $this->formTextAreaField();
$this->formRichTextField('body', '', 1);
$this->form->setAction(admin_url($this->url));
$this->form->saving(function (Form $form) {
$actions = request()->route()->getAction();
// 如果是以@store结尾则为新增操作
if (Str::contains($actions['uses'], '@post')) {
// $form->model()->setAttribute('type', $this->type);
$form->type = $this->type;
}
});
$this->form->saved(function () {
admin_toastr(trans('admin.update_succeeded'));
return redirect(admin_url($this->url));
});
return $this->form;
}
}