SystemConfigsController.php 10.9 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 管理端控制层: 社区 控制类
+-----------------------------------------------------------------------------------------------------------------------
 *
 * 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\SystemConfig;
use Encore\Admin\Form\Tools;
use Encore\Admin\Grid;
//use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;

/**
 * Class SystemConfigsController.
 *
 * @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 SystemConfigsController extends BaseController
{
    /**
     * SystemConfigsController constructor.
     *
     * @param SystemConfig $model 注入model
     */
    public function __construct(SystemConfig $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;

        // 执行父类构造方法
        parent::__construct($model);
    }

    /**
     * User setting page.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function getSetting(Content $content)
    {
        // 获取 boss
        $boss = $this->model->first();
        $form = $this->settingForm();
        $form->tools(
            function (Tools $tools) {
                $tools->disableList();
                $tools->disableDelete();
                $tools->disableView();
            }
        );

        if ($boss) {
            return $content
                ->title(trans('admin.user_setting'))
                ->body($form->edit(optional($boss)->id));
        } else {
            return $content
                ->title(trans('admin.user_setting'))
                ->body($form);
        }
    }

    /**
     * Update setting.
     *
     * @return bool|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function postSetting()
    {
        return $this->settingForm()->store();
    }

    /**
     * Update user setting.
     *
     * @return array|bool|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Routing\Redirector|mixed|\Symfony\Component\HttpFoundation\Response|null
     */
    public function putSetting()
    {
        return $this->settingForm()->update(1);
    }

    /**
     * Model-form for user setting.
     *
     * @return Form
     */
    protected function settingForm()
    {
        $this->form = new Form($this->model);
        $this->formNumberField('consumption_times', '每次提问消耗次数', 1);
        $this->formNumberField('context_timeout', '上下文超时时间')->help('单位,分钟,提问后超过该时间,上下文自动清空,下次提问不会带有上下文广西', 1);
        $this->formTextField('start_statement', '提问起始语', 255, 1);

//        $form->number('body', '详情')->required()->help('请上传小于1G的视频...')->setWidth(10, 1)->setGroupClass('col-md-12');
////        $this->formRichTextField('body', '详情', 1);
//
////        $form->textarea('name', trans('admin.name'))->rules('required')->setWidth(10, 1)->setGroupClass('col-md-12');
//        $form->table('wechat', '微信交流', function ($table) {
//            $table->text('name', '名称')->required();
//            $table->text('number', '号码')->required();
//        })->setGroupClass('col-sm-12')->setWidth(10, 1);
//
//        $form->table('phone', '电话交流', function ($table) {
//            $table->text('name', '名称')->required();
//            $table->text('number', '号码')->required();
//        })->setGroupClass('col-sm-12')->setWidth(10, 1);

        $this->form->setAction(admin_url('system-config'));

        $this->form->saved(function () {
            admin_toastr(trans('admin.update_succeeded'));

            return redirect(admin_url('system-config'));
        });

        return $this->form;
    }

    /**
     * 为grid增加筛选条件
     *
     * @return \Encore\Admin\Grid
     */
    public function renderGridFilter()
    {
        $this->grid->filter(function ($filter) {
            // 筛选条件默认展开
            $filter->expand();

            $filter->equal('key')->select(SystemConfig::getKeyOptions())->placeholder(__('parameter_key'));

            //4.去掉默认的搜索
            $filter->disableIdFilter();
        });

        return $this->grid;
    }

    /**
     * 设置默认查询条件
     * 增加数据权限的判断。步骤:
     * 1、判断用户是否是超级管理员,并判断用户的pid,如果pid为0 则为平台总用户,不进行数据权限的判断
     * 2、如果pid大于0 则为 平台子管理员,则需要进行数据权限的判断
     */
    public function setGridQuery()
    {
    }

    /**
     * 渲染grid字段
     *
     * @return void
     */
    public function renderGridFields()
    {
        $this->gridNumberField($this->grid, 'id', 'ID', 1);
        $this->gridSingleSelectField('key', __('parameter_key'), SystemConfig::getKeyOptions(), 1);
        $this->gridTextField($this->grid, 'value', __('parameter_value'))->display(function ($value) {
            // TODO
            $language = request()->cookie(config('constants.COOKIE_KEY'), 'zh-CN');
            $str = '';
            $type = $this->type;
            if ($value && is_array($value)) {
                foreach ($value as $vo) {
                    $s = $vo['value_zh'];
                    if ($language === 'en') {
                        $s = Arr::get($vo, 'value_en');
                    }
                    $str .= '<span class="label label-success" style="margin-right: 0.5rem">'.$s.'</span>';

                }
            }
            return $str;
            if ($type === 'single_select') {
                if ($value && is_array($value)) {
                    foreach ($value as $vo) {
                       $s = $vo['value_zh'];
                        if ($language === 'en') {
                            $s = $vo['value_en'];
                        }
                        $str .= '<span class="label label-success" style="margin-right: 0.5rem">'.$s.'</span>';

                    }
                }
            } else {
                if ($value && is_array($value)) {
                    foreach ($value as $vo) {
                        $s = $vo['value'];
                        if ($language === 'zh-CN') {
                            $s = $vo['value_zh'];
                        }
                        $str .= '<span class="label label-success" style="margin-right: 0.5rem">'.$s.'</span>';
                        $items = Arr::get($vo, 'items');
                        if ($items) {
                            foreach ($items as $item) {
                                $s1 = $item['value'];
                                if ($language === 'zh-CN') {
                                    $s1 = $item['value_zh'];
                                }

                                $str .= '<span class="label label-success" style="margin-right: 0.5rem">'.$s1.'</span>';


                            }
                        }

                    }
                }
            }

            return $str;

        });


//        $this->gridNumberField($this->grid, 'sort', '排序', 1);
//        $this->generateGridCreatedByField($this->grid, 'user', '创建用户');
        $this->generateGridDateField($this->grid)->sortable();
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        //
    }

    /**
     * Make a form builder.
     *
     * @param null $id
     * @return Form
     */
    public function renderFormFields($id = null)
    {
        $this->generateFormSingleSelectField($this->form, 'key', __('parameter_key'), SystemConfig::getKeyOptions(), 1);
//        $this->form->list('value', __('parameter_value'))->setGroupClass('col-sm-12 parameter_value')->setWidth(10, 1);
        $this->form->table('value', __('parameter_value'), function ($table) {
            $table->text('value_key', __('key'));
            $table->text('value_en',  __('value_en'));
            $table->text('value_zh',  __('value_zh'));
            $table->text('description', __('description'));
        })->setGroupClass('col-sm-12 parameter_value')->setWidth(10, 1);
//        $this->form->keyValue('value', __('parameter_value'))->setGroupClass('col-sm-12 parameter_value')->setWidth(10, 1);
//        $this->form->html(view('admin::system.config', compact('id', 'user_type', 'rooms')), '房间信息')->setGroupClass('col-md-12')->setWidth(12, 0);
//        $this->generateFormRadioField($this->form, 'key', __('parameter_key'), SystemConfig::getKeyOptions(), 1);

        $this->generateFormTextAreaField($this->form, 'description', __('description'))->attribute('class');
        $this->form->hidden('type');
//        $this->form->hasMany('items', '自定义字段', function (Form\NestedForm $form) {
//            $form->text('value', '字段标题')->setWidth(8, 2)->setGroupClass('col-md-6');
//            $form->text('value', '字段内容')->setWidth(8, 2)->setGroupClass('col-md-6');
//        })->setWidth(10, 1)->setGroupClass('col-md-12');

        // 渲染生成地区选择
        Admin::script("$(document.body).append(`<script src='/assets/admin/js/module/system-config.js?t=1'>`);");
    }

    /**
     * 保存前回调
     *
     * @return void
     */
    public function saving()
    {
        // dd(request()->all());
        //保存前回调
    }

    /**
     * @param $key
     * @return string[]|null
     */
    public function getKey($key) {
        $options = SystemConfig::KEY_OPTIONS;
        $config = null;
        foreach ($options as $option) {
            if ($option['key'] === $key) {
                $config = $option;
                break;
            }
        }

        return $config;
    }
}