AreaForm.php 4.3 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 管理端表单 trait层:渲染生成 小区 form 表单
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Admin\Forms\Area
 * @package   App\Admin\Forms\Area
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年1月27日11:03:37
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Admin\Forms\Area;

use App\Models\Area\Area;
use App\Models\Area\City;
use App\Models\Area\County;
use App\Models\Area\Group;
use App\Models\Area\Province;
use App\Models\Area\Township;
use App\Models\Area\Village;
use Encore\Admin\Form;
use Illuminate\Support\Facades\Hash;

/**
 * Class AreaForm.
 *
 * @category  App\Admin\Forms\Area
 * @package   App\Admin\Forms\Area
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年1月27日11:03:37
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
trait AreaForm
{
    /**
     * 渲染表单字段
     *
     * @param Int $id 用户id
     *
     * @return void
    */
    public function renderFormFields($id)
    {

        if ($this->level > Area::LEVEL_PROVINCE) {
            $this->generateFormSingleSelectField($this->form, 'province_id', __(Province::OBJ_NAME), Province::getSelectOptions(), 1)->load("city_id", "/admin/cities/select-options");
        }
        if ($this->level > Area::LEVEL_CITY) {
            $this->generateFormSingleSelectField($this->form, 'city_id', __(City::OBJ_NAME), [], 1)->load("county_id", "/admin/counties/select-options");
        }

        if ($this->level > Area::LEVEL_COUNTY) {
            $this->generateFormSingleSelectField($this->form, 'county_id', __(County::OBJ_NAME), [], 1)->load("township_id", "/admin/townships/select-options");
        }

        if ($this->level > Area::LEVEL_TOWNSHIP) {
            $this->generateFormSingleSelectField($this->form, 'township_id', __(Township::OBJ_NAME), [], 1)->load("village_id", "/admin/villages/select-options");
        }

        if ($this->level > Area::LEVEL_VILLAGE) {
            $this->generateFormSingleSelectField($this->form, 'village_id', __(Village::OBJ_NAME), [], 1);
        }

        $this->generateFormTextField($this->form, 'name', __($this->title). ' ' . strtolower(__('name')), 128, 1)
            ->creationRules(['required', "unique:{$this->table},name,NULL,id"])
            ->updateRules(['required', "unique:{$this->table},name,{{id}},id"]);

        $this->generateFormNumberField($this->form, 'sort', __('sort'));


//        $this->form->html("<a class='text text-success' style='margin-left: 5px' href='javascript:;' onclick='showMap()'>坐标拾取器</a>", "")->setWidth(10, 1);


        // 在表单提交前调用
        $this->form->submitted(function (Form $form) {
//            $form->ignore(['username','password','password_confirmation','gid','ggid']);
        });

        // 保存前回调:
        $this->form->saving(function (Form $form) {

//            dd(request()->all());

//            $form->ignore(['username','password','password_confirmation','gid','ggid']);

            // 获取操作
            $actions = request()->route()->getAction();
            // 如果是以@store结尾则为新增操作
            if (\Str::endsWith($actions['uses'], '@store')) {
                // 获取code
//                $code = Area::generateCode($this->level, $pcode);

//                $form->model()->setAttribute('level', $this->level ?? 1);
            }
        });


        // 保存后回调
        $this->form->saved(
            function (Form $form) {
                // 获取操作
                $actions = request()->route()->getAction();

                // 设置 user 表 supplier_id
                $model = $form->model();
                // 如果是以@store结尾则为新增操作
                if (\Str::endsWith($actions['uses'], '@store')) {

                } elseif (\Str::endsWith($actions['uses'], '@update')) {
                }
                $model->save();
            }
        );
    }
}