SystemSettingController.php 8.3 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 管理端控制层: SystemSetting 控制类
+-----------------------------------------------------------------------------------------------------------------------
 *
 * 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\Form;
use App\Models\System\SystemSetting;
use Encore\Admin\Form\Tools;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Response;

/**
 * Class SystemSettingController.
 *
 * @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 SystemSettingController extends BaseController
{
    /**
     * SystemSettingController constructor.
     *
     * @param SystemSetting $model 注入model
     */
    public function __construct(SystemSetting $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', '每次提问消耗次数');
        $this->formNumberField('context_timeout', '上下文超时时间')->help('单位,分钟,提问后超过该时间,上下文自动清空,下次提问不会携带上下文信息');
//        $this->formTextField('start_statement', '提问起始语', 255, 1);
        $this->formSliderField('primary_distribution_rate', '', 1)->help('一级分销用户返现比例');
        $this->formSliderField('secondary_distribution_rate', '', 1)->help('二级分销用户返现比例');
        $this->form->setAction(admin_url('system-setting'));

        $this->form->saved(function () {
            admin_toastr(trans('admin.update_succeeded'));
            return redirect(admin_url('system-setting'));
        });

        return $this->form;
    }

    /**
     * Model-form for user setting.
     *
     * @return Form
     */
    protected function settingForm1()
    {
        $form = new Form($this->model);
        $this->form = $form;
//        $form->radio('type', __('self_purchase_cashback_rate'))
//            ->options(User::ROLE_TYPE_OPTIONS)->when(1, function (Form $form) {
//
//                $form->text('name', '姓名');
//                $form->text('idcard', '身份证');
//
//            })->when(2, function (Form $form) {
//
//                $form->text('name', '姓名');
//                $form->text('passport', '护照');
//
//            });


        $options =   [
            'max' => 100, 'min' => 0, 'step' => 1, 'postfix' => ' %',
            'lineFillStyle' => '#0094ff', 'lineFillStyleGray' => '#cac7c7'
        ];

        //        $this->formSliderField('points_exchange_rate', '', 1);
        $this->formNumberField('points_deduct_rate', '', 1, 1, 1, 1, 1000)->help('多少积分可抵扣 1 元;例如10积分抵扣1元');

        $this->formSliderField('points_deduct_limit', '', 1)->help(__('每笔订单总金额的抵扣积分上限!'));

//        $this->form->rate('withdrawal_fee',__('withdrawal_fee'));

//        $this->formRateField('withdrawal_fee', '', 1);
        $this->formSliderField('withdrawal_fee', '', 1, 1, 1, 1000, 1, '‰')->help(__('用户提现平台收取手续费比例!'));

        $this->formSliderField('goods_discount_rate', '', 1)->help(__('犒劳自己或转赠他人,不想要商品话,用户实际付款进行折现比例!'));

        $this->form->embeds('cash_recharge_card_discount_rate', __('cash_recharge_card_discount_rate'), function ($form) use ($options) {
            $form->slider('general', '普通用户')->options($options)->rules('required')->setGroupClass('col-sm-12 clear-both')->setWidth(10, 1);
            $form->slider('enterprise', '企业用户')->options($options)->rules('required')->setGroupClass('col-sm-12 clear-both')->setWidth(10, 1);
//            $form->currency('general', '普通用户')->rules('required');
//            $form->currency('enterprise', '企业用户')->rules('required');
        });//->setGroupClass('col-sm-12 clear-both')->setWidth('1', 10);

        $this->form->embeds('fixed_gift_card_discount_rate', __('fixed_gift_card_discount_rate'), function ($form) use ($options) {
            $form->slider('general', '普通用户')->options($options)->rules('required')->setGroupClass('col-sm-12 clear-both')->setWidth(10, 1);
            $form->slider('enterprise', '企业用户')->options($options)->rules('required')->setGroupClass('col-sm-12 clear-both')->setWidth(10, 1);
        });

        $this->form->embeds('package_gift_card_discount_rate', __('package_gift_card_discount_rate'), function ($form) use ($options) {
            $form->slider('general', '普通用户')->options($options)->rules('required')->setGroupClass('col-sm-12 clear-both')->setWidth(10, 1);
            $form->slider('enterprise', '企业用户')->options($options)->rules('required')->setGroupClass('col-sm-12 clear-both')->setWidth(10, 1);
        });

        $this->formNumberField('share_link_limit', '', 1)->help(__('礼包分享链接有效性设置,单位:分钟'));
        $this->formNumberField('invitation_points', '', 1)->help('邀请人获得积分');
        $this->formNumberField('invited_points', '', 1)->help('被邀请人获得积分');
        $this->formSliderField('self_purchase_cashback_rate', '', 1)->help('购买商品用户返现比例');
        $this->formSliderField('primary_distribution_rate', '', 1)->help('购买商品一级分销用户返现比例');
        $this->formSliderField('secondary_distribution_rate', '', 1)->help('购买商品二级分销用户返现比例');

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

        $this->form->saving(function () {
//            dd(request()->all());
        });

        $this->form->saved(function () {
            admin_toastr(trans('admin.update_succeeded'));
            return redirect(admin_url('system-setting'));
        });

        return $this->form;
    }
}