ShowFieldTrait.php
11.2 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 自定义 show 字段
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Traits
* @package App\Admin\Traits
* @author Richer <yangzi1028@163.com>
* @date 2020年11月13日 01:26:09
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Traits;
use Carbon\Carbon;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\Arr;
/**
* Trait ShowFieldTrait
*
* @category App\Admin\Traits
* @package App\Admin\Traits
* @author Richer <yangzi1028@163.com>
* @date 2020年11月13日 01:26:09
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
trait ShowFieldTrait
{
/**
* 渲染生成通用字段类型
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param integer $limit 截取字符串长度,默认为0,不截取
* @return Show\Field
*/
public function showCommonField($field = 'title', $comment = '名称', $limit = 0)
{
if (!$comment) {
$comment = __($field);
}
return $this->show->field($field, $comment)->setWidth(10, 1)->setGroupClass('col-sm-6 clear-both');
}
/**
* 渲染生成input 为text的字段类型
*
* @param string $field
* @param string $comment
* @param int $limit
* @return Show\Field
*/
public function showTextField($field = 'title', $comment = '', $limit = 0)
{
if (!$comment) {
$comment = __($field);
}
return $this->showCommonField($field, $comment, $limit);
}
/**
* 渲染生成number类型字段
*
* @param string $field 字段名
* @param string $comment 字段注释
* @return Show\Field
*/
public function showNumberField($field, $comment)
{
if (!$comment) {
$comment = __($field);
}
return $this->showCommonField($field, $comment);
}
/**
* 渲染生成switch选择框
*
* @param string $field 字段名
* @param string $comment 字段注释
* @return Show
*/
public function showColorField($field, $comment)
{
if (!$comment) {
$comment = __($field);
}
$this->show->field($field, $comment)->as(function ($value) {
if ($value) {
return '<span style="background: '.$value .'!important;width: 100px;height: 50px;display: block"></span>';
} else {
return '--';
}
});
}
/**
* 渲染生成switch选择框
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param array $states states
* @return Show
*/
public function showSwitchField($field, $comment, $states)
{
if (!$comment) {
$comment = __($field);
}
// 设置text、color、和存储值
$show = $this->show->$field($comment)->using($states);
// 返回对象,方便继续链式调用
return $show;
}
/**
* 渲染生成select选择框
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param array $options options
* @param array $options_color
* @return void
*/
public function showSingleSelectField($field, $comment, $options = [], $options_color = [])
{
if (!$comment) {
$comment = __($field);
}
// 获取代码集配置
$options = $options ?: config('constants.'.$field.'_options');
// 获取颜色代码集配置
$options_color = $options_color ?: config('constants.'.$field.'_color_options');
//$show->$field($comment)->using($options);
$this->show->field($field, $comment)->as(function ($title) use ($options, $options_color) {
if ($title !== null) {
// 获取每个状态对应的颜色,默认为系统颜色
$color = Arr::get($options_color, $title, '');
$value = __(Arr::get($options, $title)) ? : '--';
return '<span class="text text-'.$color. '" style="color:'.$color.';white-space:nowrap;">'.$value.'</span>';
} else {
return '--';
}
});
}
/**
* 渲染生成 select 多选框
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param array $options options
* @return Show
*/
public function showMultipleSelectField($field, $comment, $options = [])
{
if (!$comment) {
$comment = __($field);
}
$show = $this->show->$field($comment)->as(function ($data) use ($field, $options) {
if ($data) {
// 转换审核状态
$array = [];
// 转换审核状态
$options = $options ?: \Config::get('constants.'.$field.'_options');
if ($options) {
foreach ($options as $key => $option) {
if (in_array($key, $data)) {
$array[] = $option;
}
}
} else {
$array = $data;
}
if (!empty($array)) {
return implode(',', $array);
}
}
});
// 返回对象,方便继续链式调用
return $show;
}
/**
* 渲染生成价格的字段类型
*
* @param string $field 字段名
* @param string $comment 字段注释
* @return Show\Field
*/
public function showCurrencyField($field, $comment)
{
if (!$comment) {
$comment = __($field);
}
return $this->show->field($field, $comment)->as(function ($data) {
return '¥'.format_money($data);
});
}
/**
* 渲染生成价格的字段类型
*
* @param string $field 字段名
* @param string $comment 字段注释
* @return Show
*/
public function showSpecialCurrencyField($field, $comment)
{
if (!$comment) {
$comment = __($field);
}
// 不存在的`full_name`字段
$show = $this->show->field($field, $comment)->display(function () {
return '<span style="color:#e08e0b">'.rtrim(rtrim($this->purchase_price, '0'), '.')
.'</span> / <span style="color: #999">' .rtrim(rtrim($this->price, '0'), '.').'</span>';
});
// 返回对象,方便继续链式调用
return $show;
}
/**
* 渲染生成单个上传图片字段
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param string $server
* @param int $width
* @param int $height
* @return Show
*/
public function showSingleImageField($field = 'cover_image', $comment = '', $server = '', $width = 800, $height = 800)
{
if (!$comment) {
$comment = __($field);
}
// 列表的图片可以点击放大,并设置大小
//$show->$field($comment)->lightbox($options);
// $show = $this->show->$field($comment)->image($options);
$show = $this->show->$field($comment)->image($server, $width, $height)->setWidth(10, 1);
//$show->column($field,$comment)->carousel($width = 300, $height = 200);
// 返回对象,方便继续链式调用
return $show;
}
/**
* 渲染生成单个上传图片字段
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param array $options
* @return Show
*/
public function showMultipleImageField($field = 'images', $comment = '', $options = ['zooming' => true,'width' => 93,'height' => 52,'class' => 'rounded'])
{
if (!$comment) {
$comment = __($field);
}
// 设置显示尺寸和图片服务器
$show = $this->show->$field($comment)->carousel($width = 600, $height = 400)->setWidth(10, 1);
// 列表的图片可以点击放大,并设置大小
//$show->$field($comment)->lightbox($options);
//$show->$field($comment)->gallery($options);
//$show->column($field,$comment)->carousel($width = 300, $height = 200);
// 返回对象,方便继续链式调用
return $show;
}
/**
* 渲染详情视频字段
*
* @param string $field
* @param string $comment
* @return Show\Field
*/
public function showVideoField($field = 'video', $comment = '视频')
{
if (!$comment) {
$comment = __($field);
}
return $this->show->field($field, $comment)->video(['videoWidth' => '90%', 'videoHeight' => '90%'])->setWidth(10, 1);
}
/**
* 渲染生成日期类型的字段
*
* @param string $field 字段名
* @param string $comment 字段注释
* @param string $format 日期转换格式
* @return Show\Field
*/
public function showDateField($field = 'created_at', $comment = '', $format = 'Y-m-d H:i:s')
{
if (!$comment) {
$comment = __($field);
}
return $this->show->field($field, $comment)->as(function ($data) use ($format) {
if ($data) {
return '<span style="white-space: nowrap;">'.Carbon::parse($data) ->format($format).'</span>';
} else {
return '--';
}
})->setWidth(10, 1);
}
/**
* 渲染生成富文本字段
*
* @param string $field 字段名
* @param string $comment 字段注释
* @return Show
*/
public function showRichTextField($field = 'body', $comment = '详情')
{
if (!$comment) {
$comment = __($field);
}
return $this->show->field($field, $comment)->setWidth(10, 1);
}
/**
* 渲染生成创建用户字段
*
* @param string $field 字段名
* @param string $comment 字段注释
* @return Show
*/
public function showCreatedByField($field, $comment)
{
if (!$comment) {
$comment = __($field);
}
// 不存在的`full_name`字段
return $this->show->field($field, $comment)->as(function ($data) {
return '<span class="username">'.Arr::get($data, 'username') .'<br/></span>'.Arr::get($data, 'nickname');
})->setWidth(10, 1);
}
/**
* 渲染生成创建用户字段
*
* @param string $field 字段名
* @param string $comment 字段注释
* @return Show\Field
*/
public function showAuditedByField($field, $comment)
{
// 不存在的`full_name`字段
return $this->show->field($field, $comment)->as(function ($data) {
return '<span class="username">'.Arr::get($data, 'username') .'<br/></span>'.Arr::get($data, 'name');
})->setWidth(10, 1);
}
}