dcat-admin 表单存改 键值对参数的快速方法

效果展示

dcat-admin 表单存改 键值对的参数的快速方法

dcat-admin 表单存改 键值对参数的快速方法

dcat-admin 表单存改 键值对参数的快速方法

像这类的配置还有很多....

需求背景说明

当我们在开发一个比复杂一点的系统时,会有很多系统参数要配置。一般都会是一种键值对形式,如:短信配置,支付配置,上传配置,分销配置等等。有时多达几十百来项。分布在后台中的各页面中,需要对参数进行存改操作。

特点要求

使用dcat-admin 的原数据表单,显然不太合适。因为需要在后台各页面中提供一个小表单存改数据。基于这些特点 工具表单 就能很好的解决问题。

  • 读取参数列表
    $flds     = ['booking_notify_phone', 'booking_notify_gzh_open_id','booking_notify_qywx_robot_url','booking_notify_mail'];
    $formdata = HotelSetting::getlists($flds,Admin::user()->hotel_id);
    $form = new WidgetsForm($formdata);
  • 存改参数
    $insdata = [
    'booking_notify_phone' => $request->booking_notify_phone,
    'booking_notify_gzh_open_id' => $request->booking_notify_gzh_open_id,
    'booking_notify_mail' => $request->booking_notify_mail,
    'booking_notify_qywx_robot_url' => $request->booking_notify_qywx_robot_url,
    ];
    $hotel_id = $request->get('hotel_id');
    $sts = HotelSetting::createRow($insdata,$hotel_id);
    return JsonResponse::make()->data($request->all())->success('成功!');

参数存表 数据结构

CREATE TABLE `hotel_setting` (
  `id` int(11) NOT NULL,
  `hotel_id` int(11) NOT NULL COMMENT '酒店ID',
  `group_name` varchar(100) DEFAULT 'none' COMMENT '分组名',
  `field_key` varchar(255) NOT NULL,
  `field_value` longtext NOT NULL,
  `field_decs` text COMMENT '描述',
  `is_delete` tinyint(11) DEFAULT '1' COMMENT '是否生效',
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime DEFAULT NULL COMMENT '更新时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='参数管理';

ALTER TABLE `hotel_setting`
  ADD PRIMARY KEY (`id`);
ALTER TABLE `hotel_setting`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;

Model 添加方法

namespace App\Models\Hotel;

use Illuminate\Database\Eloquent\Model;

class HotelSetting extends HotelBaseModel
{

    protected $table = 'hotel_setting';
    protected $guarded = [];

    /**
     * @desc 保存参数
     * @param $insdata 参数键值对数组
     * @param $hotel_id 商户ID
     * @param string $group_name 分组名
     * @return bool
     * author eRic
     * dateTime 2024-03-23 10:55
     */
    public static function createRow($insdata,$hotel_id,$group_name = 'default') {
        if(empty($hotel_id)){
           return false;
        }
        foreach ($insdata as $key => $values) {
            if(empty($values)){
                continue;
            }
            $info = self::where(['hotel_id' => $hotel_id, 'field_key' => $key])->count();
            if (!empty($info)) {
                self::where(['hotel_id' => $hotel_id, 'field_key' => $key])->update(['field_value' => $values]);
            } else {
                $rowdata = [
                    'group_name' => $group_name,
                    'hotel_id'     => $hotel_id,
                    'field_key'   => $key,
                    'field_value' => $values,
                ];
                self::create($rowdata);
            }

        }

    }

    /**
     * @desc 获取参数列表值 
     * @param array $field_keys 参数键数组
     * @param $hotel_id 商户ID
     * @return array
     * author eRic
     * dateTime 2024-03-23 10:55
     */
    public static function getlists(array $field_keys,$hotel_id){
        $data = [];
        $list = HotelSetting::whereIn('field_key',$field_keys)->where(['hotel_id'=>$hotel_id])->get();
        foreach ($list as $key => $items) {
            $data[$items['field_key']] = $items['field_value'];
        }
        return $data;
    }

}

dcat-admin 表单展示

$flds     = ['booking_notify_phone', 'booking_notify_gzh_open_id','booking_notify_qywx_robot_url','booking_notify_mail'];
        $formdata = HotelSetting::getlists($flds,Admin::user()->hotel_id);
        $form = new WidgetsForm($formdata);
        $form->action('hotel-setting-edit');
        $form->confirm('确认已经填好了吗?');
        $form->hidden('action_name')->value('booking_notify');
        $form->hidden('hotel_id')->value(Admin::user()->hotel_id);
        $form->text('booking_notify_phone','接受订房通知短信的手机号')->help('<span class="text-success">如需要提醒多人,请逗号分隔.</span><span class="text-danger">最多3个手机号</span><br/>短信内容:XXXX酒店,有客人订房:豪华大床房-2天-订单号:BXXXXX,请及时确认')->placeholder('例:176********,189********')->required();
        $form->text('booking_notify_gzh_open_id','接受订房公众号通知')->help('点击 <a href="" target="_blank"> 扫码绑定微信 </a> 需关注公众号')->placeholder('公众号的用户ID');
        $form->text('booking_notify_qywx_robot_url','接受订房通知的企业微信群')->help('添加 微信群通知机器人URL地址')->placeholder('填写 微信群通知机器人URL地址');
        $form->text('booking_notify_mail','接受订房通知的邮箱')->help('<span class="text-success">如需要提醒多人,请逗号分隔</span>')->placeholder('例:36648**@qq.com,18989**@163.com');

        $form->disableResetButton();
        $card =  Card::make('接收订房通知配置',$form);
        return $card;

表单存改接口编写

namespace App\Merchant\Controllers;

use App\Models\Hotel\HotelSetting;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Admin;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Dcat\Admin\Widgets\Form as WidgetForm;
use Dcat\Admin\Http\JsonResponse;
// 列表
class HotelSettingActionController extends Controller
{
    /**
     * @desc 参数存改主入口
     */
    public function edit(Request $request)
    {
        $action_name = $request->get('action_name');
        $validator = \Validator::make($request->all(), [
            'action_name' => 'required',
        ],[
            'action_name.required' => '操作项 不能为空',
        ]);
        if ($validator->fails()) {
            return (new WidgetForm())->response()->error($validator->errors()->all()[0]);
        }
        // 根据操作名不同 调用不用的函数做参数验证,存改
        switch ($action_name) {
            case 'booking_notify':
                return $this->bookint_notify($request);
                break;
            case 'booking_switch':
                return $this->booking_switch($request);
                break;
            default:
                break;
        }
        return true;
    }

    // 订房通知对象配置
    public function bookint_notify(Request $request){
        $validator = \Validator::make($request->all(), [
            'hotel_id' => 'required',
            'booking_notify_phone' => 'required',
            //'booking_notify_gzh_open_id' => 'required',
            'booking_notify_qywx_robot_url' => 'nullable|url',
            'booking_notify_mail' => 'nullable|email',
        ],[
            'hotel_id.required' => '酒店ID 不能为空',
            'booking_notify_phone.required' => '接受订房通知短信的手机号 不能为空',
            //'booking_notify_gzh_open_id.required' => '',
            'booking_notify_qywx_robot_url.required' => '企业微信群机器人 必须是网址',
            'booking_notify_mail.email' => '接受订房通知的邮箱 格式不正确',

        ]);
        if ($validator->fails()) {
            return (new WidgetForm())->response()->error($validator->errors()->all()[0]);
        }
        $booking_notify_phone = $request->get('booking_notify_phone');
        $booking_notify_phone_arr = explode(',',$booking_notify_phone);
        $booking_notify_phone_diff = array_flip(array_flip($booking_notify_phone_arr));
        if(count($booking_notify_phone_arr) != count($booking_notify_phone_diff)){
            return (new WidgetForm())->response()->error('接受订房通知短信的手机号 不可有重复的手机号');
        }
        if(count($booking_notify_phone_arr) > 3){
            return (new WidgetForm())->response()->error('接受订房通知短信的手机号 最多只支持3个手机号');
        }
        foreach ($booking_notify_phone_arr as $phone) {
            if(!isMobile($phone)){
                return (new WidgetForm())->response()->error('接受订房通知短信的手机号 格式不正确');
            }
        }

        $insdata = [
            'booking_notify_phone' => $request->booking_notify_phone,
            'booking_notify_gzh_open_id'           => $request->booking_notify_gzh_open_id,
            'booking_notify_mail'       => $request->booking_notify_mail,
            'booking_notify_qywx_robot_url'       => $request->booking_notify_qywx_robot_url,
        ];
        $hotel_id = $request->get('hotel_id');

        $sts = HotelSetting::createRow($insdata,$hotel_id);
        return JsonResponse::make()->data($request->all())->success('成功!');
    }

    public function booking_switch(Request $request){

    }
}

结尾

本篇记录dcat-admin 开发的一些心得, 如果你有更好的实现方法,望赐教。
如果对你有帮助,希望给个点赞+评论

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!