owl-admin-base/app/Admin/Components.php

267 lines
7.7 KiB
PHP

<?php
namespace App\Admin;
use App\Models\Keyword;
use Slowlyo\OwlAdmin\Renderers\BaseRenderer;
use Slowlyo\OwlAdmin\Renderers\WangEditor;
class Components extends BaseRenderer {
/**
* 父级选择器
*/
public function parentControl($apiUrl = null, $name ='parent_id', $label = null, $labelField = 'name', $valueField = 'id')
{
return amisMake()->TreeSelectControl()->source($apiUrl)
->name($name)->label($label ?? __('admin.components.parent_select'))
->showIcon(false)
->labelField($labelField)
->valueField($valueField);
}
/**
* 排序字段
*/
public function sortControl($name ='sort', $label = null){
return amisMake()->NumberControl()
->name($name)->label($label ?? __('admin.components.order'))
->displayMode('enhance')
->value(0)
->min(0);
}
/**
* 2位小数输入框
*/
public function decimalControl($name ='decimal', $label = null){
return amisMake()->NumberControl()
->name($name)->label($label ?? __('admin.components.decimal'))
->kilobitSeparator(true)
->percision(2)
->step(0.01)
->value(0.00)
->min(0);
}
/**
* 富文本编辑器
*/
public function fuEditorControl($name ='content', $label = null, $height = 530)
{
return WangEditor::make()
->name($name)->label($label ?? __('admin.components.content'))
->height($height)
->excludeKeys(['group-video']);
}
/**
* 开关
*/
public function enableControl($name = 'is_enable', $label= null, $mode = 'horizontal'){
return amisMake()->SwitchControl()
->name($name)->label($label ?? __('admin.components.status'))
->mode($mode)
->onText(__('admin.components.status_map.enabled'))->offText(__('admin.components.status_map.disabled'));
}
/**
* 图片上传
*/
public function imageControl($name, $label){
return amis()->ImageControl($name, $label)
->joinValues(false)
->autoUpload(true)->maxSize('5*1024*1024');//不能大于5M
}
/**
* 图片上传,带裁剪
*/
public function cropImageControl($name, $label, $aspectRatio = null){
return amis()->ImageControl($name, $label)->joinValues(false)
->crop([
'aspectRatio' => $aspectRatio ?? 1
])->autoUpload(true);
}
/**
* 普通文件上传
*/
public function fileControl($name, $label, $accept = '.txt', $multiple = false){
return amis()->FileControl($name, $label ?? __('admin.components.files'))
->multiple($multiple)
// ->receiver()
->joinValues(false)
->useChunk(false)
->maxSize(20*1024*1024)
->accept($accept);
}
/**
* 标签选择
*/
public function keywordsTagControl($name = 'tags', $label= null, $pKey = null){
return amisMake()->TagControl()
->name($name)->label($label ?? __('admin.components.tag'))
->maxTagLength(0)
->options(Keyword::where('parent_key', $pKey)->pluck('name', 'id')->toArray());
}
public function keywordsTag($label = null, $color = null){
if($color){
$tag = amisMake()->Tag()->label($label ?? __('admin.components.tag'))
->displayMode('rounded')->style([
'color' => '#fff',
'backgroundColor' =>$color,
'borderColor' => $color
]);
}else{
$tag = amisMake()->Tag()->label($label ?? __('admin.components.tag'))
->displayMode('rounded')->color('inactive');
}
return $tag;
}
/**
* 生成统计图config
* 折线图或者柱状图
*/
public function chartLineBarConfig($title = null, array $x , array $y){
$yAxisData = [];
$seriesData = [];
$color = [];
if(!isset($y[0])){
$_y = $y;
$y = [0=>$_y];
}
$i = 0;
$tips = '{b0}';
foreach($y as $item) {
//调色盘
$color[] = $item['color'];
//tips
$tips.= '<br/> {a'.$i.'}: {c'.$i.'}'.($item['unit'] ?? '');
//纵坐标
$yAxisData[] = [
'name'=>($item['unit'] ?? ''),
'type' =>'value',
'axisTick' => true,
'alignTicks' => true,
'axisLine' => [
'show' => true,
'lineStyle' => [
'color' => $item['color'] ?? ''
]
],
'axisLabel'=> [
'formatter'=>'{value} '
]
];
//数据
$_series = [
'name' => $item['name'] ?? '',
'data' => $item['data'] ?? [],
'type' => $item['type'] ?? 'line',
'yAxisIndex' => $i,
];
switch($item['type']){
case 'line':
$_series = array_merge($_series, [
'smooth'=> true,
'symbol'=> 'none',
'lineStyle' => [
'color' => $item['color'] ?? ''
],
'areaStyle' => [
'color' => $item['color'] ?? ''
],
]);
break;
case 'bar':
$_series = array_merge($_series, [
]);
break;
}
$seriesData[] = $_series;
$i++;
}
return [
'color' => $color,
'title' => [
'text' => $title,
],
"tooltip" => [//提示
'trigger'=>'axis',//坐标轴触发
'axisPointer' => [
'type' => 'cross'
],
// 'formatter' => $tips
],
'grid' => [
'left' => '8%',
'right' => '8%',
],
'xAxis' => [
'type' => 'category',
'data' => $x,
],
'yAxis' => $yAxisData,
'series' => $seriesData
];
}
/**
* 散点图
*/
public function chartScatterConfig($title = null, array $x , array $y, array $yData = null){
$yAxisData = [];
$seriesData = [];
$color = [];
if($yData){
$yAxisData = [
'type' =>'category',
// 'splitLine'=>[
// 'show'=>true,
// 'lineStyle'=>[
// 'type'=>'dashed'
// ]
// ],
'axisTick' => [
'alignWithLabel'=>true
],
'data'=> $yData
];
}
$seriesData = $y;
return [
'color' => $color,
'title' => [
'text' => $title,
],
"tooltip" => [//提示
'trigger'=>'axis',//坐标轴触发
'axisPointer' => [
'type' => 'cross'
],
],
'xAxis' => [
'type' => 'category',
'data' => $x,
],
'yAxis' => $yAxisData,
'series' => $seriesData
];
}
/**
* 生成饼状图config
* -todo
*/
public function chartPieConfig(){
return ;
}
}