SystemContentsController.php 6.6 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 管理端控制层: SystemContent 控制类
+-----------------------------------------------------------------------------------------------------------------------
 *
 * 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\SystemContent;
use App\Models\User\User;
use Encore\Admin\Form\Tools;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Response;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

/**
 * Class SystemContentsController.
 *
 * @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 SystemContentsController extends BaseController
{
    protected $type = 1;
    protected $url = 'user-agreement';
    /**
     * SystemContentsController constructor.
     *
     * @param SystemContent $model 注入model
     */
    public function __construct(SystemContent $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;

        $actions = request()->route()->getAction();
        switch (true) {
            case Str::endsWith($actions['uses'], 'UserAgreement'):
                $this->type = 1;
                $this->url = 'user-agreement';
                break;
            case Str::endsWith($actions['uses'], 'WithdrawalRule'):
                $this->type = 2;
                $this->url = 'withdrawal-rule';
                break;
            case Str::endsWith($actions['uses'], 'DrawRule'):
                $this->type = 3;
                $this->url = 'draw-rule';
                break;

        }


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

    /**
     * User setting page.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function getUserAgreement(Content $content)
    {
        return $this->getData($content);
    }

    /**
     * Update user setting.
     *
     * @return bool|JsonResponse|RedirectResponse|Response|Redirector
     */
    public function postUserAgreement()
    {
        return $this->settingForm()->store();
    }

    /**
     * Update user setting.
     *
     * @return Response
     */
    public function putUserAgreement()
    {
        $model = $this->model->where('type', $this->type)->first();

        return $this->settingForm()->update(optional($model)->id);
    }


    /**
     * User setting page.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function getWithdrawalRule(Content $content)
    {
        return $this->getData($content);
    }

    /**
     * Update user setting.
     *
     * @return bool|JsonResponse|RedirectResponse|Response|Redirector
     */
    public function postWithdrawalRule()
    {
        return $this->settingForm()->store();
    }

    /**
     * Update user setting.
     *
     * @return Response
     */
    public function putWithdrawalRule()
    {
        $model = $this->model->where('type', $this->type)->first();

        return $this->settingForm()->update(optional($model)->id);
    }

    /**
     * User setting page.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function getDrawRule(Content $content)
    {
        return $this->getData($content);
    }

    /**
     * Update user setting.
     *
     * @return bool|JsonResponse|RedirectResponse|Response|Redirector
     */
    public function postDrawRule()
    {
        return $this->settingForm()->store();
    }

    /**
     * Update user setting.
     *
     * @return Response
     */
    public function putDrawRule()
    {
        $model = $this->model->where('type', $this->type)->first();

        return $this->settingForm()->update(optional($model)->id);
    }

    /**
     * 新增
     *
     * @param $content
     * @return mixed
     */
    public function getData($content)
    {
        $model = $this->model->where('type', $this->type)->first();

        $form = $this->settingForm();
        $form->tools(
            function (Tools $tools) {
                $tools->disableList();
                $tools->disableDelete();
                $tools->disableView();
            }
        );

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

    /**
     * Model-form for user setting.
     *
     * @return Form
     */
    protected function settingForm()
    {
        $form = new Form($this->model);
        $this->form = $form;
        $form->hidden('type', $this->type);
        $this->formTextField('title', '', 100, 1);
//        $this->formTextAreaField();
        $this->formRichTextField('body', '', 1);

        $this->form->setAction(admin_url($this->url));

        $this->form->saving(function (Form $form) {
            $actions = request()->route()->getAction();
            // 如果是以@store结尾则为新增操作
            if (Str::contains($actions['uses'], '@post')) {
//                $form->model()->setAttribute('type', $this->type);
                $form->type = $this->type;
            }
        });

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

        return $this->form;
    }
}