BaseInfo.php
2.8 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
<?php
namespace App\Admin\Forms\Steps\Draw;
use App\Admin\Traits\FormFieldTrait;
use App\Models\Turntable\TurnTable;
use Encore\Admin\Widgets\StepForm;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class BaseInfo extends StepForm
{
use FormFieldTrait;
/**
* The form title.
*
* @var string
*/
public $title = '基本信息';
/**
* The form model.
*
* @var string
*/
public $model;
/**
* Create a new form instance.
*
*/
// public function __construct()
// {
// $this->model = new Plan();
// }
/**
* @return string
*/
public function model()
{
return new TurnTable();
}
/**
* Handle the form request.
*
* @param Request $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function handle(Request $request)
{
$plan = $this->save($request);
return $this->next($plan);
}
/**
* Build a form here.
*/
public function form()
{
// 获取id
$id = request('id');
if (request('back') != 1) {
$this->clear();
}
$this->hidden('id')->default($id);
$this->generateFormTextField($this, 'title', '活动标题', 15, true);
$this->generateFormDateField($this, 'start_time', '活动开始时间', true);
$this->generateFormDateField($this, 'end_time', '活动结束时间', true);
$this->generateFormTextAreaField($this, 'rule', '活动规则');
$this->generateFormNumberField($this, 'points', '单次消耗积分');
}
/**
* The data of the form.
*
* @return array $data
*/
public function data()
{
$url = str_replace('_pjax=%23pjax-container', '', request()->getUri()) ;
$id = 0;
if (Str::contains($url, 'edit') || (Str::contains($url, 'create') && Str::contains($url, 'step=plan'))) {
$id = request('id') ? : Arr::get(Arr::get(session('steps'), 'plan'), 'id');
} else {
$this->clear();
}
if ($id) {
// 根据id获取数据,并在此填充
return TurnTable::find($id);
}
}
/**
* 保存方案:新增或者更新
*
* @param $request
* @return mixed
*/
public function save($request)
{
// 获取方案信息
$request = $this->all();
$turntable = Arr::get($request, 'turntable');
// 获取id
$id = Arr::get($turntable, 'id') ?? 0 ;
if ($id) {
TurnTable::where('id', $id)->update($turntable);
} else {
// 保存方案
$data = TurnTable::create($turntable);
$turntable['id'] = $data->id;
}
return $turntable;
}
}